Select to view content in your preferred language

How do I increase polyline length

5540
23
Jump to solution
07-18-2017 01:21 PM
RobertKirkwood
Frequent Contributor

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
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Not sure if there is a tool that directly addresses this.  I know this question has been asked in one form or another on GeoNet and StackExchange, so I encourage a bit more searching.

If you up for some basic scripting, the following code snippet should work or get you most of the way there:

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
        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)
        cur.updateRow([line,dist])
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The above code simply linearly extrapolates the line that connects the last two vertices of the line.  If you want different line fitting/extending, you can implement a different formula.

NOTE:  The ArcPy Array slicing on line 11 requires ArcGIS 10.4.1.  If you are using an earlier version the line can be modified.

pn1, pn = arr[len(arr)-2], arr[len(arr)-1]

View solution in original post

23 Replies
JoshuaBixby
MVP Esteemed Contributor

Not sure if there is a tool that directly addresses this.  I know this question has been asked in one form or another on GeoNet and StackExchange, so I encourage a bit more searching.

If you up for some basic scripting, the following code snippet should work or get you most of the way there:

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
        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)
        cur.updateRow([line,dist])
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The above code simply linearly extrapolates the line that connects the last two vertices of the line.  If you want different line fitting/extending, you can implement a different formula.

NOTE:  The ArcPy Array slicing on line 11 requires ArcGIS 10.4.1.  If you are using an earlier version the line can be modified.

pn1, pn = arr[len(arr)-2], arr[len(arr)-1]
XanderBakker
Esri Esteemed Contributor

The helpful example provided by bixb0012 assumes that the line will be extended at the end of the line. A possible scenario could be to extend both ends with half the value from the attribute table.

Would be interesting to learn a little more about why you want this. 

JoshuaBixby
MVP Esteemed Contributor

I thought the same thing, about beginning, end, or both, but after reading the question a couple times, I assumed (I know about assuming) we are talking about the end.  If the OP states otherwise, I can modify the code snippet.

RobertKirkwood
Frequent Contributor

Yes it will be extended at the end of the line.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Could you mark the answer posted by Joshua as the correct answer?

RobertKirkwood
Frequent Contributor

Thank you!

0 Kudos
RobertKirkwood
Frequent Contributor

That worked fantastically! 

0 Kudos
RobertKirkwood
Frequent Contributor

How would i deal with negative values? i guess some of the lengths need to be shorter. I hadn't taken that into consideration. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

In case of negative values you could use the polyline::segmentAlongLine (start_measure, end_measure, {use_percentage}). In this case start measure is 0 and end measure is the polyline.length + the negative value and {use_percentage} would be False.