@@ -1946,11 +1946,33 @@ def check_geometry_overlaps(lop: List[Polygon]) -> bool:
19461946
19471947def check_geometry_disjoint (lop : List [Polygon ]) -> bool :
19481948 """
1949- Returns True if all polygons in 'lop' are disjoint. Returns
1949+ Returns True if any polygons in 'lop' are disjoint. Returns
19501950 False, otherwise.
19511951 """
1952- bool_acc = []
1953- for idx , poly1 in enumerate (lop ):
1954- for poly2 in lop [idx + 1 :]:
1955- bool_acc .append (poly1 .intersection (poly2 ))
1956- return not all (bool_acc )
1952+ # Build polygon connectivity network
1953+ network = {}
1954+ for idx_i , poly1 in enumerate (lop ):
1955+ for idx_j , poly2 in enumerate (lop ):
1956+ if idx_i != idx_j :
1957+ connectivity = network .get (idx_i , set ())
1958+ if poly1 .intersection (poly2 ):
1959+ connectivity .add (idx_j )
1960+ network [idx_i ] = connectivity
1961+
1962+ def walk_network (node : int , network : dict , nodes_visited : list [int ]) -> list [int ]:
1963+ """
1964+ Walks the network modifying 'nodes_visited' as it walks.
1965+ """
1966+ connections = network .get (node , set ())
1967+ for connection in connections :
1968+ if connection in nodes_visited :
1969+ continue
1970+ else :
1971+ nodes_visited .append (connection )
1972+ walk_network (connection , network , nodes_visited )
1973+ return nodes_visited
1974+
1975+ # Traverse polygon connectivity network
1976+ nodes_visited = [0 ]
1977+ walk_network (0 , network , nodes_visited )
1978+ return set (nodes_visited ) != set (network .keys ())
0 commit comments