Commit 3ac87784 authored by Justin Bronn's avatar Justin Bronn
Browse files

Fixed #15277 -- Cleaned up `ogrinspect` command, added tests and extended...

Fixed #15277 -- Cleaned up `ogrinspect` command, added tests and extended support beyond file-based OGR data sources.  Thanks, willinoed for bug report and jpaulett for initial patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16845 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent f97a5741
Loading
Loading
Loading
Loading
+0 −15
Original line number Diff line number Diff line
from django.core.management.base import BaseCommand, CommandError

class ArgsCommand(BaseCommand):
    """
    Command class for commands that take multiple arguments.
    """
    args = '<arg arg ...>'

    def handle(self, *args, **options):
        if not args:
            raise CommandError('Must provide the following arguments: %s' % self.args)
        return self.handle_args(*args, **options)

    def handle_args(self, *args, **options):
        raise NotImplementedError()
+14 −17
Original line number Diff line number Diff line
import os
from optparse import make_option
from django.contrib.gis import gdal
from django.contrib.gis.management.base import ArgsCommand, CommandError
from django.core.management.base import LabelCommand, CommandError

def layer_option(option, opt, value, parser):
    """
@@ -26,13 +26,13 @@ def list_option(option, opt, value, parser):
        dest = [s for s in value.split(',')]
    setattr(parser.values, option.dest, dest)

class Command(ArgsCommand):
class Command(LabelCommand):
    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 + (
    option_list = LabelCommand.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 '
@@ -72,7 +72,7 @@ class Command(ArgsCommand):

    requires_model_validation = False

    def handle_args(self, *args, **options):
    def handle(self, *args, **options):
        try:
            data_source, model_name = args
        except ValueError:
@@ -81,10 +81,6 @@ class Command(ArgsCommand):
        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])

@@ -97,8 +93,9 @@ class Command(ArgsCommand):
        # 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`.
        # 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.
@@ -119,4 +116,4 @@ class Command(ArgsCommand):
                           '%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)
        return '\n'.join(output) + '\n'
+4 −6
Original line number Diff line number Diff line
@@ -29,15 +29,13 @@ def geo_apps(namespace=True, runtests=False):

    # The following GeoDjango test apps depend on GDAL support.
    if HAS_GDAL:
        # Geographic admin requires GDAL
        apps.append('geoadmin')
        # Geographic admin, LayerMapping, and ogrinspect test apps
        # all require GDAL.
        apps.extend(['geoadmin', 'layermap', 'inspectapp'])

        # 3D apps use LayerMapping, which uses GDAL.
        # 3D apps use LayerMapping, which uses GDAL and require GEOS 3.1+.
        if connection.ops.postgis and GEOS_PREPARE:
            apps.append('geo3d')

        apps.append('layermap')

    if runtests:
        return [('django.contrib.gis.tests', app) for app in apps]
    elif namespace:
+0 −0

Empty file added.

+13 −0
Original line number Diff line number Diff line
from django.contrib.gis.db import models

class AllOGRFields(models.Model):
    f_decimal = models.FloatField()
    f_float = models.FloatField()
    f_int = models.IntegerField()
    f_char = models.CharField(max_length=10)
    f_date = models.DateField()
    f_datetime = models.DateTimeField()
    f_time = models.TimeField()
    geom = models.PolygonField()

    objects = models.GeoManager()
Loading