Exemple #1
0
    def handle(self, *args, **options):
        data_source, model_name = options.pop('data_source'), options.pop('model_name')
        if not gdal.HAS_GDAL:
            raise CommandError('GDAL is required to inspect geospatial data sources.')

        # Getting the OGR DataSource from the string parameter.
        try:
            ds = gdal.DataSource(data_source)
        except gdal.OGRException as msg:
            raise CommandError(msg)

        # Returning the output of ogrinspect with the given arguments
        # and options.
        from django.contrib.gis.utils.ogrinspect import _ogrinspect, mapping
        # Filter options to params accepted by `_ogrinspect`
        ogr_options = dict((k, v) for k, v in options.items()
                           if k in inspect.getargspec(_ogrinspect).args and v is not None)
        output = [s for s in _ogrinspect(ds, model_name, **ogr_options)]

        if options['mapping']:
            # Constructing the keyword arguments for `mapping`, and
            # calling it on the data source.
            kwargs = {'geom_name': options['geom_name'],
                      'layer_key': options['layer_key'],
                      'multi_geom': options['multi_geom'],
                      }
            mapping_dict = mapping(ds, **kwargs)
            # This extra legwork is so that the dictionary definition comes
            # out in the same order as the fields in the model definition.
            rev_mapping = dict((v, k) for k, v in mapping_dict.items())
            output.extend(['', '# Auto-generated `LayerMapping` dictionary for %s model' % model_name,
                           '%s_mapping = {' % model_name.lower()])
            output.extend("    '%s' : '%s'," % (rev_mapping[ogr_fld], ogr_fld) for ogr_fld in ds[options['layer_key']].fields)
            output.extend(["    '%s' : '%s'," % (options['geom_name'], mapping_dict[options['geom_name']]), '}'])
        return '\n'.join(output) + '\n'
Exemple #2
0
    def handle(self, *args, **options):
        try:
            data_source, model_name = args
        except ValueError:
            raise CommandError("Invalid arguments, must provide: %s" % self.args)

        if not gdal.HAS_GDAL:
            raise CommandError("GDAL is required to inspect geospatial data sources.")

        # Removing options with `None` values.
        options = dict([(k, v) for k, v in options.items() if not v is None])

        # Getting the OGR DataSource from the string parameter.
        try:
            ds = gdal.DataSource(data_source)
        except gdal.OGRException as msg:
            raise CommandError(msg)

        # Whether the user wants to generate the LayerMapping dictionary as well.
        show_mapping = options.pop("mapping", False)

        # Getting rid of settings that `_ogrinspect` doesn't like.
        verbosity = options.pop("verbosity", False)
        settings = options.pop("settings", False)

        # Returning the output of ogrinspect with the given arguments
        # and options.
        from django.contrib.gis.utils.ogrinspect import _ogrinspect, mapping

        output = [s for s in _ogrinspect(ds, model_name, **options)]
        if show_mapping:
            # Constructing the keyword arguments for `mapping`, and
            # calling it on the data source.
            kwargs = {
                "geom_name": options["geom_name"],
                "layer_key": options["layer_key"],
                "multi_geom": options["multi_geom"],
            }
            mapping_dict = mapping(ds, **kwargs)
            # This extra legwork is so that the dictionary definition comes
            # out in the same order as the fields in the model definition.
            rev_mapping = dict([(v, k) for k, v in mapping_dict.items()])
            output.extend(
                [
                    "",
                    "# Auto-generated `LayerMapping` dictionary for %s model" % model_name,
                    "%s_mapping = {" % model_name.lower(),
                ]
            )
            output.extend(
                ["    '%s' : '%s'," % (rev_mapping[ogr_fld], ogr_fld) for ogr_fld in ds[options["layer_key"]].fields]
            )
            output.extend(["    '%s' : '%s'," % (options["geom_name"], mapping_dict[options["geom_name"]]), "}"])
        return "\n".join(output) + "\n"
Exemple #3
0
    def handle(self, *args, **options):
        try:
            data_source, model_name = args
        except ValueError:
            raise CommandError("Invalid arguments, must provide: %s" % self.args)

        if not gdal.HAS_GDAL:
            raise CommandError("GDAL is required to inspect geospatial data sources.")

        # Getting the OGR DataSource from the string parameter.
        try:
            ds = gdal.DataSource(data_source)
        except gdal.OGRException as msg:
            raise CommandError(msg)

        # Returning the output of ogrinspect with the given arguments
        # and options.
        from django.contrib.gis.utils.ogrinspect import _ogrinspect, mapping

        # Filter options to params accepted by `_ogrinspect`
        ogr_options = dict(
            (k, v) for k, v in options.items() if k in inspect.getargspec(_ogrinspect).args and v is not None
        )
        output = [s for s in _ogrinspect(ds, model_name, **ogr_options)]

        if options["mapping"]:
            # Constructing the keyword arguments for `mapping`, and
            # calling it on the data source.
            kwargs = {
                "geom_name": options["geom_name"],
                "layer_key": options["layer_key"],
                "multi_geom": options["multi_geom"],
            }
            mapping_dict = mapping(ds, **kwargs)
            # This extra legwork is so that the dictionary definition comes
            # out in the same order as the fields in the model definition.
            rev_mapping = dict((v, k) for k, v in mapping_dict.items())
            output.extend(
                [
                    "",
                    "# Auto-generated `LayerMapping` dictionary for %s model" % model_name,
                    "%s_mapping = {" % model_name.lower(),
                ]
            )
            output.extend(
                "    '%s' : '%s'," % (rev_mapping[ogr_fld], ogr_fld) for ogr_fld in ds[options["layer_key"]].fields
            )
            output.extend(["    '%s' : '%s'," % (options["geom_name"], mapping_dict[options["geom_name"]]), "}"])
        return "\n".join(output) + "\n"
def load_world_data():
  from vsi.tools.dir_util import TempDir
  from voxel_globe.world import models
  from zipfile import ZipFile
  from glob import glob

  with TempDir(cd=True, mkdtemp=True) as temp_dir:
    with ZipFile(env['VIP_DJANGO_REGRESSION_SHAPEFILE'], 'r') as zip_file:
      zip_file.extractall(temp_dir)

    world_shp = glob(os.path.join(temp_dir, '*.shp'))[0]
    #print world_shp, '\n'

    ds = DataSource(world_shp)

    geometryName = u'mpoly';

    if 1: #Completely optional
      from django.contrib.gis.utils.ogrinspect import _ogrinspect
      print >>logStdOut, 'Your class model SHOULD look like this:\n'
      print >>logStdOut, '\n'.join([s for s in _ogrinspect(ds, 'WorldBorder', geom_name=geometryName, layer_key=0, srid=4326, multi_geom=True)]);

    world_mapping = mapping(ds, multi_geom=True, geom_name=geometryName, layer_key=0);

    print >>logStdOut, 'Loading database using mapping:'
    pprint(world_mapping, stream=logStdOut)

    try:
      lm = LayerMapping(models.WorldBorder, world_shp, world_mapping,
                        transform=False, encoding='iso-8859-1');
      lm.save(strict=True, verbose=True);
    except:
      print 'Failed to load mapping data. It probably already exists!'

    #Cleanup so that TempDir can delete the dir
    del(ds, world_mapping, lm)
Exemple #5
0
class Command(ArgsCommand):
    help = (
        'Inspects the given OGR-compatible data source (e.g., a shapefile) and outputs\n'
        'a GeoDjango model with the given model name. For example:\n'
        ' ./manage.py ogrinspect zipcode.shp Zipcode')
    args = '[data_source] [model_name]'

    option_list = ArgsCommand.option_list + (
        make_option(
            '--blank',
            dest='blank',
            type='string',
            action='callback',
            callback=list_option,
            default=False,
            help='Use a comma separated list of OGR field names to add '
            'the `blank=True` option to the field definition.  Set with'
            '`true` to apply to all applicable fields.'),
        make_option(
            '--decimal',
            dest='decimal',
            type='string',
            action='callback',
            callback=list_option,
            default=False,
            help='Use a comma separated list of OGR float fields to '
            'generate `DecimalField` instead of the default '
            '`FloatField`. Set to `true` to apply to all OGR float fields.'),
        make_option('--geom-name',
                    dest='geom_name',
                    type='string',
                    default='geom',
                    help='Specifies the model name for the Geometry Field '
                    '(defaults to `geom`)'),
        make_option('--layer',
                    dest='layer_key',
                    type='string',
                    action='callback',
                    callback=layer_option,
                    default=0,
                    help='The key for specifying which layer in the OGR data '
                    'source to use. Defaults to 0 (the first layer). May be '
                    'an integer or a string identifier for the layer.'),
        make_option(
            '--multi-geom',
            action='store_true',
            dest='multi_geom',
            default=False,
            help=
            'Treat the geometry in the data source as a geometry collection.'),
        make_option(
            '--name-field',
            dest='name_field',
            help=
            'Specifies a field name to return for the `__unicode__` function.'
        ),
        make_option(
            '--no-imports',
            action='store_false',
            dest='imports',
            default=True,
            help='Do not include `from django.contrib.gis.db import models` '
            'statement.'),
        make_option(
            '--null',
            dest='null',
            type='string',
            action='callback',
            callback=list_option,
            default=False,
            help='Use a comma separated list of OGR field names to add '
            'the `null=True` option to the field definition.  Set with'
            '`true` to apply to all applicable fields.'),
        make_option(
            '--srid',
            dest='srid',
            help='The SRID to use for the Geometry Field.  If it can be '
            'determined, the SRID of the data source is used.'),
        make_option(
            '--mapping',
            action='store_true',
            dest='mapping',
            help='Generate mapping dictionary for use with `LayerMapping`.'))

    requires_model_validation = False

    def handle_args(self, *args, **options):
        try:
            data_source, model_name = args
        except ValueError:
            raise CommandError('Invalid arguments, must provide: %s' %
                               self.args)

        if not gdal.HAS_GDAL:
            raise CommandError(
                'GDAL is required to inspect geospatial data sources.')

        # TODO: Support non file-based OGR datasources.
        if not os.path.isfile(data_source):
            raise CommandError('The given data source cannot be found: "%s"' %
                               data_source)

        # Removing options with `None` values.
        options = dict([(k, v) for k, v in options.items() if not v is None])

        # Getting the OGR DataSource from the string parameter.
        try:
            ds = gdal.DataSource(data_source)
        except gdal.OGRException, msg:
            raise CommandError(msg)

        # Whether the user wants to generate the LayerMapping dictionary as well.
        show_mapping = options.pop('mapping', False)

        # Popping the verbosity global option, as it's not accepted by `_ogrinspect`.
        verbosity = options.pop('verbosity', False)

        # Returning the output of ogrinspect with the given arguments
        # and options.
        from django.contrib.gis.utils.ogrinspect import _ogrinspect, mapping
        output = [s for s in _ogrinspect(ds, model_name, **options)]
        if show_mapping:
            # Constructing the keyword arguments for `mapping`, and
            # calling it on the data source.
            kwargs = {
                'geom_name': options['geom_name'],
                'layer_key': options['layer_key'],
                'multi_geom': options['multi_geom'],
            }
            mapping_dict = mapping(ds, **kwargs)
            # This extra legwork is so that the dictionary definition comes
            # out in the same order as the fields in the model definition.
            rev_mapping = dict([(v, k) for k, v in mapping_dict.items()])
            output.extend([
                '',
                '# Auto-generated `LayerMapping` dictionary for %s model' %
                model_name,
                '%s_mapping = {' % model_name.lower()
            ])
            output.extend([
                "    '%s' : '%s'," % (rev_mapping[ogr_fld], ogr_fld)
                for ogr_fld in ds[options['layer_key']].fields
            ])
            output.extend([
                "    '%s' : '%s'," %
                (options['geom_name'], mapping_dict[options['geom_name']]), '}'
            ])
        return '\n'.join(output)
import argparse