gdal.txt 31 KB

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