SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NWWriter_SUMO.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Exporter writing networks using the SUMO format
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 #include <cmath>
33 #include <algorithm>
37 #include <utils/common/ToString.h>
41 #include <netbuild/NBEdge.h>
42 #include <netbuild/NBEdgeCont.h>
43 #include <netbuild/NBNode.h>
44 #include <netbuild/NBNodeCont.h>
45 #include <netbuild/NBNetBuilder.h>
47 #include <netbuild/NBDistrict.h>
48 #include "NWFrame.h"
49 #include "NWWriter_SUMO.h"
50 
51 #ifdef CHECK_MEMORY_LEAKS
52 #include <foreign/nvwa/debug_new.h>
53 #endif // CHECK_MEMORY_LEAKS
54 
55 
56 
57 // ===========================================================================
58 // method definitions
59 // ===========================================================================
60 // ---------------------------------------------------------------------------
61 // static methods
62 // ---------------------------------------------------------------------------
63 void
65  // check whether a sumo net-file shall be generated
66  if (!oc.isSet("output-file")) {
67  return;
68  }
69  OutputDevice& device = OutputDevice::getDevice(oc.getString("output-file"));
70  device.writeXMLHeader("net", NWFrame::MAJOR_VERSION + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.dlr.de/xsd/net_file.xsd\""); // street names may contain non-ascii chars
71  device.lf();
72  // get involved container
73  const NBNodeCont& nc = nb.getNodeCont();
74  const NBEdgeCont& ec = nb.getEdgeCont();
75  const NBDistrictCont& dc = nb.getDistrictCont();
76 
77  // write network offsets and projection
79 
80  // write edge types and restrictions
81  nb.getTypeCont().writeTypes(device);
82 
83  // write inner lanes
84  bool origNames = oc.getBool("output.original-names");
85  if (!oc.getBool("no-internal-links")) {
86  bool hadAny = false;
87  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
88  hadAny |= writeInternalEdges(device, *(*i).second, origNames);
89  }
90  if (hadAny) {
91  device.lf();
92  }
93  }
94 
95  // write edges with lanes and connected edges
96  bool noNames = !oc.getBool("output.street-names");
97  for (std::map<std::string, NBEdge*>::const_iterator i = ec.begin(); i != ec.end(); ++i) {
98  writeEdge(device, *(*i).second, noNames, origNames);
99  }
100  device.lf();
101 
102  // write tls logics
103  writeTrafficLights(device, nb.getTLLogicCont());
104 
105  // write the nodes (junctions)
106  std::set<NBNode*> roundaboutNodes;
107  const bool checkLaneFoesAll = oc.getBool("check-lane-foes.all");
108  const bool checkLaneFoesRoundabout = !checkLaneFoesAll && oc.getBool("check-lane-foes.roundabout");
109  if (checkLaneFoesRoundabout) {
110  const std::set<EdgeSet>& roundabouts = ec.getRoundabouts();
111  for (std::set<EdgeSet>::const_iterator i = roundabouts.begin(); i != roundabouts.end(); ++i) {
112  for (EdgeSet::const_iterator j = (*i).begin(); j != (*i).end(); ++j) {
113  roundaboutNodes.insert((*j)->getToNode());
114  }
115  }
116  }
117  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
118  const bool checkLaneFoes = checkLaneFoesAll || (checkLaneFoesRoundabout && roundaboutNodes.count((*i).second) > 0);
119  writeJunction(device, *(*i).second, checkLaneFoes);
120  }
121  device.lf();
122  const bool includeInternal = !oc.getBool("no-internal-links");
123  if (includeInternal) {
124  // ... internal nodes if not unwanted
125  bool hadAny = false;
126  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
127  hadAny |= writeInternalNodes(device, *(*i).second);
128  }
129  if (hadAny) {
130  device.lf();
131  }
132  }
133 
134  // write the successors of lanes
135  unsigned int numConnections = 0;
136  for (std::map<std::string, NBEdge*>::const_iterator it_edge = ec.begin(); it_edge != ec.end(); it_edge++) {
137  NBEdge* from = it_edge->second;
139  const std::vector<NBEdge::Connection> connections = from->getConnections();
140  numConnections += (unsigned int)connections.size();
141  for (std::vector<NBEdge::Connection>::const_iterator it_c = connections.begin(); it_c != connections.end(); it_c++) {
142  writeConnection(device, *from, *it_c, includeInternal);
143  }
144  }
145  if (numConnections > 0) {
146  device.lf();
147  }
148  if (includeInternal) {
149  // ... internal successors if not unwanted
150  bool hadAny = false;
151  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
152  hadAny |= writeInternalConnections(device, *(*i).second);
153  }
154  if (hadAny) {
155  device.lf();
156  }
157  }
158  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
159  NBNode* node = (*i).second;
160  // write connections from pedestrian crossings
161  const std::vector<NBNode::Crossing>& crossings = node->getCrossings();
162  for (std::vector<NBNode::Crossing>::const_iterator it = crossings.begin(); it != crossings.end(); it++) {
163  NWWriter_SUMO::writeInternalConnection(device, (*it).id, (*it).nextWalkingArea, 0, 0, "");
164  }
165  // write connections from pedestrian walking areas
166  const std::vector<NBNode::WalkingArea>& WalkingAreas = node->getWalkingAreas();
167  for (std::vector<NBNode::WalkingArea>::const_iterator it = WalkingAreas.begin(); it != WalkingAreas.end(); it++) {
168  if ((*it).nextCrossing != "") {
169  const NBNode::Crossing& nextCrossing = node->getCrossing((*it).nextCrossing);
170  // connection to next crossing (may be tls-controlled)
172  device.writeAttr(SUMO_ATTR_FROM, (*it).id);
173  device.writeAttr(SUMO_ATTR_TO, (*it).nextCrossing);
174  device.writeAttr(SUMO_ATTR_FROM_LANE, 0);
175  device.writeAttr(SUMO_ATTR_TO_LANE, 0);
176  if (node->isTLControlled()) {
177  device.writeAttr(SUMO_ATTR_TLID, (*node->getControllingTLS().begin())->getID());
178  assert(nextCrossing.tlLinkNo >= 0);
179  device.writeAttr(SUMO_ATTR_TLLINKINDEX, nextCrossing.tlLinkNo);
180  }
183  device.closeTag();
184  }
185  // optional connections from/to sidewalk
186  for (std::vector<std::string>::const_iterator it_sw = (*it).nextSidewalks.begin(); it_sw != (*it).nextSidewalks.end(); ++it_sw) {
187  NWWriter_SUMO::writeInternalConnection(device, (*it).id, (*it_sw), 0, 0, "");
188  }
189  for (std::vector<std::string>::const_iterator it_sw = (*it).prevSidewalks.begin(); it_sw != (*it).prevSidewalks.end(); ++it_sw) {
190  NWWriter_SUMO::writeInternalConnection(device, (*it_sw), (*it).id, 0, 0, "");
191  }
192  }
193  }
194 
195  // write loaded prohibitions
196  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
197  writeProhibitions(device, i->second->getProhibitions());
198  }
199 
200  // write roundabout information
201  writeRoundabouts(device, ec.getRoundabouts(), ec);
202 
203  // write the districts
204  for (std::map<std::string, NBDistrict*>::const_iterator i = dc.begin(); i != dc.end(); i++) {
205  writeDistrict(device, *(*i).second);
206  }
207  if (dc.size() != 0) {
208  device.lf();
209  }
210  device.close();
211 }
212 
213 
214 bool
215 NWWriter_SUMO::writeInternalEdges(OutputDevice& into, const NBNode& n, bool origNames) {
216  bool ret = false;
217  const EdgeVector& incoming = n.getIncomingEdges();
218  for (EdgeVector::const_iterator i = incoming.begin(); i != incoming.end(); i++) {
219  const std::vector<NBEdge::Connection>& elv = (*i)->getConnections();
220  if (elv.size() > 0) {
221  bool haveVia = false;
222  NBEdge* toEdge = 0;
223  std::string internalEdgeID = "";
224  // first pass: compute average lengths of non-via edges
225  std::map<NBEdge*, SUMOReal> lengthSum;
226  std::map<NBEdge*, int> numLanes;
227  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
228  lengthSum[(*k).toEdge] += MAX2((*k).shape.length(), POSITION_EPS);
229  numLanes[(*k).toEdge] += 1;
230  }
231  // second pass: write non-via edges
232  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
233  if ((*k).toEdge == 0) {
234  assert(false); // should never happen. tell me when it does
235  continue;
236  }
237  if (toEdge != (*k).toEdge) {
238  internalEdgeID = (*k).id;
239  if (toEdge != 0) {
240  // close the previous edge
241  into.closeTag();
242  }
243  toEdge = (*k).toEdge;
244  into.openTag(SUMO_TAG_EDGE);
245  into.writeAttr(SUMO_ATTR_ID, internalEdgeID);
247  // open a new edge
248  }
249  // to avoid changing to an internal lane which has a successor
250  // with the wrong permissions we need to inherit them from the successor
251  const NBEdge::Lane& successor = (*k).toEdge->getLanes()[(*k).toLane];
252  const SUMOReal length = lengthSum[toEdge] / numLanes[toEdge];
253  // @note the actual length should be used once sumo supports lanes of
254  // varying length within the same edge
255  //const SUMOReal length = MAX2((*k).shape.length(), POSITION_EPS);
256  writeLane(into, internalEdgeID, (*k).getInternalLaneID(), (*k).vmax,
257  successor.permissions, successor.preferred,
258  NBEdge::UNSPECIFIED_OFFSET, successor.width, (*k).shape, (*k).origID,
259  length, (*k).internalLaneIndex, origNames, &n);
260  haveVia = haveVia || (*k).haveVia;
261  }
262  ret = true;
263  into.closeTag(); // close the last edge
264  // third pass: write via edges
265  if (haveVia) {
266  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
267  if (!(*k).haveVia) {
268  continue;
269  }
270  if ((*k).toEdge == 0) {
271  assert(false); // should never happen. tell me when it does
272  continue;
273  }
274  const NBEdge::Lane& successor = (*k).toEdge->getLanes()[(*k).toLane];
275  into.openTag(SUMO_TAG_EDGE);
276  into.writeAttr(SUMO_ATTR_ID, (*k).viaID);
278  writeLane(into, (*k).viaID, (*k).viaID + "_0", (*k).viaVmax, SVCAll, SVCAll,
279  NBEdge::UNSPECIFIED_OFFSET, successor.width, (*k).viaShape, (*k).origID,
280  MAX2((*k).viaShape.length(), POSITION_EPS), // microsim needs positive length
281  0, origNames, &n);
282  into.closeTag();
283  }
284  }
285  }
286  }
287  // write pedestrian crossings
288  const std::vector<NBNode::Crossing>& crossings = n.getCrossings();
289  for (std::vector<NBNode::Crossing>::const_iterator it = crossings.begin(); it != crossings.end(); it++) {
290  into.openTag(SUMO_TAG_EDGE);
291  into.writeAttr(SUMO_ATTR_ID, (*it).id);
293  into.writeAttr(SUMO_ATTR_CROSSING_EDGES, (*it).edges);
294  writeLane(into, (*it).id, (*it).id + "_0", 1, SVC_PEDESTRIAN, 0,
295  NBEdge::UNSPECIFIED_OFFSET, (*it).width, (*it).shape, "", (*it).shape.length(), 0, false, &n);
296  into.closeTag();
297  }
298  // write pedestrian walking areas
299  const std::vector<NBNode::WalkingArea>& WalkingAreas = n.getWalkingAreas();
300  for (std::vector<NBNode::WalkingArea>::const_iterator it = WalkingAreas.begin(); it != WalkingAreas.end(); it++) {
301  const NBNode::WalkingArea& wa = *it;
302  into.openTag(SUMO_TAG_EDGE);
303  into.writeAttr(SUMO_ATTR_ID, wa.id);
305  writeLane(into, wa.id, wa.id + "_0", 1, SVC_PEDESTRIAN, 0,
306  NBEdge::UNSPECIFIED_OFFSET, wa.width, wa.shape, "", wa.length, 0, false, &n);
307  into.closeTag();
308  }
309  return ret;
310 }
311 
312 
313 void
314 NWWriter_SUMO::writeEdge(OutputDevice& into, const NBEdge& e, bool noNames, bool origNames) {
315  // write the edge's begin
318  into.writeAttr(SUMO_ATTR_TO, e.getToNode()->getID());
319  if (!noNames && e.getStreetName() != "") {
321  }
323  if (e.getTypeID() != "") {
325  }
326  if (e.isMacroscopicConnector()) {
328  }
329  // write the spread type if not default ("right")
332  }
333  if (e.hasLoadedLength()) {
335  }
336  if (!e.hasDefaultGeometry()) {
338  }
339  // write the lanes
340  const std::vector<NBEdge::Lane>& lanes = e.getLanes();
341 
342  SUMOReal length = e.getLoadedLength();
343  if (OptionsCont::getOptions().getBool("no-internal-links") && !e.hasLoadedLength()) {
344  // use length to junction center even if a modified geometry was given
348  length = geom.length();
349  }
350  if (length <= 0) {
351  length = POSITION_EPS;
352  }
353  for (unsigned int i = 0; i < (unsigned int) lanes.size(); i++) {
354  const NBEdge::Lane& l = lanes[i];
355  writeLane(into, e.getID(), e.getLaneID(i), l.speed,
356  l.permissions, l.preferred, l.endOffset, l.width, l.shape, l.origID,
357  length, i, origNames);
358  }
359  // close the edge
360  into.closeTag();
361 }
362 
363 
364 void
365 NWWriter_SUMO::writeLane(OutputDevice& into, const std::string& eID, const std::string& lID,
366  SUMOReal speed, SVCPermissions permissions, SVCPermissions preferred,
367  SUMOReal endOffset, SUMOReal width, PositionVector shape,
368  const std::string& origID, SUMOReal length, unsigned int index, bool origNames,
369  const NBNode* node) {
370  // output the lane's attributes
372  // the first lane of an edge will be the depart lane
373  into.writeAttr(SUMO_ATTR_INDEX, index);
374  // write the list of allowed/disallowed vehicle classes
375  if (permissions != SVC_UNSPECIFIED) {
376  writePermissions(into, permissions);
377  }
378  writePreferences(into, preferred);
379  // some further information
380  if (speed == 0) {
381  WRITE_WARNING("Lane #" + toString(index) + " of edge '" + eID + "' has a maximum velocity of 0.");
382  } else if (speed < 0) {
383  throw ProcessError("Negative velocity (" + toString(speed) + " on edge '" + eID + "' lane#" + toString(index) + ".");
384  }
385  if (endOffset > 0) {
386  length = length - endOffset;
387  }
388  into.writeAttr(SUMO_ATTR_SPEED, speed);
389  into.writeAttr(SUMO_ATTR_LENGTH, length);
390  if (endOffset != NBEdge::UNSPECIFIED_OFFSET) {
391  into.writeAttr(SUMO_ATTR_ENDOFFSET, endOffset);
392  }
393  if (width != NBEdge::UNSPECIFIED_WIDTH) {
394  into.writeAttr(SUMO_ATTR_WIDTH, width);
395  }
396  if (node != 0) {
397  const NBNode::CustomShapeMap& cs = node->getCustomLaneShapes();
398  NBNode::CustomShapeMap::const_iterator it = cs.find(lID);
399  if (it != cs.end()) {
400  shape = it->second;
401  into.writeAttr(SUMO_ATTR_CUSTOMSHAPE, true);
402  }
403  }
404  into.writeAttr(SUMO_ATTR_SHAPE, endOffset > 0 ?
405  shape.getSubpart(0, shape.length() - endOffset) : shape);
406  if (origNames && origID != "") {
407  into.openTag(SUMO_TAG_PARAM);
408  into.writeAttr(SUMO_ATTR_KEY, "origId");
409  into.writeAttr(SUMO_ATTR_VALUE, origID);
410  into.closeTag();
411  into.closeTag();
412  } else {
413  into.closeTag();
414  }
415 }
416 
417 
418 void
419 NWWriter_SUMO::writeJunction(OutputDevice& into, const NBNode& n, const bool checkLaneFoes) {
420  // write the attributes
422  into.writeAttr(SUMO_ATTR_TYPE, n.getType());
424  // write the incoming lanes
425  std::string incLanes;
426  const std::vector<NBEdge*>& incoming = n.getIncomingEdges();
427  for (std::vector<NBEdge*>::const_iterator i = incoming.begin(); i != incoming.end(); ++i) {
428  unsigned int noLanes = (*i)->getNumLanes();
429  for (unsigned int j = 0; j < noLanes; j++) {
430  incLanes += (*i)->getLaneID(j);
431  if (i != incoming.end() - 1 || j < noLanes - 1) {
432  incLanes += ' ';
433  }
434  }
435  }
436  const std::vector<NBNode::Crossing>& crossings = n.getCrossings();
437  for (std::vector<NBNode::Crossing>::const_iterator it = crossings.begin(); it != crossings.end(); it++) {
438  incLanes += ' ' + (*it).prevWalkingArea + "_0";
439  }
440  into.writeAttr(SUMO_ATTR_INCLANES, incLanes);
441  // write the internal lanes
442  std::string intLanes;
443  if (!OptionsCont::getOptions().getBool("no-internal-links")) {
444  unsigned int l = 0;
445  for (EdgeVector::const_iterator i = incoming.begin(); i != incoming.end(); i++) {
446  const std::vector<NBEdge::Connection>& elv = (*i)->getConnections();
447  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
448  if ((*k).toEdge == 0) {
449  continue;
450  }
451  if (l != 0) {
452  intLanes += ' ';
453  }
454  if (!(*k).haveVia) {
455  intLanes += (*k).getInternalLaneID();
456  } else {
457  intLanes += (*k).viaID + "_0";
458  }
459  l++;
460  }
461  }
462  }
463  if (n.getType() != NODETYPE_DEAD_END && n.getType() != NODETYPE_NOJUNCTION) {
464  for (std::vector<NBNode::Crossing>::const_iterator it = crossings.begin(); it != crossings.end(); it++) {
465  intLanes += ' ' + (*it).id + "_0";
466  }
467  }
468  into.writeAttr(SUMO_ATTR_INTLANES, intLanes);
469  // close writing
471  // write optional radius
474  }
475  // specify whether a custom shape was used
476  if (n.hasCustomShape()) {
477  into.writeAttr(SUMO_ATTR_CUSTOMSHAPE, true);
478  }
479  if (n.getType() == NODETYPE_DEAD_END) {
480  into.closeTag();
481  } else {
482  // write right-of-way logics
483  n.writeLogic(into, checkLaneFoes);
484  into.closeTag();
485  }
486 }
487 
488 
489 bool
491  bool ret = false;
492  const std::vector<NBEdge*>& incoming = n.getIncomingEdges();
493  // build the list of internal lane ids
494  std::vector<std::string> internalLaneIDs;
495  for (EdgeVector::const_iterator i = incoming.begin(); i != incoming.end(); i++) {
496  const std::vector<NBEdge::Connection>& elv = (*i)->getConnections();
497  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
498  if ((*k).toEdge != 0) {
499  internalLaneIDs.push_back((*k).getInternalLaneID());
500  }
501  }
502  }
503  const std::vector<NBNode::Crossing>& crossings = n.getCrossings();
504  for (std::vector<NBNode::Crossing>::const_iterator it_c = crossings.begin(); it_c != crossings.end(); ++it_c) {
505  internalLaneIDs.push_back((*it_c).id + "_0");
506  }
507  // write the internal nodes
508  for (std::vector<NBEdge*>::const_iterator i = incoming.begin(); i != incoming.end(); i++) {
509  const std::vector<NBEdge::Connection>& elv = (*i)->getConnections();
510  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
511  if ((*k).toEdge == 0 || !(*k).haveVia) {
512  continue;
513  }
514  Position pos = (*k).shape[-1];
515  into.openTag(SUMO_TAG_JUNCTION).writeAttr(SUMO_ATTR_ID, (*k).viaID + "_0");
517  NWFrame::writePositionLong(pos, into);
518  std::string incLanes = (*k).getInternalLaneID();
519  if ((*k).foeIncomingLanes.length() != 0) {
520  incLanes += " " + (*k).foeIncomingLanes;
521  }
522  into.writeAttr(SUMO_ATTR_INCLANES, incLanes);
523  const std::vector<unsigned int>& foes = (*k).foeInternalLinks;
524  std::vector<std::string> foeIDs;
525  for (std::vector<unsigned int>::const_iterator it = foes.begin(); it != foes.end(); ++it) {
526  foeIDs.push_back(internalLaneIDs[*it]);
527  }
528  into.writeAttr(SUMO_ATTR_INTLANES, joinToString(foeIDs, " "));
529  into.closeTag();
530  ret = true;
531  }
532  }
533  return ret;
534 }
535 
536 
537 void
539  bool includeInternal, ConnectionStyle style) {
540  assert(c.toEdge != 0);
542  into.writeAttr(SUMO_ATTR_FROM, from.getID());
543  into.writeAttr(SUMO_ATTR_TO, c.toEdge->getID());
546  if (c.mayDefinitelyPass && style != TLL) {
548  }
549  if ((from.getToNode()->getKeepClear() == false || c.keepClear == false) && style != TLL) {
550  into.writeAttr<bool>(SUMO_ATTR_KEEP_CLEAR, false);
551  }
552  if (style != PLAIN) {
553  if (includeInternal) {
555  }
556  // set information about the controlling tl if any
557  if (c.tlID != "") {
558  into.writeAttr(SUMO_ATTR_TLID, c.tlID);
560  }
561  if (style == SUMONET) {
562  // write the direction information
563  LinkDirection dir = from.getToNode()->getDirection(&from, c.toEdge, OptionsCont::getOptions().getBool("lefthand"));
564  assert(dir != LINKDIR_NODIR);
565  into.writeAttr(SUMO_ATTR_DIR, toString(dir));
566  // write the state information
567  const LinkState linkState = from.getToNode()->getLinkState(
568  &from, c.toEdge, c.fromLane, c.mayDefinitelyPass, c.tlID);
569  into.writeAttr(SUMO_ATTR_STATE, linkState);
570  }
571  }
572  into.closeTag();
573 }
574 
575 
576 bool
578  bool ret = false;
579  const std::vector<NBEdge*>& incoming = n.getIncomingEdges();
580  for (std::vector<NBEdge*>::const_iterator i = incoming.begin(); i != incoming.end(); ++i) {
581  NBEdge* from = *i;
582  const std::vector<NBEdge::Connection>& connections = from->getConnections();
583  for (std::vector<NBEdge::Connection>::const_iterator j = connections.begin(); j != connections.end(); ++j) {
584  const NBEdge::Connection& c = *j;
585  assert(c.toEdge != 0);
586  if (c.haveVia) {
587  // internal split
588  writeInternalConnection(into, c.id, c.toEdge->getID(), c.internalLaneIndex, c.toLane, c.viaID + "_0");
589  writeInternalConnection(into, c.viaID, c.toEdge->getID(), 0, c.toLane, "");
590  } else {
591  // no internal split
593  }
594  ret = true;
595  }
596  }
597  return ret;
598 }
599 
600 
601 void
603  const std::string& from, const std::string& to,
604  int fromLane, int toLane, const std::string& via) {
606  into.writeAttr(SUMO_ATTR_FROM, from);
607  into.writeAttr(SUMO_ATTR_TO, to);
608  into.writeAttr(SUMO_ATTR_FROM_LANE, fromLane);
609  into.writeAttr(SUMO_ATTR_TO_LANE, toLane);
610  if (via != "") {
611  into.writeAttr(SUMO_ATTR_VIA, via);
612  }
613  into.writeAttr(SUMO_ATTR_DIR, "s");
614  into.writeAttr(SUMO_ATTR_STATE, (via != "" ? "m" : "M"));
615  into.closeTag();
616 }
617 
618 
619 void
620 NWWriter_SUMO::writeRoundabouts(OutputDevice& into, const std::set<EdgeSet>& roundabouts,
621  const NBEdgeCont& ec) {
622  // make output deterministic
623  std::vector<std::vector<std::string> > edgeIDs;
624  for (std::set<EdgeSet>::const_iterator i = roundabouts.begin(); i != roundabouts.end(); ++i) {
625  std::vector<std::string> tEdgeIDs;
626  for (EdgeSet::const_iterator j = (*i).begin(); j != (*i).end(); ++j) {
627  // the edges may have been erased from NBEdgeCont but their pointers are still valid
628  // we verify their existance in writeRoundabout()
629  tEdgeIDs.push_back((*j)->getID());
630  }
631  std::sort(tEdgeIDs.begin(), tEdgeIDs.end());
632  edgeIDs.push_back(tEdgeIDs);
633  }
634  std::sort(edgeIDs.begin(), edgeIDs.end());
635  // write
636  for (std::vector<std::vector<std::string> >::const_iterator i = edgeIDs.begin(); i != edgeIDs.end(); ++i) {
637  writeRoundabout(into, *i, ec);
638  }
639  if (roundabouts.size() != 0) {
640  into.lf();
641  }
642 }
643 
644 
645 void
646 NWWriter_SUMO::writeRoundabout(OutputDevice& into, const std::vector<std::string>& edgeIDs,
647  const NBEdgeCont& ec) {
648  std::vector<std::string> validEdgeIDs;
649  std::vector<std::string> invalidEdgeIDs;
650  std::vector<std::string> nodeIDs;
651  for (std::vector<std::string>::const_iterator i = edgeIDs.begin(); i != edgeIDs.end(); ++i) {
652  const NBEdge* edge = ec.retrieve(*i);
653  if (edge != 0) {
654  nodeIDs.push_back(edge->getToNode()->getID());
655  validEdgeIDs.push_back(edge->getID());
656  } else {
657  invalidEdgeIDs.push_back(*i);
658  }
659  }
660  std::sort(nodeIDs.begin(), nodeIDs.end());
661  if (validEdgeIDs.size() > 0) {
663  into.writeAttr(SUMO_ATTR_NODES, joinToString(nodeIDs, " "));
664  into.writeAttr(SUMO_ATTR_EDGES, joinToString(validEdgeIDs, " "));
665  into.closeTag();
666  if (invalidEdgeIDs.size() > 0) {
667  WRITE_WARNING("Writing incomplete roundabout. Edges: '"
668  + joinToString(invalidEdgeIDs, " ") + "' no longer exist'");
669  }
670  }
671 }
672 
673 
674 void
676  std::vector<SUMOReal> sourceW = d.getSourceWeights();
678  std::vector<SUMOReal> sinkW = d.getSinkWeights();
680  // write the head and the id of the district
682  if (d.getShape().size() > 0) {
684  }
685  size_t i;
686  // write all sources
687  const std::vector<NBEdge*>& sources = d.getSourceEdges();
688  for (i = 0; i < sources.size(); i++) {
689  // write the head and the id of the source
690  into.openTag(SUMO_TAG_TAZSOURCE).writeAttr(SUMO_ATTR_ID, sources[i]->getID()).writeAttr(SUMO_ATTR_WEIGHT, sourceW[i]);
691  into.closeTag();
692  }
693  // write all sinks
694  const std::vector<NBEdge*>& sinks = d.getSinkEdges();
695  for (i = 0; i < sinks.size(); i++) {
696  // write the head and the id of the sink
697  into.openTag(SUMO_TAG_TAZSINK).writeAttr(SUMO_ATTR_ID, sinks[i]->getID()).writeAttr(SUMO_ATTR_WEIGHT, sinkW[i]);
698  into.closeTag();
699  }
700  // write the tail
701  into.closeTag();
702 }
703 
704 
705 std::string
707  SUMOReal time = STEPS2TIME(steps);
708  if (time == std::floor(time)) {
709  return toString(int(time));
710  } else {
711  return toString(time);
712  }
713 }
714 
715 
716 void
718  for (NBConnectionProhibits::const_iterator j = prohibitions.begin(); j != prohibitions.end(); j++) {
719  NBConnection prohibited = (*j).first;
720  const NBConnectionVector& prohibiting = (*j).second;
721  for (NBConnectionVector::const_iterator k = prohibiting.begin(); k != prohibiting.end(); k++) {
722  NBConnection prohibitor = *k;
726  into.closeTag();
727  }
728  }
729 }
730 
731 
732 std::string
734  return c.getFrom()->getID() + "->" + c.getTo()->getID();
735 }
736 
737 
738 void
740  std::vector<NBTrafficLightLogic*> logics = tllCont.getComputed();
741  for (std::vector<NBTrafficLightLogic*>::iterator it = logics.begin(); it != logics.end(); it++) {
743  into.writeAttr(SUMO_ATTR_ID, (*it)->getID());
744  into.writeAttr(SUMO_ATTR_TYPE, (*it)->getType());
745  into.writeAttr(SUMO_ATTR_PROGRAMID, (*it)->getProgramID());
746  into.writeAttr(SUMO_ATTR_OFFSET, writeSUMOTime((*it)->getOffset()));
747  // write params
748  const std::map<std::string, std::string>& params = (*it)->getMap();
749  for (std::map<std::string, std::string>::const_iterator i = params.begin(); i != params.end(); ++i) {
750  into.openTag(SUMO_TAG_PARAM);
751  into.writeAttr(SUMO_ATTR_KEY, (*i).first);
752  into.writeAttr(SUMO_ATTR_VALUE, (*i).second);
753  into.closeTag();
754  }
755  // write the phases
756  const std::vector<NBTrafficLightLogic::PhaseDefinition>& phases = (*it)->getPhases();
757  for (std::vector<NBTrafficLightLogic::PhaseDefinition>::const_iterator j = phases.begin(); j != phases.end(); ++j) {
758  into.openTag(SUMO_TAG_PHASE);
759  into.writeAttr(SUMO_ATTR_DURATION, writeSUMOTime(j->duration));
760  into.writeAttr(SUMO_ATTR_STATE, j->state);
761  into.closeTag();
762  }
763  into.closeTag();
764  }
765  if (logics.size() > 0) {
766  into.lf();
767  }
768 }
769 /****************************************************************************/
770 
static void writeRoundabout(OutputDevice &into, const std::vector< std::string > &r, const NBEdgeCont &ec)
Writes a roundabout.
std::string id
Definition: NBEdge.h:179
The information about how to spread the lanes from the given position.
void writePermissions(OutputDevice &into, SVCPermissions permissions)
writes allowed disallowed attributes if needed;
PositionVector cutAtIntersection(const PositionVector &old) const
cut shape at the intersection shapes
Definition: NBEdge.cpp:442
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
const EdgeVector & getIncomingEdges() const
Returns this node&#39;s incoming edges.
Definition: NBNode.h:247
void close()
Closes the device and removes it from the dictionary.
static void writeLocation(OutputDevice &into)
writes the location element
const std::string & getTypeID() const
Definition: NBEdge.h:895
SUMOReal endOffset
This lane&#39;s offset to the intersection begin.
Definition: NBEdge.h:136
bool getKeepClear() const
Returns the keepClear flag.
Definition: NBNode.h:285
A structure which describes a connection between edges or lanes.
Definition: NBEdge.h:148
int toLane
The lane the connections yields in.
Definition: NBEdge.h:166
Position getCenter() const
Returns a position that is guaranteed to lie within the node shape.
Definition: NBNode.cpp:2352
a list of node ids, used for controlling joining
static const SUMOReal UNSPECIFIED_WIDTH
unspecified lane width
Definition: NBEdge.h:203
NBTypeCont & getTypeCont()
Returns the type container.
Definition: NBNetBuilder.h:170
Whether vehicles must keep the junction clear.
PositionVector shape
The lane&#39;s shape.
Definition: NBEdge.h:128
whether a given shape is user-defined
is a pedestrian
static void writeDistrict(OutputDevice &into, const NBDistrict &d)
Writes a district.
std::string viaID
Definition: NBEdge.h:184
NBEdge * toEdge
The edge the connections yields in.
Definition: NBEdge.h:164
bool hasDefaultGeometry() const
Returns whether the geometry consists only of the node positions.
Definition: NBEdge.cpp:415
SUMOReal getRadius() const
Returns the turning radius of this node.
Definition: NBNode.h:279
const std::set< EdgeSet > getRoundabouts() const
Returns the determined roundabouts.
Definition: NBEdgeCont.cpp:947
size_t size() const
Returns the number of districts inside the container.
A container for traffic light definitions and built programs.
SUMOReal length
This lane&#39;s width.
Definition: NBNode.h:172
SUMOReal width
This lane&#39;s width.
Definition: NBNode.h:170
bool isTLControlled() const
Returns whether this node is controlled by any tls.
Definition: NBNode.h:311
void writePreferences(OutputDevice &into, SVCPermissions preferred)
writes allowed disallowed attributes if needed;
const std::vector< NBEdge::Lane > & getLanes() const
Returns the lane definitions.
Definition: NBEdge.h:498
const Crossing & getCrossing(const std::string &id) const
return the crossing with the given id
Definition: NBNode.cpp:2328
int SVCPermissions
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
The representation of a single edge during network building.
Definition: NBEdge.h:71
static std::string escapeXML(const std::string &orig)
Replaces the standard escapes by their XML entities.
static void writeProhibitions(OutputDevice &into, const NBConnectionProhibits &prohibitions)
writes the given prohibitions
const std::vector< NBEdge * > & getSinkEdges() const
Returns the sinks.
Definition: NBDistrict.h:214
A container for districts.
const std::vector< Crossing > & getCrossings() const
return this junctions pedestrian crossings
Definition: NBNode.h:646
T MAX2(T a, T b)
Definition: StdDefs.h:74
the weight of a district&#39;s source or sink
bool hasCustomShape() const
return whether the shape was set by the user
Definition: NBNode.h:526
bool mayDefinitelyPass
Information about being definitely free to drive (on-ramps)
Definition: NBEdge.h:172
std::string id
the (edge)-id of this walkingArea
Definition: NBNode.h:168
const SVCPermissions SVCAll
static void writeTrafficLights(OutputDevice &into, const NBTrafficLightLogicCont &tllCont)
writes the traffic light logics to the given device
static const SUMOReal UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:205
static void writeInternalConnection(OutputDevice &into, const std::string &from, const std::string &to, int fromLane, int toLane, const std::string &via)
Writes a single internal connection.
NBEdge * getFrom() const
returns the from-edge (start of the connection)
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
bool priority
whether the pedestrians have priority
Definition: NBNode.h:152
LinkDirection
The different directions a link between two lanes may take (or a stream between two edges)...
bool writeXMLHeader(const std::string &rootElement, const std::string &attrs="", const std::string &comment="")
Writes an XML header with optional configuration.
The link is a straight direction.
The state of a link.
SUMOReal speed
The speed allowed on this lane.
Definition: NBEdge.h:130
A class representing a single district.
Definition: NBDistrict.h:72
bool keepClear
whether the junction must be kept clear when using this connection
Definition: NBEdge.h:174
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
std::map< std::string, PositionVector > CustomShapeMap
Definition: NBNode.h:83
static std::string prohibitionConnection(const NBConnection &c)
the attribute value for a prohibition
SUMOReal getLoadedLength() const
Returns the length was set explicitly or the computed length if it wasn&#39;t set.
Definition: NBEdge.h:411
An (internal) definition of a single lane of an edge.
Definition: NBEdge.h:123
const std::string & getID() const
Returns the id.
Definition: Named.h:60
SVCPermissions permissions
List of vehicle types that are allowed on this lane.
Definition: NBEdge.h:132
static void writeNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Writes the network into a SUMO-file.
void push_front_noDoublePos(const Position &p)
const Position & getPosition() const
Returns the position of this node.
Definition: NBNode.h:235
The turning radius at an intersection in m.
std::map< std::string, NBEdge * >::const_iterator end() const
Returns the pointer to the end of the stored edges.
Definition: NBEdgeCont.h:198
const std::set< NBTrafficLightDefinition * > & getControllingTLS() const
Returns the traffic lights that were assigned to this node.
Definition: NBNode.h:325
the edges of a route
int getPriority() const
Returns the priority of the edge.
Definition: NBEdge.h:351
std::map< std::string, NBDistrict * >::const_iterator begin() const
Returns the pointer to the begin of the stored districts.
std::string getLaneID(unsigned int lane) const
Definition: NBEdge.cpp:2159
const std::vector< NBEdge * > & getSourceEdges() const
Returns the sources.
Definition: NBDistrict.h:198
std::string tlID
The id of the traffic light that controls this connection.
Definition: NBEdge.h:168
SVCPermissions preferred
List of vehicle types that are preferred on this lane.
Definition: NBEdge.h:134
std::string getInternalLaneID() const
Definition: NBEdge.cpp:78
This is an uncontrolled, minor link, has to brake.
static bool writeInternalConnections(OutputDevice &into, const NBNode &n)
Writes inner connections within the node.
int fromLane
The lane the connections starts at.
Definition: NBEdge.h:162
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
std::map< std::string, NBDistrict * >::const_iterator end() const
Returns the pointer to the end of the stored districts.
NBEdgeCont & getEdgeCont()
Returns the edge container.
Definition: NBNetBuilder.h:154
A list of positions.
static void writeLane(OutputDevice &into, const std::string &eID, const std::string &lID, SUMOReal speed, SVCPermissions permissions, SVCPermissions preferred, SUMOReal endOffset, SUMOReal width, PositionVector shape, const std::string &origID, SUMOReal length, unsigned int index, bool origNames, const NBNode *node=0)
Writes a lane (&lt;lane ...) of an edge.
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:272
static void writeEdge(OutputDevice &into, const NBEdge &e, bool noNames, bool origNames)
Writes an edge (&lt;edge ...)
bool hasLoadedLength() const
Returns whether a length was set explicitly.
Definition: NBEdge.h:419
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
const PositionVector & getShape() const
Returns the shape.
Definition: NBDistrict.h:222
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic, in MSLink and GNEInternalLane.
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
#define POSITION_EPS
Definition: config.h:189
const CustomShapeMap & getCustomLaneShapes() const
sets a custom shape for an internal lane
Definition: NBNode.h:511
static void writePositionLong(const Position &pos, OutputDevice &dev)
Writes the given position to device in long format (one attribute per dimension)
Definition: NWFrame.cpp:143
std::vector< NBTrafficLightLogic * > getComputed() const
Returns a list of all computed logics.
std::map< std::string, NBNode * >::const_iterator end() const
Returns the pointer to the end of the stored nodes.
Definition: NBNodeCont.h:135
LinkDirection getDirection(const NBEdge *const incoming, const NBEdge *const outgoing, bool leftHand=false) const
Returns the representation of the described stream&#39;s direction.
Definition: NBNode.cpp:1365
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
const PositionVector & getShape() const
retrieve the junction shape
Definition: NBNode.cpp:1526
std::vector< NBConnection > NBConnectionVector
Definition of a connection vector.
static void normaliseSum(std::vector< T > &v, T msum=1.0)
Definition: VectorHelper.h:58
void writeTypes(OutputDevice &into) const
writes all types a s XML
Definition: NBTypeCont.cpp:127
std::string origID
An original ID, if given (.
Definition: NBEdge.h:140
std::map< std::string, NBEdge * >::const_iterator begin() const
Returns the pointer to the begin of the stored edges.
Definition: NBEdgeCont.h:190
static bool writeInternalEdges(OutputDevice &into, const NBNode &n, bool origNames)
Writes internal edges (&lt;edge ... with id[0]==&#39;:&#39;) of the given node.
SUMOReal length() const
Returns the length.
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:367
std::map< NBConnection, NBConnectionVector > NBConnectionProhibits
Definition of a container for connection block dependencies Includes a list of all connections which ...
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:274
LinkState getLinkState(const NBEdge *incoming, NBEdge *outgoing, int fromLane, bool mayDefinitelyPass, const std::string &tlID) const
Definition: NBNode.cpp:1429
NBNodeCont & getNodeCont()
Returns the node container.
Definition: NBNetBuilder.h:162
Instance responsible for building networks.
Definition: NBNetBuilder.h:113
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
int tlLinkNo
the traffic light index of this crossing (if controlled)
Definition: NBNode.h:154
std::vector< NBEdge * > EdgeVector
Definition: NBCont.h:41
static bool writeInternalNodes(OutputDevice &into, const NBNode &n)
Writes internal junctions (&lt;junction with id[0]==&#39;:&#39; ...) of the given node.
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:519
A definition of a pedestrian walking area.
Definition: NBNode.h:161
bool isMacroscopicConnector() const
Returns whether this edge was marked as a macroscopic connector.
Definition: NBEdge.h:849
A storage for options typed value containers)
Definition: OptionsCont.h:108
This is an uncontrolled, major link, may pass.
void sortOutgoingConnectionsByIndex()
sorts the outgoing connections by their from-lane-index and their to-lane-index
Definition: NBEdge.cpp:871
NBTrafficLightLogicCont & getTLLogicCont()
Returns the traffic light logics container.
Definition: NBNetBuilder.h:178
unsigned int tlLinkNo
The index of this connection within the controlling traffic light.
Definition: NBEdge.h:170
NBEdge * getTo() const
returns the to-edge (end of the connection)
The abstract direction of a link.
LaneSpreadFunction getLaneSpreadFunction() const
Returns how this edge&#39;s lanes&#39; lateral offset is computed.
Definition: NBEdge.h:599
Represents a single node (junction) during network building.
Definition: NBNode.h:75
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:159
A definition of a pedestrian crossing.
Definition: NBNode.h:133
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:215
static const SUMOReal UNSPECIFIED_RADIUS
unspecified lane width
Definition: NBNode.h:193
static void writeJunction(OutputDevice &into, const NBNode &n, const bool checkLaneFoes)
Writes a junction (&lt;junction ...)
bool writeLogic(OutputDevice &into, const bool checkLaneFoes) const
Definition: NBNode.cpp:711
const std::vector< SUMOReal > & getSourceWeights() const
Returns the weights of the sources.
Definition: NBDistrict.h:190
void push_back_noDoublePos(const Position &p)
const std::vector< SUMOReal > & getSinkWeights() const
Returns the weights of the sinks.
Definition: NBDistrict.h:206
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:64
unsigned int internalLaneIndex
The lane index of this internal lane within the internal edge.
Definition: NBEdge.h:192
const std::vector< WalkingArea > & getWalkingAreas() const
return this junctions pedestrian walking areas
Definition: NBNode.h:651
NBDistrictCont & getDistrictCont()
Returns the districts container.
Definition: NBNetBuilder.h:186
const std::string & getStreetName() const
Returns the street name of this edge.
Definition: NBEdge.h:461
const std::vector< Connection > & getConnections() const
Returns the connections.
Definition: NBEdge.h:751
static const std::string MAJOR_VERSION
The version number for written files.
Definition: NWFrame.h:77
std::map< std::string, NBNode * >::const_iterator begin() const
Returns the pointer to the begin of the stored nodes.
Definition: NBNodeCont.h:127
static void writeRoundabouts(OutputDevice &into, const std::set< EdgeSet > &roundabouts, const NBEdgeCont &ec)
Writes roundabouts.
PositionVector getSubpart(SUMOReal beginOffset, SUMOReal endOffset) const
static std::string writeSUMOTime(SUMOTime time)
writes a SUMOTime as int if possible, otherwise as a float
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:235
const SVCPermissions SVC_UNSPECIFIED
SUMOReal width
This lane&#39;s width.
Definition: NBEdge.h:138
static void writeConnection(OutputDevice &into, const NBEdge &from, const NBEdge::Connection &c, bool includeInternal, ConnectionStyle style=SUMONET)
Writes connections outgoing from the given edge (also used in NWWriter_XML)
PositionVector shape
The polygonal shape.
Definition: NBNode.h:174
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
The link has no direction (is a dead end link)
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:359