# Convert GPlinearunit to float in arcpy

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

dX = x + float(d) * math.cos(rad)
dY = y + float(d) * math.sin(rad)
``````

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)))
letters = str(filter(str.isalpha, str(d)))

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

dX = x + dfloat * math.cos(rad)
dY = y + dfloat * math.sin(rad)
``````
• 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. 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). 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

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

print kmFloat
print type(kmFloat)
``````

You should see:

``````>>>
<type 'str'>
5.0
<type 'float'>
>>>
``````
• 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? 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