4

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

1 Answer 1

8

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

1
  • 1
    This works great; however, in my tests the reprojected CSV file has two WKT columns - both named WKT (the name of the geometry column in the input CSV). Is there any way to avoid the column duplication, or at least to name to reprojected WKT column with a different name? Commented Sep 4, 2015 at 21:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.