Saturday, 21 February 2015

Using ArcGIS Field Calculator to create classes in new field?


I first classified firstCol into groups and then I want to give the groups with different weighted values in another field, SecondCol. I will need to give different weighted values very often. I find it very time-consuming whenever I modify the weighted values in my script.




My column looks like this:


enter image description here



I use the following script to calculate the SecondCol:


def TextValue(gridcode):
if gridcode <= 5:
return 1
elif 5 < gridcode <= 15:
return 2
elif 15 < gridcode <= 30:
return 3
elif 30 < gridcode <= 40:
return 4

elif 40 < gridcode <= 55:
return 5
elif 55 < gridcode <= 100:
return 6
elif 100 < gridcode :
return 7
else:
return "N/A"

TextValue( !firstCol! )


As you can see, I give the weighted value as below:


(weighted values  ,  group).

1 = group "firstCol < 5"
2 = group "firstCol = 5 - 15"
3 = group "firstCol = 15 - 30"
4 = group "firstCol = 30 - 40"
5 = group "firstCol = 40 - 55"
6 = group "firstCol = 55 - 100"

7 = group "firstCol > 100"



I wonder if there is a better way to give new weighted values more efficiently. Like, just give a new column in the beginning of the script.


61  =   group "firstCol  < 5"   
8 = group "firstCol = 5 - 15"
88 = group "firstCol = 15 - 30"
70 = group "firstCol = 30 - 40"
52 = group "firstCol = 40 - 55"
2 = group "firstCol = 55 - 100"

6 = group "firstCol > 100"

def TextValue( gridcode):
if ...
...
else...

TextValue( !firstCol! )

Then, the script will use the new values, instead of me modifying it in the TextValue.




Answer



If you need MORE control over the class/label name and interval WISELY and want to use ANY type of equality check then a way is to use dictionary that keeps order (i.e. OrderedDict) and eval check as below where you have good command in interval for class and label for a class.To edit class (i.e. 1,2,3 etc.) and interval (i.e. '0 etc.) just edit


{1:'0

See full implementation


from collections import OrderedDict
#Setup class name and interal definition for that class/label- you can change here as you want
lookup_table = OrderedDict({1:'0#Define function that determines the class/label
def labeller(val):

try:
#find the True value for check that is used to determine class/label
chks = [eval(v.replace('x',str(val))) for v in lookup_table.values()]#It is just a true/false list
if True in chks and chks.count(True)==1:
v= lookup_table[chks.index(True)+1]
for ky,vl in lookup_table.iteritems():
if vl == v:
return str(ky)
except:
return 'N/A'


And run it-


labeller(!firstCol!)

N.B. I used OrderedDict for performance - you can use normal dictionary too.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...