[gdal-dev] ogr python: copying features from postgis to postgis

Stefan Ziegler stefan.ziegler.de at gmail.com
Sat Nov 26 12:18:50 PST 2016


Hi

I'm trying to copy some features from one postgis table to another postgis
table:

dstLayer.StartTransaction()

srcFeature = srcLayer.GetNextFeature()
while srcFeature is not None:
    srcLayerDefn = srcLayer.GetLayerDefn()

    dstLayerDefn = dstLayer.GetLayerDefn()
    dstFeature = ogr.Feature(dstLayerDefn)
    dstFeature.SetFID(-1)

    for i in range(0, dstLayerDefn.GetFieldCount()):
        attrName = dstLayerDefn.GetFieldDefn(i).GetNameRef()

        # ATTRIBUTES
        try:
            attrVal = srcFeature.GetField(attrName)
            dstFeature.SetField(attrName, attrVal)
        except ValueError:
            pass

    # GEOMETRY
    geom = srcFeature.GetGeometryRef()
    dstFeature.SetGeometry(geom)

    # ADD FEATURE
    dstLayer.CreateFeature(dstFeature)

    srcFeature = srcLayer.GetNextFeature()

dstLayer.CommitTransaction()

It throws an error when trying to create the new feature:

GNM: GNMRegisterAllInternal
GNM: RegisterGNMFile
GNM: RegisterGNMdatabase
PG: DBName="xanadu2"
PG: PostgreSQL version string : 'PostgreSQL 9.5.4 on x86_64-pc-linux-gnu,
compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609, 64-bit'
PG: PostGIS version string : '2.2 USE_GEOS=1 USE_PROJ=1 USE_STATS=1'
GDAL: GDALOpen(PG: host=localhost dbname=xanadu2 user=stefan
password=XXXXXXXXXX this=0x11e41b0) succeeds as PostgreSQL.
PG: Command Results Tuples = 11
GDAL: GDALDriver::Create(Memory,,0,0,0,Unknown,(nil))
PG: 11 features read on layer 'sql_statement'.
PG: DBName="xanadu2"
PG: PostgreSQL version string : 'PostgreSQL 9.5.4 on x86_64-pc-linux-gnu,
compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609, 64-bit'
PG: PostGIS version string : '2.2 USE_GEOS=1 USE_PROJ=1 USE_STATS=1'
GDAL: GDALOpen(PG: host=localhost dbname=xanadu2 user=stefan
password=XXXXXXXXXX this=0x12db330) succeeds as PostgreSQL.
PG: Primary key name (FID): t_id, type : int8
PG: Using column 't_id' as FID for table
'nachfuehrngskrise_nachfuehrungskreis'
Traceback (most recent call last):
  File "./datenumbau.py", line 117, in <module>
    dstLayer.CreateFeature(dstFeature)
  File "/usr/local/gdal_master/lib/python2.7/site-packages/osgeo/ogr.py",
line 1479, in CreateFeature
    return _ogr.Layer_CreateFeature(self, *args)
RuntimeError: OGR Error: General Error
GDAL: GDALClose(PG: host=localhost dbname=xanadu2 user=stefan
password=XXXXXXXXXX this=0x12db330)
GDAL: GDALClose(PG: host=localhost dbname=xanadu2 user=stefan
password=XXXXXXXXXX this=0x11e41b0)
GDAL: In GDALDestroy - unloading GDAL shared library.
GDAL: Force close of  (0x136abb0) in GDALDriverManager cleanup.
Mem: 1 features read on layer 'sql_statement'.

I'm bit confused about the "general error". Does anyone has an idea?

regards
Stefan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20161126/c93ebf55/attachment.html>


More information about the gdal-dev mailing list