Skip to content

Instantly share code, notes, and snippets.

@andrewbt
Created January 14, 2021 16:54
Show Gist options
  • Save andrewbt/5248b10b1a8951ab77ae61998842b9e2 to your computer and use it in GitHub Desktop.
Save andrewbt/5248b10b1a8951ab77ae61998842b9e2 to your computer and use it in GitHub Desktop.
This python3 script uses OGR (you can run it from OSGeo4W Shell), to count all features of shapefiles in the current directory, sum the total, and give an average
import os
from osgeo import ogr
feature_count_array = []
top = os.getcwd()
for root, dirs, files in os.walk(top):
for file in files:
if file.endswith(".shp"):
dataset = ogr.Open(file)
print(dataset.name)
layer = dataset.GetLayerByIndex()
feature_count = layer.GetFeatureCount()
print(feature_count)
feature_count_array.append(feature_count)
feature_count_sum = sum(feature_count_array)
print("sum of features: " + str(feature_count_sum))
feature_count_avg = feature_count_sum / len(feature_count_array)
print("avg per shpfile: " + str(feature_count_avg))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment