I have 2 layer of points, PointLayer1 and PointLayer2. PointLayer1 has to be snapped to PointLayer2.

I have a table that has IDs of PointLayer1 that has to be snapped to PointLayer2.

Sample Table:

ID1        ID2
20         2445
22         2557
23         2456

At the end, all the nodes form PointLayer1 with ID1 has to be snapped to PointLayer2 with ID2.

Completed Code so far:

import pandas
import arcpy

all_snapped_nodes = 'all_snapped_nodes.shp'
all_new_nodes = 'AllnewNodes.shp'

new_old_node_conversion = pandas.read_csv('new_old_node_conversion.csv',index_col = 0, header = None)
#more code thats snaps IDs from layer1 to layer2.

This will take three cursors and a couple dictionary. First iterate your id table, associating id1 with id2 in a dictionary. Then iterate new nodes and associate id2s with xys. Finally, iterate your all snapped feature class. Link id1 to id2 from your first dictionary, then id2 to xy from your second.

import arcpy

all_snapped_nodes = 'all_snapped_nodes.shp'
all_new_nodes = 'AllnewNodes.shp'
snapTable = "SnapTable"

#create dictionary of id1 to id2
snapDi = {}
with arcpy.da.SearchCursor (snapTable, ["ID1", "ID2"]) as curs:
    for id1, id2 in curs:
        snapDi [id1] = id2

#create dictionary with xy from new nodes by id
xyDi = {}
#spatial reference of all_snapped shapefile
sr = arcpy.Describe (all_snapped_nodes).spatialReference
#iterate all_new_nodes
with arcpy.da.SearchCursor (all_new_nodes, ["ID", "SHAPE@XY"],
                            spatial_reference = sr) as curs:
    for id2, xy in curs:
        #update xy
        xyDi [id2] = xy

#update all_snapped_nodes
with arcpy.da.UpdateCursor (all_snapped_nodes, ["ID", "SHAPE@XY"]) as curs:
    for id1, xy in curs:
        #get id2
        try: id2 = snapDi [id1]
        except KeyError: continue
        #get xy of id2
        try: xy = xyDi [id2]
        except KeyError: continue
        row = (id1, xy)
        curs.updateRow (row)
  • from what I understood, you first created a dictionary of ID1 and ID2. Second, you generated xy coordinates of ID2. Third, you updated all_snapped_nodes to those calculated coordinates. Right? Where did the actual snapping occur? I am confused. – pnkjmndhl Nov 22 '17 at 19:06
  • 1
    SHAPE@XY in an update cursor allows you to update xy coordinates. This is the snapping. – Emil Brundage Nov 22 '17 at 19:09
  • my coordinate system is WGS1984. Should I change the code for that?? – pnkjmndhl Nov 22 '17 at 19:12
  • 1
    I’m pretty sure the code will still work. – Emil Brundage Nov 22 '17 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.