29 Apr 2011

Visual Basic - IF THEN Usage in ArcGis Field Calculator

Recently I had to do some statistics on a numerical field in an attribute table and I had to classify this field according to a set of intervals for this purpose. I had to learn that there is no tool for this. So I had to use the fieldcalculator with the appropiate IF THEN VBA-Syntax:

Here is how it can be done (one of many ways):

data: date_long is date in long format, which is aimed
to be clasiffied into 3 classes {<1850; <1900; >= 1900}
values are returned in field date_class which must
be added to the attribute table first.

Open attribute table and choose the
FIELD CALCULATOR:


Box "Pre-Logic VBA Script Code"
(!) check "Advanced" (!)
***********************************************
Dim dc as Integer
IF [date_long] < 1850 THEN
   dc = 1
ElseIf [date_long] < 1900 THEN
   dc = 2
Else dc = 3
EndIf
***********************************************

Box "date_class ="
***********************************************
dc
***********************************************


Resulting attribute table:
***********************************************
date_long    date_class
1945        3
1886        2
1929        3
1927        3
1927        3
1927        3
1927        3
1929        3
1840        1
1939        3
1884        2
1853        2
...        ...
***********************************************

If you just needed graphical representation of classes you sure would have used the symbology and its classifications options but whenever you need the values for further calculations the above or alternative methods using the field calculator will be useful.

No comments :

Post a Comment