10

Is it possible to directly pipe data to ogr2ogr? I'm trying to curl a GeoJSON file and pipe it to ogr2ogr to covert it to another format.

Without using a pipe, I was able to achieve this by first writing a file and then converting it using the following command, but I'm curious if it is possible to skip that step.

curl "https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_50m_admin_0_countries.geojson" -o "countries.geojson" && ogr2ogr -f "KML" countries.kml countries.geojson

I also tried creating a variable with the curl output instead of piping it, but ogr2ogr was not able to read it:

DATA=$(curl -s "https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_50m_admin_0_countries.geojson") && ogr2ogr -f "KML" countries.kml $DATA

Thanks!

16

OGR has its own idiom for stdin, /vsistdin/. Use that as ogr2ogr's first argument (the dst_datasource_name) and you can pipe curl's output to it:

curl "https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_50m_admin_0_countries.geojson" | ogr2ogr -f "KML" countries.kml /vsistdin/
7

You can use URLs as paths to OGR data sources, so you don't need curl or pipes.

ogr2ogr -f KML countries.kml https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_50m_admin_0_countries.geojson
  • 1
    That's awesome! I had no idea that was a possibility. This is a cleaner solution than the accepted answer, but the question was explicitly about piping into ogr2ogr. – jczaplew Nov 13 '14 at 22:09
  • 2
    Good one, Mike! I wouldn't call either of them super clean (ogr2ogr putting the destination coming before the source bugs me). Treating a URL as a local file works... until it doesn't. And curl has a lot of options (verbose mode, security) that ogr2or doesn't. – sgillies Nov 13 '14 at 22:53
  • The source files being listed last is a reasonable choice, because there can be more than one. – Steve Bennett Mar 31 at 23:01

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.