arcpy中的剪辑分析

时间:2016-01-20 19:35:15

标签: arcpy arcmap

我有一个覆盖整个城市的shapefile,以及一个shapefile列表,它们是城市中不同位置的缓冲区。我想用每个缓冲区剪辑城市。我尝试在Python中使用ArcPy但代码不起作用。我做错了什么?

import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "U:\Park and Residential Area\Test\SBA park_res_buffer_5\SBA.gdb"
infeature= "U:\Park and Residential Area\Test\park_res_merge.shp"
clipfeatture = arcpy.ListFeatureClasses("*", "polygon")
for i in clipfeatture:
    outclipfeatture = arcpy.Clip_analysis(infeature,i)
    outclipfeatture.save("U:\Park and Residential Area\Test\SBA park_res_buffer_5/"*i)

6 个答案:

答案 0 :(得分:4)

这是适当的语法for using Clip in ArcPy

  

arcpy.Clip_analysis(in_features, clip_features, out_feature_class)

所以你的for循环应该是这样的:

for i in clipfeatture:
    outfeature = "U:\Foo\Bar\" + i
    arcpy.Clip_analysis(infeature, i, outfeature)

答案 1 :(得分:1)

我还会print()每个文件路径字符串,以便您可以检查其语法是否正确使用。反斜杠是Python中的转义字符,并且在后跟关键字母时可以具有特殊属性。

我总是在包含文件路径的任何字符串前放置r,例如r"\\srvr\drv\proj\gdb.gdb\fc";这告诉Python它是一个原始字符串并忽略了转义函数。

请参阅下面的链接,了解有关处理文件名中反斜杠的有趣类比。

https://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%E2%80%94-backslashes-in-windows-filenames/

答案 2 :(得分:1)

null

答案 3 :(得分:0)

要使用多个剪辑功能制作多个剪辑,必须首先创建所有剪辑功能的列表并进行迭代。

import arcpy
arcpy.env.workspace = file_path
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
    arcpy.Clip_analysis(input_feature, fc, output_feature)

对于多个输出,请确保使用不同的名称。您可以使用arcpy.CreateUniqueName()创建不同的名称,例如Buffer.shp,Buffer_1.shp,Buffer_2.shp ...

答案 4 :(得分:0)

使用arcpy在ArcMap中裁剪几层:

import os
import arcpy as arc
if __name__ == '__main__':
    input_layers = arc.GetParameterAsText(0) #list of layers for clipping
    clip_layer = arc.GetParameterAsText(1) # contour clip layer
    xy_tolerance = arc.GetParameterAsText(2)
    output_db = arc.GetParameterAsText(3) # database with saved layers
    for lay_for_clipping in input_layers.split(";"):
        arc.AddMessage("Clip for " + lay_for_clipping)
        layer_path, layer_name = os.path.split(lay_for_clipping)
        output_layer = os.path.join(output_db, layer_name)
        arc.Clip_analysis(lay_for_clipping, clip_layer, output_layer, xy_tolerance)

答案 5 :(得分:-1)

如果每个地方都是唯一的,您也可以从模型构建器导出迭代特征选择工具。

http://desktop.arcgis.com/en/arcmap/10.3/tools/modelbuilder-toolbox/iterate-feature-selection.htm

# Import arcpy module
import arcpy

# Load required toolboxes
arcpy.ImportToolbox("Model Functions")


 # Local variables:
Selected_Features = ""
Value = "1"

# Process: Iterate Feature Selection
arcpy.IterateFeatureSelection_mb("inputfeature", "fields", "false")