Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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)
share|improve this question
4  
Found the solution but I'm not allowed to answer my own question because I don't have enough karma, I'm supposed to wait 8 hours :-p You can run multiple times to write separate shapefiles and give eg. the option -where "OGR_GEOMETRY='Point'" to include only Points in one output file, then run again with -where "OGR_GEOMETRY='LineString'", etc. –  slinkp Jun 1 '12 at 16:57

2 Answers 2

up vote 8 down vote accepted

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
share|improve this answer

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.

share|improve this answer
    
Thanks, but that is not the question I asked. –  slinkp Jun 4 '12 at 19:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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