Skip to content

Count number of vertices in a shapefile using OGR

An answer to this question on the GIS Stack Exchange.

Question

I'd like to extract the total number of vertices in a shapefile using command-line OGR.

This answer suggests a method for intersections, but it isn't clear how to cross-apply it.

Many other methods suggest writing little Python scripts, but this seems like a needlessly complex solution.

Answer

I accomplished it using the following command:

ogrinfo -al myshapefile.shp | grep POLYGON | sed 's/$/,/' | tr -d -c "," | wc

This begins by printing the shapefile contents to stdout, including all of the geometry. (ogrinfo -al myshapefile.shp)

Next, we extract only the lines containing geometry (grep POLYGON)

This results in a list of points of the form, for a multipolygon, of:

(((34 43,22 10,70 5),(23 43,54 1,89 2)),((23 43,43 2)))

Notice that each point is followed by a comma, except the last one.

Therefore, we add a comma to the end of the line. (sed 's/$/,/')

Finally, we eliminate every character that is not a comma (tr -d -c ",") and count the commas (wc) which gives the number of points.