2010-03-26 20:14:53 +00:00
========
GEOS API
========
.. module:: django.contrib.gis.geos
2016-02-19 07:31:25 +00:00
:synopsis: GeoDjango's high-level interface to the GEOS library.
2010-03-26 20:14:53 +00:00
Background
==========
What is GEOS?
-------------
2014-02-22 17:30:28 +00:00
`GEOS`__ stands for **Geometry Engine - Open Source**,
2010-03-26 20:14:53 +00:00
and is a C++ library, ported from the `Java Topology Suite`__. GEOS
implements the OpenGIS `Simple Features for SQL`__ spatial predicate functions
and spatial operators. GEOS, now an OSGeo project, was initially developed and
maintained by `Refractions Research`__ of Victoria, Canada.
2022-01-17 15:00:14 +00:00
__ https://libgeos.org/
2017-05-20 15:51:21 +00:00
__ https://sourceforge.net/projects/jts-topo-suite/
2021-04-27 11:09:00 +00:00
__ https://www.ogc.org/standards/sfs
2010-03-26 20:14:53 +00:00
__ http://www.refractions.net/
Features
--------
GeoDjango implements a high-level Python wrapper for the GEOS library, its
features include:
* A BSD-licensed interface to the GEOS geometry routines, implemented purely
in Python using ``ctypes``.
* Loosely-coupled to GeoDjango. For example, :class:`GEOSGeometry` objects
2013-11-02 19:13:36 +00:00
may be used outside of a Django project/application. In other words,
2020-04-30 10:12:05 +00:00
no need to have :envvar:`DJANGO_SETTINGS_MODULE` set or use a database, etc.
2010-03-26 20:14:53 +00:00
* Mutability: :class:`GEOSGeometry` objects may be modified.
2017-04-23 20:06:12 +00:00
* Cross-platform and tested; compatible with Windows, Linux, Solaris, and
macOS platforms.
2010-03-26 20:14:53 +00:00
.. _geos-tutorial:
Tutorial
========
2010-12-26 00:37:14 +00:00
This section contains a brief introduction and tutorial to using
2010-03-26 20:14:53 +00:00
:class:`GEOSGeometry` objects.
Creating a Geometry
-------------------
:class:`GEOSGeometry` objects may be created in a few ways. The first is
to simply instantiate the object on some spatial input -- the following
are examples of creating the same geometry from WKT, HEX, WKB, and GeoJSON::
>>> from django.contrib.gis.geos import GEOSGeometry
>>> pnt = GEOSGeometry('POINT(5 23)') # WKT
>>> pnt = GEOSGeometry('010100000000000000000014400000000000003740') # HEX
>>> pnt = GEOSGeometry(buffer('\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x007@'))
>>> pnt = GEOSGeometry('{ "type": "Point", "coordinates": [ 5.000000, 23.000000 ] }') # GeoJSON
Another option is to use the constructor for the specific geometry type
that you wish to create. For example, a :class:`Point` object may be
created by passing in the X and Y coordinates into its constructor::
>>> from django.contrib.gis.geos import Point
>>> pnt = Point(5, 23)
2015-11-25 21:25:22 +00:00
All these constructors take the keyword argument ``srid``. For example::
>>> from django.contrib.gis.geos import GEOSGeometry, LineString, Point
>>> print(GEOSGeometry('POINT (0 0)', srid=4326))
2016-04-16 18:05:47 +00:00
SRID=4326;POINT (0 0)
2015-11-25 21:25:22 +00:00
>>> print(LineString((0, 0), (1, 1), srid=4326))
2016-04-16 18:05:47 +00:00
SRID=4326;LINESTRING (0 0, 1 1)
2015-11-25 21:25:22 +00:00
>>> print(Point(0, 0, srid=32140))
2016-04-16 18:05:47 +00:00
SRID=32140;POINT (0 0)
2015-11-25 21:25:22 +00:00
2015-11-13 09:44:15 +00:00
Finally, there is the :func:`fromfile` factory method which returns a
:class:`GEOSGeometry` object from a file::
2010-03-26 20:14:53 +00:00
2015-11-13 09:44:15 +00:00
>>> from django.contrib.gis.geos import fromfile
2010-03-26 20:14:53 +00:00
>>> pnt = fromfile('/path/to/pnt.wkt')
>>> pnt = fromfile(open('/path/to/pnt.wkt'))
2012-08-03 09:17:52 +00:00
.. _geos-exceptions-in-logfile:
.. admonition:: My logs are filled with GEOS-related errors
You find many ``TypeError`` or ``AttributeError`` exceptions filling your
2021-07-23 06:48:16 +00:00
web server's log files. This generally means that you are creating GEOS
2012-08-03 09:17:52 +00:00
objects at the top level of some of your Python modules. Then, due to a race
condition in the garbage collector, your module is garbage collected before
the GEOS object. To prevent this, create :class:`GEOSGeometry` objects
inside the local scope of your functions/methods.
2010-12-26 00:37:14 +00:00
Geometries are Pythonic
2010-03-26 20:14:53 +00:00
-----------------------
:class:`GEOSGeometry` objects are 'Pythonic', in other words components may
be accessed, modified, and iterated over using standard Python conventions.
For example, you can iterate over the coordinates in a :class:`Point`::
>>> pnt = Point(5, 23)
>>> [coord for coord in pnt]
[5.0, 23.0]
With any geometry object, the :attr:`GEOSGeometry.coords` property
may be used to get the geometry coordinates as a Python tuple::
>>> pnt.coords
(5.0, 23.0)
You can get/set geometry components using standard Python indexing
techniques. However, what is returned depends on the geometry type
of the object. For example, indexing on a :class:`LineString`
returns a coordinate tuple::
>>> from django.contrib.gis.geos import LineString
>>> line = LineString((0, 0), (0, 50), (50, 50), (50, 0), (0, 0))
>>> line[0]
(0.0, 0.0)
>>> line[-2]
(50.0, 0.0)
Whereas indexing on a :class:`Polygon` will return the ring
(a :class:`LinearRing` object) corresponding to the index::
>>> from django.contrib.gis.geos import Polygon
>>> poly = Polygon( ((0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0, 0.0), (0.0, 0.0)) )
>>> poly[0]
<LinearRing object at 0x1044395b0>
>>> poly[0][-2] # second-to-last coordinate of external ring
(50.0, 0.0)
In addition, coordinates/components of the geometry may added or modified,
just like a Python list::
>>> line[0] = (1.0, 1.0)
>>> line.pop()
(0.0, 0.0)
>>> line.append((1.0, 1.0))
>>> line.coords
((1.0, 1.0), (0.0, 50.0), (50.0, 50.0), (50.0, 0.0), (1.0, 1.0))
2015-12-04 07:22:29 +00:00
Geometries support set-like operators::
>>> from django.contrib.gis.geos import LineString
>>> ls1 = LineString((0, 0), (2, 2))
>>> ls2 = LineString((1, 1), (3, 3))
>>> print(ls1 | ls2) # equivalent to `ls1.union(ls2)`
MULTILINESTRING ((0 0, 1 1), (1 1, 2 2), (2 2, 3 3))
>>> print(ls1 & ls2) # equivalent to `ls1.intersection(ls2)`
LINESTRING (1 1, 2 2)
>>> print(ls1 - ls2) # equivalent to `ls1.difference(ls2)`
LINESTRING(0 0, 1 1)
>>> print(ls1 ^ ls2) # equivalent to `ls1.sym_difference(ls2)`
MULTILINESTRING ((0 0, 1 1), (2 2, 3 3))
.. admonition:: Equality operator doesn't check spatial equality
The :class:`~GEOSGeometry` equality operator uses
:meth:`~GEOSGeometry.equals_exact`, not :meth:`~GEOSGeometry.equals`, i.e.
it requires the compared geometries to have the same coordinates in the
2016-11-23 08:23:06 +00:00
same positions with the same SRIDs::
2015-12-04 07:22:29 +00:00
2016-02-19 07:31:25 +00:00
>>> from django.contrib.gis.geos import LineString
>>> ls1 = LineString((0, 0), (1, 1))
>>> ls2 = LineString((1, 1), (0, 0))
2016-11-23 08:23:06 +00:00
>>> ls3 = LineString((1, 1), (0, 0), srid=4326)
2016-02-19 07:31:25 +00:00
>>> ls1.equals(ls2)
True
>>> ls1 == ls2
False
2016-11-23 08:23:06 +00:00
>>> ls3 == ls2 # different SRIDs
False
2010-03-26 20:14:53 +00:00
Geometry Objects
================
``GEOSGeometry``
----------------
2015-07-27 12:35:21 +00:00
.. class:: GEOSGeometry(geo_input, srid=None)
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
:param geo_input: Geometry input value (string or buffer)
:param srid: spatial reference identifier
:type srid: int
2010-03-26 20:14:53 +00:00
This is the base class for all GEOS geometry objects. It initializes on the
given ``geo_input`` argument, and then assumes the proper geometry subclass
(e.g., ``GEOSGeometry('POINT(1 1)')`` will create a :class:`Point` object).
2017-03-30 13:38:26 +00:00
The ``srid`` parameter, if given, is set as the SRID of the created geometry if
``geo_input`` doesn't have an SRID. If different SRIDs are provided through the
``geo_input`` and ``srid`` parameters, ``ValueError`` is raised::
>>> from django.contrib.gis.geos import GEOSGeometry
>>> GEOSGeometry('POINT EMPTY', srid=4326).ewkt
'SRID=4326;POINT EMPTY'
>>> GEOSGeometry('SRID=4326;POINT EMPTY', srid=4326).ewkt
'SRID=4326;POINT EMPTY'
>>> GEOSGeometry('SRID=1;POINT EMPTY', srid=4326)
Traceback (most recent call last):
...
ValueError: Input geometry already has SRID: 1.
2010-03-26 20:14:53 +00:00
The following input formats, along with their corresponding Python types,
are accepted:
2017-01-20 21:04:05 +00:00
======================= ==========
2015-12-23 10:37:49 +00:00
Format Input Type
2017-01-20 21:04:05 +00:00
======================= ==========
WKT / EWKT ``str``
HEX / HEXEWKB ``str``
2015-12-23 10:37:49 +00:00
WKB / EWKB ``buffer``
2020-04-15 11:11:13 +00:00
:rfc:`GeoJSON <7946>` ``str``
2017-01-20 21:04:05 +00:00
======================= ==========
2010-03-26 20:14:53 +00:00
2017-03-30 15:17:13 +00:00
For the GeoJSON format, the SRID is set based on the ``crs`` member. If ``crs``
isn't provided, the SRID defaults to 4326.
2016-06-10 06:50:07 +00:00
.. classmethod:: GEOSGeometry.from_gml(gml_string)
Constructs a :class:`GEOSGeometry` from the given GML string.
2010-03-26 20:14:53 +00:00
Properties
~~~~~~~~~~
.. attribute:: GEOSGeometry.coords
2016-04-16 17:49:10 +00:00
Returns the coordinates of the geometry as a tuple.
2010-03-26 20:14:53 +00:00
2015-11-03 12:16:08 +00:00
.. attribute:: GEOSGeometry.dims
2016-04-16 17:49:10 +00:00
Returns the dimension of the geometry:
2015-11-03 12:16:08 +00:00
2016-04-16 17:49:10 +00:00
* ``0`` for :class:`Point`\s and :class:`MultiPoint`\s
* ``1`` for :class:`LineString`\s and :class:`MultiLineString`\s
* ``2`` for :class:`Polygon`\s and :class:`MultiPolygon`\s
* ``-1`` for empty :class:`GeometryCollection`\s
* the maximum dimension of its elements for non-empty
:class:`GeometryCollection`\s
2015-11-03 12:16:08 +00:00
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.empty
2016-04-16 17:49:10 +00:00
Returns whether or not the set of points in the geometry is empty.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.geom_type
2016-04-16 17:49:10 +00:00
Returns a string corresponding to the type of geometry. For example::
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
>>> pnt = GEOSGeometry('POINT(5 23)')
>>> pnt.geom_type
'Point'
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.geom_typeid
2016-04-16 17:49:10 +00:00
Returns the GEOS geometry type identification number. The following table
shows the value for each geometry type:
=========================== ========
Geometry ID
=========================== ========
:class:`Point` 0
:class:`LineString` 1
:class:`LinearRing` 2
:class:`Polygon` 3
:class:`MultiPoint` 4
:class:`MultiLineString` 5
:class:`MultiPolygon` 6
:class:`GeometryCollection` 7
=========================== ========
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.num_coords
2016-04-16 17:49:10 +00:00
Returns the number of coordinates in the geometry.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.num_geom
2016-04-16 17:49:10 +00:00
Returns the number of geometries in this geometry. In other words, will
return 1 on anything but geometry collections.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.hasz
2016-04-16 17:49:10 +00:00
Returns a boolean indicating whether the geometry is three-dimensional.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.ring
2016-04-16 17:49:10 +00:00
Returns a boolean indicating whether the geometry is a ``LinearRing``.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.simple
2016-04-16 17:49:10 +00:00
Returns a boolean indicating whether the geometry is 'simple'. A geometry
is simple if and only if it does not intersect itself (except at boundary
points). For example, a :class:`LineString` object is not simple if it
intersects itself. Thus, :class:`LinearRing` and :class:`Polygon` objects
are always simple because they do cannot intersect themselves, by
definition.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.valid
2016-04-16 17:49:10 +00:00
Returns a boolean indicating whether the geometry is valid.
2010-03-26 20:14:53 +00:00
2010-12-26 00:37:14 +00:00
.. attribute:: GEOSGeometry.valid_reason
2010-11-04 01:29:37 +00:00
2016-04-16 17:49:10 +00:00
Returns a string describing the reason why a geometry is invalid.
2010-11-04 01:29:37 +00:00
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.srid
2016-04-16 17:49:10 +00:00
Property that may be used to retrieve or set the SRID associated with the
geometry. For example::
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
>>> pnt = Point(5, 23)
>>> print(pnt.srid)
None
>>> pnt.srid = 4326
>>> pnt.srid
4326
2010-03-26 20:14:53 +00:00
Output Properties
~~~~~~~~~~~~~~~~~
The properties in this section export the :class:`GEOSGeometry` object into
a different. This output may be in the form of a string, buffer, or even
another object.
.. attribute:: GEOSGeometry.ewkt
2016-04-16 17:49:10 +00:00
Returns the "extended" Well-Known Text of the geometry. This representation
is specific to PostGIS and is a superset of the OGC WKT standard. [#fnogc]_
Essentially the SRID is prepended to the WKT representation, for example
``SRID=4326;POINT(5 23)``.
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
.. note::
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
The output from this property does not include the 3dm, 3dz, and 4d
information that PostGIS supports in its EWKT representations.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.hex
2016-04-16 17:49:10 +00:00
Returns the WKB of this Geometry in hexadecimal form. Please note
that the SRID value is not included in this representation
because it is not a part of the OGC specification (use the
:attr:`GEOSGeometry.hexewkb` property instead).
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.hexewkb
2016-04-16 17:49:10 +00:00
Returns the EWKB of this Geometry in hexadecimal form. This is an
extension of the WKB specification that includes the SRID value
that are a part of this geometry.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.json
2016-04-16 17:49:10 +00:00
Returns the GeoJSON representation of the geometry. Note that the result is
not a complete GeoJSON structure but only the ``geometry`` key content of a
GeoJSON structure. See also :doc:`/ref/contrib/gis/serializers`.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.geojson
2016-04-16 17:49:10 +00:00
Alias for :attr:`GEOSGeometry.json`.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.kml
2016-04-16 17:49:10 +00:00
Returns a `KML`__ (Keyhole Markup Language) representation of the
geometry. This should only be used for geometries with an SRID of
4326 (WGS84), but this restriction is not enforced.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.ogr
2016-04-16 17:49:10 +00:00
Returns an :class:`~django.contrib.gis.gdal.OGRGeometry` object
corresponding to the GEOS geometry.
2010-03-26 20:14:53 +00:00
.. _wkb:
.. attribute:: GEOSGeometry.wkb
2016-04-16 17:49:10 +00:00
Returns the WKB (Well-Known Binary) representation of this Geometry
as a Python buffer. SRID value is not included, use the
:attr:`GEOSGeometry.ewkb` property instead.
2010-03-26 20:14:53 +00:00
.. _ewkb:
.. attribute:: GEOSGeometry.ewkb
2016-04-16 17:49:10 +00:00
Return the EWKB representation of this Geometry as a Python buffer.
This is an extension of the WKB specification that includes any SRID
value that are a part of this geometry.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.wkt
2016-04-16 17:49:10 +00:00
Returns the Well-Known Text of the geometry (an OGC standard).
2010-03-26 20:14:53 +00:00
2012-06-28 08:49:07 +00:00
__ https://developers.google.com/kml/documentation/
2010-03-26 20:14:53 +00:00
Spatial Predicate Methods
~~~~~~~~~~~~~~~~~~~~~~~~~
All of the following spatial predicate methods take another
:class:`GEOSGeometry` instance (``other``) as a parameter, and
return a boolean.
.. method:: GEOSGeometry.contains(other)
2016-04-16 17:49:10 +00:00
Returns ``True`` if :meth:`other.within(this) <GEOSGeometry.within>` returns
``True``.
2010-03-26 20:14:53 +00:00
2015-11-20 10:11:25 +00:00
.. method:: GEOSGeometry.covers(other)
2016-04-16 17:49:10 +00:00
Returns ``True`` if this geometry covers the specified geometry.
2015-11-20 10:11:25 +00:00
2016-04-16 17:49:10 +00:00
The ``covers`` predicate has the following equivalent definitions:
2015-11-20 10:11:25 +00:00
2016-04-16 17:49:10 +00:00
* Every point of the other geometry is a point of this geometry.
* The DE-9IM Intersection Matrix for the two geometries is
``T*****FF*``, ``*T****FF*``, ``***T**FF*``, or ``****T*FF*``.
2015-11-20 10:11:25 +00:00
2016-04-16 17:49:10 +00:00
If either geometry is empty, returns ``False``.
2015-11-20 10:11:25 +00:00
2016-04-16 17:49:10 +00:00
This predicate is similar to :meth:`GEOSGeometry.contains`, but is more
inclusive (i.e. returns ``True`` for more cases). In particular, unlike
:meth:`~GEOSGeometry.contains` it does not distinguish between points in the
boundary and in the interior of geometries. For most situations,
``covers()`` should be preferred to :meth:`~GEOSGeometry.contains`. As an
added benefit, ``covers()`` is more amenable to optimization and hence
should outperform :meth:`~GEOSGeometry.contains`.
2015-11-20 10:11:25 +00:00
2010-03-26 20:14:53 +00:00
.. method:: GEOSGeometry.crosses(other)
2016-04-16 17:49:10 +00:00
Returns ``True`` if the DE-9IM intersection matrix for the two Geometries
is ``T*T******`` (for a point and a curve,a point and an area or a line
and an area) ``0********`` (for two curves).
2010-03-26 20:14:53 +00:00
.. method:: GEOSGeometry.disjoint(other)
2016-04-16 17:49:10 +00:00
Returns ``True`` if the DE-9IM intersection matrix for the two geometries
is ``FF*FF****``.
2010-03-26 20:14:53 +00:00
.. method:: GEOSGeometry.equals(other)
2016-04-16 17:49:10 +00:00
Returns ``True`` if the DE-9IM intersection matrix for the two geometries
is ``T*F**FFF*``.
2010-03-26 20:14:53 +00:00
.. method:: GEOSGeometry.equals_exact(other, tolerance=0)
2016-04-16 17:49:10 +00:00
Returns true if the two geometries are exactly equal, up to a
specified tolerance. The ``tolerance`` value should be a floating
point number representing the error tolerance in the comparison, e.g.,
``poly1.equals_exact(poly2, 0.001)`` will compare equality to within
one thousandth of a unit.
2010-03-26 20:14:53 +00:00
.. method:: GEOSGeometry.intersects(other)
2016-04-16 17:49:10 +00:00
Returns ``True`` if :meth:`GEOSGeometry.disjoint` is ``False``.
2010-03-26 20:14:53 +00:00
.. method:: GEOSGeometry.overlaps(other)
2016-04-16 17:49:10 +00:00
Returns true if the DE-9IM intersection matrix for the two geometries
is ``T*T***T**`` (for two points or two surfaces) ``1*T***T**``
(for two curves).
2010-03-26 20:14:53 +00:00
.. method:: GEOSGeometry.relate_pattern(other, pattern)
2016-04-16 17:49:10 +00:00
Returns ``True`` if the elements in the DE-9IM intersection matrix
for this geometry and the other matches the given ``pattern`` --
a string of nine characters from the alphabet: {``T``, ``F``, ``*``, ``0``}.
2010-03-26 20:14:53 +00:00
.. method:: GEOSGeometry.touches(other)
2016-04-16 17:49:10 +00:00
Returns ``True`` if the DE-9IM intersection matrix for the two geometries
is ``FT*******``, ``F**T*****`` or ``F***T****``.
2010-03-26 20:14:53 +00:00
.. method:: GEOSGeometry.within(other)
2016-04-16 17:49:10 +00:00
Returns ``True`` if the DE-9IM intersection matrix for the two geometries
is ``T*F**F***``.
2010-03-26 20:14:53 +00:00
Topological Methods
~~~~~~~~~~~~~~~~~~~
.. method:: GEOSGeometry.buffer(width, quadsegs=8)
2016-04-16 17:49:10 +00:00
Returns a :class:`GEOSGeometry` that represents all points whose distance
from this geometry is less than or equal to the given ``width``. The
optional ``quadsegs`` keyword sets the number of segments used to
approximate a quarter circle (defaults is 8).
2010-03-26 20:14:53 +00:00
2018-01-09 16:15:04 +00:00
.. method:: GEOSGeometry.buffer_with_style(width, quadsegs=8, end_cap_style=1, join_style=1, mitre_limit=5.0)
Same as :meth:`buffer`, but allows customizing the style of the buffer.
* ``end_cap_style`` can be round (``1``), flat (``2``), or square (``3``).
* ``join_style`` can be round (``1``), mitre (``2``), or bevel (``3``).
* Mitre ratio limit (``mitre_limit``) only affects mitered join style.
2010-03-26 20:14:53 +00:00
.. method:: GEOSGeometry.difference(other)
2016-04-16 17:49:10 +00:00
Returns a :class:`GEOSGeometry` representing the points making up this
geometry that do not make up other.
2010-03-26 20:14:53 +00:00
2012-09-29 09:01:08 +00:00
.. method:: GEOSGeometry.interpolate(distance)
.. method:: GEOSGeometry.interpolate_normalized(distance)
2016-04-16 17:49:10 +00:00
Given a distance (float), returns the point (or closest point) within the
geometry (:class:`LineString` or :class:`MultiLineString`) at that distance.
The normalized version takes the distance as a float between 0 (origin) and
1 (endpoint).
2012-09-29 09:01:08 +00:00
2016-04-16 17:49:10 +00:00
Reverse of :meth:`GEOSGeometry.project`.
2012-09-29 09:01:08 +00:00
2015-12-18 10:36:55 +00:00
.. method:: GEOSGeometry.intersection(other)
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
Returns a :class:`GEOSGeometry` representing the points shared by this
geometry and other.
2010-03-26 20:14:53 +00:00
2012-09-29 09:01:08 +00:00
.. method:: GEOSGeometry.project(point)
.. method:: GEOSGeometry.project_normalized(point)
2016-04-16 17:49:10 +00:00
Returns the distance (float) from the origin of the geometry
(:class:`LineString` or :class:`MultiLineString`) to the point projected on
the geometry (that is to a point of the line the closest to the given
point). The normalized version returns the distance as a float between 0
(origin) and 1 (endpoint).
2012-09-29 09:01:08 +00:00
2016-04-16 17:49:10 +00:00
Reverse of :meth:`GEOSGeometry.interpolate`.
2012-09-29 09:01:08 +00:00
2010-03-26 20:14:53 +00:00
.. method:: GEOSGeometry.relate(other)
2016-04-16 17:49:10 +00:00
Returns the DE-9IM intersection matrix (a string) representing the
topological relationship between this geometry and the other.
2010-03-26 20:14:53 +00:00
.. method:: GEOSGeometry.simplify(tolerance=0.0, preserve_topology=False)
2016-04-16 17:49:10 +00:00
Returns a new :class:`GEOSGeometry`, simplified to the specified tolerance
using the Douglas-Peucker algorithm. A higher tolerance value implies
fewer points in the output. If no tolerance is provided, it defaults to 0.
2014-07-25 00:24:52 +00:00
2016-04-16 17:49:10 +00:00
By default, this function does not preserve topology. For example,
:class:`Polygon` objects can be split, be collapsed into lines, or
disappear. :class:`Polygon` holes can be created or disappear, and lines may
cross. By specifying ``preserve_topology=True``, the result will have the
same dimension and number of components as the input; this is significantly
slower, however.
2010-03-26 20:14:53 +00:00
.. method:: GEOSGeometry.sym_difference(other)
2016-04-16 17:49:10 +00:00
Returns a :class:`GEOSGeometry` combining the points in this geometry
not in other, and the points in other not in this geometry.
2010-03-26 20:14:53 +00:00
.. method:: GEOSGeometry.union(other)
2016-04-16 17:49:10 +00:00
Returns a :class:`GEOSGeometry` representing all the points in this
geometry and the other.
2010-03-26 20:14:53 +00:00
Topological Properties
~~~~~~~~~~~~~~~~~~~~~~
.. attribute:: GEOSGeometry.boundary
2016-04-16 17:49:10 +00:00
Returns the boundary as a newly allocated Geometry object.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.centroid
2016-04-16 17:49:10 +00:00
Returns a :class:`Point` object representing the geometric center of
the geometry. The point is not guaranteed to be on the interior
of the geometry.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.convex_hull
2016-04-16 17:49:10 +00:00
Returns the smallest :class:`Polygon` that contains all the points in
the geometry.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.envelope
2016-04-16 17:49:10 +00:00
Returns a :class:`Polygon` that represents the bounding envelope of
this geometry. Note that it can also return a :class:`Point` if the input
geometry is a point.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.point_on_surface
2016-04-16 17:49:10 +00:00
Computes and returns a :class:`Point` guaranteed to be on the interior
of this geometry.
2010-03-26 20:14:53 +00:00
2015-11-09 17:36:12 +00:00
.. attribute:: GEOSGeometry.unary_union
2016-04-16 17:49:10 +00:00
Computes the union of all the elements of this geometry.
2015-11-09 17:36:12 +00:00
2016-04-16 17:49:10 +00:00
The result obeys the following contract:
2015-11-09 17:36:12 +00:00
2016-04-16 17:49:10 +00:00
* Unioning a set of :class:`LineString`\s has the effect of fully noding and
dissolving the linework.
2015-11-09 17:36:12 +00:00
2016-04-16 17:49:10 +00:00
* Unioning a set of :class:`Polygon`\s will always return a :class:`Polygon`
or :class:`MultiPolygon` geometry (unlike :meth:`GEOSGeometry.union`,
which may return geometries of lower dimension if a topology collapse
occurs).
2015-11-09 17:36:12 +00:00
2010-03-26 20:14:53 +00:00
Other Properties & Methods
~~~~~~~~~~~~~~~~~~~~~~~~~~
.. attribute:: GEOSGeometry.area
2016-04-16 17:49:10 +00:00
This property returns the area of the Geometry.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.extent
2016-04-16 17:49:10 +00:00
This property returns the extent of this geometry as a 4-tuple,
consisting of ``(xmin, ymin, xmax, ymax)``.
2010-03-26 20:14:53 +00:00
.. method:: GEOSGeometry.clone()
2016-04-16 17:49:10 +00:00
This method returns a :class:`GEOSGeometry` that is a clone of the original.
2010-03-26 20:14:53 +00:00
.. method:: GEOSGeometry.distance(geom)
2016-04-16 17:49:10 +00:00
Returns the distance between the closest points on this geometry and the
given ``geom`` (another :class:`GEOSGeometry` object).
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
.. note::
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
GEOS distance calculations are linear -- in other words, GEOS does not
perform a spherical calculation even if the SRID specifies a geographic
coordinate system.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.length
2016-04-16 17:49:10 +00:00
Returns the length of this geometry (e.g., 0 for a :class:`Point`,
the length of a :class:`LineString`, or the circumference of
a :class:`Polygon`).
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.prepared
2016-04-16 17:49:10 +00:00
Returns a GEOS ``PreparedGeometry`` for the contents of this geometry.
``PreparedGeometry`` objects are optimized for the contains, intersects,
covers, crosses, disjoint, overlaps, touches and within operations. Refer to
the :ref:`prepared-geometries` documentation for more information.
2010-03-26 20:14:53 +00:00
.. attribute:: GEOSGeometry.srs
2016-04-16 17:49:10 +00:00
Returns a :class:`~django.contrib.gis.gdal.SpatialReference` object
corresponding to the SRID of the geometry or ``None``.
2010-03-26 20:14:53 +00:00
2010-12-22 17:43:30 +00:00
.. method:: GEOSGeometry.transform(ct, clone=False)
2016-04-16 17:49:10 +00:00
Transforms the geometry according to the given coordinate transformation
parameter (``ct``), which may be an integer SRID, spatial reference WKT
2020-05-10 20:30:03 +00:00
string, a PROJ string, a :class:`~django.contrib.gis.gdal.SpatialReference`
object, or a :class:`~django.contrib.gis.gdal.CoordTransform` object. By
default, the geometry is transformed in-place and nothing is returned.
However if the ``clone`` keyword is set, then the geometry is not modified
and a transformed clone of the geometry is returned instead.
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
.. note::
2010-03-26 20:14:53 +00:00
2016-06-14 14:18:33 +00:00
Raises :class:`~django.contrib.gis.geos.GEOSException` if GDAL is not
available or if the geometry's SRID is ``None`` or less than 0. It
doesn't impose any constraints on the geometry's SRID if called with a
:class:`~django.contrib.gis.gdal.CoordTransform` object.
2015-10-24 14:12:38 +00:00
2021-09-24 08:15:23 +00:00
.. method:: GEOSGeometry.make_valid()
.. versionadded:: 4.1
Returns a valid :class:`GEOSGeometry` equivalent, trying not to lose any of
the input vertices. If the geometry is already valid, it is returned
untouched. This is similar to the
:class:`~django.contrib.gis.db.models.functions.MakeValid` database
function. Requires GEOS 3.8.
2016-11-30 12:39:03 +00:00
.. method:: GEOSGeometry.normalize()
Converts this geometry to canonical form::
>>> g = MultiPoint(Point(0, 0), Point(2, 2), Point(1, 1))
>>> print(g)
MULTIPOINT (0 0, 2 2, 1 1)
>>> g.normalize()
>>> print(g)
MULTIPOINT (2 2, 1 1, 0 0)
2010-03-26 20:14:53 +00:00
``Point``
---------
2015-11-24 09:21:32 +00:00
.. class:: Point(x=None, y=None, z=None, srid=None)
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
``Point`` objects are instantiated using arguments that represent the
component coordinates of the point or with a single sequence coordinates.
For example, the following are equivalent::
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
>>> pnt = Point(5, 23)
>>> pnt = Point([5, 23])
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
Empty ``Point`` objects may be instantiated by passing no arguments or an
empty sequence. The following are equivalent::
2015-11-24 09:21:32 +00:00
2016-02-19 07:31:25 +00:00
>>> pnt = Point()
>>> pnt = Point([])
2015-11-24 09:21:32 +00:00
2010-03-26 20:14:53 +00:00
``LineString``
--------------
.. class:: LineString(*args, **kwargs)
2016-02-19 07:31:25 +00:00
``LineString`` objects are instantiated using arguments that are either a
sequence of coordinates or :class:`Point` objects. For example, the
following are equivalent::
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
>>> ls = LineString((0, 0), (1, 1))
>>> ls = LineString(Point(0, 0), Point(1, 1))
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
In addition, ``LineString`` objects may also be created by passing in a
single sequence of coordinate or :class:`Point` objects::
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
>>> ls = LineString( ((0, 0), (1, 1)) )
>>> ls = LineString( [Point(0, 0), Point(1, 1)] )
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
Empty ``LineString`` objects may be instantiated by passing no arguments
or an empty sequence. The following are equivalent::
2015-11-24 09:21:32 +00:00
2016-02-19 07:31:25 +00:00
>>> ls = LineString()
>>> ls = LineString([])
2015-11-24 09:21:32 +00:00
2016-02-19 07:31:25 +00:00
.. attribute:: closed
2015-12-18 10:26:22 +00:00
2016-04-16 17:49:10 +00:00
Returns whether or not this ``LineString`` is closed.
2015-12-18 10:26:22 +00:00
2010-03-26 20:14:53 +00:00
``LinearRing``
--------------
.. class:: LinearRing(*args, **kwargs)
2016-02-19 07:31:25 +00:00
``LinearRing`` objects are constructed in the exact same way as
:class:`LineString` objects, however the coordinates must be *closed*, in
other words, the first coordinates must be the same as the last
coordinates. For example::
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
>>> ls = LinearRing((0, 0), (0, 1), (1, 1), (0, 0))
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
Notice that ``(0, 0)`` is the first and last coordinate -- if they were not
equal, an error would be raised.
2010-03-26 20:14:53 +00:00
2019-10-17 08:17:42 +00:00
.. attribute:: is_counterclockwise
Returns whether this ``LinearRing`` is counterclockwise.
2010-03-26 20:14:53 +00:00
``Polygon``
-----------
.. class:: Polygon(*args, **kwargs)
2016-02-19 07:31:25 +00:00
``Polygon`` objects may be instantiated by passing in parameters that
represent the rings of the polygon. The parameters must either be
:class:`LinearRing` instances, or a sequence that may be used to construct a
:class:`LinearRing`::
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
>>> ext_coords = ((0, 0), (0, 1), (1, 1), (1, 0), (0, 0))
>>> int_coords = ((0.4, 0.4), (0.4, 0.6), (0.6, 0.6), (0.6, 0.4), (0.4, 0.4))
>>> poly = Polygon(ext_coords, int_coords)
>>> poly = Polygon(LinearRing(ext_coords), LinearRing(int_coords))
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
.. classmethod:: from_bbox(bbox)
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
Returns a polygon object from the given bounding-box, a 4-tuple
comprising ``(xmin, ymin, xmax, ymax)``.
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
.. attribute:: num_interior_rings
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
Returns the number of interior rings in this geometry.
2010-03-26 20:14:53 +00:00
2012-11-28 21:31:13 +00:00
.. admonition:: Comparing Polygons
Note that it is possible to compare ``Polygon`` objects directly with ``<``
or ``>``, but as the comparison is made through Polygon's
:class:`LineString`, it does not mean much (but is consistent and quick).
You can always force the comparison with the :attr:`~GEOSGeometry.area`
property::
>>> if poly_1.area > poly_2.area:
>>> pass
2021-12-19 06:15:09 +00:00
.. _geos-geometry-collections:
2010-03-26 20:14:53 +00:00
Geometry Collections
====================
``MultiPoint``
--------------
.. class:: MultiPoint(*args, **kwargs)
2016-02-19 07:31:25 +00:00
``MultiPoint`` objects may be instantiated by passing in :class:`Point`
objects as arguments, or a single sequence of :class:`Point` objects::
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
>>> mp = MultiPoint(Point(0, 0), Point(1, 1))
>>> mp = MultiPoint( (Point(0, 0), Point(1, 1)) )
2010-03-26 20:14:53 +00:00
``MultiLineString``
-------------------
.. class:: MultiLineString(*args, **kwargs)
2016-02-19 07:31:25 +00:00
``MultiLineString`` objects may be instantiated by passing in
:class:`LineString` objects as arguments, or a single sequence of
:class:`LineString` objects::
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
>>> ls1 = LineString((0, 0), (1, 1))
>>> ls2 = LineString((2, 2), (3, 3))
>>> mls = MultiLineString(ls1, ls2)
>>> mls = MultiLineString([ls1, ls2])
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
.. attribute:: merged
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
Returns a :class:`LineString` representing the line merge of
all the components in this ``MultiLineString``.
2010-12-26 00:37:14 +00:00
2016-02-19 07:31:25 +00:00
.. attribute:: closed
2015-12-18 10:26:22 +00:00
2021-09-24 16:52:08 +00:00
Returns ``True`` if and only if all elements are closed.
2010-03-26 20:14:53 +00:00
``MultiPolygon``
----------------
.. class:: MultiPolygon(*args, **kwargs)
2016-02-19 07:31:25 +00:00
``MultiPolygon`` objects may be instantiated by passing :class:`Polygon`
objects as arguments, or a single sequence of :class:`Polygon` objects::
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
>>> p1 = Polygon( ((0, 0), (0, 1), (1, 1), (0, 0)) )
>>> p2 = Polygon( ((1, 1), (1, 2), (2, 2), (1, 1)) )
>>> mp = MultiPolygon(p1, p2)
>>> mp = MultiPolygon([p1, p2])
2010-03-26 20:14:53 +00:00
``GeometryCollection``
----------------------
.. class:: GeometryCollection(*args, **kwargs)
2016-02-19 07:31:25 +00:00
``GeometryCollection`` objects may be instantiated by passing in other
:class:`GEOSGeometry` as arguments, or a single sequence of
:class:`GEOSGeometry` objects::
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
>>> poly = Polygon( ((0, 0), (0, 1), (1, 1), (0, 0)) )
>>> gc = GeometryCollection(Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly)
>>> gc = GeometryCollection((Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly))
2010-03-26 20:14:53 +00:00
.. _prepared-geometries:
Prepared Geometries
===================
2019-06-17 14:54:55 +00:00
In order to obtain a prepared geometry, access the
2010-03-26 20:14:53 +00:00
:attr:`GEOSGeometry.prepared` property. Once you have a
``PreparedGeometry`` instance its spatial predicate methods, listed below,
may be used with other ``GEOSGeometry`` objects. An operation with a prepared
2010-12-26 00:37:14 +00:00
geometry can be orders of magnitude faster -- the more complex the geometry
2010-03-26 20:14:53 +00:00
that is prepared, the larger the speedup in the operation. For more information,
2015-11-29 16:29:46 +00:00
please consult the `GEOS wiki page on prepared geometries <https://trac.osgeo.org/geos/wiki/PreparedGeometry>`_.
2010-03-26 20:14:53 +00:00
For example::
>>> from django.contrib.gis.geos import Point, Polygon
>>> poly = Polygon.from_bbox((0, 0, 5, 5))
>>> prep_poly = poly.prepared
>>> prep_poly.contains(Point(2.5, 2.5))
True
``PreparedGeometry``
--------------------
.. class:: PreparedGeometry
2016-04-16 17:49:10 +00:00
All methods on ``PreparedGeometry`` take an ``other`` argument, which
must be a :class:`GEOSGeometry` instance.
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
.. method:: contains(other)
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
.. method:: contains_properly(other)
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
.. method:: covers(other)
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
.. method:: crosses(other)
2013-12-24 15:53:09 +00:00
2016-04-16 17:49:10 +00:00
.. method:: disjoint(other)
2013-12-24 15:53:09 +00:00
2016-04-16 17:49:10 +00:00
.. method:: intersects(other)
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
.. method:: overlaps(other)
2013-12-24 15:53:09 +00:00
2016-04-16 17:49:10 +00:00
.. method:: touches(other)
2013-12-24 15:53:09 +00:00
2016-04-16 17:49:10 +00:00
.. method:: within(other)
2013-12-24 15:53:09 +00:00
2010-03-26 20:14:53 +00:00
Geometry Factories
==================
.. function:: fromfile(file_h)
2016-02-19 07:31:25 +00:00
:param file_h: input file that contains spatial data
:type file_h: a Python ``file`` object or a string path to the file
:rtype: a :class:`GEOSGeometry` corresponding to the spatial data in the file
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
Example::
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
>>> from django.contrib.gis.geos import fromfile
>>> g = fromfile('/home/bob/geom.wkt')
2010-03-26 20:14:53 +00:00
2015-07-27 12:35:21 +00:00
.. function:: fromstr(string, srid=None)
2010-03-26 20:14:53 +00:00
2016-02-19 07:31:25 +00:00
:param string: string that contains spatial data
2018-02-12 14:09:46 +00:00
:type string: str
2016-02-19 07:31:25 +00:00
:param srid: spatial reference identifier
:type srid: int
:rtype: a :class:`GEOSGeometry` corresponding to the spatial data in the string
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
``fromstr(string, srid)`` is equivalent to
:class:`GEOSGeometry(string, srid) <GEOSGeometry>`.
2015-11-13 09:44:15 +00:00
2016-04-16 17:49:10 +00:00
Example::
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
>>> from django.contrib.gis.geos import fromstr
>>> pnt = fromstr('POINT(-90.5 29.5)', srid=4326)
2010-03-26 20:14:53 +00:00
I/O Objects
===========
Reader Objects
--------------
2019-06-17 14:54:55 +00:00
The reader I/O classes return a :class:`GEOSGeometry` instance from the WKB
and/or WKT input given to their ``read(geom)`` method.
2010-03-26 20:14:53 +00:00
.. class:: WKBReader
2016-04-16 17:49:10 +00:00
Example::
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
>>> from django.contrib.gis.geos import WKBReader
>>> wkb_r = WKBReader()
>>> wkb_r.read('0101000000000000000000F03F000000000000F03F')
<Point object at 0x103a88910>
2010-03-26 20:14:53 +00:00
.. class:: WKTReader
2016-04-16 17:49:10 +00:00
Example::
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
>>> from django.contrib.gis.geos import WKTReader
>>> wkt_r = WKTReader()
>>> wkt_r.read('POINT(1 1)')
<Point object at 0x103a88b50>
2010-03-26 20:14:53 +00:00
Writer Objects
--------------
All writer objects have a ``write(geom)`` method that returns either the
WKB or WKT of the given geometry. In addition, :class:`WKBWriter` objects
also have properties that may be used to change the byte order, and or
2012-09-22 09:55:37 +00:00
include the SRID value (in other words, EWKB).
2010-03-26 20:14:53 +00:00
2016-04-16 08:50:24 +00:00
.. class:: WKBWriter(dim=2)
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
``WKBWriter`` provides the most control over its output. By default it
returns OGC-compliant WKB when its ``write`` method is called. However,
it has properties that allow for the creation of EWKB, a superset of the
WKB standard that includes additional information. See the
:attr:`WKBWriter.outdim` documentation for more details about the ``dim``
argument.
2016-04-16 08:50:24 +00:00
2016-04-16 17:49:10 +00:00
.. method:: WKBWriter.write(geom)
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
Returns the WKB of the given geometry as a Python ``buffer`` object.
Example::
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
>>> from django.contrib.gis.geos import Point, WKBWriter
>>> pnt = Point(1, 1)
>>> wkb_w = WKBWriter()
>>> wkb_w.write(pnt)
<read-only buffer for 0x103a898f0, size -1, offset 0 at 0x103a89930>
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
.. method:: WKBWriter.write_hex(geom)
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
Returns WKB of the geometry in hexadecimal. Example::
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
>>> from django.contrib.gis.geos import Point, WKBWriter
>>> pnt = Point(1, 1)
>>> wkb_w = WKBWriter()
>>> wkb_w.write_hex(pnt)
'0101000000000000000000F03F000000000000F03F'
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
.. attribute:: WKBWriter.byteorder
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
This property may be set to change the byte-order of the geometry
representation.
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
=============== =================================================
Byteorder Value Description
=============== =================================================
0 Big Endian (e.g., compatible with RISC systems)
1 Little Endian (e.g., compatible with x86 systems)
=============== =================================================
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
Example::
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
>>> from django.contrib.gis.geos import Point, WKBWriter
>>> wkb_w = WKBWriter()
>>> pnt = Point(1, 1)
>>> wkb_w.write_hex(pnt)
'0101000000000000000000F03F000000000000F03F'
>>> wkb_w.byteorder = 0
'00000000013FF00000000000003FF0000000000000'
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
.. attribute:: WKBWriter.outdim
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
This property may be set to change the output dimension of the geometry
representation. In other words, if you have a 3D geometry then set to 3
so that the Z value is included in the WKB.
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
============ ===========================
Outdim Value Description
============ ===========================
2 The default, output 2D WKB.
3 Output 3D WKB.
============ ===========================
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
Example::
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
>>> from django.contrib.gis.geos import Point, WKBWriter
>>> wkb_w = WKBWriter()
>>> wkb_w.outdim
2
>>> pnt = Point(1, 1, 1)
>>> wkb_w.write_hex(pnt) # By default, no Z value included:
'0101000000000000000000F03F000000000000F03F'
>>> wkb_w.outdim = 3 # Tell writer to include Z values
>>> wkb_w.write_hex(pnt)
'0101000080000000000000F03F000000000000F03F000000000000F03F'
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
.. attribute:: WKBWriter.srid
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
Set this property with a boolean to indicate whether the SRID of the
geometry should be included with the WKB representation. Example::
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
>>> from django.contrib.gis.geos import Point, WKBWriter
>>> wkb_w = WKBWriter()
>>> pnt = Point(1, 1, srid=4326)
>>> wkb_w.write_hex(pnt) # By default, no SRID included:
'0101000000000000000000F03F000000000000F03F'
>>> wkb_w.srid = True # Tell writer to include SRID
>>> wkb_w.write_hex(pnt)
'0101000020E6100000000000000000F03F000000000000F03F'
2010-03-26 20:14:53 +00:00
2016-04-16 08:50:24 +00:00
.. class:: WKTWriter(dim=2, trim=False, precision=None)
This class allows outputting the WKT representation of a geometry. See the
:attr:`WKBWriter.outdim`, :attr:`trim`, and :attr:`precision` attributes for
details about the constructor arguments.
2016-04-16 17:49:10 +00:00
.. method:: WKTWriter.write(geom)
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
Returns the WKT of the given geometry. Example::
2010-03-26 20:14:53 +00:00
2016-04-16 17:49:10 +00:00
>>> from django.contrib.gis.geos import Point, WKTWriter
>>> pnt = Point(1, 1)
>>> wkt_w = WKTWriter()
>>> wkt_w.write(pnt)
'POINT (1.0000000000000000 1.0000000000000000)'
2016-04-16 08:50:24 +00:00
2016-04-16 17:49:10 +00:00
.. attribute:: WKTWriter.outdim
2016-04-16 08:50:24 +00:00
2016-04-16 17:49:10 +00:00
See :attr:`WKBWriter.outdim`.
2015-12-05 14:05:52 +00:00
2016-04-16 17:49:10 +00:00
.. attribute:: WKTWriter.trim
2015-12-05 14:05:52 +00:00
2016-04-16 17:49:10 +00:00
This property is used to enable or disable trimming of
unnecessary decimals.
2015-12-05 14:05:52 +00:00
2016-04-16 17:49:10 +00:00
>>> from django.contrib.gis.geos import Point, WKTWriter
>>> pnt = Point(1, 1)
>>> wkt_w = WKTWriter()
>>> wkt_w.trim
False
>>> wkt_w.write(pnt)
'POINT (1.0000000000000000 1.0000000000000000)'
>>> wkt_w.trim = True
>>> wkt_w.write(pnt)
'POINT (1 1)'
2015-12-05 14:05:52 +00:00
2016-04-16 17:49:10 +00:00
.. attribute:: WKTWriter.precision
2015-12-05 14:05:52 +00:00
2016-04-16 17:49:10 +00:00
This property controls the rounding precision of coordinates;
if set to ``None`` rounding is disabled.
>>> from django.contrib.gis.geos import Point, WKTWriter
>>> pnt = Point(1.44, 1.66)
>>> wkt_w = WKTWriter()
>>> print(wkt_w.precision)
None
>>> wkt_w.write(pnt)
'POINT (1.4399999999999999 1.6599999999999999)'
>>> wkt_w.precision = 0
>>> wkt_w.write(pnt)
'POINT (1 2)'
>>> wkt_w.precision = 1
>>> wkt_w.write(pnt)
'POINT (1.4 1.7)'
2010-03-26 20:14:53 +00:00
.. rubric:: Footnotes
2017-03-16 18:01:45 +00:00
.. [#fnogc] *See* `PostGIS EWKB, EWKT and Canonical Forms <https://postgis.net/docs/using_postgis_dbmanagement.html#EWKB_EWKT>`_, PostGIS documentation at Ch. 4.1.2.
2010-03-26 20:14:53 +00:00
Settings
========
.. setting:: GEOS_LIBRARY_PATH
2016-01-24 21:26:11 +00:00
``GEOS_LIBRARY_PATH``
---------------------
2010-03-26 20:14:53 +00:00
A string specifying the location of the GEOS C library. Typically,
this setting is only used if the GEOS C library is in a non-standard
location (e.g., ``/home/bob/lib/libgeos_c.so``).
.. note::
2010-12-26 00:37:14 +00:00
The setting must be the *full* path to the **C** shared library; in
2010-03-26 20:14:53 +00:00
other words you want to use ``libgeos_c.so``, not ``libgeos.so``.
2013-01-01 13:12:42 +00:00
Exceptions
==========
.. exception:: GEOSException
2016-04-16 17:49:10 +00:00
The base GEOS exception, indicates a GEOS-related error.