How do I increase polyline length

4668
23
Jump to solution
07-18-2017 01:21 PM
RobertKirkwood
Occasional Contributor III

I have 250 polylines. I have a value i calculated (all different) that needs to be added to the length of the. The values are in the attribute table. How can I increase the length of the lines using the values in the table as the distance to add to the lines? 

Tags (1)
0 Kudos
23 Replies
JoshuaBixby
MVP Esteemed Contributor

The part I haven't tested with segmentAloneLine is how it handles multi-part lines.  My code snippet above treats each part independently, i.e., it extends each part by the specified length (I know, yet another assumption).  Does the OP want each part of a multi-part shortened/lengthened or just the last part of the multi-part line?  This is where Esri's geometry model (not ST_Geometry) drives me nuts where Polylines are MultilineStrings and Polygons are really MultiPolygons.

RobertKirkwood
Occasional Contributor III

Shortened and lengthened of a non-multipart line. 

0 Kudos
RobertKirkwood
Occasional Contributor III

How does that fit into the above code?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor
import math

fc = # Path to feature class containing lines to be extended
dist_fld = # Name of field containing distance to extend line

with arcpy.da.UpdateCursor(fc, ["SHAPE@", dist_fld]) as cur:
    for line, dist in cur:
        SR = line.spatialReference
        if dist > 0:
            parts = arcpy.Array()
            for part in line.getPart():
                pn1, pn = part[-2:]
                angle = math.atan2(pn.Y - pn1.Y, pn.X - pn1.X)
                p = arcpy.Point(pn.X + dist * math.cos(angle),
                                pn.Y + dist * math.sin(angle))
                part.append(p)
                parts.append(part)
            line = arcpy.Polyline(parts, SR)
        else:
            line = line.segmentAlongLine(0., line.length + dist)
        cur.updateRow([line,dist])
RobertKirkwood
Occasional Contributor III

Thanks so much! This has saved me a lot of time. I am using Oil and Gas data (points) for horizontal wells. The data comes from the Wyoming Oil and Gas Commission. There is a top hole location and bottom hole location. The bottom hole locations are not always correct. So we have to pick the bottom bases on Electric Logs of the subsurface and determine what formation the well was completed in. So, the bottom hole location changes, maybe deeper or shallower. So the point moves. This way is a lot quicker to take the 2 points create a line and recalculate the  length based on the new data. 

Thanks again! 

XanderBakker
Esri Esteemed Contributor

Thanks for posting back. I do wonder if in your case you really don't need any Z values?

RobertKirkwood
Occasional Contributor III

Would be nice but most of the data is old and collected using standard GPS.

0 Kudos
HeatherAnderson
New Contributor

Hi Josh.  I realize this is a super old post, but I am trying to use your script and am having trouble.   (I'm not exactly super knowledgeable in Python).  In the line below, what is the "SHAPE@" referring to?

with arcpy.da.UpdateCursor(fc, ["SHAPE@", dist_fld]) as cur:

JoshuaBixby
MVP Esteemed Contributor

The SHAPE@ is a "token" for retrieving the records geometry object as an ArcPy Geometry—ArcGIS Pro | Documentation 

HeatherAnderson
New Contributor

Roger and THANK YOU.  I got it worked out now.  I GREATLY appreciate it, as I think this script is going to save me a ton of work.