"""
Name: TransferAttributes_example_script2.py
Description: Perform attribute transfer from newly updated roads (source) to existing
base roads (target). When the source and target features are matched in
many-to-one or many-to-many (m:n) relationships, attributes are transferred
from only one of the m source features to the n target features. This script
includes a postprocess that flags resulting target features with the m:n
match relationship. You can inspect the flagged features and retrieve the
attributes from the desired source features if necessary.
"""
import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"D:\conflationTools\ScriptExamples\data.gdb"
sourceFeatures = "updateRoads"
targetFeatures = "baseRoads"
transfer_fields = [["RD_NAME", RD_ID"]]
search_distance = "300 Feet"
match_fields = [["RD_NAME", "FULLNAME"]]
outMatchTable = "ta_mtbl"
# Make a copy of the targetFeatures for attribute transfer
targetCopy = targetFeatures + "_Copy"
arcpy.management.CopyFeatures(targetFeatures, targetCopy)
# Performs attribute transfer
arcpy.edit.TransferAttributes(sourceFeatures, targetCopy, transfer_fields, search_distance, match_fields, outMatchTable)
"""
Note 1: The result of TransferAttributes may contain errors; see the tool reference.
Additional analysis steps may be necessary to check the results. These steps
are not included in this script.
The following process identifies m:n matches between source and target features
and flags features in targetCopy for inspection.
"""
# Add a field srcM_inMN to the match table to store the m count of source features in m:n relationship
field_srcM_inMN = "srcM_inMN"
arcpy.management.AddField(outMatchTable, field_srcM_inMN)
codeblock = """
def getM(fld):
x = fld.split(\":\")[0]
if x.isnumeric():
if int(x) > 0:
return int(x)
return -1
"""
arcpy.management.CalculateField(outMatchTable, field_srcM_inMN, "getM(!FM_MN!)", "PYTHON_9.3", codeblock)
arcpy.management.MakeTableView(outMatchTable, "mtable_view", field_srcM_inMN + "> 1")
arcpy.management.JoinField(targetCopy, "OBJECTID", "mtable_view", "TGT_FID", [["field_srcM_inMN", "SRC_FID"]])
"""
Note 2: Now the fields srcM_inMN and SRC_FID are in the copy of the target features.
The srcM_inMN values are the counts of matched source features. The SRC_FID
values indicate the source feature IDs from which the transferred attributes
originated.
You can interactively review the transferred attributes for the
flagged features. To replace any of them with those from a different
source feature, make the edits as needed.
"""