Monday, April 5, 2010

Geometric Field Calculations

There are a number of common geometric field calculations that I use on a regular basis.  Calculating polygon area & primiter, linear/arc/path distance, or feature centroid coordinates are surprisingly simple to obtain and can prove to be very powerful information.

If you're working in a geodatabase, many of these will be automatically calculated and updated as features are edited, however these must be re-calculated using the Field Calculator when working with shapefiles.  Here's how:
  • Open a layer's attribute table
  • From the Options menu at the bottom of the attribute table window choose Add Field... if some kind of area/length/etc. field does not already exist.  
  • Name the new field and choose Double for its type.

    Again, if the layer is in a geodatabase, the SHAPE_AREA and SHAPE_LENGTH fields will be available and updated.  These fields will be present in a shapefile format if they were exported from a geodatabase coverage, but you will need to re-calculate the values.  Don't bother adding a new field if this is the case - just update the existing fields using the following steps
  • Right click on the appropriate field (of type Double) and choose Field Calculator
  • Check the Advanced option box
  •  Use the following code snippets to calculate the appropriate type of geometric information for each feature.  I'll calculate polygon Area for this example, however I can also calculate perimeter or centroids, etc.

    Paste the code (see below) in the Pre-Logic VBA Script Code box, and type the object (the variable that is created in the first line: in this case it's dblArea) in bottom most text box.  Click OK when finished
  •  That's it!
Below are code snippets for various types of geometric calculations.  Be sure to paste the corresponding object created in the first line (dblArea/dblPerimeter/etc.) into the text box at the bottom of the Field Calculator.

Area
Dim dblArea as Double
Dim pArea as IArea
Set pArea = [shape]
dblArea = pArea.area



Perimeter
Dim dblPerimeter as Double
Dim pCurve as ICurve
Set pCurve = [shape]
dblPerimeter = pCurve.Length



Length
Dim dblLength as Double
Dim pCurve as ICurve
Set pCurve = [shape]
dblLength = pCurve.Length



X-coordinate of a point
Dim dblX As Double
Dim pPoint As IPoint
Set pPoint = [Shape]
dblX = pPoint.X



Y-coordinate of a point
Dim dblY As Double
Dim pPoint As IPoint
Set pPoint = [Shape]
dblY = pPoint.Y



X-coordinate of a polygon centroid
Dim dblX As Double
Dim pArea As IArea
Set pArea = [Shape]
dblX = pArea.Centroid.X



Y-coordinate of a polygon centroid
Dim dblY As Double
Dim pArea As IArea
Set pArea = [Shape]
dblY = pArea.Centroid.Y


Read more in these selected ESRI's articles:
 - Making Field Calculations
 - The Field Calculator Unleashed

No comments: