0

I am having trouble getting a feature that is at the start and end point of a line. I can get the start and end point of the line but I am not sure how to grab the Point feature that is at the start and end point of a line. I need to get the point feature that is at the start and end point of a line and then grab some data from the point feature and update the Line feature data. How can I get a point feature that is at the start or end point of a Line and then get that feature data?

    with arcpy.da.SearchCursor("GravityMains",["Material","SHAPE@"]) as curs:
        for row in curs:
            if row[1]:
                startptx = row[1].firstPoint.X
                startpty = row[1].firstPoint.Y
                arcpy.SelectLayerByLocation_management("Manhole","INTERSECT",arcpy.PointGeometry(arcpy.Point(startptx,startpty)))

             else:   
                 print "NULL"

I am using ArcMap 10.3.1

7
  • How far have you got? Can you post an example of what you've tried?
    – Midavalo
    Commented Feb 6, 2016 at 21:30
  • Yes,I will add the code to my post.
    – JotDot
    Commented Feb 6, 2016 at 21:52
  • Are you wanting to just select a point feature, or actually take attributes from the point feature and add to fields on the line feature? I have some code that copies an attribute from From/To points into From/To fields against the line. I can share that here if that's what you're after
    – Midavalo
    Commented Feb 6, 2016 at 21:54
  • I am wanting to actually take attributes from the point feature and add to fields on the line feature. I was just trying to use the select method to help rap my head around how to get a feature at a point. I need to know if the line feature is at the start point or end point because if the point feature is at the start point I update a field in the Line feature. If the point feature is at the end point of the line, I will update a different field in the Line feature.
    – JotDot
    Commented Feb 6, 2016 at 22:04
  • Possible duplicate of Assign point IDs to respective start and end attributes of a polyline
    – FelixIP
    Commented Feb 7, 2016 at 5:09

1 Answer 1

3

I use the following code, which copies values from fields on From/To points into From/To fields against each line (different fields if start or end point). This runs on selected lines, but can be easily modified to run on all lines.

import sys, string, os, arcpy, fpformat

mxd = arcpy.mapping.MapDocument("CURRENT")

layers = arcpy.mapping.ListLayers(mxd)

strLineFc = "Line" # Insert name of Line layer 
strPointFc = "Point" # Insert name of Point layer 
lyrexists = 0

for layer in layers:
    if layer.name == strLineFc: 
        desc = arcpy.Describe(layer.name)
        if not desc.FIDSet  == '': # Check for selected lines
            lyrexists = 1
            arcpy.AddMessage("Layer Found")

if lyrexists == 1:
    # Line cursor   
    lineRows = arcpy.UpdateCursor (strLineFc)

    lineRow = lineRows.next()

    # Edit each Line
    while lineRow:
        # Get the Line geometry
        linePointsArray = lineRow.Shape.getPart(0)
        pt_count = linePointsArray.count
        pt_begin = linePointsArray.next() # From coordinate
        # Check for non two-part lines (multiple vertices)
        vertices = 2
        while vertices < pt_count:
            pt_end = linePointsArray.next() 
            vertices = vertices + 1
        pt_end = linePointsArray.next() # To coordinate

        # Print the line object id
        arcpy.AddMessage (str(lineRow.ObjectID))
        # Point cursor
        pointRows = arcpy.SearchCursor (strPointFc)
        pointRow = pointRows.next()

        # Seach Point database
        while pointRow:
            # Grab the Point location
            point = pointRow.Shape.getPart(0)

            # Find the from Point and update the From attributes

            if fpformat.fix(point.X,2) == fpformat.fix(pt_begin.X,2) and fpformat.fix(point.Y,2) == fpformat.fix(pt_begin.Y,2):

                lineRow.FromID = pointRow.FeatID
                arcpy.AddMessage("\t" + pointRow.FeatID)
                lineRows.updateRow(lineRow)

            # Find the To Point and update the line To attributes
            if fpformat.fix(point.X,2) == fpformat.fix(pt_end.X,2) and fpformat.fix(point.Y,2) == fpformat.fix(pt_end.Y,2):

                lineRow.ToID = pointRow.FeatID
                arcpy.AddMessage("\t" + pointRow.FeatID)

                lineRows.updateRow(lineRow)             

            # Next Point
            pointRow = pointRows.next()

        # Next Line
        lineRow = lineRows.next()
    del pointRows
    del pointRow
    del lineRows
    del lineRow
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.