Monday 26 January 2015

gdal - Converting csv to kml/geojson using ogr2ogr or equivalent command line tools?



I've just unsuccessfully tried to use ogr2ogr to convert CSV into KML like so:


ogr2ogr -f "KML" output.kml input.csv

The csv has "latitude" and "longitude" columns.


The attributes are all loaded but if you inspect the KML's text, the Coordinate elements are not being added so its not working.


I feel like this will work if I can inform gdal to interpret certain columns ("latitude", "longitude") as coordinates.



How might I do this?



Answer



According to the ogr2ogr csv documentation and also this answer, you need to specify which fields contain the geometry in a VRT file:




test.csv
wkbPoint
WGS84





Save this as a file with VRT extension and use it as the source:


ogr2ogr -f KML output.kml input.vrt

The csv is specified in test.csv. So for this example:



  1. open a text editor and save the first code block as input.vrt

  2. put your csv (test.csv), in the same directory

  3. open a console or command window, change to that same directory and run the ogr2ogr command shown above.



The same steps apply for different output formats, e.g. shapefile, geojson, etc.


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