Search code examples
javascriptalgorithmleafletgisrectangles

The percentage of the rectangle in the other rectangle on leaflet map


There is a rectangle (we call it A) defined by two map points:

A = [southWest1, northEast1]

We also have a second rectangle:

B = [southWest2, northEast2]

where southWest_i and northEast_i - a point on the map, i.e. couple [lat, lng]

I want to find the best solution for finding the percentage of the rectangle A in the rectangle B.

enter image description here


Solution

  • There is no such thing as "percentage of a polygon inside another polygon". I think you rather mean ratio between the area of a polygon and the intersection of that polygon with a second one.

    So:

    • Calculate the intersection of A and B (let's call this C)
    • Calculate area of B
    • Calculate area of C
    • Divide area of C by area of B

    There are lots of ways to calculate areas and intersections of polygons. If you plan to use Javascript, I suggest you look at TurfJS, specifically at its intersect() and area() methods