Now that you know about dBase formats, most of the procedures involved for adding and deleting fields, defining field types, and adding and deleting records in ArcView Table documents should be natural and obvious. What is perhaps not so obvious is just how powerful ArcView's Field Calculator is. On this page we show how the Field Calculator can be used to:
| Transform attributes | |
| Obtain properties of features, such as area, length, and coordinates | |
| Save the current selection | |
| Create features based on attributes | |
| Create keys for nested sorts |
In the dark ages of database computing, if you wanted to do some operation on a table of data you had to write computer programs to loop over the records one by one, describing exactly what to do. Getting work done at this level of detail was a difficult, error prone, time consuming process.
Many people approach Table editing in ArcView the same old-fashioned way. You can find dozens, if not hundreds, of scripts on ESRI's ArcScripts pages that are little more than glorified loops around very simple calculations.
Looping and script writing are usually not necessary.
ArcView's Field Calculator
is the key.
It is very simple in concept: it computes values for all selected records in the
active field. But consider this alternative way of looking at it.
The Field Calculator is a computing facility that
| Operates on the Table as a unit (rather than on just a single specified record). | |
| Provides a simple language-based mechanism for accessing field values. | |
| Provides a language (Avenue) having a rich set of computational methods. | |
| Allows for selective computation based on ArcView's built-in selection mechanism. |
It's perhaps easiest to illustrate the Field Calculator's power rather than to describe it abstractly. The following practical examples show what you can do.
Tip: View these pages in your browser on the same computer where you are running ArcView. You can then copy the Avenue expressions (shown in text like this) from your browser directly into the Field Calculator text box; use ctrl-V in the Field Calculator to paste text from the clipboard.
For statistical, mapping, and visualization purposes it is often desirable to alter values of a field mathematically. Perhaps the commonest and most useful is the logarithm, but powers, roots, exponentials, trigonometric, and hyperbolic functions have their places, too. For example, count data are often best analyzed in terms of their square roots. Data shown using graduated symbols usually should be graduated by the square root of the values, too, so that the areas of the symbols become directly proportional to the values shown.
For this exercise, suppose your table has a field [Z] but what you would like to compute with is its logarithm.
[Z].Log(10)
Of course you must replace the "Z" in the Field Calculator expression with the name of the field you want to transform.
Try this exercise on the themes you used in Chapter 9. Think about the additional capabilities it gives you for visualizing data.
The Avenue language contains methods for obtaining properties of geographic features and computing additional properties. For detailed information, search for "Shape (class)" in the help system and follow the pointers to the shape types that interest you.
In this exercise you will compute three new fields in an attribute table for polygonal features. You may try this out on the "Attributes of Clayton County" table used in GTKAV Exercise 15b.
[Shape].ReturnArea
After checking your work, proceed immediately to compute the [X] values (the X coordinates of the "centers" of each feature):
[Shape].ReturnCenter.GetX
After checking your work, compute the [Y] values in a similar fashion:
[Shape].ReturnCenter.GetY
Questions (answer these with appropriate experimentation):
Note the syntax: it's easy to guess the "area" part of "[Shape].ReturnArea" or the "center" and "X" parts of "[Shape].ReturnCenter.GetX". But what about the "Return" and "Get" parts? The distinction is made, unfortunately, on the basis of how ArcView represents objects internally. Essential information (the "attributes") about an object (such as a shape) is stored in RAM. Other information has to be computed from the attributes. So, for example, areas and centers must be computed, but once a center has been computed (it is just a point), ArcView already has its coordinates (X and Y) with no further computation needed. Thus requests for areas and centers use "return" and requests for point coordinates use "get".
This guessing game can be helpful, but in most cases you must simply search the ArcView help system for the proper syntax.
Suppose you have just worked hard doing a geographic analysis resulting in some records in a table being selected. You could save this result (before it is lost) by exporting the table (File|Export), because only the selected records will be exported. This can be inconvenient, since now you have a separate table to deal with.
Instead, this exercise uses the Field Calculator to save the selection as a Boolean (true/false) field within the original table. It assumes the selection is "proper"; that is, some but not all records are selected (otherwise there's not much to remember).
true
false
This procedure makes essential use of the Field Calculator's ability to modify just the selected records. The first calculation sets the boolean field to true for the selected records. The second calculation sets the field values to false for the originally unselected records.
You can now recreate your saved selection at any time with a very simple Query. Remember that query expressions are Avenue statements that result in either a true or false value. Well, a boolean field is either true or false, so its name alone is sufficient. For example, if you named your field "Is good", then the appropriate query to recreate the selection is simply
[Is good]
Note that you do not need the redundant "[Is good] = true". Display your understanding by avoiding such unnecessary constructs.
Frequently, people gather geographic in numerical form. A GPS receiver, for example, gathers sequences of point coordinates. Surveyors can report surveyed points in coordinate form or in range-bearing format (essentially a local polar coordinate system).
In this exercise you will move a collection of features by a fixed amount. (Such operations are useful for changing datums or changing the origin of a coordinate system.)
[Shape].Move(100, -200)
This particular combination (100, -200) of coordinates moved all shapes 100 units to the right and 200 units down. Use whatever combination is appropriate for your application.
In a similar fashion you can actually construct shapes from field values. For example, you can create a grid of rectangles from a spreadsheet of rectangle centers and sizes. These procedures, though, usually require a join to implement correctly, so we will put off a discussion of this advanced technique until all the basic procedures have been learned.
You can easily and quickly sort records in an ArcView table on the values of
the active field using the Sort Up
and
Sort Down
buttons. But what
if you want to do a nested sort? For example, you might want to see the
records arranged by [Township] and then, among each group of common [Township]
values, to see them ordered by [Car_buyers]. The only way to accomplish
this in ArcView is to create a new field containing the combination of these two
(their "concatenation") and then to sort on it. Note that
[Township] is a string field and [Car_buyers] is a numeric field.
[Township] + "#" + [Car_buyers].AsString
You can now sort using [Key] to achieve the desired result.
There are several things of note in this exercise:
| The "#" is a delimiter. It is a character (or sequence of characters) that should not appear in either the [Township] or [Car_buyers] field values. | |
| The "+" operator is not mathematical addition when applied to strings. (It is an "overloaded" operator.) Here, it concatenates (mashes together) two strings. | |
| The ".AsString" request converts numbers to strings. Usually ArcView rounds the numbers to integers. You can change this using the ".SetFormatPrecision" request (look it up in the help system). | |
| You probably did not get exactly the sort order you wanted, because [Key] is sorted alphabetically. For example, "Forest Park-Morrow#14" comes before "Forest Park-Morrow#2". You can fix this by padding the numeric values using a conventional trick: |
[Township] + "#" + (" " + [Car_buyers].AsString).Right(8)
(In this expression, " " contains at least 8 blanks.) This will work provided there are no negative values to deal with.