ArcMap

Tips and tricks – accessing feature shape in Calculate Field

With the Calculate Field tool, you can easily create expressions that use some property of a feature’s shape, such as length or area. You can also convert length and area to different units. The illustration below shows calculating the MILES field to the length (in miles) of each line feature.

Details about the Expression syntax

The expression syntax:
!shape.length@miles!
starts and ends with the special ‘decode field’ character. This decode field character is different depending on the value of the Expression Type parameter.  For Expression Type of PYTHON or PYTHON_9.3, the character is an exclamation point (“!”),  as shown in the illustration above. For VB, the decode field characters are “[” and “]“.

Inside the decode field characters, you can put the name of any field found on the input table. You can also use the reserved field name “shape” followed by a period and an attribute of the shape. I n this case, we’re using the length attribute: !shape.length!

Finally, we convert shape.length to miles with the use of the special unit conversion syntax ‘@<unit>‘.  <unit> can be any of the following:

CENTIMETERS | DECIMALDEGREES | DECIMETERS | FEET | INCHES | KILOMETERS | METERS | MILES | MILLIMETERS | NAUTICALMILES | POINTS | YARDS

If your shape type is polygon, you can use these areal unit keywords:

ACRES | ARES | HECTARES | SQUARECENTIMETERS | SQUAREDECIMETERS | SQUAREINCHES | SQUAREFEET | SQUAREKILOMETERS | SQUAREMETERS | SQUAREMILES | SQUAREMILLIMETERS | SQUAREYARDS

More things you can do with the shape object and its attributes

The shape field is actually a geometry object with the following properties.

If, for example, you wanted the X-coordinate of the first point of a line, you would use !shape.firstpoint.x!
To find the minimum X-coordinate of the line, use !shape.extent.xmin!

Simple equations

The expression can contain simple equations. For example, to calculate
minutes it would take to traverse a street segment going 30 miles per hour,
use:

!shape.length@miles! / (30.0 / 60.0)

Note that:

!shape.length@miles! / (30 / 60)

won’t work – when doing real arithmetic you need the decimal point after 30 and 60.

Code blocks

See http://www.esri.com/news/arcuser/0507/files/pythonscript.pdf for information on using code blocks in Calculate Field

Next Article

Your Living Atlas Questions Answered

Read this article