[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 17:51:50 EDT 2011


Le mercredi 06 juillet 2011 23:36:52, Luke Peterson a écrit :
> On Wed, Jul 6, 2011 at 3:56 PM, Even Rouault

ok, with the help of the traces, this makes sense now. The real issue is that 
the warning and error :

> Warning 1: Geometry to be inserted is of type Multi Polygon, whereas
> the layer geometry type is Polygon.
> Insertion is likely to fail
> ERROR 1: INSERT command for new feature failed.
> ERROR:  new row for relation "temp_block" violates check constraint
> "enforce_geotype_wkb_geometry"

Always look at the errors that appear first. They are generally the most 
significant ones. The error message you see later about the fid is just a guess, 
that is actually wrong in your case. There's no issue about FIDs in your case. 
It is just that CreateLayer() will declare the geometry as being Polygon, but 
insert MultiPolygon sometimes. That's one of the caveats with the shapefile 
format/driver that will report a layer as being of type wkbPolygon but can 
return geometries that might actually be POLYGON or MULTIPOLYGON.

Clearly, CreateLayer() is a simplistic implementation that doesn't have all 
the subtelties and options of ogr2ogr... With ogr2ogr, you would typically 
workaround this by using the -nlt MULTIPOLYGON option that would convert 
polygons to multipolygons before passing them to the PG driver.

You can as well change your code to declare the layer as of type 
ogr.wkbMultiPolygon and use :

geom = newFeature.StealGeometry()
newFeature.SetGeometry(ogr.ForceToMultiPolygon(geom))


More information about the gdal-dev mailing list