Select to view content in your preferred language

Toolbox Script to Make a Line on the map

1718
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
Sorry- forgot to mention that the purpose of the script is to create a line on the map, though most probably could figure that out from looking at the code...
0 Kudos
DanPatterson_Retired
MVP Emeritus
You will note that your
origin_x variable is a text parameter (ie GetParameterAsText), hence, you need to cast it to a float value before performing any math operations using it.  This line:

origin_x = origin_x/(180/pi)

should be

origin_x = float(origin_x) / (180 / pi)

You will need to correct the others in the same fashion.
0 Kudos
GregDelapaix
Emerging Contributor
@Dan-
Thank you, that resolved my error. Now, I am getting the following:

<type 'exceptions.AttributeError'>: 'module' object has no attribute 'CreateFeatureClass'
Failed to execute (Script).

Not sure how to fix this- do I need to create a gdb for the FeatureClass to write to?
The error looks like arcpy has no CreateFeatureClass, which is not the case according to Esri
(http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000002p000000.htm)

'''
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 = "offset-line.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 buffering the line and including features in buffer zone would go here

# yes, this shouldn't really be necessary
lineArray.removeAll()
0 Kudos
DarrenWiens2
MVP Alum
Everything in Arcpy is case sensitive, so you should try: CreateFeatureclass_management
0 Kudos
GregDelapaix
Emerging Contributor
@dkwiens Thank you! I stared at the esri documentation for half an hour and didn't see that...


I have another error I am unable to figure out, mainly because I'm new to Python, ArcPy, etc.
Here it is:

<class 'arcgisscripting.ExecuteError'>: ERROR 000354: The name contains invalid characters
Failed to execute (CreateFeatureclass).

Failed to execute (Script).

What might these invalid characters be?

Also, I am concerned about one other thing, which is that the script doesn't contain information for distance in terms of units- I want it to be in miles- seems like something is missing for the script to create the dataset, let alone place the line on the map with the right units

Any further help GREATLY appreciated

GD
0 Kudos
DanPatterson_Retired
MVP Emeritus
Not to mention, that Python requires all paths to be forward slashes "/" and not backslashes, or use double backslashes "\\" or the "raw" identifier (see python help) if you insist in using backslashes
0 Kudos
GregDelapaix
Emerging Contributor
Thanks Dan, that did cross my mind at one point though I forgot. I'm using ArcMap on a remote server via a Citrix plug-in, and I am used to forward slashes, but I thought for the Windows machine I maybe needed backslashes... oi
0 Kudos
GregDelapaix
Emerging Contributor
.....still looking for any help on invalid characters with CreateFeatureclass_management, still getting the error after correcting the backslash mistake...
GD
0 Kudos
DarrenWiens2
MVP Alum
The shapefile you're trying to make has an invalid name (output = "offset-line.shp"). No hyphens allowed.

Interestingly, I was able to make a shapefile with a hyphenated name by right-clicking and choosing new shapefile. I didn't try to do anything with it, though, so who knows if it's a functional shapefile...
0 Kudos