2

I have the problem that the ArcGIS Field Calculator doesn't read a float number when it is included in Python by referring to the field that contains it, in this way:

def giveResult(floatValue):
  if floatValue>0:
    a=(1000,5000,10000,15000,20000)
    for i in range(len(a)):
      if a[i]/1000>floatValue:
        return a[i]
        break
      else:
        return 3
  else:
    return 0

This code returns only the values 3 and 0, because the line

if a[i]/1000>floatValue:

doesn't work properly. The problem seems to be my European Windows localization settings, where decimals are displayed with commas instead of points. These settings are applied to float fields in ArcGIS. It seems that Field Calculator interprets these floats as tuples because of the comma.

How can this data be handled?

Values, with Short Desending. The lowest value is 0 (as integer, that is, without decimals) Field properties: Float type. The actual name of the field isn't "floatValue", but "OFEhu_tha"


I finally have it, in this way: checking which is the variable type in each case, and only if the type is tuple, we catch the first part (it's enough to build the function); we don't mind if the variable isn't tuple (don't need to catch that condition).

Only that part of the code would be:

  for i in range(len(a)):
      if type(OFEhu)==tuple:
        OFEhu=OFEhu[0]
      if (a[i]/1000)>OFEhu:
        return a[i]
  • 2
    It'd be really nice to actually see a sample of the values passed to floatValue. What data type is the field being passed into floatValue? – ianbroad Oct 8 '14 at 18:27
  • values in the range 0 to 22,275 – Charly Oct 8 '14 at 18:43
  • 1
    is it a string field? – ianbroad Oct 8 '14 at 18:48
  • No, it's a float field. I pass the field value from the Expression textfield in this way: giveResult(!FieldName!) – Charly Oct 8 '14 at 18:55
  • 1
    This sounds like it might be a bug. What version of ArcGIS are you using? – nmpeterson Oct 8 '14 at 18:58
2

Ok, this is clearly an ArcGIS bug (as we've deduced in the comments on @ian's answer). Here's my attempt at a workaround, which assumes there can be either zero or one commas in the number (and therefore, as a result of the bug, you effectively pass the function either a single integer or a tuple of two integers):

def giveResult(*args):
    floatValue = float('.'.join((str(arg) for arg in args)))
    if floatValue > 0:
        a = (1000, 5000, 10000, 15000, 20000)
        for n in a:
            if n / 1000 > floatValue:
                return n
                break
            else:
                return 3
     else:
         return 0
0

A bit of a hack-ish workaround, but you can typecast to str and use string.replace(",", ".") then cast back to float. Would love to comment on the other workaround answer instead, still need 2 more points...

def giveResult(floatValue):
  if floatValue>0:
    a=(1000,5000,10000,15000,20000)
    for i in range(len(a)):
      product = str(a[i]/1000)
      if float(product.replace(",", ".")) > floatValue:
        return a[i]
        break
      else:
        return 3
  else:
    return 0

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.