I work for a utility company and we have a number of poles that did not have their GPS locations taken, rather where the line turns the GPS location was taken, and the span of each pole in between each "angle" pole was calculated.

I ran the point to line function to create the line that runs between the GPS'd points and am currently working on a single span where the poles all line up in a straight line (I plan to expand this script to populate multiple spans after their angles change). I have line name, pole number (the points), and the distance to the next pole (in feet, which I convert to meters) in a .csv file. I have calculated the angle of the line and used trig functions to calculate the position of the next point (I'm using UTM zone 14 so it's a grid). When I run the script, as I have it now, it places the first point exactly where it should be, based on the location of the start of the line it follows. The problem is, the way the script is written it places all the points the distance in the .csv file away from the START point of the line. What I'm stuck on is placing the point the distance in the .csv from the PREVIOUS point that I populate in the update cursor, instead of from the initial point.

Here is the script as I currently have it.

# Import system modules
import arcpy
import math
import csv
from arcpy import env

#Set Variables

input_line = r"P:\Scripts\missing_poles\missing_poles.gdb\missing_poles_script\angle_line"
angle_field ="Angle"
rotationMethod = "GEOGRAPHIC"
coordSystemField = "UTM"
pole_distance_file = r"P:\Scripts\missing_poles\missing_poles_demo_no_empty.csv"
new_points = r"P:\Scripts\missing_poles\missing_poles.gdb\missing_poles_script\updated_poles"

#Create Field to add line angle too

arcpy.AddField_management (input_line, angle_field, "FLOAT")

#Calculate the angle of the line
for row in arcpy.da.SearchCursor(input_line, ["OID@", "SHAPE@"]):
    # Print the current line ID

    print("Feature {0}:".format(row[0]))

    #Set start point
    startpt = row[1].firstPoint

    #Set Start coordinates
    startx = startpt.X
    starty = startpt.Y

    #Set end point
    endpt = row[1].lastPoint

    #Set End coordinates
    endx = endpt.X
    endy = endpt.Y

    #calculate xy of new points
    radian = math.atan((startx-endx)/(starty-endy))
    with open(pole_distance_file) as csvfile:
        readCSV = csv.reader(csvfile, delimiter=',')
        for row in readCSV:
            #convert span ahead to meters
            span_ahead = (float(row[2])*0.3048)
            pole_id = row[1]
            #calculate x and y coordinates
            x = (math.sin(radian)*span_ahead)+ startx
            y = (math.cos(radian)*span_ahead)+ starty
            #update Feature Class with new coordinates
            new_coordinates = (x,y)
            print new_coordinates
            cursor = arcpy.da.InsertCursor(new_points, ["SHAPE@XY"])
            cursor.insertRow ([new_coordinates]

And here is the .csv file I'm working off of.


1 Answer 1


Reset your startx and starty in your csv loop:

#calculate x and y coordinates
x = (math.sin(radian)*span_ahead)+ startx
y = (math.cos(radian)*span_ahead)+ starty

# Reset startx and starty for next record in loop
startx = x
starty = y

Now the next record in the loop will use this record's x and y values as the start point.

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.