Wednesday 19 October 2016

spatial database - Setting geometry types importing to PostGIS with OGR



I'm using ogr2ogr to import data into a PostGIS database. Each dataset I need to import has (theoretically) the same exact data structure, but it appears that they are not the same geometry types. Example, I've already imported the first layer, and there's a layer called "streets". Running this command, I import the first feature class from the first database (I have GDAL configured to read .mdb):


ogr2ogr -f PostgreSQL PG:"host=localhost user=postgres dbname=database port=5432" city1.mdb streets -nln streets -nlt MULTILINESTRING -lco GEOMETRY_NAME=geometry

When I go to append (using the -append flag) the same streets layer from city2.mdb, I get this error (presumably due to conflicting geometry types for the two "streets" layers:


Warning 1: Geometry to be inserted is of type 3D Line String, whereas the layer geometry type is 3D Multi Line String.
Insertion is likely to fail
ERROR 1: INSERT command for new feature failed.
ERROR: Geometry type (LineString) does not match column type (MultiLineString)

Is there a straightforward way with ogr2ogr to cast geometries to the same type on import? Or a way to handle this by predefining the schema and geometries for each table beforehand?




Answer



Use the -nlt option. In this case you want:


-nlt MULTILINESTRING

There is also PROMOTE_TO_MULTI (GDAL 1.10 and later), which chooses either MULTILINESTRING or MULTIPOLYGON depending on the input layer. The use case for this is "doing a mass conversion of shapefiles that [mix] different types of geometries".


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...