Exemplo n.º 1
0
    def _get_model(self):
        if not self.extracted:
            if not self.extract():
                raise ExtractionError("can't extract {0}".format(str(self)))

        # TODO: make this less shitty
        shps = [
            os.path.join(self.extract_path, filename)
            for filename in os.listdir(self.extract_path)
            if filename.endswith("shp")
        ]
        if len(shps) != 1:
            Archive.skipped.add(str(self))
            return ""

        filename = shps[0]

        name = self.feature.capitalize()
        model = ogrinspect(filename, name, srid=4269, multi_geom=True, imports=False)
        if name in Archive.modelcache:
            if Archive.modelcache[name] == model:
                print("match")
                self._model = model
            else:
                raise ModelError("this shouldn't happen!")
        Archive.modelcache[self.feature] = model
        return model
Exemplo n.º 2
0
    gtype = data_source[layer_key].geom_type
    if multi_geom:
        gtype.to_multi()
    _mapping[geom_name] = str(gtype).upper()
    return _mapping


def ogrinspect(*args, **kwargs):
    """
    Given a data source (either a string or a DataSource object) and a string
    model name this function will generate a GeoDjango model.

    Usage:

    >>> from django.contrib.gis.utils import ogrinspect
    >>> ogrinspect('/path/to/shapefile.shp','NewModel')

    ...will print model definition to stout

    or put this in a Python script and use to redirect the output to a new
    model like:

    $ python generate_model.py > myapp/models.py

    # generate_model.py
    from django.contrib.gis.utils import ogrinspect
    shp_file = 'data/mapping_hacks/world_borders.shp'
    model_name = 'WorldBorders'

    print(ogrinspect(shp_file, model_name, multi_geom=True, srid=4326,
                     geom_name='shapes', blank=True))
Exemplo n.º 3
0
    def gishandle( self, model_name, filepath, *args, **options ):
        from django.contrib.gis.utils import ogrinspect, mapping

        print ogrinspect( filepath, model_name, srid=options['srid'], blank=True )
        print "\n\n%s_mapping = %s" % ( model_name.lower(), mapping( filepath ))