gdal.txt 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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.6/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. .. versionadded:: 1.2
  153. Property that may be used to retrieve or set a spatial filter for this
  154. layer. A spatial filter can only be set with an :class:`OGRGeometry`
  155. instance, a 4-tuple extent, or ``None``. When set with something
  156. other than ``None``, only features that intersect the filter will be
  157. returned when iterating over the layer::
  158. >>> print layer.spatial_filter
  159. None
  160. >>> print len(layer)
  161. 3
  162. >>> [feat.get('Name') for feat in layer]
  163. ['Pueblo', 'Lawrence', 'Houston']
  164. >>> ks_extent = (-102.051, 36.99, -94.59, 40.00) # Extent for state of Kansas
  165. >>> layer.spatial_filter = ks_extent
  166. >>> len(layer)
  167. 1
  168. >>> [feat.get('Name') for feat in layer]
  169. ['Lawrence']
  170. >>> layer.spatial_filter = None
  171. >>> len(layer)
  172. 3
  173. .. method:: get_fields()
  174. A method that returns a list of the values of a given field for each
  175. feature in the layer::
  176. >>> layer.get_fields('Name')
  177. ['Pueblo', 'Lawrence', 'Houston']
  178. .. method:: get_geoms([geos=False])
  179. A method that returns a list containing the geometry of each feature
  180. in the layer. If the optional argument ``geos`` is set to ``True``
  181. then the geometries are converted to :class:`~django.contrib.gis.geos.GEOSGeometry`
  182. objects. Otherwise, they are returned as :class:`OGRGeometry` objects::
  183. >>> [pt.tuple for pt in layer.get_geoms()]
  184. [(-104.609252, 38.255001), (-95.23506, 38.971823), (-95.363151, 29.763374)]
  185. .. method:: test_capability(capability)
  186. Returns a boolean indicating whether this layer supports the
  187. given capability (a string). Examples of valid capability strings
  188. include: ``'RandomRead'``, ``'SequentialWrite'``, ``'RandomWrite'``,
  189. ``'FastSpatialFilter'``, ``'FastFeatureCount'``, ``'FastGetExtent'``,
  190. ``'CreateField'``, ``'Transactions'``, ``'DeleteFeature'``, and
  191. ``'FastSetNextByIndex'``.
  192. ``Feature``
  193. -----------
  194. .. class:: Feature
  195. ``Feature`` wraps an OGR feature. You never create a ``Feature``
  196. object directly. Instead, you retrieve them from a :class:`Layer` object.
  197. Each feature consists of a geometry and a set of fields containing
  198. additional properties. The geometry of a field is accessible via its
  199. ``geom`` property, which returns an :class:`OGRGeometry` object. A ``Feature``
  200. behaves like a standard Python container for its fields, which it returns as
  201. :class:`Field` objects: you can access a field directly by its index or name,
  202. or you can iterate over a feature's fields, e.g. in a ``for`` loop.
  203. .. attribute:: geom
  204. Returns the geometry for this feature, as an ``OGRGeometry`` object::
  205. >>> city.geom.tuple
  206. (-104.609252, 38.255001)
  207. .. attribute:: get
  208. A method that returns the value of the given field (specified by name)
  209. for this feature, **not** a ``Field`` wrapper object::
  210. >>> city.get('Population')
  211. 102121
  212. .. attribute:: geom_type
  213. Returns the type of geometry for this feature, as an :class:`OGRGeomType`
  214. object. This will be the same for all features in a given layer, and
  215. is equivalent to the :attr:`Layer.geom_type` property of the
  216. :class:`Layer`` object the feature came from.
  217. .. attribute:: num_fields
  218. Returns the number of fields of data associated with the feature.
  219. This will be the same for all features in a given layer, and is
  220. equivalent to the :attr:`Layer.num_fields` property of the
  221. :class:`Layer` object the feature came from.
  222. .. attribute:: fields
  223. Returns a list of the names of the fields of data associated with the
  224. feature. This will be the same for all features in a given layer, and
  225. is equivalent to the :attr:`Layer.fields` property of the :class:`Layer`
  226. object the feature came from.
  227. .. attribute:: fid
  228. Returns the feature identifier within the layer::
  229. >>> city.fid
  230. 0
  231. .. attribute:: layer_name
  232. Returns the name of the :class:`Layer` that the feature came from.
  233. This will be the same for all features in a given layer::
  234. >>> city.layer_name
  235. 'cities'
  236. .. attribute:: index
  237. A method that returns the index of the given field name. This will be
  238. the same for all features in a given layer::
  239. >>> city.index('Population')
  240. 1
  241. ``Field``
  242. ---------
  243. .. class:: Field
  244. .. attribute:: name
  245. Returns the name of this field::
  246. >>> city['Name'].name
  247. 'Name'
  248. .. attribute:: type
  249. Returns the OGR type of this field, as an integer. The
  250. ``FIELD_CLASSES`` dictionary maps these values onto
  251. subclasses of ``Field``::
  252. >>> city['Density'].type
  253. 2
  254. .. attribute:: type_name
  255. Returns a string with the name of the data type of this field::
  256. >>> city['Name'].type_name
  257. 'String'
  258. .. attribute:: value
  259. Returns the value of this field. The ``Field`` class itself
  260. returns the value as a string, but each subclass returns the
  261. value in the most appropriate form::
  262. >>> city['Population'].value
  263. 102121
  264. .. attribute:: width
  265. Returns the width of this field::
  266. >>> city['Name'].width
  267. 80
  268. .. attribute:: precision
  269. Returns the numeric precision of this field. This is meaningless (and
  270. set to zero) for non-numeric fields::
  271. >>> city['Density'].precision
  272. 15
  273. .. method:: as_double()
  274. Returns the value of the field as a double (float)::
  275. >>> city['Density'].as_double()
  276. 874.7
  277. .. method:: as_int()
  278. Returns the value of the field as an integer::
  279. >>> city['Population'].as_int()
  280. 102121
  281. .. method:: as_string()
  282. Returns the value of the field as a string::
  283. >>> city['Name'].as_string()
  284. 'Pueblo'
  285. .. method:: as_datetime()
  286. Returns the value of the field as a tuple of date and time components::
  287. >>> city['Created'].as_datetime()
  288. (c_long(1999), c_long(5), c_long(23), c_long(0), c_long(0), c_long(0), c_long(0))
  289. ``Driver``
  290. ----------
  291. .. class:: Driver(dr_input)
  292. The ``Driver`` class is used internally to wrap an OGR :class:`DataSource` driver.
  293. .. attribute:: driver_count
  294. Returns the number of OGR vector drivers currently registered.
  295. OGR Geometries
  296. ==============
  297. ``OGRGeometry``
  298. ---------------
  299. :class:`OGRGeometry` objects share similar functionality with
  300. :class:`~django.contrib.gis.geos.GEOSGeometry` objects, and are thin
  301. wrappers around OGR's internal geometry representation. Thus,
  302. they allow for more efficient access to data when using :class:`DataSource`.
  303. Unlike its GEOS counterpart, :class:`OGRGeometry` supports spatial reference
  304. systems and coordinate transformation::
  305. >>> from django.contrib.gis.gdal import OGRGeometry
  306. >>> polygon = OGRGeometry('POLYGON((0 0, 5 0, 5 5, 0 5))')
  307. .. class:: OGRGeometry(geom_input[, srs=None])
  308. This object is a wrapper for the `OGR Geometry`__ class.
  309. These objects are instantiated directly from the given ``geom_input``
  310. parameter, which may be a string containing WKT or HEX, a ``buffer``
  311. containing WKB data, or an :class:`OGRGeomType` object. These objects
  312. are also returned from the :class:`Feature.geom` attribute, when
  313. reading vector data from :class:`Layer` (which is in turn a part of
  314. a :class:`DataSource`).
  315. __ http://www.gdal.org/ogr/classOGRGeometry.html
  316. .. classmethod:: from_bbox(bbox)
  317. Constructs a :class:`Polygon` from the given bounding-box (a 4-tuple).
  318. .. method:: __len__
  319. Returns the number of points in a :class:`LineString`, the
  320. number of rings in a :class:`Polygon`, or the number of geometries in a
  321. :class:`GeometryCollection`. Not applicable to other geometry types.
  322. .. method:: __iter__
  323. Iterates over the points in a :class:`LineString`, the rings in a
  324. :class:`Polygon`, or the geometries in a :class:`GeometryCollection`.
  325. Not applicable to other geometry types.
  326. .. method:: __getitem__
  327. Returns the point at the specified index for a :class:`LineString`, the
  328. interior ring at the specified index for a :class:`Polygon`, or the geometry
  329. at the specified index in a :class:`GeometryCollection`. Not applicable to
  330. other geometry types.
  331. .. attribute:: dimension
  332. Returns the number of coordinated dimensions of the geometry, i.e. 0
  333. for points, 1 for lines, and so forth::
  334. >> polygon.dimension
  335. 2
  336. .. attribute:: coord_dim
  337. .. versionchanged:: 1.2
  338. Returns or sets the coordinate dimension of this geometry. For
  339. example, the value would be 2 for two-dimensional geometries.
  340. .. note::
  341. Setting this property is only available in versions 1.2 and above.
  342. .. attribute:: geom_count
  343. Returns the number of elements in this geometry::
  344. >>> polygon.geom_count
  345. 1
  346. .. attribute:: point_count
  347. Returns the number of points used to describe this geometry::
  348. >>> polygon.point_count
  349. 4
  350. .. attribute:: num_points
  351. Alias for :attr:`point_count`.
  352. .. attribute:: num_coords
  353. Alias for :attr:`point_count`.
  354. .. attribute:: geom_type
  355. Returns the type of this geometry, as an :class:`OGRGeomType` object.
  356. .. attribute:: geom_name
  357. Returns the name of the type of this geometry::
  358. >>> polygon.geom_name
  359. 'POLYGON'
  360. .. attribute:: area
  361. Returns the area of this geometry, or 0 for geometries that do not
  362. contain an area::
  363. >>> polygon.area
  364. 25.0
  365. .. attribute:: envelope
  366. Returns the envelope of this geometry, as an :class:`Envelope` object.
  367. .. attribute:: extent
  368. Returns the envelope of this geometry as a 4-tuple, instead of as an
  369. :class:`Envelope` object::
  370. >>> point.extent
  371. (0.0, 0.0, 5.0, 5.0)
  372. .. attribute:: srs
  373. This property controls the spatial reference for this geometry, or
  374. ``None`` if no spatial reference system has been assigned to it.
  375. If assigned, accessing this property returns a :class:`SpatialReference`
  376. object. It may be set with another :class:`SpatialReference` object,
  377. or any input that :class:`SpatialReference` accepts. Example::
  378. >>> city.geom.srs.name
  379. 'GCS_WGS_1984'
  380. .. attribute:: srid
  381. Returns or sets the spatial reference identifier corresponding to
  382. :class:`SpatialReference` of this geometry. Returns ``None`` if
  383. there is no spatial reference information associated with this
  384. geometry, or if an SRID cannot be determined.
  385. .. attribute:: geos
  386. Returns a :class:`~django.contrib.gis.geos.GEOSGeometry` object
  387. corresponding to this geometry.
  388. .. attribute:: gml
  389. Returns a string representation of this geometry in GML format::
  390. >>> OGRGeometry('POINT(1 2)').gml
  391. '<gml:Point><gml:coordinates>1,2</gml:coordinates></gml:Point>'
  392. .. attribute:: hex
  393. Returns a string representation of this geometry in HEX WKB format::
  394. >>> OGRGeometry('POINT(1 2)').hex
  395. '0101000000000000000000F03F0000000000000040'
  396. .. attribute:: json
  397. Returns a string representation of this geometry in JSON format::
  398. >>> OGRGeometry('POINT(1 2)').json
  399. '{ "type": "Point", "coordinates": [ 1.000000, 2.000000 ] }'
  400. .. attribute:: kml
  401. Returns a string representation of this geometry in KML format.
  402. .. attribute:: wkb_size
  403. Returns the size of the WKB buffer needed to hold a WKB representation
  404. of this geometry::
  405. >>> OGRGeometry('POINT(1 2)').wkb_size
  406. 21
  407. .. attribute:: wkb
  408. Returns a ``buffer`` containing a WKB representation of this geometry.
  409. .. attribute:: wkt
  410. Returns a string representation of this geometry in WKT format.
  411. .. attribute:: ewkt
  412. .. versionadded:: 1.2
  413. Returns the EWKT representation of this geometry.
  414. .. method:: clone()
  415. Returns a new :class:`OGRGeometry` clone of this geometry object.
  416. .. method:: close_rings()
  417. If there are any rings within this geometry that have not been closed,
  418. this routine will do so by adding the starting point to the end::
  419. >>> triangle = OGRGeometry('LINEARRING (0 0,0 1,1 0)')
  420. >>> triangle.close_rings()
  421. >>> triangle.wkt
  422. 'LINEARRING (0 0,0 1,1 0,0 0)'
  423. .. method:: transform(coord_trans, clone=False)
  424. Transforms this geometry to a different spatial reference system. May
  425. take a :class:`CoordTransform` object, a :class:`SpatialReference` object,
  426. or any other input accepted by :class:`SpatialReference` (including
  427. spatial reference WKT and PROJ.4 strings, or an integer SRID).
  428. By default nothing is returned and the geometry is transformed in-place.
  429. However, if the `clone` keyword is set to ``True`` then a transformed clone
  430. of this geometry is returned instead.
  431. .. method:: intersects(other)
  432. Returns ``True`` if this geometry intersects the other, otherwise returns
  433. ``False``.
  434. .. method:: equals(other)
  435. Returns ``True`` if this geometry is equivalent to the other, otherwise returns
  436. ``False``.
  437. .. method:: disjoint(other)
  438. Returns ``True`` if this geometry is spatially disjoint to (i.e. does
  439. not intersect) the other, otherwise returns ``False``.
  440. .. method:: touches(other)
  441. Returns ``True`` if this geometry touches the other, otherwise returns
  442. ``False``.
  443. .. method:: crosses(other)
  444. Returns ``True`` if this geometry crosses the other, otherwise returns
  445. ``False``.
  446. .. method:: within(other)
  447. Returns ``True`` if this geometry is contained within the other, otherwise returns
  448. ``False``.
  449. .. method:: contains(other)
  450. Returns ``True`` if this geometry contains the other, otherwise returns
  451. ``False``.
  452. .. method:: overlaps(other)
  453. Returns ``True`` if this geometry overlaps the other, otherwise returns
  454. ``False``.
  455. .. method:: boundary
  456. The boundary of this geometry, as a new :class:`OGRGeometry` object.
  457. .. attribute:: convex_hull
  458. The smallest convex polygon that contains this geometry, as a new
  459. :class:`OGRGeometry` object.
  460. .. method:: difference
  461. Returns the region consisting of the difference of this geometry and
  462. the other, as a new :class:`OGRGeometry` object.
  463. .. method:: intersection
  464. Returns the region consisting of the intersection of this geometry and
  465. the other, as a new :class:`OGRGeometry` object.
  466. .. method:: sym_difference
  467. Returns the region consisting of the symmetric difference of this
  468. geometry and the other, as a new :class:`OGRGeometry` object.
  469. .. method:: union
  470. Returns the region consisting of the union of this geometry and
  471. the other, as a new :class:`OGRGeometry` object.
  472. .. attribute:: tuple
  473. Returns the coordinates of a point geometry as a tuple, the
  474. coordinates of a line geometry as a tuple of tuples, and so forth::
  475. >>> OGRGeometry('POINT (1 2)').tuple
  476. (1.0, 2.0)
  477. >>> OGRGeometry('LINESTRING (1 2,3 4)').tuple
  478. ((1.0, 2.0), (3.0, 4.0))
  479. .. attribute:: coords
  480. An alias for :attr:`tuple`.
  481. .. class:: Point
  482. .. attribute:: x
  483. Returns the X coordinate of this point::
  484. >>> OGRGeometry('POINT (1 2)').x
  485. 1.0
  486. .. attribute:: y
  487. Returns the Y coordinate of this point::
  488. >>> OGRGeometry('POINT (1 2)').y
  489. 2.0
  490. .. attribute:: z
  491. Returns the Z coordinate of this point, or ``None`` if the
  492. the point does not have a Z coordinate::
  493. >>> OGRGeometry('POINT (1 2 3)').z
  494. 3.0
  495. .. class:: LineString
  496. .. attribute:: x
  497. Returns a list of X coordinates in this line::
  498. >>> OGRGeometry('LINESTRING (1 2,3 4)').x
  499. [1.0, 3.0]
  500. .. attribute:: y
  501. Returns a list of Y coordinates in this line::
  502. >>> OGRGeometry('LINESTRING (1 2,3 4)').y
  503. [2.0, 4.0]
  504. .. attribute:: z
  505. Returns a list of Z coordinates in this line, or ``None`` if the
  506. line does not have Z coordinates::
  507. >>> OGRGeometry('LINESTRING (1 2 3,4 5 6)').z
  508. [3.0, 6.0]
  509. .. class:: Polygon
  510. .. attribute:: shell
  511. Returns the shell or exterior ring of this polygon, as a ``LinearRing``
  512. geometry.
  513. .. attribute:: exterior_ring
  514. An alias for :attr:`shell`.
  515. .. attribute:: centroid
  516. Returns a :class:`Point` representing the centroid of this polygon.
  517. .. class:: GeometryCollection
  518. .. method:: add(geom)
  519. Adds a geometry to this geometry collection. Not applicable to other
  520. geometry types.
  521. ``OGRGeomType``
  522. ---------------
  523. .. class:: OGRGeomType(type_input)
  524. This class allows for the representation of an OGR geometry type
  525. in any of several ways::
  526. >>> from django.contrib.gis.gdal import OGRGeomType
  527. >>> gt1 = OGRGeomType(3) # Using an integer for the type
  528. >>> gt2 = OGRGeomType('Polygon') # Using a string
  529. >>> gt3 = OGRGeomType('POLYGON') # It's case-insensitive
  530. >>> print gt1 == 3, gt1 == 'Polygon' # Equivalence works w/non-OGRGeomType objects
  531. True True
  532. .. attribute:: name
  533. Returns a short-hand string form of the OGR Geometry type::
  534. >>> gt1.name
  535. 'Polygon'
  536. .. attribute:: num
  537. Returns the number corresponding to the OGR geometry type::
  538. >>> gt1.num
  539. 3
  540. .. attribute:: django
  541. Returns the Django field type (a subclass of GeometryField) to use for
  542. storing this OGR type, or ``None`` if there is no appropriate Django
  543. type::
  544. >>> gt1.django
  545. 'PolygonField'
  546. ``Envelope``
  547. ------------
  548. .. class:: Envelope(*args)
  549. Represents an OGR Envelope structure that contains the
  550. minimum and maximum X, Y coordinates for a rectangle bounding box.
  551. The naming of the variables is compatible with the OGR Envelope
  552. C structure.
  553. .. attribute:: min_x
  554. The value of the minimum X coordinate.
  555. .. attribute:: min_y
  556. The value of the maximum X coordinate.
  557. .. attribute:: max_x
  558. The value of the minimum Y coordinate.
  559. .. attribute:: max_y
  560. The value of the maximum Y coordinate.
  561. .. attribute:: ur
  562. The upper-right coordinate, as a tuple.
  563. .. attribute:: ll
  564. The lower-left coordinate, as a tuple.
  565. .. attribute:: tuple
  566. A tuple representing the envelope.
  567. .. attribute:: wkt
  568. A string representing this envelope as a polygon in WKT format.
  569. .. method:: expand_to_include(self, *args)
  570. Coordinate System Objects
  571. =========================
  572. ``SpatialReference``
  573. --------------------
  574. .. class:: SpatialReference(srs_input)
  575. Spatial reference objects are initialized on the given ``srs_input``,
  576. which may be one of the following:
  577. * OGC Well Known Text (WKT) (a string)
  578. * EPSG code (integer or string)
  579. * PROJ.4 string
  580. * A shorthand string for well-known standards (``'WGS84'``, ``'WGS72'``, ``'NAD27'``, ``'NAD83'``)
  581. Example::
  582. >>> wgs84 = SpatialReference('WGS84') # shorthand string
  583. >>> wgs84 = SpatialReference(4326) # EPSG code
  584. >>> wgs84 = SpatialReference('EPSG:4326') # EPSG string
  585. >>> proj4 = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs '
  586. >>> wgs84 = SpatialReference(proj4) # PROJ.4 string
  587. >>> wgs84 = SpatialReference("""GEOGCS["WGS 84",
  588. DATUM["WGS_1984",
  589. SPHEROID["WGS 84",6378137,298.257223563,
  590. AUTHORITY["EPSG","7030"]],
  591. AUTHORITY["EPSG","6326"]],
  592. PRIMEM["Greenwich",0,
  593. AUTHORITY["EPSG","8901"]],
  594. UNIT["degree",0.01745329251994328,
  595. AUTHORITY["EPSG","9122"]],
  596. AUTHORITY["EPSG","4326"]]""") # OGC WKT
  597. .. method:: __getitem__(target)
  598. Returns the value of the given string attribute node, ``None`` if the node
  599. doesn't exist. Can also take a tuple as a parameter, (target, child),
  600. where child is the index of the attribute in the WKT. For example::
  601. >>> wkt = 'GEOGCS["WGS 84", DATUM["WGS_1984, ... AUTHORITY["EPSG","4326"]]')
  602. >>> srs = SpatialReference(wkt) # could also use 'WGS84', or 4326
  603. >>> print srs['GEOGCS']
  604. WGS 84
  605. >>> print srs['DATUM']
  606. WGS_1984
  607. >>> print srs['AUTHORITY']
  608. EPSG
  609. >>> print srs['AUTHORITY', 1] # The authority value
  610. 4326
  611. >>> print srs['TOWGS84', 4] # the fourth value in this wkt
  612. 0
  613. >>> print srs['UNIT|AUTHORITY'] # For the units authority, have to use the pipe symbole.
  614. EPSG
  615. >>> print srs['UNIT|AUTHORITY', 1] # The authority value for the untis
  616. 9122
  617. .. method:: attr_value(target, index=0)
  618. The attribute value for the given target node (e.g. ``'PROJCS'``).
  619. The index keyword specifies an index of the child node to return.
  620. .. method:: auth_name(target)
  621. Returns the authority name for the given string target node.
  622. .. method:: auth_code(target)
  623. Returns the authority code for the given string target node.
  624. .. method:: clone()
  625. Returns a clone of this spatial reference object.
  626. .. method:: identify_epsg()
  627. This method inspects the WKT of this SpatialReference, and will
  628. add EPSG authority nodes where an EPSG identifier is applicable.
  629. .. method:: from_esri()
  630. Morphs this SpatialReference from ESRI's format to EPSG
  631. .. method:: to_esri()
  632. Morphs this SpatialReference to ESRI's format.
  633. .. method:: validate()
  634. Checks to see if the given spatial reference is valid, if not
  635. an exception will be raised.
  636. .. method:: import_epsg(epsg)
  637. Import spatial reference from EPSG code.
  638. .. method:: import_proj(proj)
  639. Import spatial reference from PROJ.4 string.
  640. .. method:: import_user_input(user_input)
  641. .. method:: import_wkt(wkt)
  642. Import spatial reference from WKT.
  643. .. method:: import_xml(xml)
  644. Import spatial reference from XML.
  645. .. attribute:: name
  646. Returns the name of this Spatial Reference.
  647. .. attribute:: srid
  648. Returns the SRID of top-level authority, or ``None`` if undefined.
  649. .. attribute:: linear_name
  650. Returns the name of the linear units.
  651. .. attribute:: linear_units
  652. Returns the value of the linear units.
  653. .. attribute:: angular_name
  654. Returns the name of the angular units."
  655. .. attribute:: angular_units
  656. Returns the value of the angular units.
  657. .. attribute:: units
  658. Returns a 2-tuple of the units value and the units name,
  659. and will automatically determines whether to return the linear
  660. or angular units.
  661. .. attribute:: ellisoid
  662. Returns a tuple of the ellipsoid parameters for this spatial
  663. reference: (semimajor axis, semiminor axis, and inverse flattening)
  664. .. attribute:: semi_major
  665. Returns the semi major axis of the ellipsoid for this spatial reference.
  666. .. attribute:: semi_minor
  667. Returns the semi minor axis of the ellipsoid for this spatial reference.
  668. .. attribute:: inverse_flattening
  669. Returns the inverse flattening of the ellipsoid for this spatial reference.
  670. .. attribute:: geographic
  671. Returns ``True`` if this spatial reference is geographic
  672. (root node is ``GEOGCS``).
  673. .. attribute:: local
  674. Returns ``True`` if this spatial reference is local
  675. (root node is ``LOCAL_CS``).
  676. .. attribute:: projected
  677. Returns ``True`` if this spatial reference is a projected coordinate
  678. system (root node is ``PROJCS``).
  679. .. attribute:: wkt
  680. Returns the WKT representation of this spatial reference.
  681. .. attribute:: pretty_wkt
  682. Returns the 'pretty' representation of the WKT.
  683. .. attribute:: proj
  684. Returns the PROJ.4 representation for this spatial reference.
  685. .. attribute:: proj4
  686. Alias for :attr:`SpatialReference.proj`.
  687. .. attribute:: xml
  688. Returns the XML representation of this spatial reference.
  689. ``CoordTransform``
  690. ------------------
  691. .. class:: CoordTransform(source, target)
  692. Represents a coordinate system transform. It is initialized with two
  693. :class:`SpatialReference`, representing the source and target coordinate
  694. systems, respectively. These objects should be used when performing
  695. the same coordinate transformation repeatedly on different geometries::
  696. >>> ct = CoordTransform(SpatialReference('WGS84'), SpatialReference('NAD83'))
  697. >>> for feat in layer:
  698. ... geom = feat.geom # getting clone of feature geometry
  699. ... geom.transform(ct) # transforming
  700. Settings
  701. ========
  702. .. setting:: GDAL_LIBRARY_PATH
  703. GDAL_LIBRARY_PATH
  704. -----------------
  705. A string specifying the location of the GDAL library. Typically,
  706. this setting is only used if the GDAL library is in a non-standard
  707. location (e.g., ``/home/john/lib/libgdal.so``).