Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I would like to create a Postgres database (postgreslq-9.1 and postgis-2.0.2) storing several shapefiles but I am uncertain on how to implement it so I'm searching for some advices.

I have already done this last year but was pretty uncofortable beacause I had a table per shapefile, every table signed with its SRID (stored inside geometry_columns table - I was using postgis 1.5, so this was still a table and not a view as in postgis-2).

I had to create a new table every time I converted a shapefile: pretty uncofortable because I had to query them using Django ORM, and create table mapping on the fly is not trivial.

Is that possible to create one single table having an id indicating the original shapefile and the relative SRID per geometry?

Otherwise have you got any better ideas?

Thank you.

share|improve this question
    
Have you found an answer to your question? I am searching for something like this today and I cant find info about it. –  Below the Radar Apr 5 '13 at 13:04
    
At the moment I'm experimenting with django-mutant, which permits to create run time models. In this way I create a table per shapefile, specifying at runtime which srid I want... –  caneta Apr 5 '13 at 13:31
    
Ok great! I was wondering if Dynamic Models could be a good solution. The other way im looking is to use ogrinspect (geodjango inspectdb) to update the models.py on load/delete shapefile. What have you found so far? –  Below the Radar Apr 14 '13 at 16:59

2 Answers 2

After some experiments, I think I've found in django-mutant the solution that fit my needs. With it, you can create a dynamic model in the following way:

from mutant.contrib.geo.models import GeoModel, GeometryFieldDefinition
from mutant.contrib.numeric.models import SmallIntegerFieldDefinition
from mutant.models import BaseDefinition, ModelDefinition

model_def, created = ModelDefinition.objects.get_or_create(
                         app_label='myApp',
                         object_name='myShape',
                         defaults=dict(
                         bases=(BaseDefinition(base=GeoModel),),
                         fields=(GeometryFieldDefinition(name='the_geom', srid=31258),
                         SmallIntegerFieldDefinition(name='cat'),)
                    )
                )

In this way, calling model_def.model_class() would return the same model as if you had declared the following in your models.py:

class myShape(GeoModel):
    cat = models.SmallIntegerField()
    the_geom = models.GeometryField(srid=31258)
    class Meta:
        db_table = u'myApp'

As you run ModelDefinition.objects.get_or_create() method, a table named mutant_myApp_myShape is created into PostgreSQL with a relative row in geometry_colums table. In addition, this model has a proper id entry inside django-mutant table named mutant_modeldefinition.

Further tests need to be made, but this approach seems a good starting point to me.

share|improve this answer

Are you using shp2pgsql to load your Shapefiles into PostGIS? This command-line utility comes free with PostGIS. This article describes how to load Shapefiles in batch on Windows. The utility also has a switch, -a, that will allow you to append the Shapefile to an existing table, rather than creating a new one, provided that the table and the Shapefile share the same schema. From the shp2pgsql man page:

OPTIONS
    The loader has several operating modes distinguished by command line flags:

    (Note that -a, -c, -d and -p are mutually exclusive.)

    -a     Appends data from the Shape file into the database table. Note that to use this option to load multiple files, the files must have the same attributes and same data types.
share|improve this answer
    
Thank you for your response. I knew this tool but I read each geometry individually with GeoDjango utils, because some of them are incorrect and need to be fixed. After the correction (if needed) I insert the read Geometry with a simple save(). –  caneta Aug 26 '13 at 7:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.