gdal.txt 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. .. _ref-gdal:
  2. ========
  3. GDAL API
  4. ========
  5. .. module:: django.contrib.gis.gdal
  6. :synopsis: GeoDjango's high-level interface to the GDAL library.
  7. `GDAL`__ stands for **G**\ eospatial **D**\ ata **A**\ bstraction **L**\ ibrary,
  8. and is a veritable "swiss army knife" of GIS data functionality. A subset
  9. of GDAL is the `OGR`__ Simple Features Library, which specializes
  10. in reading and writing vector geographic data in a variety of standard
  11. formats.
  12. GeoDjango provides a high-level Python interface for some of the
  13. capabilities of OGR, including the reading and coordinate transformation
  14. of vector spatial data.
  15. .. note::
  16. Although the module is named ``gdal``, GeoDjango only supports
  17. some of the capabilities of OGR. Thus, none of GDAL's features
  18. with respect to raster (image) data are supported at this time.
  19. __ http://www.gdal.org/
  20. __ http://www.gdal.org/ogr/
  21. Overview
  22. ========
  23. Sample Data
  24. -----------
  25. The GDAL/OGR tools described here are designed to help you read in
  26. your geospatial data, in order for most of them to be useful you have
  27. to have some data to work with. If you're starting out and don't yet
  28. have any data of your own to use, GeoDjango comes with a number of
  29. simple data sets that you can use for testing. This snippet will
  30. determine where these sample files are installed on your computer::
  31. >>> import os
  32. >>> import django.contrib.gis
  33. >>> GIS_PATH = os.path.dirname(django.contrib.gis.__file__)
  34. >>> CITIES_PATH = os.path.join(GIS_PATH, 'tests/data/cities/cities.shp')
  35. Vector Data Source Objects
  36. ==========================
  37. ``DataSource``
  38. --------------
  39. :class:`DataSource` is a wrapper for the OGR data source object that
  40. supports reading data from a variety of OGR-supported geospatial file
  41. formats and data sources using a simple, consistent interface. Each
  42. data source is represented by a :class:`DataSource` object which contains
  43. one or more layers of data. Each layer, represented by a :class:`Layer`
  44. object, contains some number of geographic features (:class:`Feature`),
  45. information about the type of features contained in that layer (e.g.
  46. points, polygons, etc.), as well as the names and types of any
  47. additional fields (:class:`Field`) of data that may be associated with
  48. each feature in that layer.
  49. .. class:: DataSource(ds_input)
  50. The constructor for ``DataSource`` just a single parameter: the path of
  51. the file you want to read. However, OGR
  52. also supports a variety of more complex data sources, including
  53. databases, that may be accessed by passing a special name string instead
  54. of a path. For more information, see the `OGR Vector Formats`__
  55. documentation. The :attr:`name` property of a ``DataSource``
  56. instance gives the OGR name of the underlying data source that it is
  57. using.
  58. Once you've created your ``DataSource``, you can find out how many
  59. layers of data it contains by accessing the :attr:`layer_count` property,
  60. or (equivalently) by using the ``len()`` function. For information on
  61. accessing the layers of data themselves, see the next section::
  62. >>> from django.contrib.gis.gdal import DataSource
  63. >>> ds = DataSource(CITIES_PATH)
  64. >>> ds.name # The exact filename may be different on your computer
  65. '/usr/local/lib/python2.7/site-packages/django/contrib/gis/tests/data/cities/cities.shp'
  66. >>> ds.layer_count # This file only contains one layer
  67. 1
  68. .. attribute:: layer_count
  69. Returns the number of layers in the data source.
  70. .. attribute:: name
  71. Returns the name of the data source.
  72. __ http://www.gdal.org/ogr/ogr_formats.html
  73. ``Layer``
  74. ---------
  75. .. class:: Layer
  76. ``Layer`` is a wrapper for a layer of data in a ``DataSource`` object.
  77. You never create a ``Layer`` object directly. Instead, you retrieve
  78. them from a :class:`DataSource` object, which is essentially a standard
  79. Python container of ``Layer`` objects. For example, you can access a
  80. specific layer by its index (e.g. ``ds[0]`` to access the first
  81. layer), or you can iterate over all the layers in the container in a
  82. ``for`` loop. The ``Layer`` itself acts as a container for geometric
  83. features.
  84. Typically, all the features in a given layer have the same geometry type.
  85. The :attr:`geom_type` property of a layer is an :class:`OGRGeomType`
  86. that identifies the feature type. We can use it to print out some basic
  87. information about each layer in a :class:`DataSource`::
  88. >>> for layer in ds:
  89. ... print('Layer "%s": %i %ss' % (layer.name, len(layer), layer.geom_type.name))
  90. ...
  91. Layer "cities": 3 Points
  92. The example output is from the cities data source, loaded above, which
  93. evidently contains one layer, called ``"cities"``, which contains three
  94. point features. For simplicity, the examples below assume that you've
  95. stored that layer in the variable ``layer``::
  96. >>> layer = ds[0]
  97. .. attribute:: name
  98. Returns the name of this layer in the data source.
  99. >>> layer.name
  100. 'cities'
  101. .. attribute:: num_feat
  102. Returns the number of features in the layer. Same as ``len(layer)``::
  103. >>> layer.num_feat
  104. 3
  105. .. attribute:: geom_type
  106. Returns the geometry type of the layer, as an :class:`OGRGeomType`
  107. object::
  108. >>> layer.geom_type.name
  109. 'Point'
  110. .. attribute:: num_fields
  111. Returns the number of fields in the layer, i.e the number of fields of
  112. data associated with each feature in the layer::
  113. >>> layer.num_fields
  114. 4
  115. .. attribute:: fields
  116. Returns a list of the names of each of the fields in this layer::
  117. >>> layer.fields
  118. ['Name', 'Population', 'Density', 'Created']
  119. .. attribute field_types
  120. Returns a list of the data types of each of the fields in this layer.
  121. These are subclasses of ``Field``, discussed below::
  122. >>> [ft.__name__ for ft in layer.field_types]
  123. ['OFTString', 'OFTReal', 'OFTReal', 'OFTDate']
  124. .. attribute:: field_widths
  125. Returns a list of the maximum field widths for each of the fields in
  126. this layer::
  127. >>> layer.field_widths
  128. [80, 11, 24, 10]
  129. .. attribute:: field_precisions
  130. Returns a list of the numeric precisions for each of the fields in
  131. this layer. This is meaningless (and set to zero) for non-numeric
  132. fields::
  133. >>> layer.field_precisions
  134. [0, 0, 15, 0]
  135. .. attribute:: extent
  136. Returns the spatial extent of this layer, as an :class:`Envelope`
  137. object::
  138. >>> layer.extent.tuple
  139. (-104.609252, 29.763374, -95.23506, 38.971823)
  140. .. attribute:: srs
  141. Property that returns the :class:`SpatialReference` associated
  142. with this layer::
  143. >>> print(layer.srs)
  144. GEOGCS["GCS_WGS_1984",
  145. DATUM["WGS_1984",
  146. SPHEROID["WGS_1984",6378137,298.257223563]],
  147. PRIMEM["Greenwich",0],
  148. UNIT["Degree",0.017453292519943295]]
  149. If the :class:`Layer` has no spatial reference information associated
  150. with it, ``None`` is returned.
  151. .. attribute:: spatial_filter
  152. Property that may be used to retrieve or set a spatial filter for this
  153. layer. A spatial filter can only be set with an :class:`OGRGeometry`
  154. instance, a 4-tuple extent, or ``None``. When set with something
  155. other than ``None``, only features that intersect the filter will be
  156. returned when iterating over the layer::
  157. >>> print(layer.spatial_filter)
  158. None
  159. >>> print(len(layer))
  160. 3
  161. >>> [feat.get('Name') for feat in layer]
  162. ['Pueblo', 'Lawrence', 'Houston']
  163. >>> ks_extent = (-102.051, 36.99, -94.59, 40.00) # Extent for state of Kansas
  164. >>> layer.spatial_filter = ks_extent
  165. >>> len(layer)
  166. 1
  167. >>> [feat.get('Name') for feat in layer]
  168. ['Lawrence']
  169. >>> layer.spatial_filter = None
  170. >>> len(layer)
  171. 3
  172. .. method:: get_fields()
  173. A method that returns a list of the values of a given field for each
  174. feature in the layer::
  175. >>> layer.get_fields('Name')
  176. ['Pueblo', 'Lawrence', 'Houston']
  177. .. method:: get_geoms([geos=False])
  178. A method that returns a list containing the geometry of each feature
  179. in the layer. If the optional argument ``geos`` is set to ``True``
  180. then the geometries are converted to :class:`~django.contrib.gis.geos.GEOSGeometry`
  181. objects. Otherwise, they are returned as :class:`OGRGeometry` objects::
  182. >>> [pt.tuple for pt in layer.get_geoms()]
  183. [(-104.609252, 38.255001), (-95.23506, 38.971823), (-95.363151, 29.763374)]
  184. .. method:: test_capability(capability)
  185. Returns a boolean indicating whether this layer supports the
  186. given capability (a string). Examples of valid capability strings
  187. include: ``'RandomRead'``, ``'SequentialWrite'``, ``'RandomWrite'``,
  188. ``'FastSpatialFilter'``, ``'FastFeatureCount'``, ``'FastGetExtent'``,
  189. ``'CreateField'``, ``'Transactions'``, ``'DeleteFeature'``, and
  190. ``'FastSetNextByIndex'``.
  191. ``Feature``
  192. -----------
  193. .. class:: Feature
  194. ``Feature`` wraps an OGR feature. You never create a ``Feature``
  195. object directly. Instead, you retrieve them from a :class:`Layer` object.
  196. Each feature consists of a geometry and a set of fields containing
  197. additional properties. The geometry of a field is accessible via its
  198. ``geom`` property, which returns an :class:`OGRGeometry` object. A ``Feature``
  199. behaves like a standard Python container for its fields, which it returns as
  200. :class:`Field` objects: you can access a field directly by its index or name,
  201. or you can iterate over a feature's fields, e.g. in a ``for`` loop.
  202. .. attribute:: geom
  203. Returns the geometry for this feature, as an ``OGRGeometry`` object::
  204. >>> city.geom.tuple
  205. (-104.609252, 38.255001)
  206. .. attribute:: get
  207. A method that returns the value of the given field (specified by name)
  208. for this feature, **not** a ``Field`` wrapper object::
  209. >>> city.get('Population')
  210. 102121
  211. .. attribute:: geom_type
  212. Returns the type of geometry for this feature, as an :class:`OGRGeomType`
  213. object. This will be the same for all features in a given layer, and
  214. is equivalent to the :attr:`Layer.geom_type` property of the
  215. :class:`Layer` object the feature came from.
  216. .. attribute:: num_fields
  217. Returns the number of fields of data associated with the feature.
  218. This will be the same for all features in a given layer, and is
  219. equivalent to the :attr:`Layer.num_fields` property of the
  220. :class:`Layer` object the feature came from.
  221. .. attribute:: fields
  222. Returns a list of the names of the fields of data associated with the
  223. feature. This will be the same for all features in a given layer, and
  224. is equivalent to the :attr:`Layer.fields` property of the :class:`Layer`
  225. object the feature came from.
  226. .. attribute:: fid
  227. Returns the feature identifier within the layer::
  228. >>> city.fid
  229. 0
  230. .. attribute:: layer_name
  231. Returns the name of the :class:`Layer` that the feature came from.
  232. This will be the same for all features in a given layer::
  233. >>> city.layer_name
  234. 'cities'
  235. .. attribute:: index
  236. A method that returns the index of the given field name. This will be
  237. the same for all features in a given layer::
  238. >>> city.index('Population')
  239. 1
  240. ``Field``
  241. ---------
  242. .. class:: Field
  243. .. attribute:: name
  244. Returns the name of this field::
  245. >>> city['Name'].name
  246. 'Name'
  247. .. attribute:: type
  248. Returns the OGR type of this field, as an integer. The
  249. ``FIELD_CLASSES`` dictionary maps these values onto
  250. subclasses of ``Field``::
  251. >>> city['Density'].type
  252. 2
  253. .. attribute:: type_name
  254. Returns a string with the name of the data type of this field::
  255. >>> city['Name'].type_name
  256. 'String'
  257. .. attribute:: value
  258. Returns the value of this field. The ``Field`` class itself
  259. returns the value as a string, but each subclass returns the
  260. value in the most appropriate form::
  261. >>> city['Population'].value
  262. 102121
  263. .. attribute:: width
  264. Returns the width of this field::
  265. >>> city['Name'].width
  266. 80
  267. .. attribute:: precision
  268. Returns the numeric precision of this field. This is meaningless (and
  269. set to zero) for non-numeric fields::
  270. >>> city['Density'].precision
  271. 15
  272. .. method:: as_double()
  273. Returns the value of the field as a double (float)::
  274. >>> city['Density'].as_double()
  275. 874.7
  276. .. method:: as_int()
  277. Returns the value of the field as an integer::
  278. >>> city['Population'].as_int()
  279. 102121
  280. .. method:: as_string()
  281. Returns the value of the field as a string::
  282. >>> city['Name'].as_string()
  283. 'Pueblo'
  284. .. method:: as_datetime()
  285. Returns the value of the field as a tuple of date and time components::
  286. >>> city['Created'].as_datetime()
  287. (c_long(1999), c_long(5), c_long(23), c_long(0), c_long(0), c_long(0), c_long(0))
  288. ``Driver``
  289. ----------
  290. .. class:: Driver(dr_input)
  291. The ``Driver`` class is used internally to wrap an OGR :class:`DataSource` driver.
  292. .. attribute:: driver_count
  293. Returns the number of OGR vector drivers currently registered.
  294. OGR Geometries
  295. ==============
  296. ``OGRGeometry``
  297. ---------------
  298. :class:`OGRGeometry` objects share similar functionality with
  299. :class:`~django.contrib.gis.geos.GEOSGeometry` objects, and are thin
  300. wrappers around OGR's internal geometry representation. Thus,
  301. they allow for more efficient access to data when using :class:`DataSource`.
  302. Unlike its GEOS counterpart, :class:`OGRGeometry` supports spatial reference
  303. systems and coordinate transformation::
  304. >>> from django.contrib.gis.gdal import OGRGeometry
  305. >>> polygon = OGRGeometry('POLYGON((0 0, 5 0, 5 5, 0 5))')
  306. .. class:: OGRGeometry(geom_input[, srs=None])
  307. This object is a wrapper for the `OGR Geometry`__ class.
  308. These objects are instantiated directly from the given ``geom_input``
  309. parameter, which may be a string containing WKT, HEX, GeoJSON, a ``buffer``
  310. containing WKB data, or an :class:`OGRGeomType` object. These objects
  311. are also returned from the :class:`Feature.geom` attribute, when
  312. reading vector data from :class:`Layer` (which is in turn a part of
  313. a :class:`DataSource`).
  314. __ http://www.gdal.org/ogr/classOGRGeometry.html
  315. .. classmethod:: from_bbox(bbox)
  316. Constructs a :class:`Polygon` from the given bounding-box (a 4-tuple).
  317. .. method:: __len__
  318. Returns the number of points in a :class:`LineString`, the
  319. number of rings in a :class:`Polygon`, or the number of geometries in a
  320. :class:`GeometryCollection`. Not applicable to other geometry types.
  321. .. method:: __iter__
  322. Iterates over the points in a :class:`LineString`, the rings in a
  323. :class:`Polygon`, or the geometries in a :class:`GeometryCollection`.
  324. Not applicable to other geometry types.
  325. .. method:: __getitem__
  326. Returns the point at the specified index for a :class:`LineString`, the
  327. interior ring at the specified index for a :class:`Polygon`, or the geometry
  328. at the specified index in a :class:`GeometryCollection`. Not applicable to
  329. other geometry types.
  330. .. attribute:: dimension
  331. Returns the number of coordinated dimensions of the geometry, i.e. 0
  332. for points, 1 for lines, and so forth::
  333. >> polygon.dimension
  334. 2
  335. .. attribute:: coord_dim
  336. Returns or sets the coordinate dimension of this geometry. For
  337. example, the value would be 2 for two-dimensional geometries.
  338. .. attribute:: geom_count
  339. Returns the number of elements in this geometry::
  340. >>> polygon.geom_count
  341. 1
  342. .. attribute:: point_count
  343. Returns the number of points used to describe this geometry::
  344. >>> polygon.point_count
  345. 4
  346. .. attribute:: num_points
  347. Alias for :attr:`point_count`.
  348. .. attribute:: num_coords
  349. Alias for :attr:`point_count`.
  350. .. attribute:: geom_type
  351. Returns the type of this geometry, as an :class:`OGRGeomType` object.
  352. .. attribute:: geom_name
  353. Returns the name of the type of this geometry::
  354. >>> polygon.geom_name
  355. 'POLYGON'
  356. .. attribute:: area
  357. Returns the area of this geometry, or 0 for geometries that do not
  358. contain an area::
  359. >>> polygon.area
  360. 25.0
  361. .. attribute:: envelope
  362. Returns the envelope of this geometry, as an :class:`Envelope` object.
  363. .. attribute:: extent
  364. Returns the envelope of this geometry as a 4-tuple, instead of as an
  365. :class:`Envelope` object::
  366. >>> point.extent
  367. (0.0, 0.0, 5.0, 5.0)
  368. .. attribute:: srs
  369. This property controls the spatial reference for this geometry, or
  370. ``None`` if no spatial reference system has been assigned to it.
  371. If assigned, accessing this property returns a :class:`SpatialReference`
  372. object. It may be set with another :class:`SpatialReference` object,
  373. or any input that :class:`SpatialReference` accepts. Example::
  374. >>> city.geom.srs.name
  375. 'GCS_WGS_1984'
  376. .. attribute:: srid
  377. Returns or sets the spatial reference identifier corresponding to
  378. :class:`SpatialReference` of this geometry. Returns ``None`` if
  379. there is no spatial reference information associated with this
  380. geometry, or if an SRID cannot be determined.
  381. .. attribute:: geos
  382. Returns a :class:`~django.contrib.gis.geos.GEOSGeometry` object
  383. corresponding to this geometry.
  384. .. attribute:: gml
  385. Returns a string representation of this geometry in GML format::
  386. >>> OGRGeometry('POINT(1 2)').gml
  387. '<gml:Point><gml:coordinates>1,2</gml:coordinates></gml:Point>'
  388. .. attribute:: hex
  389. Returns a string representation of this geometry in HEX WKB format::
  390. >>> OGRGeometry('POINT(1 2)').hex
  391. '0101000000000000000000F03F0000000000000040'
  392. .. attribute:: json
  393. Returns a string representation of this geometry in JSON format::
  394. >>> OGRGeometry('POINT(1 2)').json
  395. '{ "type": "Point", "coordinates": [ 1.000000, 2.000000 ] }'
  396. .. attribute:: kml
  397. Returns a string representation of this geometry in KML format.
  398. .. attribute:: wkb_size
  399. Returns the size of the WKB buffer needed to hold a WKB representation
  400. of this geometry::
  401. >>> OGRGeometry('POINT(1 2)').wkb_size
  402. 21
  403. .. attribute:: wkb
  404. Returns a ``buffer`` containing a WKB representation of this geometry.
  405. .. attribute:: wkt
  406. Returns a string representation of this geometry in WKT format.
  407. .. attribute:: ewkt
  408. Returns the EWKT representation of this geometry.
  409. .. method:: clone()
  410. Returns a new :class:`OGRGeometry` clone of this geometry object.
  411. .. method:: close_rings()
  412. If there are any rings within this geometry that have not been closed,
  413. this routine will do so by adding the starting point to the end::
  414. >>> triangle = OGRGeometry('LINEARRING (0 0,0 1,1 0)')
  415. >>> triangle.close_rings()
  416. >>> triangle.wkt
  417. 'LINEARRING (0 0,0 1,1 0,0 0)'
  418. .. method:: transform(coord_trans, clone=False)
  419. Transforms this geometry to a different spatial reference system. May
  420. take a :class:`CoordTransform` object, a :class:`SpatialReference` object,
  421. or any other input accepted by :class:`SpatialReference` (including
  422. spatial reference WKT and PROJ.4 strings, or an integer SRID).
  423. By default nothing is returned and the geometry is transformed in-place.
  424. However, if the ``clone`` keyword is set to ``True`` then a transformed
  425. clone of this geometry is returned instead.
  426. .. method:: intersects(other)
  427. Returns ``True`` if this geometry intersects the other, otherwise returns
  428. ``False``.
  429. .. method:: equals(other)
  430. Returns ``True`` if this geometry is equivalent to the other, otherwise returns
  431. ``False``.
  432. .. method:: disjoint(other)
  433. Returns ``True`` if this geometry is spatially disjoint to (i.e. does
  434. not intersect) the other, otherwise returns ``False``.
  435. .. method:: touches(other)
  436. Returns ``True`` if this geometry touches the other, otherwise returns
  437. ``False``.
  438. .. method:: crosses(other)
  439. Returns ``True`` if this geometry crosses the other, otherwise returns
  440. ``False``.
  441. .. method:: within(other)
  442. Returns ``True`` if this geometry is contained within the other, otherwise returns
  443. ``False``.
  444. .. method:: contains(other)
  445. Returns ``True`` if this geometry contains the other, otherwise returns
  446. ``False``.
  447. .. method:: overlaps(other)
  448. Returns ``True`` if this geometry overlaps the other, otherwise returns
  449. ``False``.
  450. .. method:: boundary
  451. The boundary of this geometry, as a new :class:`OGRGeometry` object.
  452. .. attribute:: convex_hull
  453. The smallest convex polygon that contains this geometry, as a new
  454. :class:`OGRGeometry` object.
  455. .. method:: difference
  456. Returns the region consisting of the difference of this geometry and
  457. the other, as a new :class:`OGRGeometry` object.
  458. .. method:: intersection
  459. Returns the region consisting of the intersection of this geometry and
  460. the other, as a new :class:`OGRGeometry` object.
  461. .. method:: sym_difference
  462. Returns the region consisting of the symmetric difference of this
  463. geometry and the other, as a new :class:`OGRGeometry` object.
  464. .. method:: union
  465. Returns the region consisting of the union of this geometry and
  466. the other, as a new :class:`OGRGeometry` object.
  467. .. attribute:: tuple
  468. Returns the coordinates of a point geometry as a tuple, the
  469. coordinates of a line geometry as a tuple of tuples, and so forth::
  470. >>> OGRGeometry('POINT (1 2)').tuple
  471. (1.0, 2.0)
  472. >>> OGRGeometry('LINESTRING (1 2,3 4)').tuple
  473. ((1.0, 2.0), (3.0, 4.0))
  474. .. attribute:: coords
  475. An alias for :attr:`tuple`.
  476. .. class:: Point
  477. .. attribute:: x
  478. Returns the X coordinate of this point::
  479. >>> OGRGeometry('POINT (1 2)').x
  480. 1.0
  481. .. attribute:: y
  482. Returns the Y coordinate of this point::
  483. >>> OGRGeometry('POINT (1 2)').y
  484. 2.0
  485. .. attribute:: z
  486. Returns the Z coordinate of this point, or ``None`` if the
  487. the point does not have a Z coordinate::
  488. >>> OGRGeometry('POINT (1 2 3)').z
  489. 3.0
  490. .. class:: LineString
  491. .. attribute:: x
  492. Returns a list of X coordinates in this line::
  493. >>> OGRGeometry('LINESTRING (1 2,3 4)').x
  494. [1.0, 3.0]
  495. .. attribute:: y
  496. Returns a list of Y coordinates in this line::
  497. >>> OGRGeometry('LINESTRING (1 2,3 4)').y
  498. [2.0, 4.0]
  499. .. attribute:: z
  500. Returns a list of Z coordinates in this line, or ``None`` if the
  501. line does not have Z coordinates::
  502. >>> OGRGeometry('LINESTRING (1 2 3,4 5 6)').z
  503. [3.0, 6.0]
  504. .. class:: Polygon
  505. .. attribute:: shell
  506. Returns the shell or exterior ring of this polygon, as a ``LinearRing``
  507. geometry.
  508. .. attribute:: exterior_ring
  509. An alias for :attr:`shell`.
  510. .. attribute:: centroid
  511. Returns a :class:`Point` representing the centroid of this polygon.
  512. .. class:: GeometryCollection
  513. .. method:: add(geom)
  514. Adds a geometry to this geometry collection. Not applicable to other
  515. geometry types.
  516. ``OGRGeomType``
  517. ---------------
  518. .. class:: OGRGeomType(type_input)
  519. This class allows for the representation of an OGR geometry type
  520. in any of several ways::
  521. >>> from django.contrib.gis.gdal import OGRGeomType
  522. >>> gt1 = OGRGeomType(3) # Using an integer for the type
  523. >>> gt2 = OGRGeomType('Polygon') # Using a string
  524. >>> gt3 = OGRGeomType('POLYGON') # It's case-insensitive
  525. >>> print(gt1 == 3, gt1 == 'Polygon') # Equivalence works w/non-OGRGeomType objects
  526. True True
  527. .. attribute:: name
  528. Returns a short-hand string form of the OGR Geometry type::
  529. >>> gt1.name
  530. 'Polygon'
  531. .. attribute:: num
  532. Returns the number corresponding to the OGR geometry type::
  533. >>> gt1.num
  534. 3
  535. .. attribute:: django
  536. Returns the Django field type (a subclass of GeometryField) to use for
  537. storing this OGR type, or ``None`` if there is no appropriate Django
  538. type::
  539. >>> gt1.django
  540. 'PolygonField'
  541. ``Envelope``
  542. ------------
  543. .. class:: Envelope(*args)
  544. Represents an OGR Envelope structure that contains the
  545. minimum and maximum X, Y coordinates for a rectangle bounding box.
  546. The naming of the variables is compatible with the OGR Envelope
  547. C structure.
  548. .. attribute:: min_x
  549. The value of the minimum X coordinate.
  550. .. attribute:: min_y
  551. The value of the maximum X coordinate.
  552. .. attribute:: max_x
  553. The value of the minimum Y coordinate.
  554. .. attribute:: max_y
  555. The value of the maximum Y coordinate.
  556. .. attribute:: ur
  557. The upper-right coordinate, as a tuple.
  558. .. attribute:: ll
  559. The lower-left coordinate, as a tuple.
  560. .. attribute:: tuple
  561. A tuple representing the envelope.
  562. .. attribute:: wkt
  563. A string representing this envelope as a polygon in WKT format.
  564. .. method:: expand_to_include(*args)
  565. Coordinate System Objects
  566. =========================
  567. ``SpatialReference``
  568. --------------------
  569. .. class:: SpatialReference(srs_input)
  570. Spatial reference objects are initialized on the given ``srs_input``,
  571. which may be one of the following:
  572. * OGC Well Known Text (WKT) (a string)
  573. * EPSG code (integer or string)
  574. * PROJ.4 string
  575. * A shorthand string for well-known standards (``'WGS84'``, ``'WGS72'``, ``'NAD27'``, ``'NAD83'``)
  576. Example::
  577. >>> wgs84 = SpatialReference('WGS84') # shorthand string
  578. >>> wgs84 = SpatialReference(4326) # EPSG code
  579. >>> wgs84 = SpatialReference('EPSG:4326') # EPSG string
  580. >>> proj4 = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs '
  581. >>> wgs84 = SpatialReference(proj4) # PROJ.4 string
  582. >>> wgs84 = SpatialReference("""GEOGCS["WGS 84",
  583. DATUM["WGS_1984",
  584. SPHEROID["WGS 84",6378137,298.257223563,
  585. AUTHORITY["EPSG","7030"]],
  586. AUTHORITY["EPSG","6326"]],
  587. PRIMEM["Greenwich",0,
  588. AUTHORITY["EPSG","8901"]],
  589. UNIT["degree",0.01745329251994328,
  590. AUTHORITY["EPSG","9122"]],
  591. AUTHORITY["EPSG","4326"]]""") # OGC WKT
  592. .. method:: __getitem__(target)
  593. Returns the value of the given string attribute node, ``None`` if the node
  594. doesn't exist. Can also take a tuple as a parameter, (target, child),
  595. where child is the index of the attribute in the WKT. For example::
  596. >>> wkt = 'GEOGCS["WGS 84", DATUM["WGS_1984, ... AUTHORITY["EPSG","4326"]]')
  597. >>> srs = SpatialReference(wkt) # could also use 'WGS84', or 4326
  598. >>> print(srs['GEOGCS'])
  599. WGS 84
  600. >>> print(srs['DATUM'])
  601. WGS_1984
  602. >>> print(srs['AUTHORITY'])
  603. EPSG
  604. >>> print(srs['AUTHORITY', 1]) # The authority value
  605. 4326
  606. >>> print(srs['TOWGS84', 4]) # the fourth value in this wkt
  607. 0
  608. >>> print(srs['UNIT|AUTHORITY']) # For the units authority, have to use the pipe symbol.
  609. EPSG
  610. >>> print(srs['UNIT|AUTHORITY', 1]) # The authority value for the units
  611. 9122
  612. .. method:: attr_value(target, index=0)
  613. The attribute value for the given target node (e.g. ``'PROJCS'``).
  614. The index keyword specifies an index of the child node to return.
  615. .. method:: auth_name(target)
  616. Returns the authority name for the given string target node.
  617. .. method:: auth_code(target)
  618. Returns the authority code for the given string target node.
  619. .. method:: clone()
  620. Returns a clone of this spatial reference object.
  621. .. method:: identify_epsg()
  622. This method inspects the WKT of this SpatialReference, and will
  623. add EPSG authority nodes where an EPSG identifier is applicable.
  624. .. method:: from_esri()
  625. Morphs this SpatialReference from ESRI's format to EPSG
  626. .. method:: to_esri()
  627. Morphs this SpatialReference to ESRI's format.
  628. .. method:: validate()
  629. Checks to see if the given spatial reference is valid, if not
  630. an exception will be raised.
  631. .. method:: import_epsg(epsg)
  632. Import spatial reference from EPSG code.
  633. .. method:: import_proj(proj)
  634. Import spatial reference from PROJ.4 string.
  635. .. method:: import_user_input(user_input)
  636. .. method:: import_wkt(wkt)
  637. Import spatial reference from WKT.
  638. .. method:: import_xml(xml)
  639. Import spatial reference from XML.
  640. .. attribute:: name
  641. Returns the name of this Spatial Reference.
  642. .. attribute:: srid
  643. Returns the SRID of top-level authority, or ``None`` if undefined.
  644. .. attribute:: linear_name
  645. Returns the name of the linear units.
  646. .. attribute:: linear_units
  647. Returns the value of the linear units.
  648. .. attribute:: angular_name
  649. Returns the name of the angular units."
  650. .. attribute:: angular_units
  651. Returns the value of the angular units.
  652. .. attribute:: units
  653. Returns a 2-tuple of the units value and the units name,
  654. and will automatically determines whether to return the linear
  655. or angular units.
  656. .. attribute:: ellisoid
  657. Returns a tuple of the ellipsoid parameters for this spatial
  658. reference: (semimajor axis, semiminor axis, and inverse flattening)
  659. .. attribute:: semi_major
  660. Returns the semi major axis of the ellipsoid for this spatial reference.
  661. .. attribute:: semi_minor
  662. Returns the semi minor axis of the ellipsoid for this spatial reference.
  663. .. attribute:: inverse_flattening
  664. Returns the inverse flattening of the ellipsoid for this spatial reference.
  665. .. attribute:: geographic
  666. Returns ``True`` if this spatial reference is geographic
  667. (root node is ``GEOGCS``).
  668. .. attribute:: local
  669. Returns ``True`` if this spatial reference is local
  670. (root node is ``LOCAL_CS``).
  671. .. attribute:: projected
  672. Returns ``True`` if this spatial reference is a projected coordinate
  673. system (root node is ``PROJCS``).
  674. .. attribute:: wkt
  675. Returns the WKT representation of this spatial reference.
  676. .. attribute:: pretty_wkt
  677. Returns the 'pretty' representation of the WKT.
  678. .. attribute:: proj
  679. Returns the PROJ.4 representation for this spatial reference.
  680. .. attribute:: proj4
  681. Alias for :attr:`SpatialReference.proj`.
  682. .. attribute:: xml
  683. Returns the XML representation of this spatial reference.
  684. ``CoordTransform``
  685. ------------------
  686. .. class:: CoordTransform(source, target)
  687. Represents a coordinate system transform. It is initialized with two
  688. :class:`SpatialReference`, representing the source and target coordinate
  689. systems, respectively. These objects should be used when performing
  690. the same coordinate transformation repeatedly on different geometries::
  691. >>> ct = CoordTransform(SpatialReference('WGS84'), SpatialReference('NAD83'))
  692. >>> for feat in layer:
  693. ... geom = feat.geom # getting clone of feature geometry
  694. ... geom.transform(ct) # transforming
  695. Settings
  696. ========
  697. .. setting:: GDAL_LIBRARY_PATH
  698. GDAL_LIBRARY_PATH
  699. -----------------
  700. A string specifying the location of the GDAL library. Typically,
  701. this setting is only used if the GDAL library is in a non-standard
  702. location (e.g., ``/home/john/lib/libgdal.so``).