OGR2OGR CSV Reprojection – How to Reproject CSV File to a Different Projection Using OGR2OGR

csvogr2ogr

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:

<OGRVRTDataSource> 
  <OGRVRTLayer name="visivisi"> 
    <SrcDataSource>visivisi.csv</SrcDataSource> 
    <GeometryType>wkbPoint</GeometryType> 
    <LayerSRS>+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</LayerSRS> 
    <GeometryField encoding="PointFromColumns" x="x" y="y"/> 
    <Field name="latitude" src="x" />
    <Field name="longitude" src="y" />
  </OGRVRTLayer> 
</OGRVRTDataSource>

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

Best 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:

<OGRVRTDataSource> 
  <OGRVRTLayer name="codepoint"> 
    <SrcDataSource>codepoint.csv</SrcDataSource> 
    <GeometryType>wkbPoint</GeometryType> 
    <LayerSRS>EPSG:27700</LayerSRS> 
    <GeometryField encoding="PointFromColumns" x="Eastings" y="Northings"/> 
  </OGRVRTLayer> 
</OGRVRTDataSource>

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

Related Question