Common VBA geometry calculations in the Field Calculator

Below are some code samples you can use to perform geometric calculations with VBA statements, rather than having the Calculate Geometry command do them for you.

To use the samples, open the Field Calculator, check Advanced, and type the VBA statement in the first text box. Then, type the variable name in the text box directly under the field name.

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

To add the z-value of the start point of a polyline

Dim dblZ As Double

Dim pLine As IPolyline

Dim pPoint as IPoint

Set pLine = [Shape]

Set pPoint = pLine.FromPoint

dblZ= pPoint.Z

!shape.area@hectares!

(or whatever other unit is required)