我有一个包含多条记录的多边形shapefile。我需要单独缓冲每一个,并为每个创建一个新的缓冲多边形shapefile。
我在ArcPy中编写了以下独立脚本。它完成了我需要的一些工作,但不是为每个记录创建单独的缓冲多边形shapefile,而是创建多个shapefile,每个shapefile包含一个包含应用于所有功能的缓冲区的多部分多边形。我想知道如何改变它。
我正在使用ArcGIS 10.3.1。
import arcpy
from arcpy import env
file_workspace = "C:\\Data\\Temp\\"
env.workspace = file_workspace
arcpy.env.overwriteOutput = True
fc_In1 = file_workspace + "fc_InPolygon.shp"
fc_In1_FieldName = "PLOTNAME"
var_Buffer = "50 Meters"
numCount = 1
# Iterate through the rows in the cursor and buffer each
with arcpy.da.SearchCursor(fc_In1, fc_In1_FieldName) as cursor:
for row in cursor:
var_PolygonName = row[0]
print "Buffering polygon " + var_PolygonName
arcpy.MakeFeatureLayer_management(fc_In1, "Poly_lyr")
arcpy.Buffer_analysis("Poly_lyr", var_PolygonName+'_Buff.shp', var_Buffer, "FULL", "ROUND", "ALL", "")
答案 0 :(得分:2)
您的脚本重复创建一个包含输入要素类中所有要素的图层,然后缓冲该图层(因此,多个输出shapefile都包含相同的内容)。
您需要使用SQL查询包含select by attribute或make your feature layer,以限制功能层在缓冲之前的内容。
在任何一种情况下,您的查询都是'"FIELDNAME" = \'POLYGONNAME\''
,或者使用变量:
'"{}" = \'{}\''.format(fc_In1_FieldName, var_PolygonName)
'"' + fc_In1_FieldName + '" = \'' + var_PolygonName + '\''
(我喜欢the format
method以便在连接时提高可读性,尤其是在处理那么多单/双引号时。)
# Using MAKE FEATURE LAYER with SQL QUERY
with arcpy.da.SearchCursor(fc_In1, fc_In1_FieldName) as cursor:
for row in cursor:
var_PolygonName = row[0]
print "Buffering polygon " + var_PolygonName
arcpy.MakeFeatureLayer_management(fc_In1, "Poly_lyr", '"{}" = \'{}\''.format(fc_In1_FieldName, var_PolygonName))
arcpy.Buffer_analysis("Poly_lyr", var_PolygonName+'_Buff.shp', var_Buffer, "FULL", "ROUND", "ALL", "")
SelectLayerByAttribute
的主要优点是您不会在内存中制作数十个/数百个要素图层,但这可能不是小要素类的关注点。
# Using SELECT BY ATTRIBUTE
arcpy.MakeFeatureLayer_management(fc_In1, "Poly_lyr") # no need to make the layer each time through the cursor
with arcpy.da.SearchCursor(fc_In1, fc_In1_FieldName) as cursor:
for row in cursor:
var_PolygonName = row[0]
print "Buffering polygon " + var_PolygonName
arcpy.SelectLayerByAttribute_management("Poly_lyr", "NEW_SELECTION", '"{}" = \'{}\''.format(fc_In1_FieldName, var_PolygonName))
arcpy.Buffer_analysis("Poly_lyr", var_PolygonName+'_Buff.shp', var_Buffer, "FULL", "ROUND", "ALL", "")