执行复杂的嵌套循环?

2024-06-28 18:56:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个列表“shapelist”,其中有许多要使用的目录。 这个循环有很多函数,在这个过程中,我必须添加另一个循环,将坐标系分配给一些文件。正确的方法是什么?你知道吗

#This is the initial loop
for i in shapelist:
    arcpy.FeatureToLine_management([i] ,i.replace('ASTENOT.shp', 'ASTENOT_lines'))
    #many more lines with functions
    #at some point I have to add coordinate system information to exported files 
    #how do I do that in this loop without creating perplexing results?

我想在某个地方加上这段代码,用来指定坐标系。你知道吗

#Finds and stores to a list the files that need the coordinate system assignment
rootfolder = r'C:\Users\user\Desktop\etg'
import os 
newlist = []
for path, subdirs, files in os.walk(rootfolder):
    for name in files:
        if name==('centerline.shp'):
            newlist.append(os.path.join(path, name))

newlist现在包含需要坐标系赋值的文件

        #follows the loop that does the assignment
        for i in newlist:
    ...     sr = arcpy.SpatialReference(2100)
    ...     arcpy.DefineProjection_management(i, sr)

如何将所有这些添加到初始循环中?你知道吗


Tags: 文件thetopathnameinloopfor
1条回答
网友
1楼 · 发布于 2024-06-28 18:56:02

由于在没有输入数据和预期输出的情况下很难测试问题的解决方案,而且我从未使用过ArcGIS,因此我只能推测您想做什么:

import pathlib

# Root directory of where your files resides
rootfolder = pathlib.Path(r'C:\Users\user\Desktop\etg')

# Get all files named 'centerline.shp' in this directory and all its
# sub-directories
newlist = rootfolder.glob('**/centerline.shp')

# Define a SpatialReference (?)
spatial_reference_to_use = arcpy.SpatialReference(2100)

# Assign a defined projection to all the files in newlist (?)
for file_to_assign in newlist:
    arcpy.DefineProjection_management(str(file_to_assign),
                                      spatial_reference_to_use)

# From the input file names (in newlist), define 
# the output file names (?)
newlist_out_features = [current_file.with_name('ASTENOT_lines')
                        for current_file in newlist]

# Change features to lines for all the files in newlist (?)
for (current_shape_file, current_out_features) in zip(newlist,
                                                      newlist_out_features):
    arcpy.FeatureToLine_management([str(current_shape_file)],
                                   str(current_out_features))

这看起来像是你想做的吗?如果不是,你就得更好地解释你想做什么。你知道吗

相关问题 更多 >