-2

我的指示:

创建一个 Python 脚本,从“coa_parcels.shp”中选择与 shapefile“floodplains.shp”相交的地块,并创建一个仅包含所选地块的新 shapefile。

工作空间的位置和三个 shapefile(coa_parcels、洪泛区和输出)应被视为使用“raw_input”语句的用户定义输入。

下面是这部分脚本的示例伪代码:

  • 开始
  • 获取工作区的用户输入
  • 获取输入要素类名称的用户输入(例如 coa_parcels.shp)
  • 获取所选要素类名称的用户输入(例如,floodplains.shp)
  • 获取输出要素类名称的用户输入(例如 selected_pa​​rcels.shp)
  • 设置工作区并覆盖输出设置
  • 创建临时要素图层
  • 根据选择的要素类按位置从图层中选择
  • 将选定要素复制到新要素类
  • 打印一条消息,让用户知道创建了一个新要素类
  • 结尾

我的脚本:

import arcpy

workSpace = raw_input("What is the workspace location? ")
inFeature = raw_input("What is the input feature class name? ")
selFeature = raw_input("What is the select feature class name? ")
outFeature = raw_input("What is the output feature class name? ")

arcpy.env.workspace = workSpace
arcpy.env.overwriteOutput = True
arcpy.MakeFeatureLayer_management("coa_parcels.shp", "lyr") 
arcpy.SelectLayerByLocation_management(coa_parcels.shp,"INTERSECT",floodplains.shp, "NEW_SELECTION")
arcpy.CopyFeatures_management("lyr", "selected_parcels")
print "A new feature class",outFeature,"has been created!"here

我的错误是: NameError: name 'coa_parcels' is not defined

4

1 回答 1

-1

仔细查看引发错误的行:

arcpy.SelectLayerByLocation_management(coa_parcels.shp,

通过不在引号中包含图层名称,您向 Python 指示它应该使用变量coa_parcels作为按位置选择图层工具的参数输入。


未经请求且与您的错误无关,制作要素图层工具不会创建 shapefile。没有什么可以阻止您包含.shp在图层名称中(显然,因为这不是您的错误出现的地方!)但是对于“最佳实践”,我建议您更清楚地命名图层,这样您就不会意外尝试将图层传递给工具这只会接受一个shapefile。

于 2017-01-23T00:04:54.953 回答