I have some code others have provided, which I modified in order to run a Python script in a toolbox in ArcMap which accepts user input for a starting point, distance, and angle to create a shapefile for a line. The script works OK except the starting point (origin_x, origin_y), which I am entering in decimal degrees, is always wrong when I place the shapefile in a map dataframe. How can I correct this code so that the math can work correctly to produce the right starting point location? It seems converting origin_x and origin_y into radians is the wrong way to go about it, but I'm not sure...

import arcpy
from arcpy import env
from math import radians, sin, cos, pi

#Set workspace
env.workspace = "G:\\Delapaix\\Geog_586_FinalProject\\line_connect"

origin_x   = arcpy.GetParameterAsText(0)
origin_y  = arcpy.GetParameterAsText(1)
distance  = arcpy.GetParameterAsText(2)
angle  = arcpy.GetParameterAsText(3)

# convert latitude and longitude to radians 
origin_x = float(origin_x)/(180/pi)
origin_y = float(origin_y)/(180/pi)

# calculate offsets with light trig
(disp_x, disp_y) = (float(distance) * sin(radians(float(angle))), float(distance) *  cos(radians(float(angle))))
(end_x, end_y) = (float(origin_x) + disp_x, float(origin_y) + disp_y)

output = "offsetLine.shp"
arcpy.CreateFeatureclass_management("G:\\Delapaix\\Geog_586_FinalProject\\line_connect",output, "Polyline")
cur = arcpy.InsertCursor(output)
lineArray = arcpy.Array()

# start point
start = arcpy.Point()
(start.ID, start.X, start.Y) = (1, origin_x, origin_y)

# end point
end = arcpy.Point()
(end.ID, end.X, end.Y) = (2, end_x, end_y)

# write feature to the shapefile
feat = cur.newRow()
feat.shape = lineArray

# code for buffering the line and including features in buffer zone would go here

# yes, this shouldn't really be necessary
  • Are you expecting the start points to be in projected coordinates? If so, then you need to create the file, project it, then use an update cursor to add the other points.
    – user681
    Jun 7 '11 at 17:05

Your Answer

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

Browse other questions tagged or ask your own question.