| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Project: OpenGIS Simple Features for OpenDRIVE |
| 4 |
* Purpose: Implementation of OGRXODRDataSource. |
| 5 |
* Author: Michael Scholz, German Aerospace Center (DLR) |
| 6 |
* Gülsen Bardak, German Aerospace Center (DLR) |
| 7 |
* |
| 8 |
****************************************************************************** |
| 9 |
* Copyright 2024 German Aerospace Center (DLR), Institute of Transportation Systems |
| 10 |
* |
| 11 |
* SPDX-License-Identifier: MIT |
| 12 |
****************************************************************************/ |
| 13 |
|
| 14 |
#include "ogr_xodr.h" |
| 15 |
using namespace odr; |
| 16 |
using namespace pugi; |
| 17 |
using namespace std; |
| 18 |
|
| 19 |
bool OGRXODRDataSource::Open(const char *pszFilename, CSLConstList openOptions) |
| 20 |
{ |
| 21 |
odr::OpenDriveMap xodr(pszFilename, false); |
| 22 |
pugi::xml_parse_result parse_result = xodr.xml_parse_result; |
| 23 |
if (!parse_result || |
| 24 |
parse_result.status != pugi::xml_parse_status::status_ok) |
| 25 |
{ |
| 26 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 27 |
"OpenDRIVE dataset %s could not be parsed: %s.", pszFilename, |
| 28 |
parse_result.description()); |
| 29 |
return FALSE; |
| 30 |
} |
| 31 |
bool parsingFailed = xodr.xml_doc.child("OpenDRIVE").empty(); |
| 32 |
if (parsingFailed) |
| 33 |
{ |
| 34 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 35 |
"The provided file does not contain any OpenDRIVE data. Is it " |
| 36 |
"empty?"); |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
std::vector<odr::Road> roads = xodr.get_roads(); |
| 41 |
if (roads.empty()) |
| 42 |
{ |
| 43 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 44 |
"OpenDRIVE dataset does not contain any roads."); |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
const char *openOptionValue = CSLFetchNameValue(openOptions, "EPSILON"); |
| 49 |
if (openOptionValue != nullptr) |
| 50 |
{ |
| 51 |
double dfEpsilon = CPLAtof(openOptionValue); |
| 52 |
if (dfEpsilon > 0.0) |
| 53 |
{ |
| 54 |
m_dfEpsilon = dfEpsilon; |
| 55 |
} |
| 56 |
else |
| 57 |
{ |
| 58 |
CPLError(CE_Warning, CPLE_AppDefined, |
| 59 |
"Invalid value for EPSILON specified. Falling back to " |
| 60 |
"default of 1.0."); |
| 61 |
} |
| 62 |
} |
| 63 |
openOptionValue = CSLFetchNameValueDef(openOptions, "DISSOLVE_TIN", "NO"); |
| 64 |
bool bDissolveTIN = CPLTestBool(openOptionValue); |
| 65 |
|
| 66 |
RoadElements roadElements = createRoadElements(roads); |
| 67 |
std::string &proj4Defn = xodr.proj4; |
| 68 |
|
| 69 |
auto refLine = |
| 70 |
std::make_unique<OGRXODRLayerReferenceLine>(roadElements, proj4Defn); |
| 71 |
auto laneBorder = |
| 72 |
std::make_unique<OGRXODRLayerLaneBorder>(roadElements, proj4Defn); |
| 73 |
auto roadMark = std::make_unique<OGRXODRLayerRoadMark>( |
| 74 |
roadElements, proj4Defn, bDissolveTIN); |
| 75 |
auto roadObject = |
| 76 |
std::make_unique<OGRXODRLayerRoadObject>(roadElements, proj4Defn); |
| 77 |
auto lane = std::make_unique<OGRXODRLayerLane>(roadElements, proj4Defn, |
| 78 |
bDissolveTIN); |
| 79 |
auto roadSignal = std::make_unique<OGRXODRLayerRoadSignal>( |
| 80 |
roadElements, proj4Defn, bDissolveTIN); |
| 81 |
|
| 82 |
m_apoLayers.push_back(std::move(refLine)); |
| 83 |
m_apoLayers.push_back(std::move(laneBorder)); |
| 84 |
m_apoLayers.push_back(std::move(roadMark)); |
| 85 |
m_apoLayers.push_back(std::move(roadObject)); |
| 86 |
m_apoLayers.push_back(std::move(lane)); |
| 87 |
m_apoLayers.push_back(std::move(roadSignal)); |
| 88 |
|
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
const OGRLayer *OGRXODRDataSource::GetLayer(int iLayer) const |
| 93 |
{ |
| 94 |
if (iLayer < 0 || static_cast<size_t>(iLayer) >= m_apoLayers.size()) |
| 95 |
return nullptr; |
| 96 |
|
| 97 |
return m_apoLayers[iLayer].get(); |
| 98 |
} |
| 99 |
|
| 100 |
int OGRXODRDataSource::TestCapability(const char *pszCap) const |
| 101 |
{ |
| 102 |
int result = FALSE; |
| 103 |
|
| 104 |
if (EQUAL(pszCap, ODsCZGeometries)) |
| 105 |
result = TRUE; |
| 106 |
|
| 107 |
return result; |
| 108 |
} |
| 109 |
|
| 110 |
RoadElements |
| 111 |
OGRXODRDataSource::createRoadElements(const std::vector<odr::Road> &roads) |
| 112 |
{ |
| 113 |
RoadElements elements; |
| 114 |
|
| 115 |
for (const odr::Road &road : roads) |
| 116 |
{ |
| 117 |
elements.roads.insert({road.id, road}); |
| 118 |
|
| 119 |
elements.referenceLines.push_back( |
| 120 |
road.ref_line.get_line(0.0, road.length, m_dfEpsilon)); |
| 121 |
|
| 122 |
for (const odr::LaneSection &laneSection : road.get_lanesections()) |
| 123 |
{ |
| 124 |
elements.laneSections.push_back(laneSection); |
| 125 |
|
| 126 |
for (const odr::Lane &lane : laneSection.get_lanes()) |
| 127 |
{ |
| 128 |
elements.laneRoadIDs.push_back(road.id); |
| 129 |
|
| 130 |
elements.lanes.push_back(lane); |
| 131 |
|
| 132 |
// laneMesh |
| 133 |
elements.laneMeshes.push_back( |
| 134 |
road.get_lane_mesh(lane, m_dfEpsilon)); |
| 135 |
|
| 136 |
// laneLineOuter |
| 137 |
elements.laneLinesOuter.push_back( |
| 138 |
road.get_lane_border_line(lane, m_dfEpsilon, true)); |
| 139 |
|
| 140 |
// laneLineInner |
| 141 |
elements.laneLinesInner.push_back( |
| 142 |
road.get_lane_border_line(lane, m_dfEpsilon, false)); |
| 143 |
|
| 144 |
const double sectionStart = laneSection.s0; |
| 145 |
const double sectionEnd = road.get_lanesection_end(laneSection); |
| 146 |
for (const odr::RoadMark &roadMark : |
| 147 |
lane.get_roadmarks(sectionStart, sectionEnd)) |
| 148 |
{ |
| 149 |
elements.roadMarks.push_back(roadMark); |
| 150 |
|
| 151 |
// roadMarkMesh |
| 152 |
elements.roadMarkMeshes.push_back( |
| 153 |
road.get_roadmark_mesh(lane, roadMark, m_dfEpsilon)); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
for (const odr::RoadObject &roadObject : road.get_road_objects()) |
| 159 |
{ |
| 160 |
elements.roadObjects.push_back(roadObject); |
| 161 |
|
| 162 |
elements.roadObjectMeshes.push_back( |
| 163 |
road.get_road_object_mesh(roadObject, m_dfEpsilon)); |
| 164 |
} |
| 165 |
|
| 166 |
for (const odr::RoadSignal &roadSignal : road.get_road_signals()) |
| 167 |
{ |
| 168 |
elements.roadSignals.push_back(roadSignal); |
| 169 |
|
| 170 |
elements.roadSignalMeshes.push_back( |
| 171 |
road.get_road_signal_mesh(roadSignal)); |
| 172 |
} |
| 173 |
} |
| 174 |
return elements; |
| 175 |
} |