1

Here each line only has two nodes.

$ cat file.csv
WKT,ID,Name
"LINESTRING (-900 -1450,-900 100)",0,900W
"LINESTRING (-800 -1450,-800 100)",1,800W

How can I use ogr2ogr -segmentize on this CSV file to make each line have more nodes?

1 Answer 1

1

The obvious approach fails, returning an identical CSV:

ogr2ogr -segmentize 100 segs.csv file.csv

Converting to a shapefile works though, and features have the segmented geometry (and the original WKT in a column, you'll notice...):

ogr2ogr -segmentize 100 segs.shp file.csv
ogrinfo -al segs.shp
....
OGRFeature(segs):0
  WKT (String) = LINESTRING (-900 -1450,-900 100)
  ID (String) = 0
  Name (String) = 900W
  LINESTRING (-900 -1450,-900 -1353.125,-900 -1256.25,-900 -1159.375,-900 -1062.5,-900 -965.625,-900 -868.75,-900 -771.875,-900 -675,-900 -578.125,-900 -481.25,-900 -384.375,-900 -287.5,-900 -190.625,-900 -93.75,-900 3.125,-900 100)

You'd think converting this to csv via ogr2ogr segs.csv segs.shp would produce a segmented CSV, but it doesn't. Instead you get the original WKT column. I think this is the problem. The WKT column overrides any geometry that ogr2ogr is generating by segmentation or from spatial data like the .shp.

Solution: drop the WKT column using some SQL to only select the other columns. You also need a creation option to get WKT output in your CSV:

    ogr2ogr -segmentize 100 -lco "GEOMETRY=AS_WKT"  -sql 'SELECT ID, Name from file'  segs.csv file.csv

This returns a CSV with segmented WKT geometry in a WKT column:

WKT,ID,Name
"LINESTRING (-900 -1450,-900 -1353.125,-900 -1256.25,-900 -1159.375,-900 -1062.5,-900 -965.625,-900 -868.75,-900 -771.875,-900 -675,-900 -578.125,-900 -4
81.25,-900 -384.375,-900 -287.5,-900 -190.625,-900 -93.75,-900 3.125,-900 100)","0",900W

If you want to keep the original WKT then rename it in SQL:

ogr2ogr -segmentize 100 -lco "GEOMETRY=AS_WKT"  -sql 'SELECT ID, Name, WKT as SRC from file'  segs.csv file.csv

Another way sets a creation option so the output geometry has a different name to the column. If your data has a lot of columns and you don't want to have to specify them on the command line, then this:

ogr2ogr -segmentize 100 -lco "GEOMETRY=AS_WKT" -lco "GEOMETRY_NAME=WKT2"  -lco "CREATE_CSVT=YES" segs.csv file.csv

And now segs.csv looks like this:

WKT2,WKT,ID,Name
"LINESTRING (-900 -1450,-900 -1353.125,-900 -1256.25,-900 -1159.375,-900 -1062.5,-900 -965.625,-900 -868.75,-900 -771.875,-900 -675,-900 -578.125,-900 -4
81.25,-900 -384.375,-900 -287.5,-900 -190.625,-900 -93.75,-900 3.125,-900 100)","LINESTRING (-900 -1450,-900 100)","0",900W

But this might hit problems because GDAL might detect two possible geometry columns...

1

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