Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Created January 13, 2020 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/81b2eb8121f353b5fb8288d2f623676e to your computer and use it in GitHub Desktop.
Save ThomasG77/81b2eb8121f353b5fb8288d2f623676e to your computer and use it in GitHub Desktop.
Get shp count from directory
# Try python3 get-count-ogr.py directory_with_shp
# Recursive
import argparse
import os
from osgeo import ogr
parser = argparse.ArgumentParser()
parser.add_argument("directory")
args = parser.parse_args()
print(args.directory)
for subdir, dirs, files in os.walk(args.directory):
for file in files:
# print os.path.join(subdir, file)
filepath = subdir + os.sep + file
if filepath.endswith(".shp"):
driver = ogr.GetDriverByName('ESRI Shapefile')
dataSource = driver.Open(filepath, 0) # 0 means read-only. 1 means writeable.
# Check to see if shapefile is found.
if dataSource is None:
print('Could not open %s' % (filepath))
else:
print('Opened %s' % (filepath))
layer = dataSource.GetLayer()
featureCount = layer.GetFeatureCount()
print("Number of features in %s: %d" % (os.path.basename(filepath),featureCount))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment