shapely - test if geometry items intersects in python list -
i have 1 list of data follows:
from shapely.geometry import box data = [box(1,2,3,4), box(5,6,7,8), box(1,2,3,4)] codes = ['a','b','c']
the list 'data' has following elements:
a = box(1,2,3,4) b = box(5,6,7,8) c = box(1,2,3,4)
i have check if element intersect other elements. if intersects, should put in 1 tuple; , if not intersect should put in different tuple. expected result is:
result = [(a,c), (b)]
how it?
i tried as:
results = [] p,c in zip(data,codes): x in data: if p.intersects(x): ##.intersects return true if overlap else false results.append(c) print results
keep dict of objects mapped a,b , c, set of matched objects , add single elements have no matches after new letter if not in our matched set possible combinations have been tested:
from shapely.geometry import box itertools import combinations codes = ["a", "b", "c"] d = dict(zip(codes, data)) prev = codes[0] matched, out = set(), [] p1, p2 in combinations(codes, 2): if d[p1].intersects(d[p2]): out.append((p1, p2)) matched.update([p1, p2]) # when p1 new letter, have tried combs prev # if prev not in matched did not intersect other # add single tuple , add matched avoid dupes elif p1 != prev , prev not in matched: out.append(tuple(prev,)) matched.add(prev) prev = p1 # catch last letter if p2 not in matched: out.append(tuple(p2,)) print(out) [('a', 'c'), ('b',)]
Comments
Post a Comment