I found that
shpdump is a very good tool to extract all vertices from an ESRI polyline shapefile, but I need a single triplet (X,Y,Z) file (ie. shpdump provides all coordinates by parts). Is there a similar command-line tool that can do this? I found some python solutions, but I need something like
ogr2ogr is a wonderful tool but it seems that I cannot extract vertices from a polyline shapefile.
I found that
I found it, this dirty hack is not elegant at all but it works great, my command-line solution as follows:
ogr2ogr -f CSV tmp.csv your.shp -lco GEOMETRY=AS_WKT -s_srs EPSG:4326 -t_srs EPSG:4326 -overwrite cat tmp.csv | sed -e '1,1d' | tr ',' '\n' | sed 's/[A-Za-z"()]*//g' | tr ' ' ',' | sed 's/^,//' > your.shp.csv
One caveat, you just need to avoid including your *.dbf file, ie. use only your .sbn, .sbx, .shp, and .shx files. If you include your dbf file, the code above won't work as expected.
I believe you can to it with one ogr2ogr command. The following solution should work for all sort of geometries and therefore I tested it with a two-part multipolygon, one of the parts having a hole.
- With -dialect SQLite GDAL tools can utilize all the SQL functions of Spatialite https://www.gaia-gis.it/gaia-sins/spatialite-sql-latest.html. DissolvePoints function converts all the vertices of a geometry into MultiPoint.
- ogr2ogr http://www.gdal.org/ogr2ogr.html has some own clever options, like -explodecollections. We will use that for exploding MultiPoint into set of simple points.
Take a shapefile with one multipolygon
ogrinfo multipoly.shp -al INFO: Open of `multipoly.shp' using driver `ESRI Shapefile' successful. Layer name: multipoly Geometry: Polygon Feature Count: 1 Extent: (449.000000, 169.000000) - (1076.000000, 517.000000) Layer SRS WKT: (unknown) OGRFeature(multipoly):0 MULTIPOLYGON (((486 464,781 517,768 256,506 240,449 395,486 464),(602 381,531 321,695 303,602 381)),((851 512,1076 486,1028 169,867 270,851 512)))
Create a new shapefile with ogr2ogr
ogr2ogr -f "ESRI Shapefile" -dialect sqlite -sql "select DissolvePoints(geometry) from multipoly" -explodecollections points.shp multipoly.shp
Check what you got
ogrinfo points.shp -al INFO: Open of `points.shp' using driver `ESRI Shapefile' successful. Layer name: points Geometry: Point Feature Count: 15 Extent: (449.000000, 169.000000) - (1076.000000, 517.000000) Layer SRS WKT: (unknown) FID: Integer64 (11.0) OGRFeature(points):0 FID (Integer64) = 0 POINT (486 464) OGRFeature(points):1 FID (Integer64) = 1 POINT (781 517) OGRFeature(points):2 FID (Integer64) = 2 POINT (768 256) OGRFeature(points):3 FID (Integer64) = 3 POINT (506 240) ...