Environment: ArcGIS Desktop 10.6.1
Below, I am attempting to snap a point feature class to the geometry of an address feature class based on a common 'Master ID' field using a join. The code originally comes from arcgis desktop - Snapping point to point based on attribute? - Geographic Information Systems Stack ...
However, this is not moving my points, and the data is clean and does match up.
arcpy.MakeFeatureLayer_management("D:\\WorkSpace\\POI\\Workspace.gdb\\COH_POI", "POI_Layer", "", "", "")
arcpy.SelectLayerByAttribute_management("POI_Layer", "NEW_SELECTION", "MASTER_ID IS NOT NULL")
pt_on_line = "D:\\WorkSpace\\POI\\Workspace.gdb\\COH_Address"
pt_on_line_ID = 'MasterID' # common ID field
pt_off_line = "POI_Layer"
pt_off_line_ID = 'MASTER_ID' # common ID field
pt_on_line_dict = {str(row[0]):row[1] for row in arcpy.da.SearchCursor(pt_on_line,[pt_on_line_ID,"SHAPE@"])} # make dictionary, like {ID:geometry}
with arcpy.da.UpdateCursor(pt_off_line,[pt_off_line_ID,"SHAPE@"]) as cursor:
for row in cursor: # loop through points
row[1] = pt_on_line_dict[str(row[0])] # move point geometry to match point geometry from dictionary
cursor.updateRow(row) # update the geometry
del cursor, row
Solved! Go to Solution.
I tested the code and it works, so that's not the problem.
It might have something to do with your selection, but the query seems straight enough...
Maybe try this:
pt_on_line = "D:\\WorkSpace\\POI\\Workspace.gdb\\COH_Address"
pt_on_line_ID = 'MasterID' # common ID field
pt_off_line = "D:\\WorkSpace\\POI\\Workspace.gdb\\COH_POI"
pt_off_line_ID = 'MASTER_ID' # common ID field
pt_on_line_dict = {str(row[0]):row[1] for row in arcpy.da.SearchCursor(pt_on_line, [pt_on_line_ID,"SHAPE@"])} # make dictionary, like {ID:geometry}
with arcpy.da.UpdateCursor(pt_off_line, [pt_off_line_ID, "SHAPE@"]) as cursor:
for row in cursor: # loop through points
if row[0]: # if row[0] is not None
try:
row[1] = pt_on_line_dict[str(row[0])] # move point geometry to match point geometry from dictionary
cursor.updateRow(row) # update the geometry
except KeyError:
print("key error: {} {} doesn't have a matching point!".format(pt_off_line_ID, row[0]))
I tested the code and it works, so that's not the problem.
It might have something to do with your selection, but the query seems straight enough...
Maybe try this:
pt_on_line = "D:\\WorkSpace\\POI\\Workspace.gdb\\COH_Address"
pt_on_line_ID = 'MasterID' # common ID field
pt_off_line = "D:\\WorkSpace\\POI\\Workspace.gdb\\COH_POI"
pt_off_line_ID = 'MASTER_ID' # common ID field
pt_on_line_dict = {str(row[0]):row[1] for row in arcpy.da.SearchCursor(pt_on_line, [pt_on_line_ID,"SHAPE@"])} # make dictionary, like {ID:geometry}
with arcpy.da.UpdateCursor(pt_off_line, [pt_off_line_ID, "SHAPE@"]) as cursor:
for row in cursor: # loop through points
if row[0]: # if row[0] is not None
try:
row[1] = pt_on_line_dict[str(row[0])] # move point geometry to match point geometry from dictionary
cursor.updateRow(row) # update the geometry
except KeyError:
print("key error: {} {} doesn't have a matching point!".format(pt_off_line_ID, row[0]))
Thank you, Johannes,
It has definitely made progress, I then receive an error:
An invalid SQL statement was used. [SELECT MasterID, OBJECTID FROM COH_Address WHERE SHAPE@]
I guess that was on line 5? Somehow I deleted an opening bracket...
I updated the code above, please try again.
Thank you, Johannes, that did the trick. Your help is appreciated.