Move inidividual polyline vertex in Python

1286
4
Jump to solution
03-19-2018 07:11 AM
NealBanerjee
Occasional Contributor

Hello,

I have a feature class with polylines that represent pipes.  Each polyline feature is single-part and generally only has small number of vertices (generally 2 - 5).  In some cases I need to update the starting and/or ending node/vertex  to a new location, without moving the other vertices.

I want to do this in Python either through field calculator or a separate script/tool.  I was planning on getting the geometry of the feature, creating an array with vertex points, updating first and/or last point, in the array, and then updating the feature geometry.  However, I am having a difficult time figuring out the syntax.  If I use a arcpy.da cursor, I can get the feature geometry (I think), but cant seem to pull the geometry properties.  Below is quick syntax I used in Python window in ArcMap.  Once I get to the row (feature), I thought I would be able to access polyline geometry properties to get the verticies, but keep getting message that data is tuple and is not callable.

Any advise would be greatly appreciated!

Thanks

Neal

>>> with arcpy.da.SearchCursor(fc,'SHAPE@') as cursor:
...     for row in cursor:
...         print(row)
...         
(<Polyline object at 0x45afe4b0[0x276eb760]>,)

0 Kudos
1 Solution

Accepted Solutions
NealBanerjee
Occasional Contributor

I got it to work on a simple test.  Below is Python code that worked to move just the first vertex of a polyline based on updated coordinates.  NewX1 and NewY1 fields are updated X,Y.  Attached is simple graphic

import arcpy
fc=r"C:\Users\nbanerjee\Documents\ArcGIS\Default.gdb\kmeline"

with arcpy.da.UpdateCursor(fc,['SHAPE@','NewX1','NewY1']) as cursor:
    for row in cursor:
        geofeat=row[0]
        geoarry=geofeat.getPart(0)
        oldX1=geoarry[0].X
        oldY1=geoarry[0].Y
        #print('{0},{1}'.format(oldX1,oldY1))
        geoarry[0].X=row[1]
        geoarry[0].Y=row[2]
        newply=arcpy.Polyline(geoarry)
        row[0]=newply
        cursor.updateRow(row)

Original line (red) and new line after 1st vertex move (green)

View solution in original post

0 Kudos
4 Replies
JoshuaBixby
MVP Esteemed Contributor

Since you are going to be updating data, I recommend UpdateCursor—Help | ArcGIS Desktop .  Whether using an ArcPy DA Update or Search cursor, the "row" is returned as a list or tuple.  If you only have a single field, it is still returned as a single item in a list or tuple, so use syntax like:  shape = row[0]

NealBanerjee
Occasional Contributor

Thank you - that was the trick.  I was hung up on syntax of working with tuple.!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Neal, if someone's response addresses your question and you don't need any further assistance, please mark it as correct so the status gets updated.

0 Kudos
NealBanerjee
Occasional Contributor

I got it to work on a simple test.  Below is Python code that worked to move just the first vertex of a polyline based on updated coordinates.  NewX1 and NewY1 fields are updated X,Y.  Attached is simple graphic

import arcpy
fc=r"C:\Users\nbanerjee\Documents\ArcGIS\Default.gdb\kmeline"

with arcpy.da.UpdateCursor(fc,['SHAPE@','NewX1','NewY1']) as cursor:
    for row in cursor:
        geofeat=row[0]
        geoarry=geofeat.getPart(0)
        oldX1=geoarry[0].X
        oldY1=geoarry[0].Y
        #print('{0},{1}'.format(oldX1,oldY1))
        geoarry[0].X=row[1]
        geoarry[0].Y=row[2]
        newply=arcpy.Polyline(geoarry)
        row[0]=newply
        cursor.updateRow(row)

Original line (red) and new line after 1st vertex move (green)

0 Kudos