我已经创建了一个工作代码来将GPX文件转换为ArcGIS中的要素类。不幸的是,我遇到了一个文件,要么是损坏的,要么是加密的(我真的不知道)。我想为这些文件创建一个例外,因为可能会有更多的文件,我不希望这些文件中断漫长的进程。我试图使用python的try and except创建一个异常,但是现在在for循环的第15行得到错误“TypeError:'NoneType”object is not iterable“。我的代码的简短说明:首先,我设置要转换的文件的名称。然后它们被转换,我已经包括了一个计数器,因为我正在转换数以千计的文件。转换后的文件被放入一个列表中,然后在合并中使用该列表在地理数据库中创建一个大的要素类。对于无法转换的文件,我希望代码使用arcpy.AddMessage()并转到其他文件。你有什么想法吗?这是我的代码:
import arcpy
import datetime
from arcpy import env
arcpy.env.overwriteOutput = True
gpxFolder = 'C:\file\with\GPXs\in\it'
outputGdb = 'C:\the\output\geodatabase'
mergedFile = 'C:the\output\geodatabase\mergedFile'
env.workspace =gpxFolder
def convertGPX2feature(gpxFolder, outputGdb): #, referenceSymbologyLayer, outputLayerFolder
i = 1
fcList = []
for file in arcpy.ListFiles("*.gpx"):
# Convert files from .gpx to feature layer
# First set up the names
inGPX = gpxFolder + "\\" + file
featureName = file.partition(".gpx")[0]
featurename2 = file.partition(".gpx")[1]
fileType = featurename2.split(".")[1]
outfile = outputGdb + "\\" + fileType + "_" + featureName
try:
# Now convert
arcpy.GPXtoFeatures_conversion(inGPX,outfile)
convertedfile = outfile
except:
arcpy.AddMessage("file " + featureName + " failed to convert")
pass
# Add a new field and populate it with the gpx file name to use for the join later
arcpy.AddField_management(convertedfile, "Original_GPX_File", "DOUBLE", 9, "", "", "Original_GPX_File", "NULLABLE", "REQUIRED")
arcpy.CalculateField_management(convertedfile, "Original_GPX_File", featureName)
fcList.append(convertedfile)
# The counter so you know where you are in the iteration
if i%250 == 0:
arcpy.AddMessage(str(i) + " files have been converted at " + str(datetime.datetime.now()))
i += 1
# Merge all of the converted files using fcList as the input
arcpy.AddMessage("And now we merge")
arcpy.Merge_management(fcList, mergedFile )
if __name__ == "__main__":
convertGPX2feature(gpxFolder, outputGdb)
所以问题是arcpy.list文件()突然停止工作。一旦我修复了这个错误(不知何故),我遇到了这样一个错误:GPX特性已经有了新的字段,并且不能用相同的名称添加另一个字段,因为convertedfile对象仍然保存着上次遍历损坏文件时传递的文件的信息。为了解决这个问题,我把arcpy.AddField()和arcpy.CalculateField()在尝试中。现在代码开始工作,并在给我消息说它无法使用转换后传递损坏的文件arcpy.AddMessage()并且只合并成功转换的文件。 新的try and except代码如下所示:
相关问题 更多 >
编程相关推荐