Wednesday 12 July 2017

Reproject CSV file to different projecton using ogr2ogr


I have a CSV file "visivisi.csv" with two columns "x,y" which is in my country local projection. The proj4 line is



+proj=tmerc +lat_0=0 +lon_0=24 +k=0.9996 +x_0=500000 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs

I am trying to use ogr2ogr to convert this CSV to a new CSV file in wgs84 projection, is this even possible? This doesn't work:


ogr2ogr -f csv -s_srs "+proj=tmerc +lat_0=0 +lon_0=24 +k=0.9996 +x_0=500000 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs" -t_srs "EPSG:4326" test.csv visivisi.csv

EDIT: Thanks to below answer, this is the solution (too long for comment section):


if you have a csv file with fields "x,y" called "visivisi.csv", make such visivisi.vrt file:


 

visivisi.csv

wkbPoint
+proj=tmerc +lat_0=0 +lon_0=24 +k=0.9996 +x_0=500000 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs






Then run this command:


ogr2ogr -overwrite -f CSV -lco GEOMETRY=AS_XY -t_srs EPSG:4326 test.csv visivisi.vrt


test.csv can not exist beforehand. the SRS in the above example is latvian LKS92



Answer



This should be possible. But I have not tried it, so test it out first.


In order to read a csv as a spatial file you need to create a .vrt file for it. So it can be loaded in virtually.


So you need something like:


 

codepoint.csv
wkbPoint

EPSG:27700




So this defines how your csv is laid out. Replace field "Easting" and file "codepoint" and projection "EPSG:27700" as needed.


Then to convert it shouldn't be an issue:


ogr2ogr -f CSV -lco GEOMETRY=AS_XY -t_srs EPSG:4326 test.csv visivisi.csv

Writing to CSV, geometry is discarded as default, so the -lco GEOMETRY=AS_XY is needed. Your source projection should be defined in the .vrt file so a -s_srs shouldn't be needed, but might.



I have a guide on loading csv data to PostGIS which may help a bit: http://gisforthought.com/loading-csv-xy-data-into-postgis-with-codepoint-open/


And the ogr csv driver details does cover most of this: http://www.gdal.org/ogr/drv_csv.html


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