def _connect_burrow_to_structure(self, contour, structure): """ extends the burrow contour such that it connects to the ground line or to other burrows """ outline = geometry.Polygon(contour) # determine burrow points close to the structure dist = structure.distance(outline) conn_points = [] while len(conn_points) == 0: dist += self.params['burrows/width']/2 conn_points = [point for point in contour if structure.distance(geometry.Point(point)) < dist] conn_points = np.array(conn_points) # cluster the points to detect multiple connections # this is important when a burrow has multiple exits to the ground if len(conn_points) >= 2: dist_max = self.params['burrows/width'] data = cluster.hierarchy.fclusterdata(conn_points, dist_max, method='single', criterion='distance') else: data = np.ones(1, np.int) burrow_width_min = self.params['burrows/width_min'] for cluster_id in np.unique(data): p_exit = conn_points[data == cluster_id].mean(axis=0) p_ground = curves.get_projection_point(structure, p_exit) line = geometry.LineString((p_exit, p_ground)) tunnel = line.buffer(distance=burrow_width_min/2, cap_style=geometry.CAP_STYLE.flat) # add this to the burrow outline outline = outline.union(tunnel.buffer(0.1)) # get the contour points outline = regions.get_enclosing_outline(outline) outline = regions.regularize_linear_ring(outline) contour = np.array(outline.coords) # fill the burrow mask, such that this extension does not have to be # done next time again cv2.fillPoly(self.burrow_mask, [np.asarray(contour, np.int32)], 1) return contour
def connect_burrow_chunks(self, burrow_chunks): """ takes a list of burrow chunks and connects them such that in the end all burrow chunks are connected to the ground line. """ if len(burrow_chunks) == 0: return [] dist_max = self.params['burrows/chunk_dist_max'] # build the contour profiles of the burrow chunks linear_rings = [geometry.LinearRing(c) for c in burrow_chunks] # handle all burrows close to the ground connected, disconnected = [], [] for k, ring in enumerate(linear_rings): ground_dist = self.ground.linestring.distance(ring) if ground_dist < dist_max: # burrow is close to ground if 1 < ground_dist: burrow_chunks[k] = \ self._connect_burrow_to_structure(burrow_chunks[k], self.ground.linestring) connected.append(k) else: disconnected.append(k) assert (set(connected) | set(disconnected)) == set(range(len(burrow_chunks))) # calculate distances to other burrows burrow_dist = np.empty([len(burrow_chunks)]*2) np.fill_diagonal(burrow_dist, np.inf) for x, contour1 in enumerate(linear_rings): for y, contour2 in enumerate(linear_rings[x+1:], x+1): dist = contour1.distance(contour2) burrow_dist[x, y] = dist burrow_dist[y, x] = dist # handle all remaining chunks, which need to be connected to other chunks while connected and disconnected: # find chunks which is closest to all the others dist = burrow_dist[disconnected, :][:, connected] k1, k2 = np.unravel_index(dist.argmin(), dist.shape) if dist[k1, k2] > dist_max: # don't connect structures that are too far from each other break c1, c2 = disconnected[k1], connected[k2] # k1 is chunk to connect, k2 is closest chunk to connect it to # connect the current chunk to the other structure structure = geometry.LinearRing(burrow_chunks[c2]) enlarged_chunk = self._connect_burrow_to_structure(burrow_chunks[c1], structure) # merge the two structures poly1 = geometry.Polygon(enlarged_chunk) poly2 = regions.regularize_polygon(geometry.Polygon(structure)) poly = poly1.union(poly2).buffer(0.1) # find and regularize the common contour contour = regions.get_enclosing_outline(poly) contour = regions.regularize_linear_ring(contour) contour = contour.coords # replace the current chunk by the merged one burrow_chunks[c1] = contour # replace all other burrow chunks with the same id id_c2 = id(burrow_chunks[c2]) for k, bc in enumerate(burrow_chunks): if id(bc) == id_c2: burrow_chunks[k] = contour # mark the cluster as connected del disconnected[k1] connected.append(c1) # return the unique burrow structures burrows = [] connected_chunks = (burrow_chunks[k] for k in connected) for contour in unique_based_on_id(connected_chunks): contour = regions.regularize_contour_points(contour) try: burrow = Burrow(contour) except ValueError: continue else: if burrow.area >= self.params['burrows/area_min']: burrows.append(burrow) return burrows