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()

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()

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.

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()

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

GD

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...

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()