我有一个txt,每行都有这样一个句子:
INT_ASTIK_EAS
ER_ASTOM_ASTIK
and so on.
我想要一个主文件夹的目录,其中包含子文件夹,每个子文件夹包含ASTIK.shp
,然后是EAS.shp
,然后是ASTOM.shp
,等等
INT
表示与此代码执行交集,在这种情况下,应该涉及ASTIK
和EAS
形状文件
inte_s=gpd.overlay(ASTIK,EAS,how='intersection')
代码:
rootdir = r'C:\Users\user\Desktop\a' # path to your root directory you walk
sfiles = {} # a dictionary with all the .shp files
for entry in os.listdir(rootdir):
dirpath = os.path.join(rootdir, entry)
if os.path.isdir(dirpath):
for file in os.listdir(dirpath): # Get all files in the subdirectories
if file.endswith('.shp'): # If it's an .shp.
filepath = os.path.join(dirpath, file)
sfiles[filepath] = gpd.read_file(filepath)
action_dict = {'INT': 'intersection', 'ER': 'difference'}
directory=input('Insert dir of the main folder')
with open(input()) as txtfile: #insert directory of txt
x = txtfile.readlines()
for line in x:
action, shape1, shape2 = line.split('_') # here line is ER_ASTOM_ASTIK or whatever line in the txt file
if shape1 in sfiles and shape2 in sfiles:
gpd.overlay(sfiles[shape1], sfiles[shape2], how=action_dict[action])
它可能不执行交叉或差异。如何将其结果存储在变量中,因为我必须对每行的每个操作的结果进行进一步的处理
在检查了sfiles
变量的问题以及它存储了什么之后:它给出了:
for x in sfiles:
print (x)
C:\Users\user\Desktop\a\ASTTOM\ASTTOM.shp
C:\Users\user\Desktop\a\PST\PST.shp
最后:
if shape1 in sfiles:
print('success')
else:
print('failed')
failed
这意味着它不传递字典中应该传递的内容,因为shapefile恰好存在于文件夹中。我怎样才能让它做叠加计算
print(sfiles)
{'C:\\Users\\user\\Desktop\\a\\ASTTOM\\ASTTOM.shp': OBJECTID FID_Asttom TheOTA_Tmp aa SHAPE_Leng SHAPE_Area \
0 1 1 20008 7 4206.346569 5.796467e+05
1 2 2 20008 11 3311.521073 6.892735e+05
2 3 3 20008 6 2131.153007 2.196689e+05
3 4 4 20008 2 1738.249605 4.731495e+04
4 5 5 20008 1 1075.903789 5.337986e+04
5 6 6 20008 10 3255.422426 3.628100e+05
6 7 7 20008 3 2469.427102 2.267493e+05
7 8 8 20008 5 2466.593786 2.042149e+05
8 9 9 20008 8 3095.473108 5.384131e+05
9 10 10 20008 4 1856.659243 1.136976e+05
10 11 11 20008 9 2293.865083 2.063858e+05
11 12 12 20104 4 4445.338873 4.455547e+05
12 13 13 20104 7 3659.621424 2.834771e+05
13 14 14 20104 2 2098.126160 1.981298e+05
它打印字典中所有SHP的属性表
如果尝试此操作:
print(sfiles[shape1], sfiles[shape2], action_dict[action])
KeyError Traceback (most recent call last)
<ipython-input-11-9f435f40293e> in <module>()
----> 1 print(sfiles[shape1], sfiles[shape2], action_dict[action])
KeyError: 'ASTTOM'
and each every time:
print(sfiles[shape1])
KeyError: 'ASTTOM'
print(sfiles[shape2])
KeyError: 'PST'
print(action_dict[action])
difference
基本上:有一个字典有这样的键:'C:\\Users\\user\\Desktop\\a\\ASTTOM\\ASTTOM.shp'
当提供键'ASTTOM'时,这就是返回错误的原因。我应该怎么做?读取txt是最好的过程吗
目前没有回答
相关问题 更多 >
编程相关推荐