I want to automate a task in ArcGIS using arcpy. The task starts with creating points with 50 meter intervals along a line. Then use a loop to iterate and calculate values 50 meters from 0 to last point. The problem is I want to calculate the actual distance value of last point and I can't get last record. I try to use rowpt[-1] and use this sample but i don't know how to update the value. How can I get the last record?

import arcpy
arcpy.env.workspace = r"F:\test\env.gdb"
arcpy.env.overwriteOutput = True
line_lyr = "line"
pt_lyr =  "my_point"
interval = 50
insertCursor = arcpy.da.InsertCursor(pt_lyr, ["SHAPE@XY"]) # this is the pre-existing pt feature class
with arcpy.da.SearchCursor(line_lyr, ['OID@','SHAPE@']) as searchCursor: # this is the line feature on which the points will be based
    for row in searchCursor:
        lengthLine = row[1].length # grab the length of the line feature, i'm using round() here to avoid weird rounding errors that prevent the numberOfPositions from being determined
        if lengthLine % interval == 0:
            numberOfPositions = int(lengthLine // interval) - 1
            numberOfPositions = int(lengthLine // interval)
            if numberOfPositions >= 0:
                distancezero = 0
                xPoint = row[1].positionAlongLine(distancezero).firstPoint.X
                yPoint = row[1].positionAlongLine(distancezero).firstPoint.Y
                xy = (xPoint, yPoint)
                for i in range(numberOfPositions):
                    distance = (i + 1) * interval
                    xPoint = row[1].positionAlongLine(distance).firstPoint.X
                    yPoint = row[1].positionAlongLine(distance).firstPoint.Y
                    xy = (xPoint, yPoint)
                distanceend = lengthLine
                xPoint = row[1].positionAlongLine(lengthLine).firstPoint.X
                yPoint = row[1].positionAlongLine(lengthLine).firstPoint.Y
                xy = (xPoint, yPoint)
            del insertCursor
with arcpy.da.UpdateCursor(pt_lyr,"Interval") as ptcursor:
    number = 0
    for rowpt in ptcursor:
        rowpt[0] = number
        number = number + 50
    if rowpt[-1]== number-50:
         rowpt[-1] = 100
  • The example you've linked allows you to modify the value in the last column for every record, not specifically just the last record. – Midavalo Oct 29 '16 at 18:54
  • The way I'd try it is count the points for each line and record the last one in a dictionary along with the count (as part of the Insert Cursor). Then in your Update Cursor multiply that count by the interval and update that value into the corresponding point record. I can't test this right now though so may have overlooked some things. – Midavalo Oct 29 '16 at 18:59
  • @Midavalo I Used arcpy.GetCount_management(point).getOutput(0) – BBG_GIS Oct 31 '16 at 13:41

The block starting with "if numberOfPositions >= 0:" should not be nested under the else statement, un-indent that whole block.

Instead of using the update cursor to go back and add interval, include that value as you insert new rows. The distance from the first point to the last point is equal to line length. Use the .length() method on the line geometry object to get the overall length rather than recalculate that value.

  • Thank you for your answer. I solveed my problem with arcpy.GetCount_management(point).getOutput(0) – BBG_GIS Nov 2 '16 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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