使用按位置语法选择图层时遇到一些问题
Having some issues using selecting layer by location syntax
我的说明:
Create a Python script that selects parcels from "coa_parcels.shp"
that intersect with the shapefile "floodplains.shp" and creates a new
shapefile that only contains the selected parcels.
The location of the workspace and the three shapefiles (coa_parcels,
floodplains, and the output) should be treated as user-defined inputs
using "raw_input" statements.
下面是这部分脚本的示例伪代码:
- 开始
- 获取工作区的用户输入
- 获取输入特征 class 名称的用户输入(例如 coa_parcels.shp)
- 获取 select 功能 class 名称的用户输入(例如 floodplains.shp)
- 获取用户输入的输出特征 class 名称(例如 selected_parcels.shp)
- 设置工作区并覆盖输出设置
- 创建临时要素图层
- Select 基于 select 特征 class
逐层定位
- 将 selected 功能复制到新功能 class
- 打印一条消息,让用户知道已创建新功能 class
- 结束
我的脚本:
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
仔细查看引发错误的行:
arcpy.SelectLayerByLocation_management(coa_parcels.shp,
通过不在引号中包含图层名称,您是在向 Python 表明它应该使用变量 coa_parcels
作为 select 图层定位工具的参数输入.
未经请求且与您的错误无关的“创建要素图层”工具不会创建 shapefile。没有什么可以阻止您在层名称中包含 .shp
(显然,因为那不是您的错误出现的地方!)但是对于 "best practices" 我建议更清楚地命名层,这样您就不会不小心尝试将图层传递给只接受 shapefile 的工具。
我的说明:
Create a Python script that selects parcels from "coa_parcels.shp" that intersect with the shapefile "floodplains.shp" and creates a new shapefile that only contains the selected parcels.
The location of the workspace and the three shapefiles (coa_parcels, floodplains, and the output) should be treated as user-defined inputs using "raw_input" statements.
下面是这部分脚本的示例伪代码:
- 开始
- 获取工作区的用户输入
- 获取输入特征 class 名称的用户输入(例如 coa_parcels.shp)
- 获取 select 功能 class 名称的用户输入(例如 floodplains.shp)
- 获取用户输入的输出特征 class 名称(例如 selected_parcels.shp)
- 设置工作区并覆盖输出设置
- 创建临时要素图层
- Select 基于 select 特征 class 逐层定位
- 将 selected 功能复制到新功能 class
- 打印一条消息,让用户知道已创建新功能 class
- 结束
我的脚本:
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
仔细查看引发错误的行:
arcpy.SelectLayerByLocation_management(coa_parcels.shp,
通过不在引号中包含图层名称,您是在向 Python 表明它应该使用变量 coa_parcels
作为 select 图层定位工具的参数输入.
未经请求且与您的错误无关的“创建要素图层”工具不会创建 shapefile。没有什么可以阻止您在层名称中包含 .shp
(显然,因为那不是您的错误出现的地方!)但是对于 "best practices" 我建议更清楚地命名层,这样您就不会不小心尝试将图层传递给只接受 shapefile 的工具。