1

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)
10
  • Please edit your question to include a code snippet so we can see what you've got and what you need to change
    – Midavalo
    Sep 15, 2016 at 21:04
  • Sorry, I added the code.
    – Novvier
    Sep 15, 2016 at 21:19
  • what happens when you use float(d)?
    – Midavalo
    Sep 15, 2016 at 21:28
  • The message is: Traceback (most recent call last): File "C:\AIR-GIS\toolbox\scripts\Perfil.py", line 26, in <module> dX = x + float(d) * math.cos(rad) ValueError: invalid literal for float(): 5 Kilometers Failed to execute (Perfil).
    – Novvier
    Sep 15, 2016 at 21:31
  • Try printing d to see what is being pulled through from your tool dialog. I think you will find it is a string (you are using GetParameterAsText) of "5 Kilometers" and not a number as string i.e. "5".
    – PolyGeo
    Sep 15, 2016 at 21:47

1 Answer 1

1

Try printing d to see what is being pulled through from your tool dialog.

I think you will find it is a string (because you are using arcpy.GetParameterAsText()) of "5 Kilometers" and not a number as string i.e. "5".

To see what I am getting at perhaps run this test from any Python IDE:

d = "5 Kilometers"

print type(d)

kmFloat = float(d.split(" ")[0])

print kmFloat
print type(kmFloat)

You should see:

>>> 
<type 'str'>
5.0
<type 'float'>
>>> 
2
  • It works! But, split the string. When the input is in "meters", the output is different. Is possible that arcpy transform all inputs (kilometers, meters, feet, miles) in the units of XY coordinates system?
    – Novvier
    Sep 15, 2016 at 22:19
  • @MarcoUscuchagua Perhaps, I found this poorly documented when I looked just now. If you want to edit your question to improve it with that test case and test output then I can re-test and adjust my answer.
    – PolyGeo
    Sep 15, 2016 at 22:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.