geos.txt 28 KB

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