0

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.

yoyo
  • 23
  • 7

1 Answers1

0

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...
  • You are a godsend! – yoyo Mar 09 '21 at 22:21
  • Thank you very very much! I am confused about ** ignore_index = True**. If possible please explain this. With np.repeat are you repeating something here? Also, will, it be possible to show all the circles in the graph with the rectangles? – yoyo Mar 09 '21 at 22:26
  • Actually, for a specific circle if that circle intersects many polygons will this be able to capture it? I think all the circles should be for looped and then for each circle, all the polygons should be checked for intersection... – yoyo Mar 10 '21 at 00:23
  • 'ignore_index = True' is there to create normal increasing indexes, and drop those from the initial DataFrame. Without it we would have indexes starting from 0 for each new DataFrame appended. – Adrien Wehrlé Mar 10 '21 at 06:48
  • Each rectangle is checked with all circles, so all combinations are found. If one circle intersects several rectangles, then it will be detected for each of them and added in the DataFrame. – Adrien Wehrlé Mar 10 '21 at 06:49
  • Thank you. But I don't understand how you are finding out all possible combinations of rectangles intersecting with each circle- I am saying that maybe the information is there in results but how do I extract this information? – yoyo Mar 10 '21 at 07:39
  • Thank you ! I wrote a code myself...thanks again without you couldn't have done it ..thanks a billion! – yoyo Mar 10 '21 at 08:10
  • Each rectangle/circle combination corresponds to one line. To find all circles intersecting a given rectangle, just look for the different apparition of that precise polygon in the rectangle column! – Adrien Wehrlé Mar 10 '21 at 22:08