You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ogrinspect.py 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. """
  2. This module is for inspecting OGR data sources and generating either
  3. models for GeoDjango and/or mapping dictionaries for use with the
  4. `LayerMapping` utility.
  5. """
  6. from django.contrib.gis.gdal import DataSource
  7. from django.contrib.gis.gdal.field import (
  8. OFTDate, OFTDateTime, OFTInteger, OFTInteger64, OFTReal, OFTString,
  9. OFTTime,
  10. )
  11. def mapping(data_source, geom_name='geom', layer_key=0, multi_geom=False):
  12. """
  13. Given a DataSource, generate a dictionary that may be used
  14. for invoking the LayerMapping utility.
  15. Keyword Arguments:
  16. `geom_name` => The name of the geometry field to use for the model.
  17. `layer_key` => The key for specifying which layer in the DataSource to use;
  18. defaults to 0 (the first layer). May be an integer index or a string
  19. identifier for the layer.
  20. `multi_geom` => Boolean (default: False) - specify as multigeometry.
  21. """
  22. if isinstance(data_source, str):
  23. # Instantiating the DataSource from the string.
  24. data_source = DataSource(data_source)
  25. elif isinstance(data_source, DataSource):
  26. pass
  27. else:
  28. raise TypeError('Data source parameter must be a string or a DataSource object.')
  29. # Creating the dictionary.
  30. _mapping = {}
  31. # Generating the field name for each field in the layer.
  32. for field in data_source[layer_key].fields:
  33. mfield = field.lower()
  34. if mfield[-1:] == '_':
  35. mfield += 'field'
  36. _mapping[mfield] = field
  37. gtype = data_source[layer_key].geom_type
  38. if multi_geom:
  39. gtype.to_multi()
  40. _mapping[geom_name] = str(gtype).upper()
  41. return _mapping
  42. def ogrinspect(*args, **kwargs):
  43. """
  44. Given a data source (either a string or a DataSource object) and a string
  45. model name this function will generate a GeoDjango model.
  46. Usage:
  47. >>> from django.contrib.gis.utils import ogrinspect
  48. >>> ogrinspect('/path/to/shapefile.shp','NewModel')
  49. ...will print model definition to stout
  50. or put this in a Python script and use to redirect the output to a new
  51. model like:
  52. $ python generate_model.py > myapp/models.py
  53. # generate_model.py
  54. from django.contrib.gis.utils import ogrinspect
  55. shp_file = 'data/mapping_hacks/world_borders.shp'
  56. model_name = 'WorldBorders'
  57. print(ogrinspect(shp_file, model_name, multi_geom=True, srid=4326,
  58. geom_name='shapes', blank=True))
  59. Required Arguments
  60. `datasource` => string or DataSource object to file pointer
  61. `model name` => string of name of new model class to create
  62. Optional Keyword Arguments
  63. `geom_name` => For specifying the model name for the Geometry Field.
  64. Otherwise will default to `geom`
  65. `layer_key` => The key for specifying which layer in the DataSource to use;
  66. defaults to 0 (the first layer). May be an integer index or a string
  67. identifier for the layer.
  68. `srid` => The SRID to use for the Geometry Field. If it can be determined,
  69. the SRID of the datasource is used.
  70. `multi_geom` => Boolean (default: False) - specify as multigeometry.
  71. `name_field` => String - specifies a field name to return for the
  72. __str__() method (which will be generated if specified).
  73. `imports` => Boolean (default: True) - set to False to omit the
  74. `from django.contrib.gis.db import models` code from the
  75. autogenerated models thus avoiding duplicated imports when building
  76. more than one model by batching ogrinspect()
  77. `decimal` => Boolean or sequence (default: False). When set to True
  78. all generated model fields corresponding to the `OFTReal` type will
  79. be `DecimalField` instead of `FloatField`. A sequence of specific
  80. field names to generate as `DecimalField` may also be used.
  81. `blank` => Boolean or sequence (default: False). When set to True all
  82. generated model fields will have `blank=True`. If the user wants to
  83. give specific fields to have blank, then a list/tuple of OGR field
  84. names may be used.
  85. `null` => Boolean (default: False) - When set to True all generated
  86. model fields will have `null=True`. If the user wants to specify
  87. give specific fields to have null, then a list/tuple of OGR field
  88. names may be used.
  89. Note: Call the _ogrinspect() helper to do the heavy lifting.
  90. """
  91. return '\n'.join(s for s in _ogrinspect(*args, **kwargs))
  92. def _ogrinspect(data_source, model_name, geom_name='geom', layer_key=0, srid=None,
  93. multi_geom=False, name_field=None, imports=True,
  94. decimal=False, blank=False, null=False):
  95. """
  96. Helper routine for `ogrinspect` that generates GeoDjango models corresponding
  97. to the given data source. See the `ogrinspect` docstring for more details.
  98. """
  99. # Getting the DataSource
  100. if isinstance(data_source, str):
  101. data_source = DataSource(data_source)
  102. elif isinstance(data_source, DataSource):
  103. pass
  104. else:
  105. raise TypeError('Data source parameter must be a string or a DataSource object.')
  106. # Getting the layer corresponding to the layer key and getting
  107. # a string listing of all OGR fields in the Layer.
  108. layer = data_source[layer_key]
  109. ogr_fields = layer.fields
  110. # Creating lists from the `null`, `blank`, and `decimal`
  111. # keyword arguments.
  112. def process_kwarg(kwarg):
  113. if isinstance(kwarg, (list, tuple)):
  114. return [s.lower() for s in kwarg]
  115. elif kwarg:
  116. return [s.lower() for s in ogr_fields]
  117. else:
  118. return []
  119. null_fields = process_kwarg(null)
  120. blank_fields = process_kwarg(blank)
  121. decimal_fields = process_kwarg(decimal)
  122. # Gets the `null` and `blank` keywords for the given field name.
  123. def get_kwargs_str(field_name):
  124. kwlist = []
  125. if field_name.lower() in null_fields:
  126. kwlist.append('null=True')
  127. if field_name.lower() in blank_fields:
  128. kwlist.append('blank=True')
  129. if kwlist:
  130. return ', ' + ', '.join(kwlist)
  131. else:
  132. return ''
  133. # For those wishing to disable the imports.
  134. if imports:
  135. yield '# This is an auto-generated Django model module created by ogrinspect.'
  136. yield 'from django.contrib.gis.db import models'
  137. yield ''
  138. yield ''
  139. yield 'class %s(models.Model):' % model_name
  140. for field_name, width, precision, field_type in zip(
  141. ogr_fields, layer.field_widths, layer.field_precisions, layer.field_types):
  142. # The model field name.
  143. mfield = field_name.lower()
  144. if mfield[-1:] == '_':
  145. mfield += 'field'
  146. # Getting the keyword args string.
  147. kwargs_str = get_kwargs_str(field_name)
  148. if field_type is OFTReal:
  149. # By default OFTReals are mapped to `FloatField`, however, they
  150. # may also be mapped to `DecimalField` if specified in the
  151. # `decimal` keyword.
  152. if field_name.lower() in decimal_fields:
  153. yield ' %s = models.DecimalField(max_digits=%d, decimal_places=%d%s)' % (
  154. mfield, width, precision, kwargs_str
  155. )
  156. else:
  157. yield ' %s = models.FloatField(%s)' % (mfield, kwargs_str[2:])
  158. elif field_type is OFTInteger:
  159. yield ' %s = models.IntegerField(%s)' % (mfield, kwargs_str[2:])
  160. elif field_type is OFTInteger64:
  161. yield ' %s = models.BigIntegerField(%s)' % (mfield, kwargs_str[2:])
  162. elif field_type is OFTString:
  163. yield ' %s = models.CharField(max_length=%s%s)' % (mfield, width, kwargs_str)
  164. elif field_type is OFTDate:
  165. yield ' %s = models.DateField(%s)' % (mfield, kwargs_str[2:])
  166. elif field_type is OFTDateTime:
  167. yield ' %s = models.DateTimeField(%s)' % (mfield, kwargs_str[2:])
  168. elif field_type is OFTTime:
  169. yield ' %s = models.TimeField(%s)' % (mfield, kwargs_str[2:])
  170. else:
  171. raise TypeError('Unknown field type %s in %s' % (field_type, mfield))
  172. # TODO: Autodetection of multigeometry types (see #7218).
  173. gtype = layer.geom_type
  174. if multi_geom:
  175. gtype.to_multi()
  176. geom_field = gtype.django
  177. # Setting up the SRID keyword string.
  178. if srid is None:
  179. if layer.srs is None:
  180. srid_str = 'srid=-1'
  181. else:
  182. srid = layer.srs.srid
  183. if srid is None:
  184. srid_str = 'srid=-1'
  185. elif srid == 4326:
  186. # WGS84 is already the default.
  187. srid_str = ''
  188. else:
  189. srid_str = 'srid=%s' % srid
  190. else:
  191. srid_str = 'srid=%s' % srid
  192. yield ' %s = models.%s(%s)' % (geom_name, geom_field, srid_str)
  193. if name_field:
  194. yield ''
  195. yield ' def __str__(self): return self.%s' % name_field