3

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 or shpdump. ogr2ogr is a wonderful tool but it seems that I cannot extract vertices from a polyline shapefile.

  • 1
    Are you prepared to write some code? All polylines and polygons are made from arrays of points so it's not too difficult to break them down in either PyQGIS or arcpy and export CSV... I personally don't know of any tool (besides Vertices to Points in Esri) that would do that for me... QGIS has a great plugin library, if you found something similar a few edits might make it exactly what you want. If you like ShpDump so well why not modify the file from one format to the other in a simple python script? – Michael Stimson Sep 1 '15 at 2:38
  • @MichaelMiles-Stimson thanks for your comment, but I think you are wrong about a simple way to do this, I found a solution, it's not elegant but it does the job, hope it will work to someone else. – Gery Sep 1 '15 at 4:34
7

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.

  • 1
    What is 'cat'? Is this on a linux system? I work on windows; I have also been programming for many years so I don't often go looking for tools to do 'simple' tasks... I would write a python script to do it. If this method works for you that's great, there are quite a few GIS professionals who choose not to script/program who would benefit from this. – Michael Stimson Sep 1 '15 at 5:01
  • @MichaelMiles-Stimson I also used to do all these tasks in windows under cygwin, these command line tools are available there (eg. cat, sed, awk, etc). – Gery Sep 1 '15 at 9:54
6

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.

Background:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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