Выполнение сложного вложенного цикла? - PullRequest
0 голосов
/ 03 сентября 2018

У меня есть список 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)

Как я могу добавить все это в начальный цикл?

1 Ответ

0 голосов
/ 07 сентября 2018

Поскольку сложно протестировать решение для вашей проблемы без входных данных и ожидаемого результата и того, что я никогда не работал с 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))

Это похоже на то, что вы хотели сделать? Если это не так, вам придется лучше объяснить, что вы хотите сделать.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...