Select to view content in your preferred language

GetCellValue error: invalid literal for float()

7332
6
01-16-2016 02:26 PM
by Anonymous User
Not applicable

I'm trying to Get Raster Cell Value iterating a row selection over a point feature class and passing the coordinates as a string like this "1590388,53 4881616,56". But i receive this error:

Traceback (most recent call last):

  File "C:\Users\ivn\Desktop\sir\getcellval.py", line 7, in <module>

    Val = float(CellVal.getOutput(0))

ValueError: invalid literal for float(): 3,5872333

This is the script:

import arcpy

rs = arcpy.GetParameterAsText(0) #Input Raster

X_Y = arcpy.GetParameterAsText(1) #Coordinates of point

CellVal = arcpy.GetCellValue_management(rs, X_Y, "1")

Val = float(CellVal.getOutput(0))

0 Kudos
6 Replies
DanPatterson_Retired
MVP Emeritus

Get Cell Value—Help | ArcGIS for Desktop

shows using int and it appears it is using local formatting.  have you had issues with the internationalization of decimal formating in the past with the spatial analyst functions.

import arcpy

result = arcpy.GetCellValue_management("C:/data/rgb.img", "480785 3807335", "2;3")

cellSize = int(result.getOutput(0))

print(cellSize)

by Anonymous User
Not applicable

I receive this error for  int:

>>> Val = int(CellVal.getOutput(0))

Runtime error

Traceback (most recent call last):

  File "<string>", line 1, in <module>

ValueError: invalid literal for int() with base 10: '3,5872333'

0 Kudos
DanPatterson_Retired
MVP Emeritus

did you just try printing it out, before doing any conversion?  It would be nice to see if it is even convertable to an integer or a string.  for example, you can search for ValueError invalid literal for int() as an error message.  There are lots of possiblities depending on what your print statement returns.

For example arcgis desktop - What does ValueError: invalid literal for int() with base 10 indicate? - Geographic...

NeilAyres
MVP Alum

Are you sure that the point XY as typed in, is within the extent of the raster?

0 Kudos
XanderBakker
Esri Esteemed Contributor

This sounds like a case of regional settings. Your result (as a string) contains a comma as decimal (regional settings), but Python will need a point to be able to convert it to float.

Try this:

Val = float(CellVal.getOutput(0).replace(',','.'))

0 Kudos
DanPatterson_Retired
MVP Emeritus

22.2. locale — Internationalization services — Python 2.7.11 documentation

It is possible to internationalize if more than a quick substitution is needed for the decimal delimiter.  The web is aghast with examples .... most use the old replace approach unless you have to do this all the time