Skip to main content
1 of 2
swiss_knight
  • 10.9k
  • 9
  • 50
  • 132

ogr2ogr keep geometry when converting to CSV?

Using ogr2ogr, how to keep the geometry when converting to CSV?
( Nothing relevant here: http://www.gdal.org/drv_csv.html )

$ ogr2ogr -f "CSV" MyLayer.csv MyLayer.geojson    

Here is the begin of the result file:

$ head -n4 MyLayer.csv
OBJECTID,ID,VAR1,VAR_TXT,VAR2,VAR_TXT
677406,145477,0,AB94,51,rocks � distribute
267075,145400,0,AB94,47,road acces � distribute
101700,145434,0,AB94,47,road acces � distribute

I would like to keep the geometry (X,Y) on the two first columns for example:

$ head -n4 MyLayer.csv
X,Y,OBJECTID,ID,VAR1,VAR_TXT,VAR2,VAR_TXT
152.282,635.743,677406,145477,0,AB94,51,rocks � distribute
150.624,632.052,267075,145400,0,AB94,47,road acces � distribute
151.194,631.049,101700,145434,0,AB94,47,road acces � distribute

And I encountered an error if I translate the same data but as a shapefile (or a *.mif Mapinfo file) to CSV:

$ ogr2ogr -f "CSV" MyLayer2.csv MyLayer.shp
FAILED: Layer MyLayer already exists, and -append not specified.
        Consider using -append, or -overwrite.
ERROR 1: Terminating translation prematurely after failed
translation of layer MyLayer (use -skipfailures to skip errors)

It seems it absolutely wants to keep using the previously named file (from the conversion from the GeoJSON file above).

Any help appreciated. Thanks.


Usage of ogr2ogr:

Usage: ogr2ogr [--help-general] [-skipfailures] [-append] [-update]
           [-select field_list] [-where restricted_where]
           [-progress] [-sql <sql statement>] [-dialect dialect]
           [-preserve_fid] [-fid FID]
           [-spat xmin ymin xmax ymax] [-geomfield field]
           [-a_srs srs_def] [-t_srs srs_def] [-s_srs srs_def]
           [-f format_name] [-overwrite] [[-dsco NAME=VALUE] ...]
           dst_datasource_name src_datasource_name
           [-lco NAME=VALUE] [-nln name] [-nlt type] [-dim 2|3|layer_dim] [layer [layer ...]]
swiss_knight
  • 10.9k
  • 9
  • 50
  • 132