A pyqgis script
It handles other formats (shp, tab, gpkg, geojson...) all formats that ogr can handle
But it does not handle sublayers
import os
import csv
from qgis.core import QgsVectorLayer
FOLDER = 'c:/folder/to/scan'
RESULT = 'c:/folder/metadata.csv'
def scan_directory(directory, extension=('.tab', '.shp', '.gpkg')):
"""return the list of files that matche the extension
Args:
directory (str): the folder to scan
extension (tuple, optional): the extension. Defaults to ('.tab', '.shp', '.gpkg').
"""
extension = tuple(map(str.lower, extension))
for root, dirs, files in os.walk(directory, topdown=False):
for filename in files:
if filename.lower().endswith(extension):
yield(root, filename, os.path.join(root, filename))
def get_metadata(layer):
"""Return metadata
Args:
layer ([ogr vectorlayer]): [Not sublayer]
Returns:
metadata: (name, comment, encoding....)
"""
name = layer.name()
comment = layer.dataComment()
encoding = layer.dataProvider().encoding()
datasource = layer.publicSource()
feature_count = layer.featureCount()
geom_type = geom_wkbtype = crs = extent = None
if layer.isSpatial():
geom_type = layer.geometryType()
if geom_type <0 or geom_type > QgsWkbTypes.NullGeometry:
print(f'{layer} invalid geometry type')
else:
geom_wkbtype = QgsWkbTypes.displayString(layer.wkbType())
geom_type = QgsWkbTypes.geometryDisplayString(geom_type)
if layer.crs().isValid():
crs = layer.crs().userFriendlyIdentifier(
QgsCoordinateReferenceSystem.FullString )
extent=layer.extent().toString()
return (name, comment, encoding, geom_type,
geom_wkbtype, crs, extent, feature_count, datasource)
with open(RESULT, 'w', newline='') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(['name', 'comment', 'encoding',
'geom_type', 'geom_wkbtype', 'crs', 'extent', 'feature_count','datasource'])
for root, filename, path in scan_directory(FOLDER):
layer = QgsVectorLayer(path, filename,'ogr')
if not layer.isValid():
print(f'{layer.name()} is not valid')
continue
wr.writerow(get_metadata(layer))
Code available : https://gist.github.com/42a6b3ab8e1069e3e0523ca90c361aaa
ogrinfo
and a for loop will do this-sql
.