见如下代码。
这个代码是根据我自己业务需要写的,整体是可以根据自己的需要进行适当调整和修改。许多坑已经都跨过去了。
def mutil_shapefiles_merge(input_folder_path, output_shpfile_abspath, delete_area_threshold=0.03):
'''本函数用于多个矢量文件的合并
第一个参数为需要合并矢量文件的文件夹路径
第二个是合并后矢量文件的完整路径'''
ptdriver = ogr.GetDriverByName('ESRI Shapefile')
file_end_with = '.shp'
# 若存在同名图层,则先删除再创键
if os.path.exists(output_shpfile_abspath):
ptdriver.DeleteDataSource(output_shpfile_abspath)
print('The already existed shapefile was successfully deleted.')
out_ds = ptdriver.CreateDataSource(output_shpfile_abspath)
one_shp_provide_spatial = glob.glob(os.path.join(input_folder_path, '*.shp'))[-1]
ds = ogr.Open(one_shp_provide_spatial)
ds_ly = ds.GetLayer()
ds_spatial = ds_ly.GetSpatialRef()
srs = ds_spatial
ds.Destroy()
out_layer = out_ds.CreateLayer(