Created
January 13, 2020 16:20
-
-
Save ThomasG77/81b2eb8121f353b5fb8288d2f623676e to your computer and use it in GitHub Desktop.
Get shp count from directory
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
# 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