SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NWWriter_OpenDrive.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // Exporter writing networks using the openDRIVE format
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
10 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <ctime>
32 #include "NWWriter_OpenDrive.h"
34 #include <netbuild/NBEdge.h>
35 #include <netbuild/NBEdgeCont.h>
36 #include <netbuild/NBNode.h>
37 #include <netbuild/NBNodeCont.h>
38 #include <netbuild/NBNetBuilder.h>
42 #include <utils/common/StdDefs.h>
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
55 // ---------------------------------------------------------------------------
56 // static methods
57 // ---------------------------------------------------------------------------
58 void
60  // check whether an opendrive-file shall be generated
61  if (!oc.isSet("opendrive-output")) {
62  return;
63  }
64  // some internal mapping containers
65  int edgeID = 0;
66  int nodeID = 0;
67  StringBijection<int> edgeMap;
68  StringBijection<int> nodeMap;
69  //
70  OutputDevice& device = OutputDevice::getDevice(oc.getString("opendrive-output"));
71  device << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
72  device << "<OpenDRIVE>\n";
73  time_t now = time(0);
74  std::string dstr(ctime(&now));
75  const NBNodeCont& nc = nb.getNodeCont();
76  const NBEdgeCont& ec = nb.getEdgeCont();
78  device << " <header revMajor=\"1\" revMinor=\"3\" name=\"\" version=\"1.00\" date=\"" << dstr.substr(0, dstr.length() - 1)
79  << "\" north=\"" << b.ymax() << "\" south=\"" << b.ymin() << "\" east=\"" << b.xmax() << "\" west=\"" << b.xmin()
80  << "\" maxRoad=\"" << ec.size() << "\" maxJunc=\"" << nc.size() << "\" maxPrg=\"0\"/>\n";
81  // write normal edges (road)
82  for (std::map<std::string, NBEdge*>::const_iterator i = ec.begin(); i != ec.end(); ++i) {
83  const NBEdge* e = (*i).second;
84  device << " <road name=\"" << StringUtils::escapeXML(e->getStreetName()) << "\" length=\"" << e->getLength() << "\" id=\"" << getID(e->getID(), edgeMap, edgeID) << "\" junction=\"-1\">\n";
85  device << " <link>\n";
86  device << " <predecessor elementType=\"junction\" elementId=\"" << getID(e->getFromNode()->getID(), nodeMap, nodeID) << "\"/>\n";
87  device << " <successor elementType=\"junction\" elementId=\"" << getID(e->getToNode()->getID(), nodeMap, nodeID) << "\"/>\n";
88  device << " </link>\n";
89  device << " <type s=\"0\" type=\"town\"/>\n";
90  const std::vector<NBEdge::Lane>& lanes = e->getLanes();
91  unsigned int li = (unsigned int)lanes.size() - 1;
92  PositionVector ls = e->getLaneShape(li);
93  try {
94  ls.move2side(-e->getLaneWidth(li) / 2.);
95  } catch (InvalidArgument&) {
96  // we do not write anything, as this should have been reported, already
97  }
98  writePlanView(ls, device);
99  device << " <elevationProfile><elevation s=\"0\" a=\"0\" b=\"0\" c=\"0\" d=\"0\"/></elevationProfile>\n";
100  device << " <lateralProfile/>\n";
101  device << " <lanes>\n";
102  device << " <laneSection s=\"0\">\n";
103  writeEmptyCenterLane(device, "solid", 0.13);
104  device << " <right>\n";
105  for (int j = e->getNumLanes(); --j >= 0;) {
106  device << " <lane id=\"-" << e->getNumLanes() - j << "\" type=\"driving\" level=\"0\">\n";
107  device << " <link>\n";
108  device << " <predecessor id=\"-1\"/>\n"; // internal roads have this
109  device << " <successor id=\"-1\"/>\n"; // internal roads have this
110  device << " </link>\n";
111  device << " <width sOffset=\"0\" a=\"" << e->getLaneWidth(j) << "\" b=\"0\" c=\"0\" d=\"0\"/>\n";
112  std::string markType = "broken";
113  if (j == 0) {
114  markType = "solid";
115  }
116  device << " <roadMark sOffset=\"0\" type=\"" << markType << "\" weight=\"standard\" color=\"standard\" width=\"0.13\"/>\n";
117  device << " </lane>\n";
118  }
119  device << " </right>\n";
120  device << " </laneSection>\n";
121  device << " </lanes>\n";
122  device << " <objects/>\n";
123  device << " <signals/>\n";
124  device << " </road>\n";
125  }
126  device << "\n";
127  // write junction-internal edges (road)
128  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
129  NBNode* n = (*i).second;
130  const std::vector<NBEdge*>& incoming = (*i).second->getIncomingEdges();
131  for (std::vector<NBEdge*>::const_iterator j = incoming.begin(); j != incoming.end(); ++j) {
132  const std::vector<NBEdge::Connection>& elv = (*j)->getConnections();
133  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
134  if ((*k).toEdge == 0) {
135  continue;
136  }
137  const NBEdge::Connection& c = *k;
138  PositionVector shape = c.shape;
139  if (c.haveVia) {
140  shape.append(c.viaShape);
141  }
142  const SUMOReal width = SUMO_const_laneWidth;
143  // @todo: this if-clause is a hack which assures that the code also works with connections of zero length, what may be possible
144  // probably, it would make sense to mark such connections and connect the incoming/outgoing streets directly in such cases.
145  try {
146  shape.move2side(-width / 2.);
147  } catch (InvalidArgument&) {
148  // we do not write anything, maybe we should
149  }
150  device << " <road name=\"" << c.id << "\" length=\"" << shape.length() << "\" id=\"" << getID(c.id, edgeMap, edgeID) << "\" junction=\"" << getID(n->getID(), nodeMap, nodeID) << "\">\n";
151  device << " <link>\n";
152  device << " <predecessor elementType=\"road\" elementId=\"" << getID((*j)->getID(), edgeMap, edgeID) << "\"/>\n";
153  device << " <successor elementType=\"road\" elementId=\"" << getID((*k).toEdge->getID(), edgeMap, edgeID) << "\"/>\n";
154  device << " </link>\n";
155  device << " <type s=\"0\" type=\"town\"/>\n";
156  writePlanView(shape, device);
157  device << " <elevationProfile><elevation s=\"0\" a=\"0\" b=\"0\" c=\"0\" d=\"0\"/></elevationProfile>\n";
158  device << " <lateralProfile/>\n";
159  device << " <lanes>\n";
160  device << " <laneSection s=\"0\">\n";
161  writeEmptyCenterLane(device, "none", 0);
162  device << " <right>\n";
163  device << " <lane id=\"-1\" type=\"driving\" level=\"0\">\n";
164  device << " <link>\n";
165  device << " <predecessor id=\"-" << (*j)->getNumLanes() - c.fromLane << "\"/>\n";
166  device << " <successor id=\"-" << c.toEdge->getNumLanes() - c.toLane << "\"/>\n";
167  device << " </link>\n";
168  device << " <width sOffset=\"0\" a=\"" << width << "\" b=\"0\" c=\"0\" d=\"0\"/>\n";
169  device << " <roadMark sOffset=\"0\" type=\"none\" weight=\"standard\" color=\"standard\" width=\"0.13\"/>\n";
170  device << " </lane>\n";
171  device << " </right>\n";
172  device << " </laneSection>\n";
173  device << " </lanes>\n";
174  device << " <objects/>\n";
175  device << " <signals/>\n";
176  device << " </road>\n";
177  }
178  }
179  }
180 
181  // write junctions (junction)
182  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
183  NBNode* n = (*i).second;
184  device << " <junction name=\"" << n->getID() << "\" id=\"" << getID(n->getID(), nodeMap, nodeID) << "\">\n";
185  unsigned int index = 0;
186  const std::vector<NBEdge*>& incoming = n->getIncomingEdges();
187  for (std::vector<NBEdge*>::const_iterator j = incoming.begin(); j != incoming.end(); ++j) {
188  const std::vector<NBEdge::Connection>& elv = (*j)->getConnections();
189  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
190  if ((*k).toEdge == 0) {
191  continue;
192  }
193  device << " <connection id=\"" << index << "\" incomingRoad=\"" << getID((*j)->getID(), edgeMap, edgeID)
194  << "\" connectingRoad=\"" << getID((*k).id, edgeMap, edgeID) << "\" contactPoint=\"start\"/>\n";
195  ++index;
196  }
197  }
198  device << " </junction>\n";
199  }
200 
201  device << "</OpenDRIVE>\n";
202  device.close();
203 }
204 
205 
206 void
208  device << " <planView>\n";
209  SUMOReal offset = 0;
210  for (unsigned int j = 0; j < shape.size() - 1; ++j) {
211  const Position& p = shape[j];
212  Line l = shape.lineAt(j);
213  SUMOReal hdg = atan2(l.p2().y() - l.p1().y(), l.p2().x() - l.p1().x());
214  device << " <geometry s=\"" << offset << "\" x=\"" << p.x() << "\" y=\"" << p.y() << "\" hdg=\"" << hdg << "\" length=\"" << l.length() << "\"><line/></geometry>\n";
215  offset += l.length();
216  }
217  device << " </planView>\n";
218 }
219 
220 
221 void
222 NWWriter_OpenDrive::writeEmptyCenterLane(OutputDevice& device, const std::string& mark, SUMOReal markWidth) {
223  device << " <center>\n";
224  device << " <lane id=\"0\" type=\"none\" level= \"0\">\n";
225  device << " <link/>\n";
226  device << " <roadMark sOffset=\"0\" type=\"" << mark << "\" weight=\"standard\" color=\"standard\" width=\"" << markWidth << "\"/>\n";
227  device << " <width sOffset=\"0\" a=\"0\" b=\"0\" c=\"0\" d=\"0\"/>\n";
228  device << " </lane>\n";
229  device << " </center>\n";
230 }
231 
232 
233 int
234 NWWriter_OpenDrive::getID(const std::string& origID, StringBijection<int>& map, int& lastID) {
235  if (map.hasString(origID)) {
236  return map.get(origID);
237  }
238  map.insert(origID, lastID++);
239  return lastID - 1;
240 }
241 
242 
243 /****************************************************************************/
244