2

I have multiple 2D polygons (with solid color) coordinates as a numpy array which are lying on top of each other. I want to calculate the visible are of each polygon.

Example:

    polygon1 = np.array([[0, 461],[1919, 512],[1919, 0],[0, 0]])
    polygon2 = np.array([[0, 420],[ 437, 380],[1057, 350],[1572, 347],[1919, 405],[1919, 639],[0, 639]])

Some part of polygon2 lies on first polygon. How to calculate the total visible area of polygon1 or area of intersection ?

Scott
  • 4,974
  • 6
  • 35
  • 62
Rudesh_Bala
  • 85
  • 10
  • This sounds like a mathematics problem – Artog Sep 06 '19 at 07:40
  • 1
    This may help: https://stackoverflow.com/questions/4229425/how-do-i-find-the-overlapping-area-between-two-arbitrary-polygons – Lewis Sep 06 '19 at 07:47
  • 1
    The Shapely module (https://shapely.readthedocs.io/en/latest/) might be interesting; it has functions for computing intersections between 2d shapes (including nasty cases like non-convex polygons with holes). – Ture Pålsson Sep 06 '19 at 07:52
  • This seems like intersecting rows across two 2D numpy arrays :) – Scott Sep 06 '19 at 07:54
  • Possible duplicate of [Calculate overlapped area between two rectangles](https://stackoverflow.com/questions/27152904/calculate-overlapped-area-between-two-rectangles) – Georgy Sep 07 '19 at 14:14

1 Answers1

4

I want to pre-face my answer by stating that I am confused as to what exactly you are looking for, but you mentioned calculating the area of intersection.

# Import shapely dependencies 
from shapely import geometry, ops

# Polygons - no need for arrays; but if need be, you can use them instead 

polygon1 = geometry.Polygon(((0, 461),(1919, 512),(1919, 0),(0, 0)))
polygon2 = geometry.Polygon(((0, 420), (437, 380), (1057, 350),(1572, 347), (1919, 405), (1919, 639),(0, 639)))

polygon1.intersection(polygon2).area

Obviously, you would have to first install shapely. If for some reason this is not what you want, but you want to find the area of the union - use:

multipoly = geometry.MultiPolygon(polygons = (polygon1, polygon2))
ops.unary_union(multipoly).area
DarkDrassher34
  • 69
  • 3
  • 15