Tuesday 17 November 2015

arcpy - How to add vertex on the center of a polyline object?


I've got an arcpy.Polyline() object and I have to insert a new vertex in it, which has to be placed on a center of a line. Right now I can calculate the center point coordinates but how to add it to an existing Polyline object in the right place of points order? The existing vertices order should not be changed, so the output line has to have previous vertices plus the new one somehere in the middle.


with arcpy.da.UpdateCursor(line_in,'SHAPE@') as uc:
for row in uc:
Midpoint = row[0].positionAlongLine(0.50,True).firstPoint

mid_p = arcpy.Point(Midpoint.X,Midpoint.Y)

ArcGIS 10.2 has no segmentAlongLine method so is it possible to update line's vertices here?




Due to the suggestion that this question has been already asked I would like to clarify that I don't need a new layer with points. The goal is to add a vertex to existing line.



Answer



Recently with the piece of advice of Sergei Norin I have found the solution for this case. It is about calculations of distances between vertexes within the line.


with arcpy.da.SearchCursor(line_in,'SHAPE@') as sc:

for row in sc:

aa = list(row[0].getPart(0)) # GETTING A LIST OF VERTEXES AS POINTS


fst = arcpy.Array() # THE ARRAY OF COORDINATES OF THE FIRST HALF OF THE LINE
snd = arcpy.Array() # THE ARRAY OF COORDINATES OF THE SECOND HALF OF THE LINE

midpoint = row[0].positionAlongLine(0.50,True).firstPoint
mid_p = arcpy.Point(midpoint.X,midpoint.Y) #CREATING THE MIDPOINT OF THE INITIAL LINE

snd.add(mid_p) # ADDING THE MIDPOINT AS A DEFAULT START POINT IN THE SECOND HALF


mediana = row[0].getLength()/2 #GETTING THE HALF OF THE INITIAL LINE'S LENGTH

nu = 0 # THE LENGTH CALCULATING FROM EACH VERTEX FROM THE BEGINNIG OF A LINE
for i, p in enumerate(aa): #LOOP THROUGH VERTEXES'
try:
ini = i # GETTING THE NUMBER OF INITIAL LOOPING VERTEX
nex = i+1 # GETTING THE NUMBER OF THE NEXT VERTEX

# GETTING THEIR COORDINATES


px0 = aa[ini].X
px1 = aa[nex].X
py0 = aa[ini].Y
py1 = aa[nex].Y

# CALCULATING A DISTANCE BETWEEN INITIAL AND NEXT VERTEXES

dif = math.sqrt(math.pow((px1-px0),2)+math.pow((py1-py0),2))


# ADDING VERTEX TO A NEW COORDIANTE LIST

poi = arcpy.Point(p.X,p.Y)
fst.add(poi)



nu=nu+dif # SUM DISTANCE BETWEEN VERTEXES WITH A TOTAL LENGTH FROM THE BEGINNIG

#IN CASE WHEN THE LENGTH OF PREVIOUSLY COUNTED LENGTH IS MORE THAN "MEDIANA" PARAMETER, THIS LOOP ENDS UP WITH ADDING THE REMAINING POINTS TO THE LIST


if nu>mediana:
fst.add(mid_p)
for n, j in enumerate(aa):
try:

px3 = aa[nex].X
px4 = aa[nex].Y
pei = arcpy.Point(px3,px4)
fst.add(pei)

nex=nex+1
except:
pass


#fst.add(mid_p)

break
except:
pass



# A NEW LINE IS CREATED AND PLACED

f_lin = arcpy.Polyline(fst,sr)

icc = arcpy.da.InsertCursor(line_in,'SHAPE@')
icc.insertRow([f_lin])

del icc

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...