Answers:
您可以使用“按属性分割”工具:
通过唯一属性拆分输入数据集
有可用的版本:
我无权访问ArcMap 10(仅9.3),但我希望它不会与此有所不同。
您可以在Python中创建一个简单的脚本,该脚本检查您的属性字段中是否存在不同的值,然后针对每个值对原始Shapefile运行SELECT操作。
如果您不熟悉python脚本,只需打开IDLE(python GUI)创建一个新文件,然后复制以下代码即可。在为my_shapefile,outputdir和my_attribute修改代码后,它应该可以工作。
# Script created to separate one shapefile in multiple ones by one specific
# attribute
# Example for a Inputfile called "my_shapefile" and a field called "my_attribute"
import arcgisscripting
# Starts Geoprocessing
gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = 1
#Set Input Output variables
inputFile = u"C:\\GISTemp\\My_Shapefile.shp" #<-- CHANGE
outDir = u"C:\\GISTemp\\" #<-- CHANGE
# Reads My_shapefile for different values in the attribute
rows = gp.searchcursor(inputFile)
row = rows.next()
attribute_types = set([])
while row:
attribute_types.add(row.my_attribute) #<-- CHANGE my_attribute to the name of your attribute
row = rows.next()
# Output a Shapefile for each different attribute
for each_attribute in attribute_types:
outSHP = outDir + each_attribute + u".shp"
print outSHP
gp.Select_analysis (inputFile, outSHP, "\"my_attribute\" = '" + each_attribute + "'") #<-- CHANGE my_attribute to the name of your attribute
del rows, row, attribute_types, gp
#END
你有没有看到剥离层通过属性工具更新的ArcMap中10 在这里?如果它不起作用,则可以根据需要使用拆分(分析)。
拆分输入要素会创建多个输出要素类的子集。“拆分字段”的唯一值构成输出要素类的名称。它们保存在目标工作区中。
示例代码:
import arcpy
arcpy.env.workspace = "c:/data"
arcpy.Split_analysis("Habitat_Analysis.gdb/vegtype", "climate.shp", "Zone",
"C:/output/Output.gdb", "1 Meters")
Split By Attribute
功能,而您的答案主要是关于Split [By Geometry]
。
我使用了@AlexandreNeto的脚本,并为ArcGIS 10.x用户更新了它。现在,您主要必须导入“ arcpy”而不是“ arcgisscripting”:
# Script created to separate one shapefile in multiple ones by one specific
# attribute
# Example for a Inputfile called "my_shapefile" and a field called "my_attribute"
import arcpy
#Set Input Output variables
inputFile = u"D:\DXF-Export\my_shapefile.shp" #<-- CHANGE
outDir = u"D:\DXF-Export\\" #<-- CHANGE
# Reads My_shapefile for different values in the attribute
rows = arcpy.SearchCursor(inputFile)
row = rows.next()
attribute_types = set([])
while row:
attribute_types.add(row.my_attribute) #<-- CHANGE my_attribute to the name of your attribute
row = rows.next()
# Output a Shapefile for each different attribute
for each_attribute in attribute_types:
outSHP = outDir + each_attribute + u".shp"
print outSHP
arcpy.Select_analysis (inputFile, outSHP, "\"my_attribute\" = '" + each_attribute + "'") #<-- CHANGE my_attribute to the name of your attribute
del rows, row, attribute_types
#END
这是执行此操作的一种甚至更简单的方法……它输出到GDB中。
http://www.umesc.usgs.gov/management/dss/split_by_attribute_tool.html
从USGS下载该工具,花了我3分钟的时间来完成1小时的尝试。
我知道您可以在模型构建器中使用迭代器,但是如果您喜欢使用python,这是我想到的。将脚本按参数依次添加到工具箱中,如输入shpfile,字段(多值,从输入获取)和工作区。该脚本将根据您选择的字段将shapefile分成多个shapefile,并将它们输出到您选择的文件夹中。
import arcpy, re
arcpy.env.overwriteOutput = True
Input = arcpy.GetParameterAsText(0)
Flds = "%s" % (arcpy.GetParameterAsText(1))
OutWorkspace = arcpy.GetParameterAsText(2)
myre = re.compile(";")
FldsSplit = myre.split(Flds)
sort = "%s A" % (FldsSplit[0])
rows = arcpy.SearchCursor(Input, "", "", Flds, sort)
for row in rows:
var = []
for r in range(len(FldsSplit)):
var.append(row.getValue(FldsSplit[r]))
Query = ''
Name = ''
for x in range(len(var)):
if x == 0:
fildz = FldsSplit[x]
Name = var[x] + "_"
Query += (""" "%s" = '%s'""" % (fildz, var[x]))
if x > 0:
fildz = FldsSplit[x]
Name += var[x] + "_"
Query += (""" AND "%s" = '%s' """ % (fildz, var[x]))
OutputShp = OutWorkspace + r"\%s.shp" % (Name)
arcpy.Select_analysis(Input, OutputShp, Query)
我最终将其与SearchCursor和Select_analysis一起使用
arcpy.env.workspace = strInPath
# create a set to hold the attributes
attributes=set([])
# ---- create a list of feature classes in the current workspace ----
listOfFeatures = arcpy.SearchCursor(strInPath,"","",strFieldName,"")
for row in listOfFeatures:
attributes.add(row.getValue(strFieldName))
count=1
try:
for row in attributes:
stroOutputClass = strBaseName + "_" +str(count)# (str(row.getValue(strFieldName))).replace('/','_')
strOutputFeatureClass = os.path.join(strOutGDBPath, stroOutputClass)
arcpy.Select_analysis(strInPath,strOutputFeatureClass,strQueryExp)#"["+strFieldName+"]"+"='"+row+"'")
count=count+1
del attributes
except:
arcpy.AddMessage('Error found')
我对ModelBuilder中的“迭代特征选择”工具不熟悉,但是仅作为Python代码表明可以使用arcpy对其进行调用而导出。
# Created on: 2015-05-19 15:26:10.00000
# (generated by ArcGIS/ModelBuilder)
# Description:
# ---------------------------------------------------------------------------
# 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("", "", "false")
您可以使用搜索光标在要素类中循环浏览各个要素,并将几何仅写入唯一的要素类。在此示例中,我使用了美国的要素类,并将状态导出到新的shapefile:
import arcpy
# This is a path to an ESRI FC of the USA
states = r'C:\Program Files (x86)\ArcGIS\Desktop10.2\TemplateData\TemplateData.gdb\USA\states'
out_path = r'C:\temp'
with arcpy.da.SearchCursor(states, ["STATE_NAME", "SHAPE@"]) as cursor:
for row in cursor:
out_name = str(row[0]) # Define the output shapefile name (e.g. "Hawaii")
arcpy.FeatureClassToFeatureClass_conversion(row[1], out_path, out_name)
cursor
操作的其他工作流程中。
您可以在复制要素(数据管理)中使用几何图形标记(SHAPE @)导出每个要素。
import arcpy, os
shp = r'C:\temp\yourSHP.shp'
outws = r'C:\temp'
with arcpy.da.SearchCursor(shp, ["OBJECTID","SHAPE@"]) as cursor:
for row in cursor:
outfc = os.path.join(outws, "fc" + str(row[0]))
arcpy.CopyFeatures_management(row[1], outfc)
在Arcpy中,光标会选择图层/ TableView选择。根据获取使用Python代码在ArcGIS for Desktop中选定功能的列表?,您可以简单地迭代功能选择。
但是,如果要使用arcpy进行选择,请使用SelectLayerByAttribute_management工具。
Split By Attributes
不断生成单个.dbf
表,而不是单个要素类。但是,在ArcGIS Desktop 10.6中,同一工具可以正确生成单个shapefile。我不明白为什么,尝试将工作目录设置为文件夹或地理数据库时,得到了相同的输出。