Line data Source code
1 : /****************************************************************************** 2 : * Project: OpenGIS Simple Features for OpenDRIVE 3 : * Purpose: Definition of OGR driver components for OpenDRIVE. 4 : * Author: Michael Scholz, German Aerospace Center (DLR) 5 : * Gülsen Bardak, German Aerospace Center (DLR) 6 : * 7 : ****************************************************************************** 8 : * Copyright 2024 German Aerospace Center (DLR), Institute of Transportation Systems 9 : * 10 : * SPDX-License-Identifier: MIT 11 : ****************************************************************************/ 12 : 13 : #pragma once 14 : #include "ogrsf_frmts.h" 15 : #include "ogr_api.h" 16 : #include <iostream> 17 : #include <OpenDriveMap.h> 18 : #include <pugixml/pugixml.hpp> 19 : #include <vector> 20 : 21 : struct RoadElements 22 : { 23 : /* Map of road to its original OpenDRIVE ID for fast lookup. */ 24 : std::map<std::string, odr::Road> roads{}; 25 : std::vector<odr::Line3D> referenceLines{}; 26 : 27 : std::vector<odr::Lane> lanes{}; 28 : std::vector<odr::LaneSection> laneSections{}; 29 : std::vector<std::string> laneRoadIDs{}; 30 : std::vector<odr::Mesh3D> laneMeshes{}; 31 : 32 : std::vector<odr::Line3D> laneLinesInner{}; 33 : std::vector<odr::Line3D> laneLinesOuter{}; 34 : 35 : std::vector<odr::RoadMark> roadMarks{}; 36 : std::vector<odr::Mesh3D> roadMarkMeshes{}; 37 : 38 : std::vector<odr::RoadObject> roadObjects{}; 39 : std::vector<odr::Mesh3D> roadObjectMeshes{}; 40 : 41 : std::vector<odr::RoadSignal> roadSignals{}; 42 : std::vector<odr::Mesh3D> roadSignalMeshes{}; 43 : }; 44 : 45 : /*--------------------------------------------------------------------*/ 46 : /*--------------------- Layer declarations -------------------------*/ 47 : /*--------------------------------------------------------------------*/ 48 : 49 : class OGRXODRLayer : public OGRLayer 50 : { 51 : private: 52 1933 : virtual OGRFeatureDefn *GetLayerDefn() override 53 : { 54 1933 : return m_poFeatureDefn.get(); 55 : } 56 : 57 : /** 58 : * Initializes XODR road elements and iterators. 59 : */ 60 : void resetRoadElementIterators(); 61 : 62 : protected: 63 : RoadElements m_roadElements{}; 64 : bool m_bDissolveTIN{false}; 65 : OGRSpatialReference m_oSRS{}; 66 : /* Unique feature ID which is automatically incremented for any new road feature creation. */ 67 : int m_nNextFID{0}; 68 : 69 : std::map<std::string, odr::Road>::iterator m_roadIter{}; 70 : std::vector<odr::Line3D>::iterator m_referenceLineIter{}; 71 : 72 : std::vector<odr::Lane>::iterator m_laneIter{}; 73 : std::vector<odr::LaneSection>::iterator m_laneSectionIter{}; 74 : std::vector<std::string>::iterator m_laneRoadIDIter{}; 75 : std::vector<odr::Mesh3D>::iterator m_laneMeshIter{}; 76 : 77 : std::vector<odr::Line3D>::iterator m_laneLinesInnerIter{}; 78 : std::vector<odr::Line3D>::iterator m_laneLinesOuterIter{}; 79 : 80 : std::vector<odr::RoadMark>::iterator m_roadMarkIter{}; 81 : std::vector<odr::Mesh3D>::iterator m_roadMarkMeshIter{}; 82 : 83 : std::vector<odr::RoadObject>::iterator m_roadObjectIter{}; 84 : std::vector<odr::Mesh3D>::iterator m_roadObjectMeshesIter{}; 85 : 86 : std::vector<odr::RoadSignal>::iterator m_roadSignalIter{}; 87 : std::vector<odr::Mesh3D>::iterator m_roadSignalMeshesIter{}; 88 : 89 : std::unique_ptr<OGRFeatureDefn> m_poFeatureDefn{}; 90 : 91 : /** 92 : * Builds an ordinary TIN from libOpenDRIVE's mesh. 93 : */ 94 : std::unique_ptr<OGRTriangulatedSurface> 95 : triangulateSurface(const odr::Mesh3D &mesh); 96 : 97 : public: 98 : OGRXODRLayer(const RoadElements &xodrRoadElements, 99 : const std::string &proj4Defn); 100 : /** 101 : * \param dissolveTriangulatedSurface True if original triangulated surface meshes from 102 : * libOpenDRIVE are to be dissolved into simpler geometries. 103 : * Only applicable for layer types derived from meshes. 104 : */ 105 : OGRXODRLayer(const RoadElements &xodrRoadElements, 106 : const std::string &proj4Defn, 107 : const bool dissolveTriangulatedSurface); 108 : void ResetReading() override; 109 : }; 110 : 111 : class OGRXODRLayerReferenceLine 112 : : public OGRXODRLayer, 113 : public OGRGetNextFeatureThroughRaw<OGRXODRLayerReferenceLine> 114 : { 115 : protected: 116 : OGRFeature *GetNextRawFeature(); 117 : 118 : public: 119 : const std::string FEATURE_CLASS_NAME = "ReferenceLine"; 120 : 121 : OGRXODRLayerReferenceLine(const RoadElements &xodrRoadElements, 122 : const std::string &proj4Defn); 123 : virtual int TestCapability(const char *pszCap) override; 124 1178 : DEFINE_GET_NEXT_FEATURE_THROUGH_RAW(OGRXODRLayerReferenceLine) 125 : }; 126 : 127 : class OGRXODRLayerLaneBorder 128 : : public OGRXODRLayer, 129 : public OGRGetNextFeatureThroughRaw<OGRXODRLayerLaneBorder> 130 : { 131 : protected: 132 : OGRFeature *GetNextRawFeature(); 133 : 134 : public: 135 : const std::string FEATURE_CLASS_NAME = "LaneBorder"; 136 : 137 : OGRXODRLayerLaneBorder(const RoadElements &xodrRoadElements, 138 : const std::string &proj4Defn); 139 : virtual int TestCapability(const char *pszCap) override; 140 6116 : DEFINE_GET_NEXT_FEATURE_THROUGH_RAW(OGRXODRLayerLaneBorder) 141 : }; 142 : 143 : class OGRXODRLayerRoadMark 144 : : public OGRXODRLayer, 145 : public OGRGetNextFeatureThroughRaw<OGRXODRLayerRoadMark> 146 : 147 : { 148 : protected: 149 : OGRFeature *GetNextRawFeature(); 150 : 151 : public: 152 : const std::string FEATURE_CLASS_NAME = "RoadMark"; 153 : 154 : OGRXODRLayerRoadMark(const RoadElements &xodrRoadElements, 155 : const std::string &proj4Defn, 156 : const bool dissolveTriangulatedSurface); 157 : virtual int TestCapability(const char *pszCap) override; 158 11262 : DEFINE_GET_NEXT_FEATURE_THROUGH_RAW(OGRXODRLayerRoadMark) 159 : }; 160 : 161 : class OGRXODRLayerRoadObject 162 : : public OGRXODRLayer, 163 : public OGRGetNextFeatureThroughRaw<OGRXODRLayerRoadObject> 164 : { 165 : protected: 166 : OGRFeature *GetNextRawFeature(); 167 : 168 : public: 169 : const std::string FEATURE_CLASS_NAME = "RoadObject"; 170 : 171 : OGRXODRLayerRoadObject(const RoadElements &xodrRoadElements, 172 : const std::string &proj4Defn); 173 : virtual int TestCapability(const char *pszCap) override; 174 6071 : DEFINE_GET_NEXT_FEATURE_THROUGH_RAW(OGRXODRLayerRoadObject) 175 : }; 176 : 177 : class OGRXODRLayerRoadSignal 178 : : public OGRXODRLayer, 179 : public OGRGetNextFeatureThroughRaw<OGRXODRLayerRoadSignal> 180 : { 181 : protected: 182 : OGRFeature *GetNextRawFeature(); 183 : 184 : public: 185 : const std::string FEATURE_CLASS_NAME = "RoadSignal"; 186 : 187 : OGRXODRLayerRoadSignal(const RoadElements &xodrRoadElements, 188 : const std::string &proj4Defn, 189 : const bool dissolveTriangulatedSurface); 190 : virtual int TestCapability(const char *pszCap) override; 191 1392 : DEFINE_GET_NEXT_FEATURE_THROUGH_RAW(OGRXODRLayerRoadSignal) 192 : }; 193 : 194 : class OGRXODRLayerLane : public OGRXODRLayer, 195 : public OGRGetNextFeatureThroughRaw<OGRXODRLayerLane> 196 : { 197 : protected: 198 : OGRFeature *GetNextRawFeature(); 199 : 200 : public: 201 : const std::string FEATURE_CLASS_NAME = "Lane"; 202 : 203 : OGRXODRLayerLane(const RoadElements &xodrRoadElements, 204 : const std::string &proj4Defn, 205 : const bool dissolveTriangulatedSurface); 206 : virtual int TestCapability(const char *pszCap) override; 207 4634 : DEFINE_GET_NEXT_FEATURE_THROUGH_RAW(OGRXODRLayerLane) 208 : }; 209 : 210 : /*--------------------------------------------------------------------*/ 211 : /*-------------------- Data source declarations ----------------------*/ 212 : /*--------------------------------------------------------------------*/ 213 : 214 : class OGRXODRDataSource : public GDALDataset 215 : { 216 : private: 217 : std::vector<std::unique_ptr<OGRXODRLayer>> m_apoLayers{}; 218 : 219 : /** 220 : * Approximation factor for sampling of continuous geometry functions into discrete 221 : * OGC Simple Feature geometries. 222 : */ 223 : double m_dfEpsilon{1.0}; 224 : 225 : /** 226 : * Retrieves all necessary road elements from the underlying OpenDRIVE structure. 227 : * 228 : * \param roads Roads of the dataset. 229 : */ 230 : RoadElements createRoadElements(const std::vector<odr::Road> &roads); 231 : 232 : public: 233 : bool Open(const char *pszFilename, CSLConstList openOptions); 234 : 235 510 : int GetLayerCount() override 236 : { 237 510 : return static_cast<int>(m_apoLayers.size()); 238 : } 239 : 240 : OGRLayer *GetLayer(int) override; 241 : 242 : virtual int TestCapability(const char *pszCap) override; 243 : };