layermapping.txt 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. .. _ref-layermapping:
  2. ====================================
  3. ``LayerMapping`` data import utility
  4. ====================================
  5. .. module:: django.contrib.gis.utils.layermapping
  6. :synopsis: Spatial data import utility for GeoDjango models.
  7. .. currentmodule:: django.contrib.gis.utils
  8. The :class:`LayerMapping` class provides a way to map the contents of
  9. vector spatial data files (e.g. shapefiles) intoto GeoDjango models.
  10. This utility grew out of the author's personal needs to eliminate
  11. the code repetition that went into pulling geometries and fields out of
  12. a vector layer, converting to another coordinate system (e.g. WGS84), and
  13. then inserting into a GeoDjango model.
  14. .. note::
  15. Use of :class:`LayerMapping` requires GDAL.
  16. .. warning ::
  17. GIS data sources, like shapefiles, may be very large. If you find
  18. that :class:`LayerMapping` is using too much memory, set
  19. :setting:`DEBUG` to ``False`` in your settings. When :setting:`DEBUG`
  20. is set to ``True``, Django :ref:`automatically logs <faq-see-raw-sql-queries>`
  21. *every* SQL query -- thus, when SQL statements contain geometries, it is
  22. easy to consume more memory than is typical.
  23. Example
  24. =======
  25. 1. You need a GDAL-supported data source, like a shapefile (here we're using
  26. a simple polygon shapefile, ``test_poly.shp``, with three features)::
  27. >>> from django.contrib.gis.gdal import DataSource
  28. >>> ds = DataSource('test_poly.shp')
  29. >>> layer = ds[0]
  30. >>> print layer.fields # Exploring the fields in the layer, we only want the 'str' field.
  31. ['float', 'int', 'str']
  32. >>> print len(layer) # getting the number of features in the layer (should be 3)
  33. 3
  34. >>> print layer.geom_type # Should be 'Polygon'
  35. Polygon
  36. >>> print layer.srs # WGS84 in WKT
  37. GEOGCS["GCS_WGS_1984",
  38. DATUM["WGS_1984",
  39. SPHEROID["WGS_1984",6378137,298.257223563]],
  40. PRIMEM["Greenwich",0],
  41. UNIT["Degree",0.017453292519943295]]
  42. 2. Now we define our corresponding Django model (make sure to use ``syncdb``)::
  43. from django.contrib.gis.db import models
  44. class TestGeo(models.Model):
  45. name = models.CharField(max_length=25) # corresponds to the 'str' field
  46. poly = models.PolygonField(srid=4269) # we want our model in a different SRID
  47. objects = models.GeoManager()
  48. def __unicode__(self):
  49. return 'Name: %s' % self.name
  50. 3. Use :class:`LayerMapping` to extract all the features and place them in the
  51. database::
  52. >>> from django.contrib.gis.utils import LayerMapping
  53. >>> from geoapp.models import TestGeo
  54. >>> mapping = {'name' : 'str', # The 'name' model field maps to the 'str' layer field.
  55. 'poly' : 'POLYGON', # For geometry fields use OGC name.
  56. } # The mapping is a dictionary
  57. >>> lm = LayerMapping(TestGeo, 'test_poly.shp', mapping)
  58. >>> lm.save(verbose=True) # Save the layermap, imports the data.
  59. Saved: Name: 1
  60. Saved: Name: 2
  61. Saved: Name: 3
  62. Here, :class:`LayerMapping` just transformed the three geometries from the
  63. shapefile in their original spatial reference system (WGS84) to the spatial
  64. reference system of the GeoDjango model (NAD83). If no spatial reference
  65. system is defined for the layer, use the ``source_srs`` keyword with a
  66. :class:`~django.contrib.gis.gdal.SpatialReference` object to specify one.
  67. ``LayerMapping`` API
  68. ====================
  69. .. class:: LayerMapping(model, data_source, mapping[, layer=0, source_srs=None, encoding=None, transaction_mode='commit_on_success', transform=True, unique=True, using='default'])
  70. The following are the arguments and keywords that may be used during
  71. instantiation of ``LayerMapping`` objects.
  72. ================= =========================================================
  73. Argument Description
  74. ================= =========================================================
  75. ``model`` The geographic model, *not* an instance.
  76. ``data_source`` The path to the OGR-supported data source file
  77. (e.g., a shapefile). Also accepts
  78. :class:`django.contrib.gis.gdal.DataSource` instances.
  79. ``mapping`` A dictionary: keys are strings corresponding to
  80. the model field, and values correspond to
  81. string field names for the OGR feature, or if the
  82. model field is a geographic then it should
  83. correspond to the OGR geometry type,
  84. e.g., ``'POINT'``, ``'LINESTRING'``, ``'POLYGON'``.
  85. ================= =========================================================
  86. ===================== =====================================================
  87. Keyword Arguments
  88. ===================== =====================================================
  89. ``layer`` The index of the layer to use from the Data Source
  90. (defaults to 0)
  91. ``source_srs`` Use this to specify the source SRS manually (for
  92. example, some shapefiles don't come with a '.prj'
  93. file). An integer SRID, WKT or PROJ.4 strings, and
  94. :class:`django.contrib.gis.gdal.SpatialReference`
  95. objects are accepted.
  96. ``encoding`` Specifies the character set encoding of the strings
  97. in the OGR data source. For example, ``'latin-1'``,
  98. ``'utf-8'``, and ``'cp437'`` are all valid encoding
  99. parameters.
  100. ``transaction_mode`` May be ``'commit_on_success'`` (default) or
  101. ``'autocommit'``.
  102. ``transform`` Setting this to False will disable coordinate
  103. transformations. In other words, geometries will
  104. be inserted into the database unmodified from their
  105. original state in the data source.
  106. ``unique`` Setting this to the name, or a tuple of names,
  107. from the given model will create models unique
  108. only to the given name(s). Geometries will from
  109. each feature will be added into the collection
  110. associated with the unique model. Forces
  111. the transaction mode to be ``'autocommit'``.
  112. ``using`` New in version 1.2. Sets the database to use when
  113. importing spatial data. Default is ``'default'``
  114. ===================== =====================================================
  115. ``save()`` Keyword Arguments
  116. ----------------------------
  117. .. method:: LayerMapping.save([verbose=False, fid_range=False, step=False, progress=False, silent=False, stream=sys.stdout, strict=False])
  118. The ``save()`` method also accepts keywords. These keywords are
  119. used for controlling output logging, error handling, and for importing
  120. specific feature ranges.
  121. =========================== =================================================
  122. Save Keyword Arguments Description
  123. =========================== =================================================
  124. ``fid_range`` May be set with a slice or tuple of
  125. (begin, end) feature ID's to map from
  126. the data source. In other words, this
  127. keyword enables the user to selectively
  128. import a subset range of features in the
  129. geographic data source.
  130. ``progress`` When this keyword is set, status information
  131. will be printed giving the number of features
  132. processed and successfully saved. By default,
  133. progress information will be printed every 1000
  134. features processed, however, this default may
  135. be overridden by setting this keyword with an
  136. integer for the desired interval.
  137. ``silent`` By default, non-fatal error notifications are
  138. printed to ``sys.stdout``, but this keyword may
  139. be set to disable these notifications.
  140. ``step`` If set with an integer, transactions will
  141. occur at every step interval. For example, if
  142. ``step=1000``, a commit would occur after the
  143. 1,000th feature, the 2,000th feature etc.
  144. ``stream`` Status information will be written to this file
  145. handle. Defaults to using ``sys.stdout``, but
  146. any object with a ``write`` method is supported.
  147. ``strict`` Execution of the model mapping will cease upon
  148. the first error encountered. The default value
  149. (``False``)
  150. behavior is to attempt to continue.
  151. ``verbose`` If set, information will be printed
  152. subsequent to each model save
  153. executed on the database.
  154. =========================== =================================================
  155. Troubleshooting
  156. ===============
  157. Running out of memory
  158. ---------------------
  159. As noted in the warning at the top of this section, Django stores all SQL
  160. queries when ``DEBUG=True``. Set ``DEBUG=False`` in your settings, and this
  161. should stop excessive memory use when running ``LayerMapping`` scripts.
  162. MySQL: ``max_allowed_packet`` error
  163. -----------------------------------
  164. If you encounter the following error when using ``LayerMapping`` and MySQL::
  165. OperationalError: (1153, "Got a packet bigger than 'max_allowed_packet' bytes")
  166. Then the solution is to increase the value of the ``max_allowed_packet``
  167. setting in your MySQL configuration. For example, the default value may
  168. be something low like one megabyte -- the setting may be modified in MySQL's
  169. configuration file (``my.cnf``) in the ``[mysqld]`` section::
  170. max_allowed_packet = 10M