Is it possible that arcpy converts all inputs of the a value registered as GPlinearunit (kilometers, meters, feet, miles, etc) into units of XY coordinates system? I need to perform calculations with it, since ArcPy GPlinearunit recognizes the parameter as a string.
I have:
import arcpy, math
source = arcpy.GetParameterAsText(0) # < feeature point
w = arcpy.GetParameterAsText(1) # <- Double
d = arcpy.GetParameterAsText(2) # <- GPlinearunit
for row in arcpy.da.SearchCursor(source, ["SHAPE@XY"]):
x, y = row[0]
arcpy.AddMessage("{}, {}".format(x, y))
rad = float(w) / 57.2957795
arcpy.AddMessage("{}".format(rad))
dX = x + float(d) * math.cos(rad)
dY = y + float(d) * math.sin(rad)
arcpy.AddMessage("{}, {}".format(dX, dY))
Error message:
Traceback (most recent call last): File "C:\AIR-GIS\toolbox\scripts\Perfil.py", line 26, in dX = x + float(d) * math.cos(rad) ValueError: invalid literal for float(): 5 Kilometers Failed to execute (Perfil).
I found a solution: convert all units in meters.
digit = int(filter(str.isdigit, str(d)))
arcpy.AddMessage(str(digit))
letters = str(filter(str.isalpha, str(d)))
arcpy.AddMessage(str(letters))
if letters == "Meters":
dfloat = float(digit)
elif letters == "Kilometers":
dfloat = float(digit) * 1000
elif letters == "Unknown":
dfloat = float(digit)
elif letters == "Feet":
dfloat = float(digit) * 3.28084
rad = float(w) / 57.2957795
dX = x + dfloat * math.cos(rad)
dY = y + dfloat * math.sin(rad)
float(d)
?d
to see what is being pulled through from your tool dialog. I think you will find it is a string (you are usingGetParameterAsText
) of "5 Kilometers" and not a number as string i.e. "5".