Wednesday, 10 February 2016

python - Compute total area of several polygons within a polygon


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.



Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...