Get total vertex count for a shapefile
I needed a quick way to get the total vertex count in a shapefile. The ogrinfo
command includes vertex count in its output. This command:
$ ogrinfo -dialect SQLite -sql "SELECT sum(ST_NPoints(geometry)) AS vertex_count FROM OK_second_congressional_district" OK_second_congressional_district.shp
yields:
INFO: Open of `OK_second_congressional_district.shp'
using driver `ESRI Shapefile' successful.
Layer name: SELECT
Geometry: None
Feature Count: 1
Layer SRS WKT:
(unknown)
vertex_count: Integer (0.0)
OGRFeature(SELECT):0
vertex_count (Integer) = 203
But that is a lot to type (and remember).
Since the only thing that changes between running commands is the name of a shapefile, it's easy to call this from a shell script:
#!/usr/bin/env bash
printf "\nLayer: $1\n"
ogrinfo -dialect SQLite -sql "SELECT sum(ST_NPoints(geometry)) AS vertex_count FROM $1" $1.shp | grep "vertex_count (Integer) ="
printf "\n"
Using $1
says take the second argument (in our case, the name of a layer / shapefile) and use that in the ogrinfo
command.
To use this script, save it as file as somewhere (I called it vc), make it executable, copy it to a directory in $PATH
, and now it's much easier to get that count I wanted:
$ vc OK_second_congressional_district
Layer: OK_second_congressional_district
vertex_count (Integer) = 203