Calculating area of only selected features without writing to table

2725
5
09-30-2013 11:04 AM
SarahFranklin
New Contributor
Good afternoon,
I'm working on a custom button using the Python GUI and perhaps add-in manager in 10.1 to calculate the area of a selected polygon and then display the result as a popup message box.

I've got several of the pieces together, using an IF loop with GetCount() to verify a record has been selected before proceeding and easygui.msgbox to report the result; however I'm struggling to figure out how to calculate the area of the selection without writing to the attribute table. I don't want to have to create a new feature class or table - I want to allow a user to interactively select a polygon, calculate the geometry on the fly, or in memory, convert the number to acres, if necessary, and use that result as the variable to report in my pop-up.

Any ideas?
Tags (2)
0 Kudos
5 Replies
DaveBarrett
Occasional Contributor
Hi,

You should be able to achieve what you want using the polygon geometry class.

http://resources.arcgis.com/en/help/main/10.1/index.html#/Polygon/018z00000061000000/

You will probable need to use a data access module search cursor to access the Geometry using the SHAPE@ token

Hope it helps,

Dave
0 Kudos
SarahFranklin
New Contributor
Thanks Dave.  That looks like it's what I want.

However, I'm still mentally stuck on populating the polygon object based on a selected feature.  Would you do this using the CopyFeatures_management function?
0 Kudos
DaveBarrett
Occasional Contributor
Copy features should work or you should be able to use the make feature layer tool to create the layer in memory

Dave
0 Kudos
T__WayneWhitley
Frequent Contributor
Yes, and in case you were wondering, there is additional help at the link below - if you scroll nearly to the bottom to the section on 'Outputting geometry objects', you'll see an example script summing length after copying to a geometry obj from a shp - I noticed it copies to in-memory:

Using geometry objects with geoprocessing tools
[Outputting geometry objects]
http://resources.arcgis.com/en/help/main/10.1/index.html#/Using_geometry_objects_with_geoprocessing_...

I was curious if a selection was honored from a layer object fetched from the map and it is - see the attached jpg, I also diddled with the token @acres and I that didn't work for me.  But it's a simple matter to divide by the conversion factor and I did it for each geometry...better to do the sum, then the total conversion if working with more than one feature as I was doing.  The summary from the table matches the figure computed from geom.  Just curious, but wouldn't it be quicker not to copy the geometry but to directly read the geom area via a searchcursor?

Enjoy,
Wayne


EDIT:
If there is no need for anything but area, there is no need to copy geometry to memory - using the data access (da) module and the "SHAPE@AREA" token is way faster.  (I recently installed 10.1 so the da module is new to me)...below is the code summary (no timing included but you can easily include that if you wish - it was just obviously faster to me.)
>>> import arcpy
>>> doc = arcpy.mapping.MapDocument('CURRENT')
>>> lyr = arcpy.mapping.ListLayers(doc)[0]
>>> g = arcpy.Geometry()
>>> geometryList = arcpy.CopyFeatures_management(lyr, g)
>>> acres = 0.0

>>> for geometry in geometryList:
...     acres += (geometry.area)/43560
...     
>>> print acres
776.613255807

>>> 33829273.422968/43560
776.6132558073461



>>> This below is way faster!

>>> area = 0.0

>>> with arcpy.da.SearchCursor("hab60","SHAPE@AREA") as cursor:
...     for row in cursor:
...         area += row[0]
...         
>>> print area
33829273.423
>>> print area/43560
776.613255807
>>> 
0 Kudos
SarahFranklin
New Contributor
I love this thread. I got the code working by saving a new layer and polygon object to memory but it just "felt" bulky.. I also recently just got upgraded to 10.1 so hadn't heard of the da module before, but I'm excited to try it out.

THANK YOU both!!
0 Kudos