|
1 | 1 | import logging |
2 | 2 | from dataclasses import dataclass |
3 | 3 | from omega_prime.map import Map, Lane, LaneBoundary |
4 | | -from shapely import LineString, Polygon, simplify |
| 4 | +from shapely import LineString, Polygon, simplify, make_valid |
5 | 5 | import numpy as np |
6 | 6 | from betterosi import LaneClassificationType, LaneClassificationSubtype, LaneBoundaryClassificationType |
7 | 7 | from pyxodr_omega_prime.road_objects.network import RoadNetwork as PyxodrRoadNetwork |
|
17 | 17 | from collections import namedtuple |
18 | 18 | import warnings |
19 | 19 |
|
20 | | - |
21 | 20 | logger = logging.getLogger(__name__) |
22 | 21 |
|
23 | 22 |
|
@@ -352,19 +351,17 @@ def create( |
352 | 351 | lane_idx: int = None, |
353 | 352 | ): |
354 | 353 | if side == "left": |
355 | | - if len(boundary.boundary_line) == 1: |
356 | | - polyline = LineString([boundary.boundary_line[0]] * 2) |
357 | | - else: |
358 | | - polyline = LineString(boundary.boundary_line) |
| 354 | + bl = boundary.boundary_line |
359 | 355 | elif side == "right": |
360 | | - if len(boundary.lane_reference_line) == 1: |
361 | | - polyline = LineString([boundary.lane_reference_line[0]] * 2) |
362 | | - else: |
363 | | - polyline = LineString(boundary.lane_reference_line) |
364 | | - |
| 356 | + bl = boundary.lane_reference_line |
365 | 357 | else: |
366 | 358 | raise ValueError(f"Invalid side '{side}'. Expected 'left' or 'right'.") |
367 | 359 |
|
| 360 | + if len(bl) == 1: |
| 361 | + polyline = LineString([bl[0]] * 2) |
| 362 | + else: |
| 363 | + polyline = LineString(bl) |
| 364 | + |
368 | 365 | if type is None and hasattr(boundary, "lane_xml"): |
369 | 366 | type = cls._extract_lane_boundary_type_from_xml(boundary, side) |
370 | 367 |
|
@@ -402,17 +399,23 @@ class LaneXodr(Lane): |
402 | 399 |
|
403 | 400 | @classmethod |
404 | 401 | def create(cls, lane: PyxodrLane, road: PyxodrRoad, lane_section_id: int, lane_idx: int = None): |
| 402 | + idx = XodrLaneId(road.id, lane.id, lane_section_id) |
405 | 403 | centre_line = getattr(lane, "centre_line", None) |
406 | 404 | if centre_line is None or not len(centre_line): |
407 | 405 | raise ValueError(f"Lane {lane.id} has no centre_line") |
408 | 406 |
|
409 | | - centerline_2d = lane.centre_line[:, :2] |
| 407 | + centerline = LineString(centre_line[:, :2]) |
| 408 | + if not centerline.is_valid: |
| 409 | + centerline = make_valid(centerline) |
| 410 | + warnings.warn( |
| 411 | + f"Needed to make centerline of lane {idx} valid. Most likely, because the OpenDRIVE geometry is translated to a zero length polyline. Try to decrease `step_size`." |
| 412 | + ) |
| 413 | + |
410 | 414 | lane_type, lane_subtype = cls._determine_lane_type_and_subtype(lane, road) |
411 | | - idx = XodrLaneId(road.id, lane.id, lane_section_id) |
412 | 415 | return cls( |
413 | 416 | _xodr=lane, |
414 | 417 | idx=idx, |
415 | | - centerline=LineString(centerline_2d), |
| 418 | + centerline=centerline, |
416 | 419 | type=lane_type, |
417 | 420 | subtype=lane_subtype, |
418 | 421 | successor_ids=[ |
|
0 commit comments