我在尝试将要素类复制到地理数据库时遇到问题。我循环遍历文件夹中的所有要素类,并且仅复制多边形要素类。我的问题是,当我复制第一个多边形要素类时,它会将其重命名为'shp',然后尝试将第二个多边形要素类也命名为'shp‘。变量fcname
在复制函数外部返回完整的要素类名称('counties.shp‘和'new_mexico.shp'),但它在函数内部不能正常工作。
下面的代码将我想要运行的函数注释掉,以测试fcname
变量。文件夹中有五个要素类,其中两个是多边形要素类。如果未注释,代码将一直运行到第一个多边形要素类,其中fcname
会生成'shp‘而不是'counties.shp’。它对第二个要素类执行相同的操作,这会导致错误,因为'shp‘已经存在于gdb
中。
import arcpy
# Set initial variables with different pathnames available
# whether I am working on my home or work computer
pathhome = "G:/ESRIScriptArcGIS/Python/Data/Exercise06"
pathwork = "C:/ESRIPress/Python/Data/Exercise06"
arcpy.env.workspace = pathwork
gdbname ="NewDatabase.gdb"
fclist = arcpy.ListFeatureClasses()
# Create new gdb
##arcpy.management.CreateFileGDB(path, gdbname)
newgdb = path + "/" + gdbname
# Loop through list
for fc in fclist:
desc = arcpy.Describe(fc)
fcname = desc.name
outpath = newgdb + "/" + fcname
# Check for polygon then copy
if desc.shapeType == "Polygon":
##arcpy.management.CopyFeatures(fcname,outpath)
##print fcname + "copied."
print fcname
else:
print "Not a polygon feature class"
感谢任何能帮上忙的人!
发布于 2016-04-05 04:44:55
我找到了这个问题的答案。CopyFeatures
不希望在out_feature_class
参数中包含完整的文件路径。我从文件路径的末尾去掉了".shp“,它起作用了。
我也采纳了赫克托的建议,只过滤到ListFeatureClasses
参数中的多边形,但是,我仍然需要循环来遍历结果列表并复制每个要素类。
以下是运行的结果代码。
import arcpy
# Set initial variables with different pathnames available
# whether I am working on my home or work computer
pathhome = "G:/ESRIScriptArcGIS/Python/Data/Exercise06"
pathwork = "C:/ESRIPress/Python/Data/Exercise06"
arcpy.env.workspace = pathwork
gdbname ="NewDatabase.gdb"
fclist = arcpy.ListFeatureClasses("", "Polygon")
# Create new gdb
arcpy.management.CreateFileGDB(pathwork, gdbname)
newgdb = pathwork + "/" + gdbname
# Loop through list
for fc in fclist:
desc = arcpy.Describe(fc)
fcname = str(desc.name)
outpath = newgdb + "/" + fcname.replace(".shp","")
arcpy.management.CopyFeatures(fcname,outpath)
print fcname + " has been copied."
发布于 2016-04-04 20:28:44
您的代码中可能存在错误。然而,我在你的方法中看到了更明显的错误。
如果您的目标是按形状过滤类,则可以使用arcpy.ListFeatureClasses()
函数接受的参数feature_type
。
请参阅文档:http://pro.arcgis.com/en/pro-app/arcpy/functions/listfeatureclasses.htm
您将不再需要使用for循环来过滤数据。
https://stackoverflow.com/questions/36402370
复制相似问题