空间连接 (分析)
摘要
根据空间关系将一个要素类的属性与另一个要素类的属性进行连接。目标要素和来自连接要素的被连接属性写入到输出要素类。
用法
空间连接是指根据要素的相对空间位置将连接要素中的行匹配到目标要素中的行。默认情况下,连接要素的所有属性会被追加到目标要素的属性中。通过在连接要素的字段映射参数中控制这些属性,可以选择将写入到输出中的属性。
始终会向输出要素类添加两个新字段:Join_Count 和 TARGET_FID。Join_Count 指示与每个目标要素 (TARGET_FID) 匹配的连接要素数量。
连接要素的字段映射参数中指定的合并规则仅适用于连接要素中的属性,且仅适用于多个要素与目标要素匹配 (Join_Count > 0) 的情况。例如,如果连接 DEPTH 属性值分别为 15.5、2.5 和 3.3 的三个要素,并应用“平均值”合并规则,则输出字段的值为 6.1。
当连接操作参数为 JOIN_ONE_TO_MANY 时,输出要素类中的每个目标要素可以包含多个属性。新字段 JOIN_FID 被添加到输出中。这样易于确定连接要素 (JOIN_FID) 与目标要素 (TARGET_FID) 的具体对应关系。
只有进行如下设置,才能将所有输入目标要素写入到输出要素类:
- 将连接操作设置为 JOIN_ONE_TO_ONE,并且
- 选中保留所有目标要素(在脚本中设置为 JOIN_ALL)。
将“匹配选项”设置为“CLOSEST”时,可能会出现两个或多个连接要素与目标要素距离相等的情况。如果发生这种情况,将随机选择其中一个连接要素作为匹配要素(连接要素的 FID 对随机选择过程没有影响)。如果要查找排在第 2 位、第 3 位或第 N 位的最近要素,请使用生成近邻表工具。
语法
| 参数 | 说明 | 数据类型 |
target_features |
目标要素的属性和被连接要素的属性被传递到输出要素类。但是,可以在字段映射参数中定义属性的子集。目标要素可以是 ArcGIS 支持的任意空间数据源。 | Feature Layer |
join_features |
连接要素的属性将被连接到目标要素的属性中。有关连接操作的类型对被连接属性聚合的影响的详细信息,请参阅对连接操作参数的说明。连接要素可以是 ArcGIS 所支持的任意空间数据源。 | Feature Layer |
out_feature_class |
包含目标要素和连接要素的属性的新要素类。默认情况下,目标要素的所有属性和被连接要素的属性都被写入到输出。但是,可通过字段映射参数来控制要传递的属性集。 | Feature Class |
join_operation (可选) |
在具有相同空间关系的目标要素和连接要素之间存在一对多关系时,确定输出要素类中目标要素和连接要素之间的连接方式。
| String |
join_type (可选) |
确定是在输出要素类中保留所有目标要素(称为“外部连接”),还是仅保留那些与连接要素有指定空间关系的目标要素(称为“内部连接”)。
| Boolean |
field_mapping (可选) |
控制输出要素类中要包含的属性字段。初始列表包含目标要素和连接要素中的所有字段。可以添加、删除、重命名字段或更改字段的属性。目标要素中的所选字段按原样传递,但连接要素中的所选字段可能会根据有效的合并规则进行聚合。有关字段映射的详细信息,请参阅帮助主题使用字段映射控件并将输入字段映射到输出字段。 | Field Mappings |
match_option (可选) |
定义用于匹配行的条件。匹配选项包括:
| String |
search_radius (可选) |
如果连接要素与目标要素的距离在此范围内,则有可能进行空间连接。仅当“匹配选项”指定为 INTERSECT 或 CLOSEST 时,搜索半径才有效。在“匹配选项”为 INTERSECT 时使用 100 米作为搜索半径表示:如果连接要素位于目标要素周围的 100 米范围内,则将连接要素的属性传递到目标要素。在“匹配选项”为 CLOSEST 时使用 100 米作为搜索半径表示:如果连接要素位于目标要素周围的 100 米范围内,并且是距该目标要素最近的连接要素,则将连接要素的属性传递到该目标要素。 | Linear unit |
distance_field_name (可选) |
添加到输出要素类中的字段的名称,其中包含目标要素和最近连接要素之间的距离。只有将“匹配选项”指定为 CLOSEST 时此选项才有效。如果不希望显示任何输出字段,可以将此参数留空。 | String |
代码示例
以下脚本演示了如何在 Python 窗口中使用 SpatialJoin 函数。
import arcpy target_features = "C:/data/usa.gdb/states" join_features = "C:/data/usa.gdb/cities" out_feature_class = "C:/data/usa.gdb/states_cities" arcpy.SpatialJoin_analysis(target_features, join_features, out_feature_class)
以下独立脚本演示了如何使用 SpatialJoin 将城市的属性连接到州。
# Name: SpatialJoin_Example2.py
# Description: Join attributes of cities to states based on spatial relationships.
# Requirements: os module
# Import system modules
import arcpy
import os
# Set local variables
workspace = r"C:\gpqa\mytools\spatialjoin\usa.gdb"
outWorkspace = r"C:\gpqa\mytools\spatialjoin\output.gdb"
# Want to join USA cities to states and calculate the mean city population
# for each state
targetFeatures = os.path.join(workspace, "states")
joinFeatures = os.path.join(workspace, "cities")
# Output will be the target features, states, with a mean city population field (mcp)
outfc = os.path.join(outWorkspace, "states_mcp2")
# Create a new fieldmappings and add the two input feature classes.
fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(targetFeatures)
fieldmappings.addTable(joinFeatures)
# First get the POP1990 fieldmap. POP1990 is a field in the cities feature class.
# The output will have the states with the attributes of the cities. Setting the
# field's merge rule to mean will aggregate the values for all of the cities for
# each state into an average value. The field is also renamed to be more appropriate
# for the output.
pop1990FieldIndex = fieldmappings.findFieldMapIndex("POP1990")
fieldmap = fieldmappings.getFieldMap(pop1990FieldIndex)
# Get the output field's properties as a field object
field = fieldmap.outputField
# Rename the field and pass the updated field object back into the field map
field.name = "mean_city_pop"
field.aliasName = "mean_city_pop"
fieldmap.outputField = field
# Set the merge rule to mean and then replace the old fieldmap in the mappings object
# with the updated one
fieldmap.mergeRule = "mean"
fieldmappings.replaceFieldMap(pop1990FieldIndex, fieldmap)
# Delete fields that are no longer applicable, such as city CITY_NAME and CITY_FIPS
# as only the first value will be used by default
x = fieldmappings.findFieldMapIndex("CITY_NAME")
fieldmappings.removeFieldMap(x)
y = fieldmappings.findFieldMapIndex("CITY_FIPS")
fieldmappings.removeFieldMap(y)
#Run the Spatial Join tool, using the defaults for the join operation and join type
arcpy.SpatialJoin_analysis(targetFeatures, joinFeatures, outfc, "#", "#", fieldmappings)