首页 > 编程知识 正文

如何将三个文件合并,文件夹里面文件怎么合并

时间:2023-05-03 06:08:32 阅读:169422 作者:4254

见如下代码。

这个代码是根据我自己业务需要写的,整体是可以根据自己的需要进行适当调整和修改。许多坑已经都跨过去了。

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(output_shpfile_abspath, srs=srs, geom_type=ogr.wkbPolygon) filelist = os.listdir(input_folder_path) for file in filelist: if file.endswith(file_end_with): # file.startswith查询文件起始标志 file_abspath = os.path.join(input_folder_path, file) data_source = ogr.Open(file_abspath) if data_source is None: print("This file does not exist...") original_layer = data_source.GetLayer() # 根据原图层字段对象创建新图层的字段 out_layer.CreateFields(original_layer.schema) for feat in original_layer: out_feat = ogr.Feature(out_layer.GetLayerDefn()) out_feat.SetGeometry(feat.GetGeometryRef().Clone()) # out_layer.CreateFeature(out_feat) out_layer.SyncToDisk() # 获取原图层字段个数 original_layer_fields_count = feat.GetFieldCount() # 将原图层字段值写入到新图层中 for i in range(original_layer_fields_count): # 这里可设置判断条件,当字段名称等于某字符时间,才将这个字段值写入到新图层中,方便保存着图斑预测概率值 field_value = feat.GetField(i) # print(field_value) out_feat.SetField(i, field_value) out_layer.CreateFeature(out_feat) out_ds.Destroy() # ________删除多余无用的字段 # 第一步,获取需要删除的字段 dataR = shapefile.Reader(output_shpfile_abspath, encoding='gbk') fields = dataR.fields field_list = [] for field in fields: field_list.append(field[0]) delete_field_list = [] for field in field_list: if field.startswith('DN_value') or field.startswith('Area'): delete_field_list.append(field) # 第二步,删除字段 dss_driver = ogr.GetDriverByName('ESRI Shapefile') dss = dss_driver.Open(output_shpfile_abspath, 1) lyr = dss.GetLayer() for delete_field in delete_field_list: lyr.DeleteField(lyr.FindFieldIndex(delete_field, 0)) dss = None # ________给合并后的shp添加面积字段,并计算面积 # 获取文件驱动 driver = ogr.GetDriverByName('ESRI Shapefile') data_source = driver.Open(output_shpfile_abspath, 1) # 获取图层数据 layer = data_source.GetLayer(0) # 字段定义 new_field_defn = ogr.FieldDefn('AREA_ha', ogr.OFTReal) # 创建字段 layer.CreateField(new_field_defn) # ————————计算图斑面积 data_source = driver.Open(output_shpfile_abspath, 1) layer = data_source.GetLayer() for feature in layer: geom = feature.GetGeometryRef() # 面积单位公顷 area = geom.GetArea()/10000 # 将面积添加到属性表中 feature.SetField('AREA_ha', round(area, 4)) layer.SetFeature(feature) # data_source = None # ________删除合并后shp文件中面积小于指定阈值的图斑 str_filter = 'AREA_ha' + " < '" + str(delete_area_threshold) + "'" layer.SetAttributeFilter(str_filter) for p_feature in layer: p_feature_fid = p_feature.GetFID() layer.DeleteFeature(int(p_feature_fid)) str_sql = "REPACK " + str(layer.GetName()) data_source.ExecuteSQL(str_sql, None, "") layer = None data_source = None

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。