Select to view content in your preferred language

Model Builder - Calculate Field (Area)

4866
3
04-18-2014 12:08 PM
MilenaK
Emerging Contributor
Hello,

I'm a totally newbie to Python and have problems to calculate the area of an input feature in my model (model builder).
I use the "Calculate Value" function to calculate the area (in square kilometers or meters) of an input feature (either a raster or a vector feature - should both work?).

I found out that it works like that: !shape.area@squarekilometers!

I have my forumla in the expression block like x/200 and want to calculate the area of myFeature represented by x.
My code block is the following: x = !myFeature.area@squarekilometers!
I also tried the following: x = !%myFeature%.area@squarekilometers!

But I always get an error (ERROR 000989 Python syntax error: Parsing error SyntaxError: invalid syntax (line 1)).

Does anyone know what I'm doing wrong?

Thank you!
Tags (2)
0 Kudos
3 Replies
T__WayneWhitley
Honored Contributor
I've attached a zip file (Toolbox.zip) that should contain a 10.2.x toolbox (Toolbox.tbx) with a contained model (ingeniously named 'Model') - this model contains a Calculate Value tool with the corrected snippet...see further notes below.

- !shape.area@squarekilometers! is for the field calculator or the Calculate Field tool...
- For use with your tool, Calculate Value, the 'SHAPE@' token was used to return geometry with the da search cursor; area in your desired units was returned via the arcpy 'getArea' method.  The code block:
def getArea(features):
     areaTotal = sum([row[0].getArea('PLANAR', 'SQUAREKILOMETERS') for row in arcpy.da.SearchCursor(features, ['SHAPE@'])])
     return "%s %s" % (areaTotal, 'Square Kilometers')


- ...and the 'expression' calling the 'getArea' function (notice the proper use of inline variable substitution, where 'myFeatures' is a model parameter [feature layer type] by that name):
getArea("%myFeatures%")



Wayne
0 Kudos
MilenaK
Emerging Contributor
Thank you very much, I could find it out by myself yesterday, but I just tried your model and it worked immediately 🙂 !!
0 Kudos
T__WayneWhitley
Honored Contributor
Very well...one further note about computing geometry measurements--

"PLANAR measurements reflect the projection of geographic data onto the 2D surface (in other words, they will not take into account the curvature of the earth). GEODESIC, GREAT_ELLIPTIC, LOXODROME, and PRESERVE_SHAPE  measurement types may be chosen as an alternative, if desired."
http://resources.arcgis.com/en/help/main/10.2/index.html#//018z00000070000000

I used 'PLANAR' because I usually work with relatively small areas in a projected coord sys; 'GEODESIC' is the default measure type...in other words that was an optional parameter I 'forced' to PLANAR that if you left blank may give you a significantly different figure.  I suspect it would have to be a large area (smaller areas should yield near equivalent results)...if you want to compare your PLANAR result with the default computation, open the model and remove that param in the code block, save and rerun the model and note the results.

Go ahead and mark this thread answered if you're satisfied it's been closed to your satisfaction.

-Wayne
0 Kudos