def snap_points(self, points): last_way = None snapped_points = [] last_distance = None # headings before snapping headings = [] for i, p in enumerate(points): if i < len(points) - 1: next_point = points[i + 1] headings.append( Route.get_heading(p.y, p.x, next_point.y, next_point.x)) headings.append(headings[-1]) h_diff = [] for point_index in range(len(points)): point = points[point_index] heading = headings[point_index] # if last_way is not None: # projection = last_way.project(point) # distance = last_way.distance(point) # if 0 <= projection <= 1 and distance < 1.1 * last_distance: # snapped_point = last_way.interpolate(projection) # last_distance = distance # snapped_points += [snapped_point] # continue last_way = self.get_closest_way_with_heading(point, heading) # last_way = self.get_closest_way(point) projection = last_way.project(point) # last_distance = last_way.distance(point) snapped_point = last_way.interpolate(projection) h = WaySet.get_linestring_heading_at_projection( last_way, projection) h_diff += [headings[point_index] - h] snapped_points += [snapped_point] # plt.plot(h_diff) # plt.show() # headings after snapping headings = [] for i, p in enumerate(snapped_points): if i < len(snapped_points) - 1: next_point = snapped_points[i + 1] headings.append( Route.get_heading(p.y, p.x, next_point.y, next_point.x)) headings.append(headings[-1]) return Route.from_points(snapped_points, headings)
def get_linestring_heading_at_projection(linestring, projection): ps = [ linestring.project(Point(c), normalized=True) for c in linestring.coords ] for prev_c, c in zip(linestring.coords[:-1], linestring.coords[1:]): p = linestring.project(Point(c)) if p > projection: return Route.get_heading(prev_c[1], prev_c[0], c[1], c[0]) prev_c = linestring.coords[-2] c = linestring.coords[-1] return Route.get_heading(prev_c[1], prev_c[0], c[1], c[0])
def snap_point_to_lines(self, point, next_point, priority_line_index): distances = [line.distance(point) for line in self.multiline] if priority_line_index > -1: distances[priority_line_index] /= 2 min_distance = min(distances) i_dist = distances.index(min_distance) projection = self.multiline[i_dist].project(point) next_proj = 1 if next_point is None else self.multiline[ i_dist].project(next_point) new_point = self.multiline[i_dist].interpolate(projection) new_next_point = self.multiline[i_dist].interpolate(next_proj) heading = Route.get_heading(new_point.y, new_point.x, new_next_point.y, new_next_point.x) return new_point, i_dist, heading
def __init__(self, lines): self.multiline = MultiLineString(lines) self.headings = [ Route.get_heading(l.coords[0][1], l.coords[0][0], l.coords[1][1], l.coords[1][0]) for l in lines ]