layermapping.txt 9.9 KB

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