1

ogr2ogr produces invalid GeoJSON when multiple layers are involved (http://trac.osgeo.org/gdal/ticket/3842). For example:

ogr2ogr -f GeoJSON "$f.json" "$f" tracks routes

For me the bad GeoJSON seems to be a missing "] }" at the end of the file, but I don't know if that's consistent.

I'm trying to batch convert a number of GPX files. Some have tracks, some have routes; none have both. Is there a workaround that will work on both types?

2

As requested, here a WORKAROUND, which works for me in Bash. It first counts the features in the layers, then uses the resulting list of non-empty layers for further processing.

for file in *.gpx ; do 
    for layer in routes tracks ; do 
        echo -n "$file $layer " &&
        ogr2ogr -f csv /dev/stdout $file -sql "select count(*) from $layer" |
        awk 'NR>1'
    done
done | awk '$3' |
while read f l j ; do ogr2ogr -f GeoJSON "${f%.gpx}_$l.json" "$f" $l ; done
  • Thanks very much! It works, with two caveats. 1) GPX files with spaces in the name break something (maybe awk), 2) Since I don't care about the distinction between tracks and routes, I remove the _$l. – Steve Bennett Aug 21 '13 at 8:58
  • Indeed: you'd have to take care about spaces in file names. My file names never have spaces, so I forget to worry about potential pitfalls. – Hermann Aug 21 '13 at 13:16

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.