Created
January 14, 2021 16:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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