I have a code snippet that I can't figure out:

'Define a calculator for NewArea field
    pCalculator = New Calculator
    With pCalculator
        .Cursor = pCursor
        .PreExpression = "Dim dbarea as double" & vbCrLf _
                       & "Dim pArea as IArea" & vbCrLf _
                       & "Set pArea = [Shape]" & vbCrLf _
                       & "dbArea = pArea.area"
        .Expression = "dbArea"
        .Field = "NewArea"
    End With

    'Calculate the field values of NewArea field

What is pCalculator calculating here? What is [Shape] in this context? I'm migrating this old VBA code to Python, and I'm stuck at this segment.

A more general question, what is the significance of the square brackets in the pre-expression? Are they placeholders for the expression?


All this is doing is calculating the area of each feature. You can easily do the same thing with the Python version of the parser with !shape.area!. See the documentation.

Using ArcObjects, I would suggest using an update cursor as in this sample (relevant code posted below): Calculate area geoprocessing function tool

' Create an Update Cursor
indexA = inputFeatureClass.FindField(sField)
Dim updateCursor As IFeatureCursor = inputFeatureClass.Update(Nothing, False)
Dim updateFeature As IFeature = updateCursor.NextFeature()
Dim geometry As IGeometry
Dim area As IArea
Dim dArea As Double

Do While Not updateFeature Is Nothing
    geometry = updateFeature.Shape
    area = CType(geometry, IArea)
    dArea = area.Area
    updateFeature.Value(indexA) = dArea
    updateFeature = updateCursor.NextFeature()

' Release the update cursor to remove the lock on the input data.
  • 1
    Months later I'm back at this exact snippet. This code throws a "General error executing calculator." Do you know why? I'm writing a VB add-in, and this code came from VBA. Is there some syntax issue that VB doesn't like here?
    – Kyle M
    Jul 24 '13 at 3:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.