2

I'm trying to calculate geometry in Python for a script tool for ArcMap.

For the script I want to calculate area in hectares. I am trying to use the add geometry attribute (data management) tool, but it's not working and it's giving me all zeros in my output field. This is what I have:

# Set Calculate geometry variables

calculate_table = clip_output

geometry_prop = "AREA"

expression = "HECTARES"

# Run calculate geometry

arcpy.AddGeometryAttributes_management(calculate_table, geometry_prop, "", expression, "")

What's wrong? Should I use a different tool?

  • 2
    Please edit your question to specify what projection your data is in and whether it is point, line or polygon. – user2856 Oct 31 '16 at 20:04
2

try using keyword arguments for the optional arguments in your call to AddGeometryAttributes:

arcpy.AddGeometryAttributes_management(calculate_table, geometry_prop, Area_Unit=expression)

Also, I'm not sure if this is version-specific, but I had to use "AREA_GEODESIC" as the Geometry_Properties, or else I got an error.

edit to answer the second part of your question:

You can use a search cursor to select the records you're interested in with an SQL query, then add up all the areas into a sum variable, then print it using addMessage. It would look something like this:

with arcpy.da.SearchCursor(clip_output, ['area_field'], 'soil_class in (1, 2)') as cur:
  sum = 0
  for row in cur:
    sum += row[0]

arcpy.AddMessage(sum)
  • Thank you! that worked. I have another question now. To finish off my script tool i want the script to give me an add message (sum) that calculates total hectares for only 2 certain attributes (soil classes 1 and 2). I have a hectares field and a field that give soil classes 1 to 5. in the python language how would i write the expression? could you give me a hypothetical example. – Noah McLaughlin Oct 31 '16 at 20:47
  • my soil_class field is "CLASS" so i replaced that for soil_class but it didn't work. Im probably doing some thing wrong. also my 'area_field' is "Hecatares" – Noah McLaughlin Oct 31 '16 at 21:19
  • 2
    Please do not ask follow-on questions in comments. Instead ask a new question. – Vince Nov 1 '16 at 1:55
  • didnt work error says: Traceback (most recent call last): File "D:\Python\Noah_Assignment1.py", line 106, in <module> for row in cur: RuntimeError: An invalid SQL statement was used – Noah McLaughlin Nov 5 '16 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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