22

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)
21

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
4

If the output parameter is a directory instead of a file name, ogr2ogr will automatically convert all geometry types into separate shapefiles:

ogr2ogr out_dir d:\incoming\nhn_09AA001_1_0.gml

Unfortunately for the OP this doesn't work for KML, but it does for some of the other multiple geometry type formats like ArcInfo Coverages and GML. Posting here because this is the highest voted question on selecting geometry type.

D:\>dir /s/b converted\*.shp

D:\converted\NHN_HD_ISLAND_2.shp
D:\converted\NHN_HD_MANMADE_0.shp
...
D:\converted\NHN_TO_NAMEDFEA_0.shp
D:\converted\NHN_WORKUNIT_LIMIT_2.shp
-1

ogr2ogr: convert a single kml to shapefile:

ogr2ogr -f "ESRI Shapefile" C:\kml\LJ0047.shp C:\kml\LJ0047_.kml

ogr2ogr: convert multiples kml files to shapefiles:

for %f in (*.kml) do ogr2ogr -f "ESRI Shapefile" %~nf.shp %f

It's necessary to access the input folder.

  • Thanks, but that is not the question I asked. – slinkp Jun 4 '12 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.