简体   繁体   中英

Creating Multiple featureclasses from data in .txt Files

I am trying to create multiple feature classes from data with .txt extension. My code runs, but only produces one .shp file. The variable xyTable when checked does contain all the file extensions. These then should individually run through both Arcpy functions and produce the relevant featureclass files named in accordance with their .txt files.

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....

Looks like you are not giving the FeatureClassToFeatureClass tool unique shapefile names. After the first For loop finishes, fileSHP doesn't change. Looks like you have the shpFileArray set up to hold the list of fileSHPs. Perhaps try something like this to save your set of fileSHPs in the first For loop and refer to them in the second For loop. My python might not be exactly right, but I think the idea is.

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....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM