[gdal-dev] Append a shapefile to a postgis table using the GDAL/OGR Python interface

Even Rouault even.rouault at mines-paris.org
Wed Jul 6 15:56:57 EDT 2011


Le mercredi 06 juillet 2011 21:46:58, Luke Peterson a écrit :

> You're right, an error should be expected from your append case -- but
> I'm not using it to append, I'm running into this FID error on a
> simple invocation of CopyLayer() in an empty schema, trying to copy a
> layer from a Shapefile DS to a PostGIS DS.

ok, understood. The subject of your email confused me "append a shapefile to a 
postgis table"...

> 
> What CopyLayer() is supposed to do (and the code I've written does) is
> create a new layer based on an existing layer, copies a field set from
> the source layer, then iterates through each of the features in the
> source layer, creating a copy in the new layer. The problem is, if I
> use it as-is with a shapefile as the source DS (for which OGR has
> generated an FID), it will copy and create a new layer, but it will
> only insert the first feature and then die with an error saying I need
> to manually set the FID to -1. But I can't do that with CopyLayer(),
> because CopyLayer() automatically pulls an FID into the destination
> feature if one exists in the source DS in each CreateFeature iteration
> (line 318).

Yes, but that doesn't cause a problem for me when I test. Not sure why you get 
this error. If you create a new layer you should manage to create features 
with arbitrary FID. Can you set CPL_DEBUG=ON as a environement variable and 
retry ?

> The other thing I've done with mine that's different from
> CopyLayer()'s logic is I set the geometry to wkbUnknown -- I've
> encountered errors with PostGIS in cases where the first feature being
> copied is a wkbPolygon and the second feature is a wkbMultiPolygon,
> for example.

Ah yes, CopyLayer() cannot deal with that. It is just for the basic cases.

> 
> The C++ for the CopyLayer() method is here in the DataSource class
> source:
> http://trac.osgeo.org/gdal/browser/branches/1.8/gdal/ogr/ogrsf_frmts/gener
> ic/ogrdatasource.cpp

Yes I know, thanks ;-)

> 
> Here's the function I've written (working code snippet incorporating
> some of your suggestions from earlier and follows pretty much the same
> path as the C++) -- it's just a shorthand "import-a-shapefile" wrapper
> in python for a fairly easy task in OGR2OGR that I find myself doing
> often enough in python. It's a bit clunky, but has worked in all
> encountered use cases thus far:

You could also use 
http://trac.osgeo.org/gdal/browser/branches/1.8/gdal/swig/python/samples/ogr2ogr.py 
or adapt it to your needs



More information about the gdal-dev mailing list