Extend Line up to polygon feature

7615
16
Jump to solution
04-14-2017 09:52 AM
Sai_AnandPeketi1
New Contributor III

Hi  everyone,

I've some set of points which I'm joining to make a line. Refer the Picture below.

 

 

 

The problem I'm facing is there are no points(elevation points) on the polygon (River Bank). Is there any way of extending the line to the polygon?

At present m using Extend line to layer, but it taking too much of time and inconsistent in the lines. Refer the Picture below.

0 Kudos
16 Replies
JoshuaBixby
MVP Esteemed Contributor

I am using ArcGIS 10.5, which explains part of the issue.  Prior to ArcGIS 10.4.x, ArcPy Array objects supported indexing but not slicing, hence the error message you are seeing running my code.

Try replacing line 16 with:

p1, p2 = arr[0], arr[1]

and replacing line 22 with:

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

Sai_AnandPeketi1
New Contributor III

And I have Z values in line extent. While updating the geometry those are not update geometry. So, Please help to get those Z-Values.

Thanks for the help

0 Kudos
XanderBakker
Esri Esteemed Contributor

In the code sample I provided, this can be done pretty easy changing the function "ExtendPolyline" inserting line 10 setting the Z values for start and end point of the line (at the polygon boundary) to 0 (assuming this is correct) and enabling Z values for the polygon on line 13 in the example below:

def ExtendPolyline(polyline, pnt1, pnt2):
    # will snap first and last line to nearest part of given polyline
    # does not account for small segments!
    sr = polyline.spatialReference
    lst_pnts = []
    for part in polyline:
        for pnt in part:
            lst_pnts.append(pnt)

    pnt1.Z, pnt2.Z = 0, 0
    lst_pnts.insert(0, pnt1)
    lst_pnts.append(pnt2)
    return arcpy.Polyline(arcpy.Array(lst_pnts), sr, True, False)

The result viewed in 3D looks like this:

I do notice that near the shore lines (polygon boundary) some values have a Z value greater than 0. Are you sure that the measure Z values of the points used to create the profile lines correspond to the polygon boundary (same date)?

In case you want to continue with the code provided by Joshua Bixby it will be a little more complex since you can't just do this by simply adding the Z=0 property to the point "p"(lines 4 and 10) in the snippet below and changing line 13 to make it Z-aware:

                p1, p2 = arr[0], arr[1]
                angle = math.atan2(p2.Y - p1.Y, p2.X - p1.X)
                p = arcpy.Point(p1.X - dist * math.cos(angle),
                                p1.Y - dist * math.sin(angle), 0)
                arr.insert(0, p)

                pn1, pn = arr[len(arr)-2], arr[len(arr)-1]
                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), 0)
                arr.append(p)

                line = arcpy.Polyline(arr, SR, True, False)
                line = line.cut(boundary)[1]
                ucur.updateRow([line])

The line start and end before cutting would have the Z set to 0, but will no have as result that the Z value at the shore will be 0. As an alternative, you cut the 3D line, and afterwards, read out the first and last point, set the Z value to 0 and insert them back in the array and create the final 3D polyline:

import math
dist = 500

fc_line = "Pointoline"
fc_poly = "RB_23"

with arcpy.da.SearchCursor(fc_poly, "SHAPE@") as scur:
    for poly, in scur:
        boundary = poly.boundary()
        arcpy.SelectLayerByLocation_management(fc_line, "WITHIN", poly)
        with arcpy.da.UpdateCursor(fc_line, "SHAPE@") as ucur:
            for line, in ucur:
                arr, = line.getPart()
                SR = line.spatialReference

                p1, p2 = arr[0], arr[1]
                angle = math.atan2(p2.Y - p1.Y, p2.X - p1.X)
                p = arcpy.Point(p1.X - dist * math.cos(angle),
                                p1.Y - dist * math.sin(angle))
                arr.insert(0, p)

                pn1, pn = arr[len(arr)-2], arr[len(arr)-1]
                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))
                arr.append(p)

                line = arcpy.Polyline(arr, SR, True, False)
                line = line.cut(boundary)[1]

                arr, = line.getPart()
                p1, pn = arr[0], arr[len(arr)-1]
                p1.Z, pn.Z = 0, 0
                arr.insert(0, p1)
                arr.append(pn)
                line = arcpy.Polyline(arr, SR, True, False)

                ucur.updateRow([line])
JoshuaBixby
MVP Esteemed Contributor

Nice work, the OP has gotten lots of good work out of GeoNet contributors.  It is hard to pass up an interesting, and a bit challenging, of a question.

Sai_AnandPeketi1
New Contributor III

Joshua, I increased the distance. But in some line are not touching with the riverbank.Please have a look.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I re-downloaded the sample data, and it doesn't include any of these problematic lines.  To give any specific suggestions, I will need to see the data behind some of these problem lines.  Can you upload a second sample data set of the area in the image?

edlaanil
New Contributor

edlaanil_0-1644951147531.png

 

 

Feature to ploygon with two input polylines to convert to polygon getting error from python script in dot net arc gis pro

kindly help me

0 Kudos