Using the shapely library, suppose I have a shp file V and another shp file Z. For each polygon v in V, and for each polygon z in Z, I would like to know what percentage of the area of z falls within (intersects) v.


Next code:

import fiona
from shapely.geometry import shape

path1 = '/home/zeito/pyqgis_data/polygon1.shp'
path2 = '/home/zeito/pyqgis_data/polygon8.shp'

polygon1 = fiona.open(path1)
polygon8 = fiona.open(path2)

geom_p1 = [ shape(feat["geometry"]) for feat in polygon1 ]
geom_p8 = [ shape(feat["geometry"]) for feat in polygon8 ]

for i, g1 in enumerate(geom_p1):
    for j, g8 in enumerate(geom_p8):
        if g1.intersects(g8):
            print i, j, (g1.intersection(g8).area/g1.area)*100

for this situation:

enter image description here

it produces indexes and area percentages (related to polygon1 area) for each feature in polygon8.

0 1 22.1984674733
0 4 2.79999613843
0 6 12.7848188532

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