10

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


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 ...]]
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
swiss_knight
  • 10,309
  • 9
  • 45
  • 117

2 Answers2

14

For the coordinates, see Getting list of coordinates for points in layer using QGIS? or Reproject CSV file to different projecton using ogr2ogr

-lco GEOMETRY=AS_XY is what you need.

For the error part, just do as you are told:

 ogr2ogr -f "CSV" -overwrite MyLayer2.csv MyLayer.shp

This will destroy any previous content of the csv file, and add values from the shapefile to the CSV.

AndreJ
  • 76,698
  • 5
  • 86
  • 162
4

Ok thanks, it's working now but as I have lines features, the GEOMETRY=AS_XY doesn't work, of course.
I need to set -lco GEOMETRY=AS_WKT to store the whole line :

$ ogr2ogr -f "CSV" MyLayer.csv MyLayer.geojson -lco GEOMETRY=AS_WKT

And if I set the -overwrite flag I still have some error if I run the same translation as before (but it's working if I make the conversion from an other data source, like a shapefile for example and with an other destination name):

$ ogr2ogr -f "CSV" -overwrite MyLayer.csv MyLayer.geojson -lco GEOMETRY=AS_WKT
ERROR 1: Attempt to create csv layer (file) against a non-directory datasource.
ERROR 1: Terminating translation prematurely after failed
translation of layer OGRGeoJSON (use -skipfailures to skip errors)

Anyway, this is not really important as removing the file prior to the command is working fine.

Tip fore more convenience:
If you need to process the output file using a stream editor after that, I highly recommend to set -lco SEPARATOR= to something other than COMMA which is the default because comma is used as the field separators AND as the coordinates separator of your lines' points!

swiss_knight
  • 10,309
  • 9
  • 45
  • 117