Sourav Kumar Agarwal
Sourav Kumar Agarwal

Reputation: 73

Getting the maximum number of (shapely) polygons which have a common intersection among a given set of polygons?

I have a list of multiple polygons. Actually, i have a list of points which i convert into circle polygon using Point buffer. I want to find the maximum number of circles in that list which have a common intersecting area (I do not need the area, but if I can get that it will be helpful).

For example, say there are 4 circles, 3 intersect each other and the fourth one intersects with only one of them (see below image). Is there a function i can write using Shapely that will return me 3 (the maximum number of circles having a common intersection is 3, circles 2, 3 and 4).

enter image description here

Upvotes: 2

Views: 1233

Answers (1)

s-kirby
s-kirby

Reputation: 31

Shapely has a few functions for overlapping polygons:

object.intersects(other) - Returns True if object shares any area with the object other.

object.intersection(other) - Returns a polygon representing the intersecting area between two polygons.

object and other are both shapely geometry objects, which include polygons.


1. Counting Overlaps

Here's a simple solution for counting overlaps, where Polylist is your list of Polygons:

most_intersections = 0
for poly in PolyList: 
    touching = 0
    for sub_poly in PolyList:
        if poly.intersects(sub_poly):
            touching += 1
    
    if touching > highest_touch_ct:
        highest_touch = touching

There are better ways to iterate over the data, depending on your set size.


2. Overlap Area

If you want to measure the overlap between polygon A and all other polygons, you could use this:

intersect_area = 0
for poly in PolyList: 
    if poly.intersects(sub_poly):
        # Create a polygon where the two overlap, then save the area.
        overlap = (poly.intersection(sub_poly))
        intersect_area += overlap.area
    

Really hope this helps, and I'm happy to answer any questions. I'm new to Stack Overflow, so let me know if I left anything out!

Upvotes: 1

Related Questions