Search code examples
pythonmatplotlibpolygonrectanglesshapely

Draw circles and test overlap with polygons in shapely


I followed Adrien's answer to create a graph that can have multiple polygons.

I plan to create filled-up circles that span across the whole grid in the graph. I want to test if there's overlapping between the circles and the polygons to identify which polygons overlap for each circle. To that end, I have looked at this, but the circles here are cascaded whereas I want separate circles like this.

How do I create circles and test overlap?

Any suggestion would help.


Solution

  • You could do something like that (even if there is always a better solution than nested loops):

    First initialize a DataFrame that we will then fill with combinations of intersected rectangles/circles.

    Then we will loop over the rectangles stored in your_dict and create circles based on positions.

    For each circle, check if it intersects the given rectangle. If so, save the circle geometry in list.

    When all circles have been checked, create a DataFrame with two columns, rectangle where the rectangle geometry is duplicated as many times as the number of intersected circles, and circle, where the circle geometry intersecting the rectangle is stored.

    Finally, append this DataFrame to results.

    results = pd.DataFrame()
    
    for key,your_polygon in your_dict.items():
        
        intersected_circles = []
        
        for x in positions:
        
            for y in positions:
                
                sampleCircle = Point(x,y).buffer(1)
                
                intersect = sampleCircle.intersects(your_polygon)
                
                if intersect:
                    
                    intersected_circles.append(sampleCircle)
                    
        to_append = pd.DataFrame({'rectangle': np.repeat(your_polygon, len(intersected_circles)),
                                 'circle': intersected_circles})
        
        results = results.append(to_append, ignore_index = True) 
        
    

    A snippet of the first 10 lignes of results :

     rectangle                                             circle
    0     POLYGON ((0 7, 4 7, 4 12, 0 12, 0 7))  POLYGON ((2 7, 1.995184726672197 6.90198285967...
    1     POLYGON ((0 7, 4 7, 4 12, 0 12, 0 7))  POLYGON ((2 9, 1.995184726672197 8.90198285967...
    2     POLYGON ((0 7, 4 7, 4 12, 0 12, 0 7))  POLYGON ((4 7, 3.995184726672197 6.90198285967...
    3     POLYGON ((0 7, 4 7, 4 12, 0 12, 0 7))  POLYGON ((4 9, 3.995184726672197 8.90198285967...
    4     POLYGON ((0 7, 4 7, 4 12, 0 12, 0 7))  POLYGON ((6 9, 5.995184726672197 8.90198285967...
    5     POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1))  POLYGON ((8 1, 7.995184726672197 0.90198285967...
    6     POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1))  POLYGON ((8 3, 7.995184726672197 2.90198285967...
    7     POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1))  POLYGON ((8 5, 7.995184726672197 4.90198285967...
    8     POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1))  POLYGON ((8 7, 7.995184726672197 6.90198285967...
    9     POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1))  POLYGON ((8 9, 7.995184726672197 8.90198285967...
    10    POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1))  POLYGON ((10 1, 9.995184726672196 0.9019828596...