CARLA
Loading...
Searching...
No Matches
OSM2ODR.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#include "osm2odr/OSM2ODR.h"
8
9#include "carla/Logging.h"
10
11#include "osm2odr/Frame.h"
12#include "osm2odr/LaneGraph.h"
13#include "osm2odr/OsmIngest.h"
14#include "osm2odr/Signals.h"
15#include "osm2odr/XodrExport.h"
16
17#include <algorithm>
18#include <stdexcept>
19#include <string>
20
21namespace osm2odr {
22
23namespace {
24
25 // Read a "+key=value" out of a PROJ string. Returns false when absent.
26 bool ProjValue(const std::string &proj, const std::string &key, double &out) {
27 const std::string needle = "+" + key + "=";
28 const auto start = proj.find(needle);
29 if (start == std::string::npos) {
30 return false;
31 }
32 try {
33 out = std::stod(proj.substr(start + needle.size()));
34 } catch (const std::exception &) {
35 return false;
36 }
37 return true;
38 }
39
40 void WarnAboutUnsupported(const OSM2ODRSettings &settings) {
41 if (!settings.tl_excluded_highways_types.empty() &&
44 "Osm2Odr: traffic light exclusions have no effect while traffic light generation is off.");
45 }
46 if (!settings.osm_highways_types.empty()) {
48 "Osm2Odr: osm_highways_types is not honoured yet; the converter's own drivable set "
49 "is used.");
50 }
51 }
52
53} // namespace
54
55 std::string ConvertOSMToOpenDRIVE(std::string osm_file, OSM2ODRSettings settings) {
56 WarnAboutUnsupported(settings);
57
59
60 // The <bounds> of an export is the rectangle the user dragged, but the ways
61 // crossing it come out whole, so measure the nodes that actually arrived.
62 const twinmodel::BoundingBox extent = osm.NodeExtent();
63
64 double origin_lat = 0.0;
65 double origin_lon = 0.0;
66 const bool pinned = ProjValue(settings.proj_string, "lat_0", origin_lat) &&
67 ProjValue(settings.proj_string, "lon_0", origin_lon);
68 if (!pinned) {
69 if (settings.center_map) {
70 origin_lat = (extent.south + extent.north) * 0.5;
71 origin_lon = (extent.west + extent.east) * 0.5;
72 } else {
73 // CARLA's map generation expects the north west corner, which puts the
74 // network in +x / -y.
75 origin_lat = extent.north;
76 origin_lon = extent.west;
77 }
78 }
79 const twinmodel::LocalFrame frame(origin_lat, origin_lon);
80
84 if (osm.bbox.has_value()) {
85 double x0 = 0.0, y0 = 0.0, x1 = 0.0, y1 = 0.0;
86 frame.ToLocal(osm.bbox->west, osm.bbox->south, x0, y0);
87 frame.ToLocal(osm.bbox->east, osm.bbox->north, x1, y1);
88 options.clip = twinmodel::geom::Box(
89 twinmodel::geom::Point(std::min(x0, x1), std::min(y0, y1)),
90 twinmodel::geom::Point(std::max(x0, x1), std::max(y0, y1)));
91 }
92
93 twinmodel::TwinModelData model = twinmodel::BuildLaneGraph(osm, frame, "osm", options);
94 if (model.roads.empty()) {
95 throw std::runtime_error(
96 "Osm2Odr: the OSM data holds no drivable way, nothing to convert.");
97 }
98
99 twinmodel::SignalOptions signal_options;
100 signal_options.generate_traffic_lights = settings.generate_traffic_lights;
102 twinmodel::BuildSignals(osm, frame, model, signal_options);
103
104 if (settings.default_lane_width > 0.0 || settings.default_sidewalk_width > 0.0) {
105 for (twinmodel::Road &road : model.roads) {
106 for (twinmodel::Lane &lane : road.lanes) {
107 if (lane.type == twinmodel::LaneType::Driving && settings.default_lane_width > 0.0) {
108 lane.width = settings.default_lane_width;
109 }
110 if (lane.type == twinmodel::LaneType::Sidewalk && settings.default_sidewalk_width > 0.0) {
111 lane.width = settings.default_sidewalk_width;
112 }
113 }
114 }
115 }
116
117 if (settings.use_offsets) {
118 for (twinmodel::Road &road : model.roads) {
119 for (twinmodel::geom::Point3 &point : road.reference_line) {
120 point.x -= settings.offset_x;
121 point.y -= settings.offset_y;
122 }
123 }
124 }
125
126 return twinmodel::ExportXodr(model, frame);
127 }
128
129} // namespace osm2odr
geom::Point point
Definition LaneGraph.cpp:41
std::map< std::string, unsigned int > road
WGS84 <-> local ENU metres, a transverse Mercator centred on the origin.
Definition Frame.h:15
void ToLocal(double lon, double lat, double &x, double &y) const
Definition Frame.cpp:34
static void log_warning(Args &&... args)
Definition Logging.h:96
std::string ConvertOSMToOpenDRIVE(std::string osm_file, OSM2ODRSettings settings)
Definition OSM2ODR.cpp:55
bg::model::d2::point_xy< double > Point
bg::model::box< Point > Box
std::string ExportXodr(const TwinModelData &model, const LocalFrame &frame)
Serialise the model as an OpenDRIVE 1.6 document.
OsmData ParseOsmXml(const std::string &xml)
Parse an OpenStreetMap XML document.
Definition OsmIngest.cpp:47
void BuildSignals(const OsmData &osm, const LocalFrame &frame, TwinModelData &model, const SignalOptions &options)
Read traffic lights, stop and give-way signs, speed limits and crossings out of the OSM tags and atta...
Definition Signals.cpp:89
TwinModelData BuildLaneGraph(const OsmData &osm, const LocalFrame &frame, const std::string &name, const LaneGraphOptions &options)
OSM ways -> roads, lanes and junctions in model space.
std::string proj_string
Definition OSM2ODR.h:33
std::vector< std::string > osm_highways_types
Definition OSM2ODR.h:45
bool all_junctions_traffic_lights
Definition OSM2ODR.h:42
double default_sidewalk_width
Definition OSM2ODR.h:26
std::vector< std::string > tl_excluded_highways_types
Definition OSM2ODR.h:48
std::optional< geom::Box > clip
Clip the network to this box in model metres, as the Python pipeline clips to the requested bbox.
Definition LaneGraph.h:38
One lane of a road, numbered outward from the reference line.
Definition Model.h:68
BoundingBox NodeExtent() const
Extent of the nodes themselves, used when the file carries no <bounds> or the bounds are smaller than...
Definition OsmIngest.cpp:32
std::optional< BoundingBox > bbox
Definition OsmIngest.h:54
bool all_junctions_traffic_lights
Definition Signals.h:17
Everything the OpenDRIVE writer reads.
Definition Model.h:158
std::vector< Road > roads
Definition Model.h:162