import arcpy
from arcpy import env env.overwriteOutput = True env.workspace = "E:\VE-work\(6)Train_network_building\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']) points_dict={} 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: print("Feature{0}:".format(update_row[2])) from_id = update_row[0] to_id = update_row[1]
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'])
points_dict={}
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:
print("Feature{0}:".format(update_row[2]))
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
pt_array1.add(frompoint1)
elif i == 1:
topoint1 = pts.getObject(i)
print topoint1.X,topoint1.Y
pt_array1.add(topoint1)
new_polyline_geometry = arcpy.Polyline(pt_array1)
update_row[3] = new_polyline_geometry
update_cursor.updateRow(update_row)
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
pt_array2.add(frompoint2)
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
pt_array2.add(topoint2)
new_polyline_geometry = arcpy.Polyline(pt_array2)
update_row[3] = new_polyline_geometry
update_cursor.updateRow(update_row)
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
pt_array3.add(frompoint3)
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
pt_array3.add(topoint3)
new_polyline_geometry = arcpy.Polyline(pt_array3)
update_row[3] = new_polyline_geometry
update_cursor.updateRow(update_row)