5

I have joined a dataset containing the polygons of green spaces, with a dataset containing vegetation polygons using intersect. The result is a dataset with separate entries for each vegetation polygon within the green spaces. Looking like this:

Situation now

What I want is the total area of each vegetation type within each of the green spaces, similar to this (this table came from a trial using the tabulate areas, right format but wrong areas):

End Result

What would be the best way to achieve that, using python? The area would have to be calculated for each vegetation type (GRIDCODE) within each Green space. The areas are then meant to be attached to my original dataset containing a lot more information about the green spaces. I've tried using a loop to compute it separately for each row, but did not get it to work.

2 Answers 2

2

If you want one single row for each green space in your summary table, you will need indeed Python. The following code should make it (don't forget to adapt the path to your input joined feature class and output summary table of course):

import arcpy, os
arcpy.env.overwriteOutput = 1

joined_fc = r"path_to_your_joined_fc"
summary_table = os.path.join(r"C:\test.gdb", "Summary")

# Create the summary table
arcpy.CreateTable_management(r"C:\test.gdb", "Summary")
fields = [["GreenSpace_ID", "LONG"], ["Trees", "DOUBLE"], ["Bushes", "DOUBLE"], ["Grass", "DOUBLE"]]
for f in fields:
    arcpy.AddField_management(summary_table, f[0], f[1])

# Make a list with unique Greenspace IDs
with arcpy.da.SearchCursor(joined_fc, "FID_GreenSpace") as cursor:
    GreenSpaceList = sorted({row[0] for row in cursor})

# Loop over the greenspaces and populate the summary table with the sum of each category of vegetation
with arcpy.da.InsertCursor(summary_table, [f[0] for f in fields])as InCur:
    for GreenSpace in GreenSpaceList:
        # Assign the correct codes to the corresponding vegetation type, here I assume Trees = 1, Bushes = 2, Grass = 3
        SumTrees = sum([row[0] for row in arcpy.da.SearchCursor(joined_fc, "Shape@AREA", """FID_GreenSpace = {} AND GRIDCODE = 1""".format(GreenSpace))])
        SumBushes = ([row[0] for row in arcpy.da.SearchCursor(joined_fc, "Shape@AREA", """FID_GreenSpace = {} AND GRIDCODE = 2""".format(GreenSpace))])
        SumGrass = sum([row[0] for row in arcpy.da.SearchCursor(joined_fc, "Shape@AREA", """FID_GreenSpace = {} AND GRIDCODE = 3""".format(GreenSpace))])
        InCur.insertRow((GreenSpace, SumTrees, SumBushes, SumGrass))

I assume you work with 10.1 or above (make use of the arcpy.da.InsertCursor()). If you work with an older version of ArcGIS, the code can be easily adapted to use arcpy.InsertCursor() instead.

1
  • 1
    Worked perfectly and I now have my original dataset with my additional columns as intended :)
    – sdots
    Oct 19, 2015 at 11:05
2

You don't really need Python to do this, just the Summary Statistics tool: Use Shape_Area and 'Sum' for the Statistic Field + Statistic type parameter, then FID_GreenSpace and GRIDCODE as Case Fields parameter.

If necessary you can of course script this, check the syntax of the tool in the Help page.

1
  • That has worked fine for getting the area per green space type. What it does not give me yet though is a a format where the areas of the different vegetation types are in different columns, and I have only one row per green space, so I can attach it back to my original data.
    – sdots
    Oct 19, 2015 at 8:51

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.