Это пример объединения слоев OGR в Python из кулинарной книги Python GDAL / OGR.
https://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html#merge-ogr-layers
import os, ogr, osr
outputMergefn = 'merge.shp'
directory = "/Users/UserName/Downloads/"
fileStartsWith = 'test'
fileEndsWith = '.shp'
driverName = 'ESRI Shapefile'
geometryType = ogr.wkbPolygon
out_driver = ogr.GetDriverByName( driverName )
if os.path.exists(outputMergefn):
out_driver.DeleteDataSource(outputMergefn)
out_ds = out_driver.CreateDataSource(outputMergefn)
out_layer = out_ds.CreateLayer(outputMergefn, geom_type=geometryType)
fileList = os.listdir(directory)
for file in fileList:
if file.startswith(fileStartsWith) and file.endswith(fileEndsWith):
print file
ds = ogr.Open(directory+file)
lyr = ds.GetLayer()
for feat in lyr:
out_feat = ogr.Feature(out_layer.GetLayerDefn())
out_feat.SetGeometry(feat.GetGeometryRef().Clone())
out_layer.CreateFeature(out_feat)
out_feat = None
out_layer.SyncToDisk()
Однако в этом примере таблица атрибутов не объединяется.Есть ли способ сделать это с помощью OGR в Python?