2017-03-10 39 views
0

我试图从扩展名为.txt的数据创建多个要素类。我的代码运行,但只生成一个.shp文件。选中时变量xyTable包含所有文件扩展名。然后这些应该单独运行两个Arcpy函数,并生成根据它们的.txt文件命名的相关要素类文件。从.txt文件中的数据创建多个要素类

import arcpy 
import os 
import tempfile 
import shutil 
shpFileArray = [] 
print "\n" 
arcpy.env.overwriteOutput = True 

newFolder = "destinationpath" 
if os.path.exists(newFolder): 
    tmp = tempfile.mktemp(dir=os.path.dirname(newFolder)) 
    shutil.move(newFolder, tmp) 
    shutil.rmtree(tmp) 
os.makedirs(newFolder) 

arcpy.env.workspace = newFolder 

for file in os.listdir("sourcepath"): 
    layerName = file[:-4] 
    fileSHP = layerName+".shp" 



for file in os.listdir("sourcepath"): 
     if file.endswith(".txt"): 
      xyTable = (os.path.join("destinationpath", file)) 


      arcpy.MakeXYEventLayer_management(table= xyTable, in_x_field="EastingM", in_y_field="NorthingM", out_layer="layerName",...continues... 


      arcpy.FeatureClassToFeatureClass_conversion(in_features="layerName", out_path="destinationpath", out_name= fileSHP,....continues.... 

回答

0

看起来好像你没有给出FeatureClassToFeatureClass工具的唯一shapefile名称。在第一个For循环完成后,fileSHP不会更改。看起来您已将shpFileArray设置为保存fileSHP列表。也许可以尝试这样的操作来将您的一组fileSHP保存在第一个For循环中,并在第二个For循环中引用它们。我的Python可能不完全正确,但我认为这个想法是。

import arcpy 
import os 
import tempfile 
import shutil 
shpFileArray = [] 
print "\n" 
arcpy.env.overwriteOutput = True 

newFolder = "destinationpath" 
if os.path.exists(newFolder): 
    tmp = tempfile.mktemp(dir=os.path.dirname(newFolder)) 
    shutil.move(newFolder, tmp) 
    shutil.rmtree(tmp) 
os.makedirs(newFolder) 

arcpy.env.workspace = newFolder 

for file in os.listdir("sourcepath"): 
    layerName = file[:-4] 
    fileSHP = layerName+".shp" 
    shpFileArray.append(fileSHP) 



for idx, file in enumerate(os.listdir("sourcepath")): 
     if file.endswith(".txt"): 
      xyTable = (os.path.join("destinationpath", file)) 
      outShape = shapeFileArray[idx] 


      arcpy.MakeXYEventLayer_management(table= xyTable, in_x_field="EastingM", in_y_field="NorthingM", out_layer="layerName",...continues... 


      arcpy.FeatureClassToFeatureClass_conversion(in_features="layerName", out_path="destinationpath", out_name= outShape,....continues....