I want to update the point coordinates which are not correct on polyline. I tried many times to update it using python, but my script didn't work. The first attribute table is the station_id and its correct coordinates that need to update in polyline.

enter image description here

The second is the polyline's attribute table.

enter image description here

For example, the from_id "18 " in polyline is not correct, it need to be update to correct coordinates accoring to table 1.

How to find these wrong station_id (in From_id or To_id field) and replace it according to table 1 using python.


This worked for me:

import arcpy

points = r'C:\path_to_your\points.shp'
lines = r'C:\path_to_your\points.shp'
point_id_field = 'Your_ID_Field'

# grab point geometry and ID
with arcpy.da.SearchCursor(points, [point_id_field, 'SHAPE@']) as rows:
    pd = dict(r for r in rows)

# now update the line geometry based on From and To Id's
with arcpy.da.UpdateCursor(lines, ['From_id', 'To_id', 'SHAPE@']) as rows:
    for r in rows:
        if r[0] in pd and r[1] in pd:
            for part in r[2]:
                part.replace(0, pd[r[0]].centroid) # need to use centroid because it is an arcpy.Point object
                part.replace(part.count -1, pd[r[1]].centroid)
                r[2] = arcpy.Polyline(part)

One disclaimer, this is designed to work on single part geometries. If you have multi part line features you'll have to figure out how to map the start/end points of each part.

  • Many thanks, it really very helpful. I just started to learn Python. Thank you again. – Yi. WEI Oct 28 '15 at 1:09

To do this I would use arcpy.da.UpdateCursor:

UpdateCursor establishes read-write access to records returned from a feature class or table.

Returns an iterator of lists. The order of values in the list matches the order of fields specified by the field_names argument.

You can access a geometry object for the feature using the SHAPE@ token.


After trying many times, i wrote this script. It finally solved my problems which i just want to correct the wrong vertex and remain the right ones. Only the wrong vertex coordinates in point table.

import arcpy
from arcpy import env
env.overwriteOutput = True
env.workspace = "E:\\test"

points = "point.shp"
lines = "merge_line.shp"
point_id_field = 'station_id'

points_cursor = arcpy.da.SearchCursor(points, [point_id_field, 'SHAPE@X','SHAPE@Y'])
for points_row in points_cursor:
    station_id = points_row[0]
    x = points_row[1]
    y = points_row[2]
    points_dict[str(int(station_id))] = str(x),str(y)
with arcpy.da.UpdateCursor(lines,["From_id","To_id","OID@","SHAPE@"]) as update_cursor:
    for update_row in update_cursor:
        from_id = update_row[0]
        to_id = update_row[1]
        if from_id in points_dict.keys() and to_id not in points_dict.keys():
            print from_id
            geometry = update_row[3]
            pts = geometry.getPart(0)
            n = len(pts)
            pt_array1 = arcpy.Array()
            for i in xrange(n):
                if i == 0:
                    frompoint1 = pts.getObject(i)
                    print frompoint1.X, frompoint1.Y
                    frompoint1.X = points_dict[from_id][0]
                    frompoint1.Y = points_dict[from_id][1]
                    print frompoint1.X ,frompoint1.Y

                elif i == 1:
                    topoint1 = pts.getObject(i)
                    print topoint1.X,topoint1.Y
            new_polyline_geometry = arcpy.Polyline(pt_array1)        
            update_row[3] = new_polyline_geometry
        elif from_id in points_dict.keys() and to_id in points_dict.keys():
            print from_id, to_id
            geometry = update_row[3]
            pts = geometry.getPart(0)
            n = len(pts)
            pt_array2 = arcpy.Array()
            for i in xrange(n):
                if i == 0:
                    frompoint2 = pts.getObject(i)
                    print frompoint2.X, frompoint2.Y
                    frompoint2.X = points_dict[from_id][0]
                    frompoint2.Y = points_dict[from_id][1]
                    print frompoint2.X ,frompoint2.Y
                elif i == 1:
                    topoint2 = pts.getObject(i)
                    print topoint2.X, topoint2.Y
                    topoint2.X = points_dict[to_id][0]
                    topoint2.Y = points_dict[to_id][1]
                    print topoint2.X, topoint2.Y
            new_polyline_geometry = arcpy.Polyline(pt_array2)        
            update_row[3] = new_polyline_geometry
        elif to_id in points_dict.keys() and from_id not in points_dict.keys():
            print to_id
            geometry = update_row[3]
            pts = geometry.getPart(0)
            n = len(pts)
            pt_array3 = arcpy.Array()
            for i in xrange(n):
                if i == 0:
                    frompoint3 = pts.getObject(i)
                    print frompoint3.X,frompoint3.Y
                if i == 1:
                    topoint3 = pts.getObject(i)
                    print topoint3.X, topoint3.Y
                    topoint3.X = points_dict[to_id][0]
                    topoint3.Y = points_dict[to_id][1]
                    print topoint3.X, topoint3.Y
            new_polyline_geometry = arcpy.Polyline(pt_array3)        
            update_row[3] = new_polyline_geometry

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.