[GIS] Count number of vertices in a shapefile using OGR

command linecountogrvertices

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.

Best Answer

To extract the total number of vertices in a shapefile, I'd simply use a bit of Spatialite SQL:

ogrinfo states.shp -dialect SQLite -sql "SELECT sum(ST_NPoints(geometry)) AS n_vertices FROM states"

Example of output:

ogrinfo states.shp -dialect SQLite -sql
 "SELECT sum(ST_NPoints(geometry)) AS n_vertices FROM states"
INFO: Open of `states.shp'
      using driver `ESRI Shapefile' successful.

Layer name: SELECT
Geometry: None
Feature Count: 1
Layer SRS WKT:
(unknown)
n_vertices: Integer (0.0)
OGRFeature(SELECT):0
  n_vertices (Integer) = 11481
Related Question