geos.txt 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. .. _ref-geos:
  2. ========
  3. GEOS API
  4. ========
  5. .. module:: django.contrib.gis.geos
  6. :synopsis: GeoDjango's high-level interface to the GEOS library.
  7. Background
  8. ==========
  9. What is GEOS?
  10. -------------
  11. `GEOS`__ stands for **G**\ eometry **E**\ ngine - **O**\ pen **S**\ ource,
  12. and is a C++ library, ported from the `Java Topology Suite`__. GEOS
  13. implements the OpenGIS `Simple Features for SQL`__ spatial predicate functions
  14. and spatial operators. GEOS, now an OSGeo project, was initially developed and
  15. maintained by `Refractions Research`__ of Victoria, Canada.
  16. __ http://trac.osgeo.org/geos/
  17. __ http://sourceforge.net/projects/jts-topo-suite/
  18. __ http://www.opengeospatial.org/standards/sfs
  19. __ http://www.refractions.net/
  20. Features
  21. --------
  22. GeoDjango implements a high-level Python wrapper for the GEOS library, its
  23. features include:
  24. * A BSD-licensed interface to the GEOS geometry routines, implemented purely
  25. in Python using ``ctypes``.
  26. * Loosely-coupled to GeoDjango. For example, :class:`GEOSGeometry` objects
  27. may be used outside of a django project/application. In other words,
  28. no need to have ``DJANGO_SETTINGS_MODULE`` set or use a database, etc.
  29. * Mutability: :class:`GEOSGeometry` objects may be modified.
  30. * Cross-platform and tested; compatible with Windows, Linux, Solaris, and Mac
  31. OS X platforms.
  32. .. _geos-tutorial:
  33. Tutorial
  34. ========
  35. This section contains a brief introduction and tutorial to using
  36. :class:`GEOSGeometry` objects.
  37. Creating a Geometry
  38. -------------------
  39. :class:`GEOSGeometry` objects may be created in a few ways. The first is
  40. to simply instantiate the object on some spatial input -- the following
  41. are examples of creating the same geometry from WKT, HEX, WKB, and GeoJSON::
  42. >>> from django.contrib.gis.geos import GEOSGeometry
  43. >>> pnt = GEOSGeometry('POINT(5 23)') # WKT
  44. >>> pnt = GEOSGeometry('010100000000000000000014400000000000003740') # HEX
  45. >>> pnt = GEOSGeometry(buffer('\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x007@'))
  46. >>> pnt = GEOSGeometry('{ "type": "Point", "coordinates": [ 5.000000, 23.000000 ] }') # GeoJSON
  47. Another option is to use the constructor for the specific geometry type
  48. that you wish to create. For example, a :class:`Point` object may be
  49. created by passing in the X and Y coordinates into its constructor::
  50. >>> from django.contrib.gis.geos import Point
  51. >>> pnt = Point(5, 23)
  52. Finally, there are :func:`fromstr` and :func:`fromfile` factory methods, which
  53. return a :class:`GEOSGeometry` object from an input string or a file::
  54. >>> from django.contrib.gis.geos import fromstr, fromfile
  55. >>> pnt = fromstr('POINT(5 23)')
  56. >>> pnt = fromfile('/path/to/pnt.wkt')
  57. >>> pnt = fromfile(open('/path/to/pnt.wkt'))
  58. .. _geos-exceptions-in-logfile:
  59. .. admonition:: My logs are filled with GEOS-related errors
  60. You find many ``TypeError`` or ``AttributeError`` exceptions filling your
  61. Web server's log files. This generally means that you are creating GEOS
  62. objects at the top level of some of your Python modules. Then, due to a race
  63. condition in the garbage collector, your module is garbage collected before
  64. the GEOS object. To prevent this, create :class:`GEOSGeometry` objects
  65. inside the local scope of your functions/methods.
  66. Geometries are Pythonic
  67. -----------------------
  68. :class:`GEOSGeometry` objects are 'Pythonic', in other words components may
  69. be accessed, modified, and iterated over using standard Python conventions.
  70. For example, you can iterate over the coordinates in a :class:`Point`::
  71. >>> pnt = Point(5, 23)
  72. >>> [coord for coord in pnt]
  73. [5.0, 23.0]
  74. With any geometry object, the :attr:`GEOSGeometry.coords` property
  75. may be used to get the geometry coordinates as a Python tuple::
  76. >>> pnt.coords
  77. (5.0, 23.0)
  78. You can get/set geometry components using standard Python indexing
  79. techniques. However, what is returned depends on the geometry type
  80. of the object. For example, indexing on a :class:`LineString`
  81. returns a coordinate tuple::
  82. >>> from django.contrib.gis.geos import LineString
  83. >>> line = LineString((0, 0), (0, 50), (50, 50), (50, 0), (0, 0))
  84. >>> line[0]
  85. (0.0, 0.0)
  86. >>> line[-2]
  87. (50.0, 0.0)
  88. Whereas indexing on a :class:`Polygon` will return the ring
  89. (a :class:`LinearRing` object) corresponding to the index::
  90. >>> from django.contrib.gis.geos import Polygon
  91. >>> poly = Polygon( ((0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0, 0.0), (0.0, 0.0)) )
  92. >>> poly[0]
  93. <LinearRing object at 0x1044395b0>
  94. >>> poly[0][-2] # second-to-last coordinate of external ring
  95. (50.0, 0.0)
  96. In addition, coordinates/components of the geometry may added or modified,
  97. just like a Python list::
  98. >>> line[0] = (1.0, 1.0)
  99. >>> line.pop()
  100. (0.0, 0.0)
  101. >>> line.append((1.0, 1.0))
  102. >>> line.coords
  103. ((1.0, 1.0), (0.0, 50.0), (50.0, 50.0), (50.0, 0.0), (1.0, 1.0))
  104. Geometry Objects
  105. ================
  106. ``GEOSGeometry``
  107. ----------------
  108. .. class:: GEOSGeometry(geo_input[, srid=None])
  109. :param geo_input: Geometry input value (string or buffer)
  110. :param srid: spatial reference identifier
  111. :type srid: int
  112. This is the base class for all GEOS geometry objects. It initializes on the
  113. given ``geo_input`` argument, and then assumes the proper geometry subclass
  114. (e.g., ``GEOSGeometry('POINT(1 1)')`` will create a :class:`Point` object).
  115. The following input formats, along with their corresponding Python types,
  116. are accepted:
  117. ============= ======================
  118. Format Input Type
  119. ============= ======================
  120. WKT / EWKT ``str`` or ``unicode``
  121. HEX / HEXEWKB ``str`` or ``unicode``
  122. WKB / EWKB ``buffer``
  123. GeoJSON ``str`` or ``unicode``
  124. ============= ======================
  125. .. note::
  126. The new 3D/4D WKT notation with an intermediary Z or M (like
  127. ``POINT Z (3, 4, 5)``) is only supported with GEOS 3.3.0 or later.
  128. Properties
  129. ~~~~~~~~~~
  130. .. attribute:: GEOSGeometry.coords
  131. Returns the coordinates of the geometry as a tuple.
  132. .. attribute:: GEOSGeometry.empty
  133. Returns whether or not the set of points in the geometry is empty.
  134. .. attribute:: GEOSGeometry.geom_type
  135. Returns a string corresponding to the type of geometry. For example::
  136. >>> pnt = GEOSGeometry('POINT(5 23)')
  137. >>> pnt.geom_type
  138. 'Point'
  139. .. attribute:: GEOSGeometry.geom_typeid
  140. Returns the GEOS geometry type identification number. The following table
  141. shows the value for each geometry type:
  142. =========================== ========
  143. Geometry ID
  144. =========================== ========
  145. :class:`Point` 0
  146. :class:`LineString` 1
  147. :class:`LinearRing` 2
  148. :class:`Polygon` 3
  149. :class:`MultiPoint` 4
  150. :class:`MultiLineString` 5
  151. :class:`MultiPolygon` 6
  152. :class:`GeometryCollection` 7
  153. =========================== ========
  154. .. attribute:: GEOSGeometry.num_coords
  155. Returns the number of coordinates in the geometry.
  156. .. attribute:: GEOSGeometry.num_geom
  157. Returns the number of geometries in this geometry. In other words, will
  158. return 1 on anything but geometry collections.
  159. .. attribute:: GEOSGeometry.hasz
  160. Returns a boolean indicating whether the geometry is three-dimensional.
  161. .. attribute:: GEOSGeometry.ring
  162. Returns a boolean indicating whether the geometry is a ``LinearRing``.
  163. .. attribute:: GEOSGeometry.simple
  164. Returns a boolean indicating whether the geometry is 'simple'. A geometry
  165. is simple if and only if it does not intersect itself (except at boundary
  166. points). For example, a :class:`LineString` object is not simple if it
  167. intersects itself. Thus, :class:`LinearRing` and :class`Polygon` objects
  168. are always simple because they do cannot intersect themselves, by
  169. definition.
  170. .. attribute:: GEOSGeometry.valid
  171. Returns a boolean indicating whether the geometry is valid.
  172. .. attribute:: GEOSGeometry.valid_reason
  173. Returns a string describing the reason why a geometry is invalid.
  174. .. attribute:: GEOSGeometry.srid
  175. Property that may be used to retrieve or set the SRID associated with the
  176. geometry. For example::
  177. >>> pnt = Point(5, 23)
  178. >>> print(pnt.srid)
  179. None
  180. >>> pnt.srid = 4326
  181. >>> pnt.srid
  182. 4326
  183. Output Properties
  184. ~~~~~~~~~~~~~~~~~
  185. The properties in this section export the :class:`GEOSGeometry` object into
  186. a different. This output may be in the form of a string, buffer, or even
  187. another object.
  188. .. attribute:: GEOSGeometry.ewkt
  189. Returns the "extended" Well-Known Text of the geometry. This representation
  190. is specific to PostGIS and is a super set of the OGC WKT standard. [#fnogc]_
  191. Essentially the SRID is prepended to the WKT representation, for example
  192. ``SRID=4326;POINT(5 23)``.
  193. .. note::
  194. The output from this property does not include the 3dm, 3dz, and 4d
  195. information that PostGIS supports in its EWKT representations.
  196. .. attribute:: GEOSGeometry.hex
  197. Returns the WKB of this Geometry in hexadecimal form. Please note
  198. that the SRID value is not included in this representation
  199. because it is not a part of the OGC specification (use the
  200. :attr:`GEOSGeometry.hexewkb` property instead).
  201. .. attribute:: GEOSGeometry.hexewkb
  202. Returns the EWKB of this Geometry in hexadecimal form. This is an
  203. extension of the WKB specification that includes the SRID value
  204. that are a part of this geometry.
  205. .. note::
  206. GEOS 3.1 is *required* if you want valid 3D HEXEWKB.
  207. .. attribute:: GEOSGeometry.json
  208. Returns the GeoJSON representation of the geometry.
  209. .. note::
  210. Requires GDAL.
  211. .. attribute:: GEOSGeometry.geojson
  212. Alias for :attr:`GEOSGeometry.json`.
  213. .. attribute:: GEOSGeometry.kml
  214. Returns a `KML`__ (Keyhole Markup Language) representation of the
  215. geometry. This should only be used for geometries with an SRID of
  216. 4326 (WGS84), but this restriction is not enforced.
  217. .. attribute:: GEOSGeometry.ogr
  218. Returns an :class:`~django.contrib.gis.gdal.OGRGeometry` object
  219. correspondg to the GEOS geometry.
  220. .. note::
  221. Requires GDAL.
  222. .. _wkb:
  223. .. attribute:: GEOSGeometry.wkb
  224. Returns the WKB (Well-Known Binary) representation of this Geometry
  225. as a Python buffer. SRID value is not included, use the
  226. :attr:`GEOSGeometry.ewkb` property instead.
  227. .. _ewkb:
  228. .. attribute:: GEOSGeometry.ewkb
  229. Return the EWKB representation of this Geometry as a Python buffer.
  230. This is an extension of the WKB specification that includes any SRID
  231. value that are a part of this geometry.
  232. .. note::
  233. GEOS 3.1 is *required* if you want valid 3D EWKB.
  234. .. attribute:: GEOSGeometry.wkt
  235. Returns the Well-Known Text of the geometry (an OGC standard).
  236. __ https://developers.google.com/kml/documentation/
  237. Spatial Predicate Methods
  238. ~~~~~~~~~~~~~~~~~~~~~~~~~
  239. All of the following spatial predicate methods take another
  240. :class:`GEOSGeometry` instance (``other``) as a parameter, and
  241. return a boolean.
  242. .. method:: GEOSGeometry.contains(other)
  243. Returns ``True`` if :meth:`GEOSGeometry.within` is ``False``.
  244. .. method:: GEOSGeometry.crosses(other)
  245. Returns ``True`` if the DE-9IM intersection matrix for the two Geometries
  246. is ``T*T******`` (for a point and a curve,a point and an area or a line
  247. and an area) ``0********`` (for two curves).
  248. .. method:: GEOSGeometry.disjoint(other)
  249. Returns ``True`` if the DE-9IM intersection matrix for the two geometries
  250. is ``FF*FF****``.
  251. .. method:: GEOSGeometry.equals(other)
  252. Returns ``True`` if the DE-9IM intersection matrix for the two geometries
  253. is ``T*F**FFF*``.
  254. .. method:: GEOSGeometry.equals_exact(other, tolerance=0)
  255. Returns true if the two geometries are exactly equal, up to a
  256. specified tolerance. The ``tolerance`` value should be a floating
  257. point number representing the error tolerance in the comparison, e.g.,
  258. ``poly1.equals_exact(poly2, 0.001)`` will compare equality to within
  259. one thousandth of a unit.
  260. .. method:: GEOSGeometry.intersects(other)
  261. Returns ``True`` if :meth:`GEOSGeometry.disjoint` is ``False``.
  262. .. method:: GEOSGeometry.overlaps(other)
  263. Returns true if the DE-9IM intersection matrix for the two geometries
  264. is ``T*T***T**`` (for two points or two surfaces) ``1*T***T**``
  265. (for two curves).
  266. .. method:: GEOSGeometry.relate_pattern(other, pattern)
  267. Returns ``True`` if the elements in the DE-9IM intersection matrix
  268. for this geometry and the other matches the given ``pattern`` --
  269. a string of nine characters from the alphabet: {``T``, ``F``, ``*``, ``0``}.
  270. .. method:: GEOSGeometry.touches(other)
  271. Returns ``True`` if the DE-9IM intersection matrix for the two geometries
  272. is ``FT*******``, ``F**T*****`` or ``F***T****``.
  273. .. method:: GEOSGeometry.within(other)
  274. Returns ``True`` if the DE-9IM intersection matrix for the two geometries
  275. is ``T*F**F***``.
  276. Topological Methods
  277. ~~~~~~~~~~~~~~~~~~~
  278. .. method:: GEOSGeometry.buffer(width, quadsegs=8)
  279. Returns a :class:`GEOSGeometry` that represents all points whose distance
  280. from this geometry is less than or equal to the given ``width``. The optional
  281. ``quadsegs`` keyword sets the number of segments used to approximate a
  282. quarter circle (defaults is 8).
  283. .. method:: GEOSGeometry.difference(other)
  284. Returns a :class:`GEOSGeometry` representing the points making up this
  285. geometry that do not make up other.
  286. .. method:: GEOSGeometry.interpolate(distance)
  287. .. method:: GEOSGeometry.interpolate_normalized(distance)
  288. Given a distance (float), returns the point (or closest point) within the
  289. geometry (:class:`LineString` or :class:`MultiLineString`) at that distance.
  290. The normalized version takes the distance as a float between 0 (origin) and 1
  291. (endpoint).
  292. Reverse of :meth:`GEOSGeometry.project`.
  293. .. method:: GEOSGeometry:intersection(other)
  294. Returns a :class:`GEOSGeometry` representing the points shared by this
  295. geometry and other.
  296. .. method:: GEOSGeometry.project(point)
  297. .. method:: GEOSGeometry.project_normalized(point)
  298. Returns the distance (float) from the origin of the geometry
  299. (:class:`LineString` or :class:`MultiLineString`) to the point projected on the
  300. geometry (that is to a point of the line the closest to the given point).
  301. The normalized version returns the distance as a float between 0 (origin) and 1
  302. (endpoint).
  303. Reverse of :meth:`GEOSGeometry.interpolate`.
  304. .. method:: GEOSGeometry.relate(other)
  305. Returns the DE-9IM intersection matrix (a string) representing the
  306. topological relationship between this geometry and the other.
  307. .. method:: GEOSGeometry.simplify(tolerance=0.0, preserve_topology=False)
  308. Returns a new :class:`GEOSGeometry`, simplified using the Douglas-Peucker
  309. algorithm to the specified tolerance. A higher tolerance value implies
  310. less points in the output. If no tolerance is tolerance provided,
  311. it defaults to 0.
  312. By default, this function does not preserve topology - e.g.,
  313. :class:`Polygon` objects can be split, collapsed into lines or disappear.
  314. :class:`Polygon` holes can be created or disappear, and lines can cross.
  315. By specifying ``preserve_topology=True``, the result will have the same
  316. dimension and number of components as the input, however, this is
  317. significantly slower.
  318. .. method:: GEOSGeometry.sym_difference(other)
  319. Returns a :class:`GEOSGeometry` combining the points in this geometry
  320. not in other, and the points in other not in this geometry.
  321. .. method:: GEOSGeometry.union(other)
  322. Returns a :class:`GEOSGeometry` representing all the points in this
  323. geometry and the other.
  324. Topological Properties
  325. ~~~~~~~~~~~~~~~~~~~~~~
  326. .. attribute:: GEOSGeometry.boundary
  327. Returns the boundary as a newly allocated Geometry object.
  328. .. attribute:: GEOSGeometry.centroid
  329. Returns a :class:`Point` object representing the geometric center of
  330. the geometry. The point is not guaranteed to be on the interior
  331. of the geometry.
  332. .. attribute:: GEOSGeometry.convex_hull
  333. Returns the smallest :class:`Polygon` that contains all the points in
  334. the geometry.
  335. .. attribute:: GEOSGeometry.envelope
  336. Returns a :class:`Polygon` that represents the bounding envelope of
  337. this geometry.
  338. .. attribute:: GEOSGeometry.point_on_surface
  339. Computes and returns a :class:`Point` guaranteed to be on the interior
  340. of this geometry.
  341. Other Properties & Methods
  342. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  343. .. attribute:: GEOSGeometry.area
  344. This property returns the area of the Geometry.
  345. .. attribute:: GEOSGeometry.extent
  346. This property returns the extent of this geometry as a 4-tuple,
  347. consisting of (xmin, ymin, xmax, ymax).
  348. .. method:: GEOSGeometry.clone()
  349. This method returns a :class:`GEOSGeometry` that is a clone of the original.
  350. .. method:: GEOSGeometry.distance(geom)
  351. Returns the distance between the closest points on this geometry and the given
  352. ``geom`` (another :class:`GEOSGeometry` object).
  353. .. note::
  354. GEOS distance calculations are linear -- in other words, GEOS does not
  355. perform a spherical calculation even if the SRID specifies a geographic
  356. coordinate system.
  357. .. attribute:: GEOSGeometry.length
  358. Returns the length of this geometry (e.g., 0 for a :class:`Point`,
  359. the length of a :class:`LineString`, or the circumference of
  360. a :class:`Polygon`).
  361. .. attribute:: GEOSGeometry.prepared
  362. .. note::
  363. Support for prepared geometries requires GEOS 3.1.
  364. Returns a GEOS ``PreparedGeometry`` for the contents of this geometry.
  365. ``PreparedGeometry`` objects are optimized for the contains, intersects,
  366. and covers operations. Refer to the :ref:`prepared-geometries` documentation
  367. for more information.
  368. .. attribute:: GEOSGeometry.srs
  369. Returns a :class:`~django.contrib.gis.gdal.SpatialReference` object
  370. corresponding to the SRID of the geometry or ``None``.
  371. .. note::
  372. Requires GDAL.
  373. .. method:: GEOSGeometry.transform(ct, clone=False)
  374. Transforms the geometry according to the given coordinate transformation paramter
  375. (``ct``), which may be an integer SRID, spatial reference WKT string,
  376. a PROJ.4 string, a :class:`~django.contrib.gis.gdal.SpatialReference` object, or a
  377. :class:`~django.contrib.gis.gdal.CoordTransform` object. By default, the geometry
  378. is transformed in-place and nothing is returned. However if the ``clone`` keyword
  379. is set, then the geometry is not modified and a transformed clone of the geometry
  380. is returned instead.
  381. .. note::
  382. Requires GDAL.
  383. .. note::
  384. Prior to 1.3, this method would silently no-op if GDAL was not available.
  385. Now, a :class:`~django.contrib.gis.geos.GEOSException` is raised as
  386. application code relying on this behavior is in error. In addition,
  387. use of this method when the SRID is ``None`` or less than 0 now also generates
  388. a :class:`~django.contrib.gis.geos.GEOSException`.
  389. ``Point``
  390. ---------
  391. .. class:: Point(x, y, z=None, srid=None)
  392. ``Point`` objects are instantiated using arguments that represent
  393. the component coordinates of the point or with a single sequence
  394. coordinates. For example, the following are equivalent::
  395. >>> pnt = Point(5, 23)
  396. >>> pnt = Point([5, 23])
  397. ``LineString``
  398. --------------
  399. .. class:: LineString(*args, **kwargs)
  400. ``LineString`` objects are instantiated using arguments that are
  401. either a sequence of coordinates or :class:`Point` objects.
  402. For example, the following are equivalent::
  403. >>> ls = LineString((0, 0), (1, 1))
  404. >>> ls = LineString(Point(0, 0), Point(1, 1))
  405. In addition, ``LineString`` objects may also be created by passing
  406. in a single sequence of coordinate or :class:`Point` objects::
  407. >>> ls = LineString( ((0, 0), (1, 1)) )
  408. >>> ls = LineString( [Point(0, 0), Point(1, 1)] )
  409. ``LinearRing``
  410. --------------
  411. .. class:: LinearRing(*args, **kwargs)
  412. ``LinearRing`` objects are constructed in the exact same way as
  413. :class:`LineString` objects, however the coordinates must be
  414. *closed*, in other words, the first coordinates must be the
  415. same as the last coordinates. For example::
  416. >>> ls = LinearRing((0, 0), (0, 1), (1, 1), (0, 0))
  417. Notice that ``(0, 0)`` is the first and last coordinate -- if
  418. they were not equal, an error would be raised.
  419. ``Polygon``
  420. -----------
  421. .. class:: Polygon(*args, **kwargs)
  422. ``Polygon`` objects may be instantiated by passing in one or
  423. more parameters that represent the rings of the polygon. The
  424. parameters must either be :class:`LinearRing` instances, or
  425. a sequence that may be used to construct a :class:`LinearRing`::
  426. >>> ext_coords = ((0, 0), (0, 1), (1, 1), (1, 0), (0, 0))
  427. >>> int_coords = ((0.4, 0.4), (0.4, 0.6), (0.6, 0.6), (0.6, 0.4), (0.4, 0.4))
  428. >>> poly = Polygon(ext_coords, int_coords)
  429. >>> poly = Polygon(LinearRing(ext_coords), LinearRing(int_coords))
  430. .. classmethod:: from_bbox(bbox)
  431. Returns a polygon object from the given bounding-box, a 4-tuple
  432. comprising (xmin, ymin, xmax, ymax).
  433. .. attribute:: num_interior_rings
  434. Returns the number of interior rings in this geometry.
  435. .. admonition:: Comparing Polygons
  436. Note that it is possible to compare ``Polygon`` objects directly with ``<``
  437. or ``>``, but as the comparison is made through Polygon's
  438. :class:`LineString`, it does not mean much (but is consistent and quick).
  439. You can always force the comparison with the :attr:`~GEOSGeometry.area`
  440. property::
  441. >>> if poly_1.area > poly_2.area:
  442. >>> pass
  443. Geometry Collections
  444. ====================
  445. ``MultiPoint``
  446. --------------
  447. .. class:: MultiPoint(*args, **kwargs)
  448. ``MultiPoint`` objects may be instantiated by passing in one
  449. or more :class:`Point` objects as arguments, or a single
  450. sequence of :class:`Point` objects::
  451. >>> mp = MultiPoint(Point(0, 0), Point(1, 1))
  452. >>> mp = MultiPoint( (Point(0, 0), Point(1, 1)) )
  453. ``MultiLineString``
  454. -------------------
  455. .. class:: MultiLineString(*args, **kwargs)
  456. ``MultiLineString`` objects may be instantiated by passing in one
  457. or more :class:`LineString` objects as arguments, or a single
  458. sequence of :class:`LineString` objects::
  459. >>> ls1 = LineString((0, 0), (1, 1))
  460. >>> ls2 = LineString((2, 2), (3, 3))
  461. >>> mls = MultiLineString(ls1, ls2)
  462. >>> mls = MultiLineString([ls1, ls2])
  463. .. attribute:: merged
  464. Returns a :class:`LineString` representing the line merge of
  465. all the components in this ``MultiLineString``.
  466. ``MultiPolygon``
  467. ----------------
  468. .. class:: MultiPolygon(*args, **kwargs)
  469. ``MultiPolygon`` objects may be instantiated by passing one or
  470. more :class:`Polygon` objects as arguments, or a single sequence
  471. of :class:`Polygon` objects::
  472. >>> p1 = Polygon( ((0, 0), (0, 1), (1, 1), (0, 0)) )
  473. >>> p2 = Polygon( ((1, 1), (1, 2), (2, 2), (1, 1)) )
  474. >>> mp = MultiPolygon(p1, p2)
  475. >>> mp = MultiPolygon([p1, p2])
  476. .. attribute:: cascaded_union
  477. Returns a :class:`Polygon` that is the union of all of the component
  478. polygons in this collection. The algorithm employed is significantly
  479. more efficient (faster) than trying to union the geometries together
  480. individually. [#fncascadedunion]_
  481. .. note::
  482. GEOS 3.1 is *required* to peform cascaded unions.
  483. ``GeometryCollection``
  484. ----------------------
  485. .. class:: GeometryCollection(*args, **kwargs)
  486. ``GeometryCollection`` objects may be instantiated by passing in
  487. one or more other :class:`GEOSGeometry` as arguments, or a single
  488. sequence of :class:`GEOSGeometry` objects::
  489. >>> poly = Polygon( ((0, 0), (0, 1), (1, 1), (0, 0)) )
  490. >>> gc = GeometryCollection(Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly)
  491. >>> gc = GeometryCollection((Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly))
  492. .. _prepared-geometries:
  493. Prepared Geometries
  494. ===================
  495. In order to obtain a prepared geometry, just access the
  496. :attr:`GEOSGeometry.prepared` property. Once you have a
  497. ``PreparedGeometry`` instance its spatial predicate methods, listed below,
  498. may be used with other ``GEOSGeometry`` objects. An operation with a prepared
  499. geometry can be orders of magnitude faster -- the more complex the geometry
  500. that is prepared, the larger the speedup in the operation. For more information,
  501. please consult the `GEOS wiki page on prepared geometries <http://trac.osgeo.org/geos/wiki/PreparedGeometry>`_.
  502. .. note::
  503. GEOS 3.1 is *required* in order to use prepared geometries.
  504. For example::
  505. >>> from django.contrib.gis.geos import Point, Polygon
  506. >>> poly = Polygon.from_bbox((0, 0, 5, 5))
  507. >>> prep_poly = poly.prepared
  508. >>> prep_poly.contains(Point(2.5, 2.5))
  509. True
  510. ``PreparedGeometry``
  511. --------------------
  512. .. class:: PreparedGeometry
  513. All methods on ``PreparedGeometry`` take an ``other`` argument, which
  514. must be a :class:`GEOSGeometry` instance.
  515. .. method:: contains(other)
  516. .. method:: contains_properly(other)
  517. .. method:: covers(other)
  518. .. method:: intersects(other)
  519. Geometry Factories
  520. ==================
  521. .. function:: fromfile(file_h)
  522. :param file_h: input file that contains spatial data
  523. :type file_h: a Python ``file`` object or a string path to the file
  524. :rtype: a :class:`GEOSGeometry` corresponding to the spatial data in the file
  525. Example::
  526. >>> from django.contrib.gis.geos import fromfile
  527. >>> g = fromfile('/home/bob/geom.wkt')
  528. .. function:: fromstr(string, [,srid=None])
  529. :param string: string that contains spatial data
  530. :type string: string
  531. :param srid: spatial reference identifier
  532. :type srid: int
  533. :rtype: a :class:`GEOSGeometry` corresponding to the spatial data in the string
  534. Example::
  535. >>> from django.contrib.gis.geos import fromstr
  536. >>> pnt = fromstr('POINT(-90.5 29.5)', srid=4326)
  537. I/O Objects
  538. ===========
  539. Reader Objects
  540. --------------
  541. The reader I/O classes simply return a :class:`GEOSGeometry` instance from the
  542. WKB and/or WKT input given to their ``read(geom)`` method.
  543. .. class:: WKBReader
  544. Example::
  545. >>> from django.contrib.gis.geos import WKBReader
  546. >>> wkb_r = WKBReader()
  547. >>> wkb_r.read('0101000000000000000000F03F000000000000F03F')
  548. <Point object at 0x103a88910>
  549. .. class:: WKTReader
  550. Example::
  551. >>> from django.contrib.gis.geos import WKTReader
  552. >>> wkt_r = WKTReader()
  553. >>> wkt_r.read('POINT(1 1)')
  554. <Point object at 0x103a88b50>
  555. Writer Objects
  556. --------------
  557. All writer objects have a ``write(geom)`` method that returns either the
  558. WKB or WKT of the given geometry. In addition, :class:`WKBWriter` objects
  559. also have properties that may be used to change the byte order, and or
  560. include the SRID value (in other words, EWKB).
  561. .. class:: WKBWriter
  562. ``WKBWriter`` provides the most control over its output. By default it
  563. returns OGC-compliant WKB when its ``write`` method is called. However,
  564. it has properties that allow for the creation of EWKB, a superset of the
  565. WKB standard that includes additional information.
  566. .. method:: WKBWriter.write(geom)
  567. Returns the WKB of the given geometry as a Python ``buffer`` object.
  568. Example::
  569. >>> from django.contrib.gis.geos import Point, WKBWriter
  570. >>> pnt = Point(1, 1)
  571. >>> wkb_w = WKBWriter()
  572. >>> wkb_w.write(pnt)
  573. <read-only buffer for 0x103a898f0, size -1, offset 0 at 0x103a89930>
  574. .. method:: WKBWriter.write_hex(geom)
  575. Returns WKB of the geometry in hexadecimal. Example::
  576. >>> from django.contrib.gis.geos import Point, WKBWriter
  577. >>> pnt = Point(1, 1)
  578. >>> wkb_w = WKBWriter()
  579. >>> wkb_w.write_hex(pnt)
  580. '0101000000000000000000F03F000000000000F03F'
  581. .. attribute:: WKBWriter.byteorder
  582. This property may be be set to change the byte-order of the geometry
  583. representation.
  584. =============== =================================================
  585. Byteorder Value Description
  586. =============== =================================================
  587. 0 Big Endian (e.g., compatible with RISC systems)
  588. 1 Little Endian (e.g., compatible with x86 systems)
  589. =============== =================================================
  590. Example::
  591. >>> from django.contrib.gis.geos import Point, WKBWriter
  592. >>> wkb_w = WKBWriter()
  593. >>> pnt = Point(1, 1)
  594. >>> wkb_w.write_hex(pnt)
  595. '0101000000000000000000F03F000000000000F03F'
  596. >>> wkb_w.byteorder = 0
  597. '00000000013FF00000000000003FF0000000000000'
  598. .. attribute:: WKBWriter.outdim
  599. This property may be set to change the output dimension of the geometry
  600. representation. In other words, if you have a 3D geometry then set to 3
  601. so that the Z value is included in the WKB.
  602. ============ ===========================
  603. Outdim Value Description
  604. ============ ===========================
  605. 2 The default, output 2D WKB.
  606. 3 Output 3D WKB.
  607. ============ ===========================
  608. Example::
  609. >>> from django.contrib.gis.geos import Point, WKBWriter
  610. >>> wkb_w = WKBWriter()
  611. >>> wkb_w.outdim
  612. 2
  613. >>> pnt = Point(1, 1, 1)
  614. >>> wkb_w.write_hex(pnt) # By default, no Z value included:
  615. '0101000000000000000000F03F000000000000F03F'
  616. >>> wkb_w.outdim = 3 # Tell writer to include Z values
  617. >>> wkb_w.write_hex(pnt)
  618. '0101000080000000000000F03F000000000000F03F000000000000F03F'
  619. .. attribute:: WKBWriter.srid
  620. Set this property with a boolean to indicate whether the SRID of the
  621. geometry should be included with the WKB representation. Example::
  622. >>> from django.contrib.gis.geos import Point, WKBWriter
  623. >>> wkb_w = WKBWriter()
  624. >>> pnt = Point(1, 1, srid=4326)
  625. >>> wkb_w.write_hex(pnt) # By default, no SRID included:
  626. '0101000000000000000000F03F000000000000F03F'
  627. >>> wkb_w.srid = True # Tell writer to include SRID
  628. >>> wkb_w.write_hex(pnt)
  629. '0101000020E6100000000000000000F03F000000000000F03F'
  630. .. class:: WKTWriter
  631. .. method:: WKTWriter.write(geom)
  632. Returns the WKT of the given geometry. Example::
  633. >>> from django.contrib.gis.geos import Point, WKTWriter
  634. >>> pnt = Point(1, 1)
  635. >>> wkt_w = WKTWriter()
  636. >>> wkt_w.write(pnt)
  637. 'POINT (1.0000000000000000 1.0000000000000000)'
  638. .. rubric:: Footnotes
  639. .. [#fnogc] *See* `PostGIS EWKB, EWKT and Canonical Forms <http://postgis.refractions.net/docs/using_postgis_dbmanagement.html#EWKB_EWKT>`_, PostGIS documentation at Ch. 4.1.2.
  640. .. [#fncascadedunion] For more information, read Paul Ramsey's blog post about `(Much) Faster Unions in PostGIS 1.4 <http://blog.cleverelephant.ca/2009/01/must-faster-unions-in-postgis-14.html>`_ and Martin Davis' blog post on `Fast polygon merging in JTS using Cascaded Union <http://lin-ear-th-inking.blogspot.com/2007/11/fast-polygon-merging-in-jts-using.html>`_.
  641. Settings
  642. ========
  643. .. setting:: GEOS_LIBRARY_PATH
  644. GEOS_LIBRARY_PATH
  645. -----------------
  646. A string specifying the location of the GEOS C library. Typically,
  647. this setting is only used if the GEOS C library is in a non-standard
  648. location (e.g., ``/home/bob/lib/libgeos_c.so``).
  649. .. note::
  650. The setting must be the *full* path to the **C** shared library; in
  651. other words you want to use ``libgeos_c.so``, not ``libgeos.so``.
  652. Exceptions
  653. ==========
  654. .. exception:: GEOSException
  655. The base GEOS exception, indicates a GEOS-related error.