Monday, 22 August 2016

Returning percentage of area of polygon intersecting another polygon using shapely



Using shapely, 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.



Answer



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

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...