0

Actually this is a Python in GIS, so I use table in my Arcgis and try to count the field and divided it by using category.

I have Field named Elevation the data contain integer example :
1 - 2
3 - 6
2 - 3
8.5 - 12
11 - 12

I need to categorize it using rule that
if Elevation < 1 then Index = 0.3 ,if Elevation = 2 - 3 Index = 0.6, if Elevation > 3 Index = 1

I have this code :

def Reclass( Elevation ):
    r_min, r_max = (float(s.strip()) for s in Elevation.split('-'))
    print "r_min: {0}, r_max: {1}".format(r_min,r_max)
    if r_min < 1 and r_max < 1:
        return 0.333
    elif r_min >= 1 and r_max >= 1 and r_min <= 3 and r_max <= 3:
        return 0.666
    elif r_min > 3 and r_max > 3:
        return 1
    elif r_min <= 3 and r_max > 3:
        return 1
    else:
        return 999

my question is how to strip it, and categorized it using my rule above? Thanks before

Rikudo Pain
  • 441
  • 9
  • 22
  • 1
    Conventionally, only classes should start with a capital letter in Python. *shakes fist at ESRI* Also, your last `elif` could just be an `else`. Your function appears to return the value you desire, so I am assuming that your actual problem has to do with the code calling `Reclass`. I believe we will need a clearer description of the problem you're facing. Your question might also be specific to the tools you're using (namely the ArcGIS library, which I suspect is arcpy). You might have more luck getting an answer at http://gis.stackexchange.com/ if so. – jpmc26 Feb 07 '14 at 03:30
  • 1
    `6-12` and `11-12`. Is that really correct? – thefourtheye Feb 07 '14 at 03:33
  • Actually I think my problem is in my code the field that I want to categorize field that contain Integer String Integer, I think I need to strip 1st part (part not letter) integer and Last part integer (ex: 11 - 12, Strip = 11 and 12) count the Range ex: 11,12 and then do For function, For Number in Range insert the rule and result an output. but I dont know how to apply that in python – Rikudo Pain Feb 07 '14 at 03:36
  • @RikudoPain You mean it has `"3-6"`, not a number in the range of 3 and 6 – thefourtheye Feb 07 '14 at 03:38
  • "3 - 6" is elevation data, it means the elevation from 3 meters until 6 meters. and yeah there are a "space" there "3[space]-[space]6". – Rikudo Pain Feb 07 '14 at 03:40

2 Answers2

1

Based on comments, your field is a string that contains ranges of the form you describe above.

Firstly, this is horrible database design. The minimum and maximum should be separate columns of integer types. shakes fist at ESRI more for discouraging good database design

Furthermore, your rule is insufficient for dealing with a range. A range check would either need to compare against either 1 end of the range or both ends. So you will have to clarify exactly what you want for your "indexing" rule.

Given that you have strings representing ranges, your only option is to parse the range into its minimum and maximum and work with those. That's not too hard in Python:

>>> r = "3 - 6"
>>> r_min, r_max = (int(s.strip()) for s in r.split('-'))
>>> r_min
3
>>> r_max
6

What does this do?

It's pretty simple, actually. It splits the string by the -. Then it loops over the resulting list, and each element has its leading and trailing whitespace removed and is then converted into an int. Finally, Python unpacks the generator on the right to fill in the variables on the left.

Be aware that malformed data will cause errors.

Once you've clarified your "index" rule, you can figure out how to use this minimum and maximum to get your "index".

jpmc26
  • 28,463
  • 14
  • 94
  • 146
  • yeah that's what I want, now my problem is it gets error when I run it in arcGIS, I change part 'r = "3 - 6"' with 'r = "Elevation"' and 'r = !Elevation!' – Rikudo Pain Feb 07 '14 at 04:08
  • @RikudoPain What I've posted is pure Python. `Elevation` is probably your column name. You'll need a function to extract the column value from a row for this to work at all. ...Are you working in a script or some ArcMap/ArcCatalog dialog box? – jpmc26 Feb 07 '14 at 04:10
  • I work it in Arcmap dialog box, using field calculator. It's for generating a field data based on a script I Input. – Rikudo Pain Feb 07 '14 at 04:14
0

I have borrowed code from you and @jpmc26 below. This code (minus the print statements that are just there for testing) should work for you in the Field Calculator of ArcMap but it is simply Python code. The problem is that you have not told us what you want to do when the two ends of a range fall into different categories so for now I have used an else statement to put out 999.

def Reclass( Elevation ):
    r_min, r_max = (float(s.strip()) for s in Elevation.split('-'))
    print "r_min: {0}, r_max: {1}".format(r_min,r_max)
    if r_min < 1 and r_max < 1:
        return 0.333
    elif r_min >= 1 and r_max >= 1 and r_min <= 3 and r_max <= 3:
        return 0.666
    elif r_min > 3 and r_max > 3:
        return 1
    else:
        return 999

print Reclass("0 - 1.1")
print Reclass("5.2 - 10")
print Reclass("2 - 3")
print Reclass("0 - 0")
PolyGeo
  • 1,340
  • 3
  • 26
  • 59
  • Yes, It works Great. For condition when the two ends of a range fall into different categories, I use r_max value to define. By the way I just realize that some field are null, can I set the null value to be automatic categorize as 0.333? – Rikudo Pain Feb 07 '14 at 06:10
  • Try using `if Elevation is None` to test for that - as per http://stackoverflow.com/questions/3289601/null-object-in-python – PolyGeo Feb 07 '14 at 06:59
  • I've changed all the null value to 0 - 0, but it gets error when i run your command, result are some calculated, and some dont. – Rikudo Pain Feb 07 '14 at 07:04
  • I've added another test value of "0 - 0" (see my edited Answer) and it returns the expected answer of 0.333 for me. – PolyGeo Feb 07 '14 at 07:10
  • It's not working in Field calculator, when I calculate it, it's just half of the column processed and gets error Please help.. – Rikudo Pain Feb 07 '14 at 07:15
  • Oh, I'm sory there are decimal data "8.5 - 10" there I've check the geoprocesing result. it gets error there how should I fix it without changing the decimal data? – Rikudo Pain Feb 07 '14 at 07:24
  • I just changed `int` to `float` in my code which should fix it. – PolyGeo Feb 07 '14 at 08:56
  • I got error, when i run this code in pyton windows it's run great but in field calculator gets error. edited question error log – Rikudo Pain Feb 10 '14 at 01:30
  • If the code works in Python then I think the Question which is on-topic for Stack Overflow is resolved. As soon as you start using ArcGIS' Calculate Field or Field Calculator then you become on-topic for GIS Stack Exchange instead. I think you need to ask this as a new question there and rollback the edit you just made to your Question here. – PolyGeo Feb 10 '14 at 01:37
  • ok, edited question, and post on GIS Stack Exchange. Thank you @Polygeo – Rikudo Pain Feb 10 '14 at 01:58