Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

This is a silly question, but I have this 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
    pCalculator.Calculate()

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. Thanks in advance.

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

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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
    updateCursor.UpdateFeature(updateFeature)
    updateFeature.Store()
    updateFeature = updateCursor.NextFeature()
Loop

' Release the update cursor to remove the lock on the input data.
System.Runtime.InteropServices.Marshal.ReleaseComObject(updateCursor)
share|improve this answer
    
Awesome, thank you. –  Kyle M Feb 27 '13 at 3:20
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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