【发布时间】:2020-10-22 15:36:50
【问题描述】:
我有以下文件夹文件结构:
- mainfolder_segment_polygon
- folder_poly5numSeg
- subfolder_compactness40
- 子文件夹_aoi1
- file_aoi1_seg0.shp
- file_aoi1_seg1.shp
- 子文件夹_aoi2
- file_aoi2_seg0.shp
- file_aoi2_seg1.shp
- 子文件夹_aoi1
- subfolder_compactness40
- folder_poly6numSeg
- subfolder_compactness40
- 子文件夹_aoi1
- file_aoi1_seg0.shp
- file_aoi1_seg1.shp
- 子文件夹_aoi2
- file_aoi2_seg0.shp
- file_aoi2_seg1.shp
- 子文件夹_aoi1
- subfolder_compactness40
- folder_poly5numSeg
我希望能够从同一个文件夹 (segment_polygon) 加载所有文件,对其应用函数,然后导出到具有相同结构的另一组文件夹 (segment_multipoly)。
-
来自
r".\segmentation_aoi\segment_polygon\poly5numSeg\compactness40\aoi1"的文件应该一起处理并导出到r".\segmentation_aoi\segment_multipoly\multi5numSeg\compactness40\aoi1" -
来自
r".\segmentation_aoi\segment_polygon\poly6numSeg\compactness40\aoi2"的文件应该一起处理并导出到r".\segmentation_aoi\segment_multipoly\multi6numSeg\compactness40\aoi2"
等等……
名称“主文件夹”、“文件夹”、“子文件夹”、“文件”只是为了表明名称属于哪个级别,但它们不是文件夹标签的一部分。
input_path = os.path.join(src, "segment_polygon\\")
output_path = os.path.join(src, "segment_multipoly\\")
root = Path(input_path)
for maindir, subdirs, shpfiles in os.walk(input_path):
for shp in shpfiles:
aoi_root, shp_ext = shp.split("_")
for file in root.glob("*/*/*/*.shp"):
part_path = Path(file).parts
folder_numSeg_name = part_path[9] #here I get the subfolder "poly5numSeg", "poly6numSeg", etc
folder_aoi_name = part_path[11] #here I get the subfolder "aoi1", "aoi2", etc...
aoiprep_seg = part_path[12] # here I get the name of the file "aoi1_seg0.shp", aoi1_seg1.shp", etc
if aoi_root == folder_aoi_name:
'''apply a function to shp'''
shp.to_file(os.path.join(output_path, folder_numSeg_name, "compactness40\\", folder_aoi_name, shp)
我有点迷茫。 在 Windows 10、Python 3 中工作。感谢您的所有帮助。
脚本更新
segment_polygon = os.path.join(output, "segment_polygon\\") # input path
segment_multipoly = os.path.join(output, "segment_multipoly\\") # output path
# 1. get aoi directories
aoi_dir = [path for path in glob.glob(os.path.join(segment_polygon, "*/*/*"))
if os.path.isdir(path)]
# list to store the shapefiles to be intersected
input_list = []
for path in aoi_dir:
# 2. get the files
shp_paths = glob.glob(path + os.sep + '*.shp')
for shp_path in shp_paths:
# 3. do things with shp_path
full_path, seg_shp = os.path.split(shp_path)
aoi_folder = full_path[-5:] # aoi01, aoi02, aoi03....aoi25
if seg_shp.startswith(aoi_folder):
input_list.append(shp_path) # creates the new list with shapefiles that start with the same aoiX value
auto_inter = gpd.GeoDataFrame.from_file(input_list[0]) #process shp
for i in range(len(input_list)-1):
mp = gpd.GeoDataFrame.from_file(input_list[i+1]) # process shp
auto_inter = gpd.overlay(auto_inter, mp, how='intersection') #process shp
print(f"shp included in the list:\n {input_list}")
# 4. create your output file path
print(full_path)
output_path = full_path.replace("poly", "multi")
N_output_path = output_path.replace("gon", "polygon")
print(f"output_path:\n {N_output_path}")
# make sure the directories exist
if not os.path.exists(os.path.dirname(N_output_path)):
os.makedirs(os.path.dirname(N_output_path), exist_ok=True)
# create output file name
multipoly_name = aoi_folder + ".shp"
# export
auto_inter.to_file(os.path.join(N_output_path, multipoly_name)) #export shp
合并了来自 ygorg 的更改。但是,它需要所有 shapefile 进行交集。我只想要 aoi1 文件用于交集并保存在 aoi1 文件夹中。然后,aoi2 shapefiles 并保存在 aoi2 文件夹中,以此类推。这还不行。
【问题讨论】: