Beispiel #1
0
 def _compute_geometry(self):
     return [
         SumoRoadNetwork.buffered_lane_or_edge(
             edge, width=sum([lane.getWidth() for lane in edge.getLanes()])
         )
         for edge in self.edges
     ]
Beispiel #2
0
    def to_geometry(self, road_network: SumoRoadNetwork) -> Polygon:
        def resolve_offset(offset, geometry_length, lane_length,
                           buffer_from_ends):
            if offset == "base" or offset == 0:
                return buffer_from_ends
            # push off of end of lane
            elif offset == "max":
                return lane_length - geometry_length - buffer_from_ends
            elif offset == "random":
                return random.uniform(
                    0, lane_length - geometry_length - buffer_from_ends)
            else:
                return float(offset)

        lane_shapes = []
        edge_id, lane_idx, offset = self.start
        edge = road_network.edge_by_id(edge_id)
        for lane_idx in range(lane_idx, lane_idx + self.n_lanes):
            lane = edge.getLanes()[lane_idx]
            lane_length = lane.getLength()
            geom_length = max(self.length - 1e-6, 1e-6)

            assert lane_length > geom_length  # Geom is too long for lane
            assert geom_length > 0  # Geom length is negative

            lane_shape = SumoRoadNetwork.buffered_lane_or_edge(
                lane, width=lane.getWidth() + 0.3)

            min_cut = resolve_offset(offset, geom_length, lane_length, 1e-6)
            # Second cut takes into account shortening of geometry by `min_cut`.
            max_cut = min(min_cut + geom_length, lane_length - min_cut - 1e-6)

            lane_shape = road_network.split_lane_shape_at_offset(
                Polygon(lane_shape), lane, min_cut)

            if isinstance(lane_shape, GeometryCollection):
                if len(lane_shape) < 2:
                    break
                lane_shape = lane_shape[1]

            lane_shape = road_network.split_lane_shape_at_offset(
                lane_shape,
                lane,
                max_cut,
            )[0]
            lane_shapes.append(lane_shape)

        geom = unary_union(MultiPolygon(lane_shapes))
        return geom