I have two landcover classifications (as shapefiles) created using eCognition. "Level0 (FID)" consist of larger polygons and "Level1 (FID, ClassName, Area_Pxl)" of smaller polygons within these. I would prefer a solution using arcpy but other solutions will also do (R, geopandas). Script is preferred since I need to repeat this many times.
I will describe my current workflow here emphasizing what is missing:
- Create a new column Level0_ID to Level1
- Done
- Extract ID from Level0
- I need to extract the ID of that segment in Level0 to Level1_ID column when Level0 polygon contains Level1 polygon. What tool should I use?
- Create new field agric_perc to Level0
- Done
- I need to create a loop that goes through the attribute table and does the following function:
- If Level0_ID (in Level1) == ID (in Level0) Then agric_perc (Level0) is agri_perc + Area_Pxl (in Level1)
I think this should do the trick but I am having trouble formulating the script and finding the right tools. I know there is a solution for this where the Level1 shapefile is converted to raster, but I have very large dataset (millions of segments) and original pixel size is very small so the raster solution is very slow and would rather keep the classifications as shapefiles.
What I was thinking looks something like this, but maybe there is better solution:
import arcpy
from arcpy import env
import os
import sys
# Set workspace
arcpy.env.workspace = "C:\HY-Data\RPIIROIN\documents\Rami_opetus\eCognitionWorkshop\DevelopingMethods\PolyPerc"
#Create datareader
Cursor1 = arcpy.UpdateCursor("Level1.dbf")
#Create column Level0_ID to Level1
arcpy.AddField_management(Level1, "Level0_ID", "SHORT", 5, "", "", "Classified", "NULLABLE", "REQUIRED")
#Extract ID from Level0
- How to do this?
#Create column agric_perc to Level0
arcpy.AddField_management(Level0, "agric_perc", "SHORT", 5, "", "", "Classified", "NULLABLE", "REQUIRED")
#Create the loop to update agric_perc column
- How do I create a loop that sums the Level0_ID values from Level1 to Level0 agric_perc column
agric_perc
value should not be updated each time you look at a Level1 polygon, but rather all at once at the end (store the areas in a variable as you are looping through, then use that variable later with an UpdateCursor). I don't have much time at the moment to look at a detailed solution, but hopefully this suggestion either points you in the right direction or somebody else can flesh it out :)