[Gdal-dev] coordinates for csv-files with ogr2ogr
Mateusz Loskot
mateusz at loskot.net
Tue Oct 24 09:27:54 EDT 2006
C.Strobl at dlr.de wrote:
> hello all,
> is there a possibility to generate with ogr2ogr a csv-file which
> contains also the coordinates and not only the attributes.
According to OGR CSV driver docs [1] it's not possible:
"OGR supports reading and writing non-spatial tabular data stored in
text CSV files."
"OGR CSV layer never have any geometry on features, nor do they have an
associated coordinate system."
[1] http://www.gdal.org/ogr/drv_csv.html
If you want to read your CSV file, here is example of a trick
where geometry is stored in CSV as WKT, next you access it through VRT
model:
1. CSV file with geometries encoded in WKT
ID,THEGEOM
1,"LINESTRING (12.375 49.618, 12.380 49.61, 12.474 49.634)"
2,"LINESTRING (16.198 50.431, 16.205 50.434, 16.334 50.405)"
3,"LINESTRING (19.628 51.389, 20.278 51.782, 20.350 51.840)"
2. VRT definition [2]
<OGRVRTDataSource>
<OGRVRTLayer name="test">
<SrcDataSource
relativeToVRT="0">test.csv</SrcDataSource>
<SrcLayer>test</SrcLayer>
<GeometryType>wkbLineString</GeometryType>
<GeometryField encoding="WKT" field="THEGEOM" />
<LayerSRS>epsg:4326</LayerSRS>
</OGRVRTLayer>
</OGRVRTDataSource>
[2] http://www.gdal.org/ogr/drv_vrt.html
Now, you can dump the CSV file, in example, to ESRI Shapefile:
$ ogr2ogr -f "ESRI Shapefile" test.shp test.vrt
$ ogrinfo test.shp
OGR: OGROpen(test.shp/0x804dcb8) succeeded as ESRI Shapefile.
INFO: Open of `test.shp'
using driver `ESRI Shapefile' successful.
OGR: GetLayerCount() = 1
1: test (Line String)
Obviously, this solution requires to preprocess CSV file and encode
geometries to WKT format. Small script should do it well.
> thanks and greetings from munich.
> christian
> p.s. besides this ogr2ogr generates always with the csv-option a folder
> AND a file with the same name, e.g. .../file.csv/file.csv
Not exactly. The idea is to create or add .csv file to directory.
As OGR CSV doc [1] says:
"The driver supports creating new databases (as a directory of .csv
files), adding new .csv files to an existing directory or .csv file"
So, the command looks like:
$ ogr2ogr -f CSV somedir point.shp
$ ls somedir/
point.csv
'somedir' is called as a CSV database.
Now, you can add new .csv file to this database:
ogr2ogr -update -f CSV somedir line.shp
Cheers
--
Mateusz Loskot
http://mateusz.loskot.net
More information about the Gdal-dev
mailing list