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?
Solved! Go to Solution.
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]
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]
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.
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.
Yes it will be extended at the end of the line.
Could you mark the answer posted by Joshua as the correct answer?
Thank you!
That worked fantastically!
How would i deal with negative values? i guess some of the lengths need to be shorter. I hadn't taken that into consideration.
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.