Saturday 21 September 2019

gdal - Selecting feature types when using ogr2ogr to convert to shapefile?


Shapefiles can only have one geometry type, but many other formats (kml, geojson) can have multiple types.


When converting to shapefiles, is it possible to to tell ogr2ogr to create multiple shapefiles, one for each type?


It seems like the "-where" option might be useful but the man page does not explain the syntax.


For example, here's a command that fails:


$ wget http://a841-tfpweb.nyc.gov/jackson-heights/wp-content/themes/tfp/kml/transit.kml
$ ogr2ogr -f "ESRI Shapefile" transit.shp transit.kml
Warning 6: Normalized/laundered field name: 'Description' to 'Descriptio'

ERROR 1: Attempt to write non-linestring (POINT) geometry to ARC type shapefile.
ERROR 1: Terminating translation prematurely after failed
translation of layer Transit (use -skipfailures to skip errors)

Answer



Figured it out by reading the OGR SQL documentation at http://www.gdal.org/ogr/ogr_sql.html


This works, using one command and one output file per geometry type:


$ ogr2ogr -where "OGR_GEOMETRY='Point'" -f "ESRI Shapefile" transit_points.shp transit.kml
$ ogr2ogr -where "OGR_GEOMETRY='LineString'" -f "ESRI Shapefile" transit_linestrings.shp transit.kml

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...