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

Chaitanya kumar CH chaitanya.ch at gmail.com
Wed Jul 6 18:45:54 EDT 2011


Luke,

Please check if there are features with duplicate FIDs in the MSSQL
datasource. The 'uniqueness' of the FIDs is not checked throughly in OGR.
Some may slip through.

Resetting the FIDs is not always desirable. Some applications might need to
retain the old FIDs.
However, having an option to reset them could be useful in some cases. I am
not sure if doing this in OGRDataSource::CopyLayer() is appropriate.

On Wed, Jul 6, 2011 at 8:41 PM, Luke Peterson <luke.peterson at gmail.com>wrote:

>
> I'm reviving a nearly-year-old thread here, sort of. I'm trying to
> create cross-compatibility between MSSQL and PostGIS on some OGR-based
> code I wrote in Python for the MSSQL environment: I had been running
> into issues simply creating layers with OGR -- would run something
> like this:
>
> name =
> serverDS.CopyLayer(shapeDS.GetLayerByIndex(0),table,options).GetName()
>
> with serverDS being an open OGR datastore (destination) and shapeDS
> being an opened Shapefile (with an OGR-imposed FID).
>
> This code works fine when serverDS is a MSSQLSpatial driver, but I get
> hit with the following error when I run with a PostgresSQL driver:
>
> "You've inserted feature with an already set FID and that's perhaps
> the reason for the failure. If so, this can happen if you reuse the
> same feature object for sequential insertions. Indeed, since GDAL
> 1.8.0, the FID of an inserted feature is got from the server, so it is
> not a good ideato reuse it afterwards... All in all, try unsetting the
> FID with SetFID(-1) before calling CreateFeature()"
>
> OK, clear enough. I took a look at the DataStore code here and here:
>
>
> http://trac.osgeo.org/gdal/browser/branches/1.8/gdal/ogr/ogrsf_frmts/pg/ogrpgdatasource.cpp
>
>
> http://trac.osgeo.org/gdal/browser/branches/1.8/gdal/ogr/ogrsf_frmts/generic/ogrdatasource.cpp
>
> I noticed that the PG driver has no PG-native code for CopyLayer() at
> all; the generic driver's CopyLayer() function doesn't set a null FID
> for CreateFeature(). So, it seems CopyLayer() is useless for PostGIS
> in my situation.
>
> The code I'm going with right now uses CopyLayer() out of the box
> except if serverDS.GetDriver().GetName() == 'PostgreSQL', in which
> case I'm re-implementing most of the logic from CopyLayer() in Python
> except adding a SetFID(-1) call:
>
>        newLayer =
>
> serverDS.CreateLayer(table,shapeDS.GetLayerByIndex(0).GetSpatialRef(),ogr.wkbUnknown,options)
>        for x in
> xrange(shapeDS.GetLayerByIndex(0).GetLayerDefn().GetFieldCount()):
>
>  newLayer.CreateField(shapeDS.GetLayerByIndex(0).GetLayerDefn().GetFieldDefn(x))
>
>        newLayer.StartTransaction()
>        for x in xrange(shapeDS.GetLayerByIndex(0).GetFeatureCount()):
>            newFeature = shapeDS.GetLayerByIndex(0).GetFeature(x)
>            newFeature.SetFID(-1)
>            newLayer.CreateFeature(newFeature)
>            if x % 128 == 0:
>                newLayer.CommitTransaction()
>                newLayer.StartTransaction()
>        newLayer.CommitTransaction()
>
> So I guess my question is whether I'm missing something, or whether I
> should submit a bug report or feature request on the PG driver to
> create a parallel implementation for CopyLayer() that either natively
> incorporates the SetFID(-1) or that takes an option that could trigger
> that behavior. Seems a shame this function exists but doesn't work
> under certain circumstances (again -- maybe I'm missing something?)
>
> Regards,
> Luke
>
> --
Best regards,
Chaitanya kumar CH.
/tʃaɪθənjə/ /kʊmɑr/
+91-9494447584
17.2416N 80.1426E
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20110707/7fc4a2d5/attachment.html


More information about the gdal-dev mailing list