0

I would like to calculate an area of the intersection of a shapely polygon, and a mask (numpy.array):

plt.plot(*building.exterior.xy, color='brown', linewidth=4)
plt.plot(*parcelle.exterior.xy, color='orange', linewidth=4)
plt.imshow(thr, origin='upper', extent=[x_left, x_right, y_bottom, y_top], cmap='Greys')

enter image description here

I would like to be able to calculate the green area below :

enter image description here

For this, I tried using the method described here : How to transform contours obtained from OpenCV to SHP file polygons?

mask_to_polygons(np.flip(thr, 0))

enter image description here

plt.plot(*parcelle.exterior.xy)
plt.plot(*building.exterior.xy)

mask_pol = translate(mask_to_polygons(np.flip(thr, 0)), x_left, y_bottom)
for geo in mask_pol:
    plt.plot(*geo.exterior.xy, color = 'green')

However, the result is not satisfactory : enter image description here

Indeed, the bounds of the MultiPolygon mask_pol are not the dimensions of the thr array :

> mask_pol.bounds
(0.0, 0.0, 511.0, 511.0)
> thr.shape
(256, 256)

So, I'm wondering if the option to switch from mask to multipolynom shapely is the right one. I could also try to switch from shapely polynomials to masks. Are there better methods to accomplish this task?

chambeeee
  • 95
  • 9

1 Answers1

0

After several tries, I came to the conclusion that the easiest way is to create a mask from the shapely polygon, this using rasterio. It is then necessary to carry out meter - pixel conversions to obtain the total area.

chambeeee
  • 95
  • 9