2

This question is an exact duplicate of:

I have some CSV files that need to be converted to ESRI shapefile. These files are built like explained in the section "Building line geometries" from the GDAL documentation. For example one file can be like this:

> head river_xy2.csv                                                                                      
way_id,pt_id,x,y
1,1,11.836,44.829
1,2,11.827,44.829
1,3,11.818,44.829
1,4,11.809,44.838
1,5,11.8,44.847
1,6,11.791,44.856
1,7,11.782,44.856
1,8,11.773,44.856
1,9,11.764,44.856

GDAL understands this, for example I can do:

ogrinfo river_xy2.csv -dialect SQLite -sql "SELECT way_id, MakeLine(MakePoint(CAST(x AS float),CAST(y AS float))) FROM river_xy2 GROUP BY way_id"

And I get a nice list of OGRFeatures in output. However, how can I save that to a shapefile? If I try to use GDAL_translate:

> gdal_translate -of "ESRI Shapefile" -a_srs EPSG:4326 river_xy2.csv river_xy2.shp
ERROR 4: `river_xy2.csv' not recognized as a supported file format.

While if I remove the first row I get an error after the first feature:

ERROR 1: Ungridded dataset: At line 549, change of Y direction

EDIT: It looks like I can do:

ogr2ogr -dialect SQLite -sql "SELECT way_id, MakeLine(MakePoint(CAST(x AS float),CAST(y AS float))) FROM river_xy GROUP BY way_id" -a_srs EPSG:4326 river_xy.shp river_xy.csv

marked as duplicate by Antonio Falciano, whyzar, aldo_tapia, JGH, jbalk Nov 28 '17 at 0:18

This question was marked as an exact duplicate of an existing question.

3

gdal_translate is for raster data, you want to use ogr2ogr. Yes, the command names are a complete mess.

  • Would you please be able to elaborate on this? At the present state this feels more like a comment than an answer. EDIT: I guess I should do ogr2ogr -dialect SQLite -sql "SELECT way_id, MakeLine(MakePoint(CAST(x AS float),CAST(y AS float))) FROM river_xy2 GROUP BY way_id" river_xy2.shp river_xy2.csv – AF7 Nov 27 '17 at 18:11
1

Just to expand on bugmenot123's answer, say you had a .csv file called xy.csv with the following contents:

X        Y
-95.3942 28.7701
-95.356  28.7644

create the shapefile's database file with the following command:

ogr2ogr -f "ESRI Shapefile" out.dbf xy.csv

Create a text file that will be the 'virtual' shapefile (for this example, name it out.vrt) having the following contents:

<OGRVRTDataSource>
  <OGRVRTLayer name="out">
    <SrcDataSource>xy.csv</SrcDataSource>
    <SrcLayer>xy</SrcLayer>
    <GeometryType>wkbPoint</GeometryType>
    <LayerSRS>WGS84</LayerSRS>
    <GeometryField encoding="PointFromColumns" x="x" y="y"/>
  </OGRVRTLayer>
</OGRVRTDataSource>

Create the new shapefile (.shp) from the virtual shapefile (VRT) as follows:

ogr2ogr -f "ESRI Shapefile" out.shp out.vrt
  • What if I want a shapefile with the lines? Is it sufficient to use wkbLine? If ogrinfo understands the data, why can't it produce a readable output directly? – AF7 Nov 27 '17 at 18:09
  • I think you answered your own question. : ) Your command: ogr2ogr -dialect SQLite -sql "SELECT way_id, MakeLine(MakePoint(CAST(x AS float),CAST(y AS float))) FROM head_river_xy2 GROUP BY way_id" head_river_xy2.shp head_river_xy2.csv works quite nicely to convert a csv file of points / coordinates to a line shapefile ! – cm1 Nov 27 '17 at 19:18
  • Unfortunately as you can see from the updated question it seeems to have some issues... I'll test with the solution provided by @Antonio Falciano – AF7 Nov 27 '17 at 19:30
  • Do you have more than one way_id? It looks like it is connecting all the lines which may indicate they share the same way_id since they were aggregated/grouped by it. In my test here I added another (fake/testing) way_id to your example like so way_id,pt_id,x,y 1,1,11.836,44.829 1,2,11.827,44.829 1,3,11.818,44.829 1,4,11.809,44.838 1,5,11.8,44.847 1,6,11.791,44.856 1,7,11.782,44.856 1,8,11.773,44.856 1,9,11.764,44.856 2,10,12.836,45.829 2,11,12.827,45.829 2,12,12.818,45.829 2,13,12.809,45.838 – cm1 Nov 27 '17 at 19:51
  • I might have found the problem, I'll report back. I think the file was not correctly formatted by those who produced it. – AF7 Nov 27 '17 at 19:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.