ogrinspect.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import inspect
  2. from optparse import make_option
  3. from django.contrib.gis import gdal
  4. from django.core.management.base import LabelCommand, CommandError
  5. def layer_option(option, opt, value, parser):
  6. """
  7. Callback for `make_option` for the `ogrinspect` `layer_key`
  8. keyword option which may be an integer or a string.
  9. """
  10. try:
  11. dest = int(value)
  12. except ValueError:
  13. dest = value
  14. setattr(parser.values, option.dest, dest)
  15. def list_option(option, opt, value, parser):
  16. """
  17. Callback for `make_option` for `ogrinspect` keywords that require
  18. a string list. If the string is 'True'/'true' then the option
  19. value will be a boolean instead.
  20. """
  21. if value.lower() == 'true':
  22. dest = True
  23. else:
  24. dest = [s for s in value.split(',')]
  25. setattr(parser.values, option.dest, dest)
  26. class Command(LabelCommand):
  27. help = ('Inspects the given OGR-compatible data source (e.g., a shapefile) and outputs\n'
  28. 'a GeoDjango model with the given model name. For example:\n'
  29. ' ./manage.py ogrinspect zipcode.shp Zipcode')
  30. args = '[data_source] [model_name]'
  31. option_list = LabelCommand.option_list + (
  32. make_option('--blank', dest='blank', type='string', action='callback',
  33. callback=list_option, default=False,
  34. help='Use a comma separated list of OGR field names to add '
  35. 'the `blank=True` option to the field definition. Set with'
  36. '`true` to apply to all applicable fields.'),
  37. make_option('--decimal', dest='decimal', type='string', action='callback',
  38. callback=list_option, default=False,
  39. help='Use a comma separated list of OGR float fields to '
  40. 'generate `DecimalField` instead of the default '
  41. '`FloatField`. Set to `true` to apply to all OGR float fields.'),
  42. make_option('--geom-name', dest='geom_name', type='string', default='geom',
  43. help='Specifies the model name for the Geometry Field '
  44. '(defaults to `geom`)'),
  45. make_option('--layer', dest='layer_key', type='string', action='callback',
  46. callback=layer_option, default=0,
  47. help='The key for specifying which layer in the OGR data '
  48. 'source to use. Defaults to 0 (the first layer). May be '
  49. 'an integer or a string identifier for the layer.'),
  50. make_option('--multi-geom', action='store_true', dest='multi_geom', default=False,
  51. help='Treat the geometry in the data source as a geometry collection.'),
  52. make_option('--name-field', dest='name_field',
  53. help='Specifies a field name to return for the `__unicode__`/`__str__` function.'),
  54. make_option('--no-imports', action='store_false', dest='imports', default=True,
  55. help='Do not include `from django.contrib.gis.db import models` '
  56. 'statement.'),
  57. make_option('--null', dest='null', type='string', action='callback',
  58. callback=list_option, default=False,
  59. help='Use a comma separated list of OGR field names to add '
  60. 'the `null=True` option to the field definition. Set with'
  61. '`true` to apply to all applicable fields.'),
  62. make_option('--srid', dest='srid',
  63. help='The SRID to use for the Geometry Field. If it can be '
  64. 'determined, the SRID of the data source is used.'),
  65. make_option('--mapping', action='store_true', dest='mapping',
  66. help='Generate mapping dictionary for use with `LayerMapping`.')
  67. )
  68. requires_system_checks = False
  69. def handle(self, *args, **options):
  70. try:
  71. data_source, model_name = args
  72. except ValueError:
  73. raise CommandError('Invalid arguments, must provide: %s' % self.args)
  74. if not gdal.HAS_GDAL:
  75. raise CommandError('GDAL is required to inspect geospatial data sources.')
  76. # Getting the OGR DataSource from the string parameter.
  77. try:
  78. ds = gdal.DataSource(data_source)
  79. except gdal.OGRException as msg:
  80. raise CommandError(msg)
  81. # Returning the output of ogrinspect with the given arguments
  82. # and options.
  83. from django.contrib.gis.utils.ogrinspect import _ogrinspect, mapping
  84. # Filter options to params accepted by `_ogrinspect`
  85. ogr_options = dict((k, v) for k, v in options.items()
  86. if k in inspect.getargspec(_ogrinspect).args and v is not None)
  87. output = [s for s in _ogrinspect(ds, model_name, **ogr_options)]
  88. if options['mapping']:
  89. # Constructing the keyword arguments for `mapping`, and
  90. # calling it on the data source.
  91. kwargs = {'geom_name': options['geom_name'],
  92. 'layer_key': options['layer_key'],
  93. 'multi_geom': options['multi_geom'],
  94. }
  95. mapping_dict = mapping(ds, **kwargs)
  96. # This extra legwork is so that the dictionary definition comes
  97. # out in the same order as the fields in the model definition.
  98. rev_mapping = dict((v, k) for k, v in mapping_dict.items())
  99. output.extend(['', '# Auto-generated `LayerMapping` dictionary for %s model' % model_name,
  100. '%s_mapping = {' % model_name.lower()])
  101. output.extend(" '%s' : '%s'," % (rev_mapping[ogr_fld], ogr_fld) for ogr_fld in ds[options['layer_key']].fields)
  102. output.extend([" '%s' : '%s'," % (options['geom_name'], mapping_dict[options['geom_name']]), '}'])
  103. return '\n'.join(output) + '\n'