使用按位置语法选择图层时遇到一些问题

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.

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

我的脚本:

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 的工具。