Select to view content in your preferred language

Toolbox Script to Make a Line on the map

1728
10
04-26-2011 09:45 PM
GregDelapaix
Emerging Contributor
Hey all-
I am trying to place the following script into a toolbox I created. The idea is that it receives user input for x and y origins (decimal degrees), distance and angle. Distance, along with origin_x and origin_y are linear measurements, angle is set as 'any value'- I'm not sure if this is correct, but the idea with angle is that it would be a number from 0 to 360 with North being 0, West being 90, etc. I also want to make sure the script references the map properly, to create a line using the input parameters.

Here is my script so far, when I run it I get the following error:
<type 'exceptions.TypeError'>: unsupported operand type(s) for /: 'unicode' and 'float'
Failed to execute (Script). I would GREATLY appreciate any help

Code is as follows:

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

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 = origin_x/(180/pi)
origin_y = origin_y/(180/pi)

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

output = "offset-line.shp"
arcpy.CreateFeatureClassManagement("G:\Delapaix\line_connect\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)
lineArray.add(start)

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

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

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

# yes, this shouldn't really be necessary
lineArray.removeAll()
Tags (2)
0 Kudos
10 Replies
GregDelapaix
Emerging Contributor
@dkwiens
Thank you that fixed it!

If possible, I am in need of further assistance- I am attempting to make the line appear on a map which is in the same folder the shapefile is created in. As it is now, it creates the shapefile, but not the line on a map. Do I need to create a geodatabase to place the shapefile in? Otherwise I'm not sure about the script being able to detect the projection/coordinate system. I'm also not sure about making sure the units for the length of the line are consistent, in this case miles
Here is my script as it currently stands:

'''
Created on Apr 24, 2011

@author: gregdelapaix
'''
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)
lineArray.add(start)

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

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

# code for placing the line on the map here(?)
# code for buffering the line and including features in buffer zone would go here

# should I remove this?
lineArray.removeAll()
0 Kudos