Calculate Field - VB to Python

2501
6
02-13-2011 05:28 AM
AliceDeschamps1
New Contributor
I have a Calculate Field module in a Model that used the following VB expression in ArcGIS v9.0. Now that I am trying to run this same model in V10 it does not work??  

Dim Area as double
Dim pArea as IArea
Set pArea = [shape]
Area = pArea.area/10000

Any idea why it does not work?  Should I be converting to Python so that it works in v10?  Any insight on how to convert the above code to python.

Thank
0 Kudos
6 Replies
MichaelStead
Occasional Contributor III
you don't declare variables as any specific data type in VB.net. probably work just dropping the "as double" part. The python alternative involves way less typing.  Here is Chris Fox's crash course in python geometry:

I have passed along this idea for a future topic/article in some of our publications. I think a brief explination of what is happening in my field calculator expression will really help you in the future if you need to work with Geometries in the field calculator.

When you use the expression !Shape! in the field calculator you are returning the Geometry of the feature, because the geometry is stored in the Shape field. Through python we have defined a Geometry Class and this class defines the properties and methods availble to this Geometry Object.

If you look at the help topic link for the Geometry Class you will see a section for Properties. Under this section you will see properties like, area, extent, lastPoint, pointCount, etc. Each property has a brief description and a data type. To return a reference to the value in these properties you just add a "." after the reference to the Geometry, !Shape!, and then type the property name.

!Shape!.area
##returns the area value expressed as a date type double

!Shape!.pointCount
##returns the number of points/vertices in the feature expressed as a date type Integer

Now notice how some properties have a data type of Point or Extent. If you look in the tree view of the same help topic you will find corresponding topics for the Point Class or Extent Class. For example take the property firstPoint that has a data type of Point. What this means is if we write something like:

!Shape!.firstPoint

We will return a reference to the Point who has its own properties which are defined within the Point Class help topic. If you look in that help topic you will see the following properties, ID, X, Y, Z, M. So finally if we write the expression:

!Shape!.firstPoint.Z

Where:

!Shape! returns the Geometry
firstPoint returns the first Point in the Geometry
Z returns the Z value of the first Point in the Geometry as a Double

If you apply this knowledge in navigating the help documentation you can really utilize the full functionality of working with Geometries in the field calculator.
Chris Fox
Development Technical Lead
User Advocacy Group
Esri Support Services
0 Kudos
AliceDeschamps1
New Contributor
The following expression worked using Python.  This calculates Area in hectares from a file in UTM projection.

!Shape!.area/10000

Thanks.

Alice
0 Kudos
BruceLang
Occasional Contributor III
ArcEditor 10.0 SP2, Windows XP Pro SP3

We will return a reference to the Point who has its own properties which are defined within the Point Class help topic. If you look in that help topic you will see the following properties, ID, X, Y, Z, M. So finally if we write the expression:

!Shape!.firstPoint.Z


My question is similar - I'm attempting to get the X,Y values of a shape using "Calculate Field" in a model which converts an annotation geodatabase feature into a shapefile point file.  The calculate field properties are:

  • Input Table: Work_anno

  • Field Name: Longitude

  • Expression: Output

  • Expression Type: VB

  • Code Block: (as follows...)

Dim Output As Double
Dim pArea As IArea
Set pArea = [Shape]
Output = pArea.Centroid.X


I'm having trouble converting this to an "Expression Type" of PYTHON or PYTHON_9.3 - Using something like the following in "Expression" or the "Code Block":
!SHAPE!.firstPoint.Z


I'm obviously missing the correct syntax.  Help is appreciated.
0 Kudos
BruceLang
Occasional Contributor III
Additional Information:

The following knowledge base article describes the procedure HowTo: Convert an annotation feature class to a shapefile

But the following error is displayed.

Executing (Calculate Field (2)): CalculateField "\\hcgis1\gis\Arc\Projects\WTH\Work_Area\Work Data.gdb\Work_Anno" Longitude Output VB "Dim Output As Double
Dim pArea As IArea
Set pArea = [Shape]
Output = pArea.Centroid.X"
Start Time: Wed Dec 28 09:08:27 2011
General error executing calculator.
ERROR 999999: Error executing function.
Expected end of statement
Failed to execute (Calculate Field (2)).
Failed at Wed Dec 28 09:08:30 2011 (Elapsed Time: 3.00 seconds)


The same error displays when attempting the calculation; (1) directly on the field (i.e. Field Calculator) in an edit session, and (2) With the "Calculate Field" tool in my model - FYI the model "used" to work at 9.3.1.

Thanks in advance.
0 Kudos
RichardFairhurst
MVP Honored Contributor
Additional Information: 

The following knowledge base article describes the procedure   HowTo: Convert an annotation feature class to a shapefile

But the following error is displayed. 

Executing (Calculate Field (2)): CalculateField "\\hcgis1\gis\Arc\Projects\WTH\Work_Area\Work Data.gdb\Work_Anno" Longitude Output VB "Dim Output As Double
Dim pArea As IArea
Set pArea = [Shape]
Output = pArea.Centroid.X"
Start Time: Wed Dec 28 09:08:27 2011
General error executing calculator.
ERROR 999999: Error executing function.
Expected end of statement
Failed to execute (Calculate Field (2)).
Failed at Wed Dec 28 09:08:30 2011 (Elapsed Time: 3.00 seconds)


The same error displays when attempting the calculation; (1) directly on the field (i.e. Field Calculator) in an edit session, and (2) With the "Calculate Field" tool in my model - FYI the model "used" to work at 9.3.1. 

Thanks in advance.


You cannot do geometry calculations with VB Script at ArcGIS 10. VBA is no longer available in the field calculator and shape variables are not supported in VB Script. (VBA and VB Script are similar, but have key differences that prevent you from doing geometry calculations with VB Script at ArcGIS 10).

You have to convert to Python to do geometry calculations in the field calculator. Because you are using Centroid you cannot access the coordinate with the FirstPoint option. For Area based objects you probably need to use Centroid (at least for something odd like Annotation, it might work with a true Polygon, which has a starting point).

!Shape.Centroid.X!

See the field calculator help here for more on geometry calculation in ArcGIS 10. Also the Python syntax was incorrect in the post above. The exclamations points go around the Shape qualifiers as shown above. So !Shape.FirstPoint.X! might also work, but it would not return the same result as your original VBA calculation, since that is returning the Centroid.
0 Kudos
BruceLang
Occasional Contributor III


!Shape.Centroid.X!

See the field calculator help here for more on geometry calculation in ArcGIS 10.  Also the Python syntax was incorrect in the post above.  The exclamations points go around the Shape qualifiers as shown above.  So !Shape.FirstPoint.X! might also work, but it would not return the same result as your original VBA calculation, since that is returning the Centroid.


Excellent!  Using !Shape.Centroid.X! for longitude, and !Shape.Centroid.Y! for latitude work perfectly.  Now the calculate field properties (for longitude) in my model are:


  • Input Table: Work_anno

  • Field Name: Longitude

  • Expression: !Shape.Centroid.X!

  • Expression Type: PYTHON_9.3

  • Code Block: (empty)

Now my geoprocessing model for exporting an annotation feature to a points shapefile works as before. :cool:

Thank you.
0 Kudos