SUMO - Simulation of Urban MObility
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  const std::string lefthand = oc.getBool("lefthand") ? " " + toString(SUMO_ATTR_LEFTHAND) + "=\"true\"" : "";
71  const int cornerDetail = oc.getInt("junctions.corner-detail");
72  const int linkDetail = oc.getInt("junctions.internal-link-detail");
73  const std::string junctionCornerDetail = (cornerDetail > 0
74  ? " " + toString(SUMO_ATTR_CORNERDETAIL) + "=\"" + toString(cornerDetail) + "\"" : "");
75  const std::string junctionLinkDetail = (oc.isDefault("junctions.internal-link-detail") ? "" :
76  " " + toString(SUMO_ATTR_LINKDETAIL) + "=\"" + toString(linkDetail) + "\"");
77  device.writeXMLHeader("net", NWFrame::MAJOR_VERSION + lefthand + junctionCornerDetail + junctionLinkDetail +
78  " 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
79  device.lf();
80  // get involved container
81  const NBNodeCont& nc = nb.getNodeCont();
82  const NBEdgeCont& ec = nb.getEdgeCont();
83  const NBDistrictCont& dc = nb.getDistrictCont();
84 
85  // write network offsets and projection
87 
88  // write edge types and restrictions
89  nb.getTypeCont().writeTypes(device);
90 
91  // write inner lanes
92  bool origNames = oc.getBool("output.original-names");
93  if (!oc.getBool("no-internal-links")) {
94  bool hadAny = false;
95  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
96  hadAny |= writeInternalEdges(device, *(*i).second, origNames);
97  }
98  if (hadAny) {
99  device.lf();
100  }
101  }
102 
103  // write edges with lanes and connected edges
104  bool noNames = !oc.getBool("output.street-names");
105  for (std::map<std::string, NBEdge*>::const_iterator i = ec.begin(); i != ec.end(); ++i) {
106  writeEdge(device, *(*i).second, noNames, origNames);
107  }
108  device.lf();
109 
110  // write tls logics
111  writeTrafficLights(device, nb.getTLLogicCont());
112 
113  // write the nodes (junctions)
114  std::set<NBNode*> roundaboutNodes;
115  const bool checkLaneFoesAll = oc.getBool("check-lane-foes.all");
116  const bool checkLaneFoesRoundabout = !checkLaneFoesAll && oc.getBool("check-lane-foes.roundabout");
117  if (checkLaneFoesRoundabout) {
118  const std::set<EdgeSet>& roundabouts = ec.getRoundabouts();
119  for (std::set<EdgeSet>::const_iterator i = roundabouts.begin(); i != roundabouts.end(); ++i) {
120  for (EdgeSet::const_iterator j = (*i).begin(); j != (*i).end(); ++j) {
121  roundaboutNodes.insert((*j)->getToNode());
122  }
123  }
124  }
125  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
126  const bool checkLaneFoes = checkLaneFoesAll || (checkLaneFoesRoundabout && roundaboutNodes.count((*i).second) > 0);
127  writeJunction(device, *(*i).second, checkLaneFoes);
128  }
129  device.lf();
130  const bool includeInternal = !oc.getBool("no-internal-links");
131  if (includeInternal) {
132  // ... internal nodes if not unwanted
133  bool hadAny = false;
134  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
135  hadAny |= writeInternalNodes(device, *(*i).second);
136  }
137  if (hadAny) {
138  device.lf();
139  }
140  }
141 
142  // write the successors of lanes
143  unsigned int numConnections = 0;
144  for (std::map<std::string, NBEdge*>::const_iterator it_edge = ec.begin(); it_edge != ec.end(); it_edge++) {
145  NBEdge* from = it_edge->second;
147  const std::vector<NBEdge::Connection> connections = from->getConnections();
148  numConnections += (unsigned int)connections.size();
149  for (std::vector<NBEdge::Connection>::const_iterator it_c = connections.begin(); it_c != connections.end(); it_c++) {
150  writeConnection(device, *from, *it_c, includeInternal);
151  }
152  }
153  if (numConnections > 0) {
154  device.lf();
155  }
156  if (includeInternal) {
157  // ... internal successors if not unwanted
158  bool hadAny = false;
159  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
160  hadAny |= writeInternalConnections(device, *(*i).second);
161  }
162  if (hadAny) {
163  device.lf();
164  }
165  }
166  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
167  NBNode* node = (*i).second;
168  // write connections from pedestrian crossings
169  const std::vector<NBNode::Crossing>& crossings = node->getCrossings();
170  for (std::vector<NBNode::Crossing>::const_iterator it = crossings.begin(); it != crossings.end(); it++) {
171  NWWriter_SUMO::writeInternalConnection(device, (*it).id, (*it).nextWalkingArea, 0, 0, "");
172  }
173  // write connections from pedestrian walking areas
174  const std::vector<NBNode::WalkingArea>& WalkingAreas = node->getWalkingAreas();
175  for (std::vector<NBNode::WalkingArea>::const_iterator it = WalkingAreas.begin(); it != WalkingAreas.end(); it++) {
176  if ((*it).nextCrossing != "") {
177  const NBNode::Crossing& nextCrossing = node->getCrossing((*it).nextCrossing);
178  // connection to next crossing (may be tls-controlled)
180  device.writeAttr(SUMO_ATTR_FROM, (*it).id);
181  device.writeAttr(SUMO_ATTR_TO, (*it).nextCrossing);
182  device.writeAttr(SUMO_ATTR_FROM_LANE, 0);
183  device.writeAttr(SUMO_ATTR_TO_LANE, 0);
184  if (node->isTLControlled()) {
185  device.writeAttr(SUMO_ATTR_TLID, (*node->getControllingTLS().begin())->getID());
186  assert(nextCrossing.tlLinkNo >= 0);
187  device.writeAttr(SUMO_ATTR_TLLINKINDEX, nextCrossing.tlLinkNo);
188  }
191  device.closeTag();
192  }
193  // optional connections from/to sidewalk
194  for (std::vector<std::string>::const_iterator it_sw = (*it).nextSidewalks.begin(); it_sw != (*it).nextSidewalks.end(); ++it_sw) {
195  NWWriter_SUMO::writeInternalConnection(device, (*it).id, (*it_sw), 0, 0, "");
196  }
197  for (std::vector<std::string>::const_iterator it_sw = (*it).prevSidewalks.begin(); it_sw != (*it).prevSidewalks.end(); ++it_sw) {
198  NWWriter_SUMO::writeInternalConnection(device, (*it_sw), (*it).id, 0, 0, "");
199  }
200  }
201  }
202 
203  // write loaded prohibitions
204  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
205  writeProhibitions(device, i->second->getProhibitions());
206  }
207 
208  // write roundabout information
209  writeRoundabouts(device, ec.getRoundabouts(), ec);
210 
211  // write the districts
212  for (std::map<std::string, NBDistrict*>::const_iterator i = dc.begin(); i != dc.end(); i++) {
213  writeDistrict(device, *(*i).second);
214  }
215  if (dc.size() != 0) {
216  device.lf();
217  }
218  device.close();
219 }
220 
221 
222 bool
223 NWWriter_SUMO::writeInternalEdges(OutputDevice& into, const NBNode& n, bool origNames) {
224  bool ret = false;
225  const EdgeVector& incoming = n.getIncomingEdges();
226  for (EdgeVector::const_iterator i = incoming.begin(); i != incoming.end(); i++) {
227  const std::vector<NBEdge::Connection>& elv = (*i)->getConnections();
228  if (elv.size() > 0) {
229  bool haveVia = false;
230  NBEdge* toEdge = 0;
231  std::string internalEdgeID = "";
232  // first pass: compute average lengths of non-via edges
233  std::map<NBEdge*, SUMOReal> lengthSum;
234  std::map<NBEdge*, int> numLanes;
235  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
236  lengthSum[(*k).toEdge] += MAX2((*k).shape.length(), POSITION_EPS);
237  numLanes[(*k).toEdge] += 1;
238  }
239  // second pass: write non-via edges
240  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
241  if ((*k).toEdge == 0) {
242  assert(false); // should never happen. tell me when it does
243  continue;
244  }
245  if (toEdge != (*k).toEdge) {
246  internalEdgeID = (*k).id;
247  if (toEdge != 0) {
248  // close the previous edge
249  into.closeTag();
250  }
251  toEdge = (*k).toEdge;
252  into.openTag(SUMO_TAG_EDGE);
253  into.writeAttr(SUMO_ATTR_ID, internalEdgeID);
255  // open a new edge
256  }
257  // to avoid changing to an internal lane which has a successor
258  // with the wrong permissions we need to inherit them from the successor
259  const NBEdge::Lane& successor = (*k).toEdge->getLanes()[(*k).toLane];
260  const SUMOReal length = lengthSum[toEdge] / numLanes[toEdge];
261  // @note the actual length should be used once sumo supports lanes of
262  // varying length within the same edge
263  //const SUMOReal length = MAX2((*k).shape.length(), POSITION_EPS);
264  writeLane(into, internalEdgeID, (*k).getInternalLaneID(), (*k).vmax,
265  successor.permissions, successor.preferred,
266  NBEdge::UNSPECIFIED_OFFSET, successor.width, (*k).shape, (*k).origID,
267  length, (*k).internalLaneIndex, origNames, &n);
268  haveVia = haveVia || (*k).haveVia;
269  }
270  ret = true;
271  into.closeTag(); // close the last edge
272  // third pass: write via edges
273  if (haveVia) {
274  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
275  if (!(*k).haveVia) {
276  continue;
277  }
278  if ((*k).toEdge == 0) {
279  assert(false); // should never happen. tell me when it does
280  continue;
281  }
282  const NBEdge::Lane& successor = (*k).toEdge->getLanes()[(*k).toLane];
283  into.openTag(SUMO_TAG_EDGE);
284  into.writeAttr(SUMO_ATTR_ID, (*k).viaID);
286  writeLane(into, (*k).viaID, (*k).viaID + "_0", (*k).viaVmax, SVCAll, SVCAll,
287  NBEdge::UNSPECIFIED_OFFSET, successor.width, (*k).viaShape, (*k).origID,
288  MAX2((*k).viaShape.length(), POSITION_EPS), // microsim needs positive length
289  0, origNames, &n);
290  into.closeTag();
291  }
292  }
293  }
294  }
295  // write pedestrian crossings
296  const std::vector<NBNode::Crossing>& crossings = n.getCrossings();
297  for (std::vector<NBNode::Crossing>::const_iterator it = crossings.begin(); it != crossings.end(); it++) {
298  into.openTag(SUMO_TAG_EDGE);
299  into.writeAttr(SUMO_ATTR_ID, (*it).id);
301  into.writeAttr(SUMO_ATTR_CROSSING_EDGES, (*it).edges);
302  writeLane(into, (*it).id, (*it).id + "_0", 1, SVC_PEDESTRIAN, 0,
303  NBEdge::UNSPECIFIED_OFFSET, (*it).width, (*it).shape, "", (*it).shape.length(), 0, false, &n);
304  into.closeTag();
305  }
306  // write pedestrian walking areas
307  const std::vector<NBNode::WalkingArea>& WalkingAreas = n.getWalkingAreas();
308  for (std::vector<NBNode::WalkingArea>::const_iterator it = WalkingAreas.begin(); it != WalkingAreas.end(); it++) {
309  const NBNode::WalkingArea& wa = *it;
310  into.openTag(SUMO_TAG_EDGE);
311  into.writeAttr(SUMO_ATTR_ID, wa.id);
313  writeLane(into, wa.id, wa.id + "_0", 1, SVC_PEDESTRIAN, 0,
314  NBEdge::UNSPECIFIED_OFFSET, wa.width, wa.shape, "", wa.length, 0, false, &n);
315  into.closeTag();
316  }
317  return ret;
318 }
319 
320 
321 void
322 NWWriter_SUMO::writeEdge(OutputDevice& into, const NBEdge& e, bool noNames, bool origNames) {
323  // write the edge's begin
326  into.writeAttr(SUMO_ATTR_TO, e.getToNode()->getID());
327  if (!noNames && e.getStreetName() != "") {
329  }
331  if (e.getTypeID() != "") {
333  }
334  if (e.isMacroscopicConnector()) {
336  }
337  // write the spread type if not default ("right")
340  }
341  if (e.hasLoadedLength()) {
343  }
344  if (!e.hasDefaultGeometry()) {
346  }
347  // write the lanes
348  const std::vector<NBEdge::Lane>& lanes = e.getLanes();
349 
350  SUMOReal length = e.getLoadedLength();
351  if (OptionsCont::getOptions().getBool("no-internal-links") && !e.hasLoadedLength()) {
352  // use length to junction center even if a modified geometry was given
356  length = geom.length();
357  }
358  if (length <= 0) {
359  length = POSITION_EPS;
360  }
361  for (unsigned int i = 0; i < (unsigned int) lanes.size(); i++) {
362  const NBEdge::Lane& l = lanes[i];
363  writeLane(into, e.getID(), e.getLaneID(i), l.speed,
364  l.permissions, l.preferred, l.endOffset, l.width, l.shape, l.origID,
365  length, i, origNames);
366  }
367  // close the edge
368  into.closeTag();
369 }
370 
371 
372 void
373 NWWriter_SUMO::writeLane(OutputDevice& into, const std::string& eID, const std::string& lID,
374  SUMOReal speed, SVCPermissions permissions, SVCPermissions preferred,
375  SUMOReal endOffset, SUMOReal width, PositionVector shape,
376  const std::string& origID, SUMOReal length, unsigned int index, bool origNames,
377  const NBNode* node) {
378  // output the lane's attributes
380  // the first lane of an edge will be the depart lane
381  into.writeAttr(SUMO_ATTR_INDEX, index);
382  // write the list of allowed/disallowed vehicle classes
383  if (permissions != SVC_UNSPECIFIED) {
384  writePermissions(into, permissions);
385  }
386  writePreferences(into, preferred);
387  // some further information
388  if (speed == 0) {
389  WRITE_WARNING("Lane #" + toString(index) + " of edge '" + eID + "' has a maximum velocity of 0.");
390  } else if (speed < 0) {
391  throw ProcessError("Negative velocity (" + toString(speed) + " on edge '" + eID + "' lane#" + toString(index) + ".");
392  }
393  if (endOffset > 0) {
394  length = length - endOffset;
395  }
396  into.writeAttr(SUMO_ATTR_SPEED, speed);
397  into.writeAttr(SUMO_ATTR_LENGTH, length);
398  if (endOffset != NBEdge::UNSPECIFIED_OFFSET) {
399  into.writeAttr(SUMO_ATTR_ENDOFFSET, endOffset);
400  }
401  if (width != NBEdge::UNSPECIFIED_WIDTH) {
402  into.writeAttr(SUMO_ATTR_WIDTH, width);
403  }
404  if (node != 0) {
405  const NBNode::CustomShapeMap& cs = node->getCustomLaneShapes();
406  NBNode::CustomShapeMap::const_iterator it = cs.find(lID);
407  if (it != cs.end()) {
408  shape = it->second;
409  into.writeAttr(SUMO_ATTR_CUSTOMSHAPE, true);
410  }
411  }
412  into.writeAttr(SUMO_ATTR_SHAPE, endOffset > 0 ?
413  shape.getSubpart(0, shape.length() - endOffset) : shape);
414  if (origNames && origID != "") {
415  into.openTag(SUMO_TAG_PARAM);
416  into.writeAttr(SUMO_ATTR_KEY, "origId");
417  into.writeAttr(SUMO_ATTR_VALUE, origID);
418  into.closeTag();
419  into.closeTag();
420  } else {
421  into.closeTag();
422  }
423 }
424 
425 
426 void
427 NWWriter_SUMO::writeJunction(OutputDevice& into, const NBNode& n, const bool checkLaneFoes) {
428  // write the attributes
430  into.writeAttr(SUMO_ATTR_TYPE, n.getType());
432  // write the incoming lanes
433  std::string incLanes;
434  const std::vector<NBEdge*>& incoming = n.getIncomingEdges();
435  for (std::vector<NBEdge*>::const_iterator i = incoming.begin(); i != incoming.end(); ++i) {
436  unsigned int noLanes = (*i)->getNumLanes();
437  for (unsigned int j = 0; j < noLanes; j++) {
438  incLanes += (*i)->getLaneID(j);
439  if (i != incoming.end() - 1 || j < noLanes - 1) {
440  incLanes += ' ';
441  }
442  }
443  }
444  const std::vector<NBNode::Crossing>& crossings = n.getCrossings();
445  for (std::vector<NBNode::Crossing>::const_iterator it = crossings.begin(); it != crossings.end(); it++) {
446  incLanes += ' ' + (*it).prevWalkingArea + "_0";
447  }
448  into.writeAttr(SUMO_ATTR_INCLANES, incLanes);
449  // write the internal lanes
450  std::string intLanes;
451  if (!OptionsCont::getOptions().getBool("no-internal-links")) {
452  unsigned int l = 0;
453  for (EdgeVector::const_iterator i = incoming.begin(); i != incoming.end(); i++) {
454  const std::vector<NBEdge::Connection>& elv = (*i)->getConnections();
455  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
456  if ((*k).toEdge == 0) {
457  continue;
458  }
459  if (l != 0) {
460  intLanes += ' ';
461  }
462  if (!(*k).haveVia) {
463  intLanes += (*k).getInternalLaneID();
464  } else {
465  intLanes += (*k).viaID + "_0";
466  }
467  l++;
468  }
469  }
470  }
471  if (n.getType() != NODETYPE_DEAD_END && n.getType() != NODETYPE_NOJUNCTION) {
472  for (std::vector<NBNode::Crossing>::const_iterator it = crossings.begin(); it != crossings.end(); it++) {
473  intLanes += ' ' + (*it).id + "_0";
474  }
475  }
476  into.writeAttr(SUMO_ATTR_INTLANES, intLanes);
477  // close writing
479  // write optional radius
482  }
483  // specify whether a custom shape was used
484  if (n.hasCustomShape()) {
485  into.writeAttr(SUMO_ATTR_CUSTOMSHAPE, true);
486  }
487  if (n.getType() == NODETYPE_DEAD_END) {
488  into.closeTag();
489  } else {
490  // write right-of-way logics
491  n.writeLogic(into, checkLaneFoes);
492  into.closeTag();
493  }
494 }
495 
496 
497 bool
499  bool ret = false;
500  const std::vector<NBEdge*>& incoming = n.getIncomingEdges();
501  // build the list of internal lane ids
502  std::vector<std::string> internalLaneIDs;
503  for (EdgeVector::const_iterator i = incoming.begin(); i != incoming.end(); i++) {
504  const std::vector<NBEdge::Connection>& elv = (*i)->getConnections();
505  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
506  if ((*k).toEdge != 0) {
507  internalLaneIDs.push_back((*k).getInternalLaneID());
508  }
509  }
510  }
511  const std::vector<NBNode::Crossing>& crossings = n.getCrossings();
512  for (std::vector<NBNode::Crossing>::const_iterator it_c = crossings.begin(); it_c != crossings.end(); ++it_c) {
513  internalLaneIDs.push_back((*it_c).id + "_0");
514  }
515  // write the internal nodes
516  for (std::vector<NBEdge*>::const_iterator i = incoming.begin(); i != incoming.end(); i++) {
517  const std::vector<NBEdge::Connection>& elv = (*i)->getConnections();
518  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
519  if ((*k).toEdge == 0 || !(*k).haveVia) {
520  continue;
521  }
522  Position pos = (*k).shape[-1];
523  into.openTag(SUMO_TAG_JUNCTION).writeAttr(SUMO_ATTR_ID, (*k).viaID + "_0");
525  NWFrame::writePositionLong(pos, into);
526  std::string incLanes = (*k).getInternalLaneID();
527  if ((*k).foeIncomingLanes.length() != 0) {
528  incLanes += " " + (*k).foeIncomingLanes;
529  }
530  into.writeAttr(SUMO_ATTR_INCLANES, incLanes);
531  const std::vector<unsigned int>& foes = (*k).foeInternalLinks;
532  std::vector<std::string> foeIDs;
533  for (std::vector<unsigned int>::const_iterator it = foes.begin(); it != foes.end(); ++it) {
534  foeIDs.push_back(internalLaneIDs[*it]);
535  }
536  into.writeAttr(SUMO_ATTR_INTLANES, joinToString(foeIDs, " "));
537  into.closeTag();
538  ret = true;
539  }
540  }
541  return ret;
542 }
543 
544 
545 void
547  bool includeInternal, ConnectionStyle style) {
548  assert(c.toEdge != 0);
550  into.writeAttr(SUMO_ATTR_FROM, from.getID());
551  into.writeAttr(SUMO_ATTR_TO, c.toEdge->getID());
554  if (c.mayDefinitelyPass && style != TLL) {
556  }
557  if ((from.getToNode()->getKeepClear() == false || c.keepClear == false) && style != TLL) {
558  into.writeAttr<bool>(SUMO_ATTR_KEEP_CLEAR, false);
559  }
560  if (c.contPos != NBEdge::UNSPECIFIED_CONTPOS && style != TLL) {
562  }
563  if (style != PLAIN) {
564  if (includeInternal) {
566  }
567  // set information about the controlling tl if any
568  if (c.tlID != "") {
569  into.writeAttr(SUMO_ATTR_TLID, c.tlID);
571  }
572  if (style == SUMONET) {
573  // write the direction information
574  LinkDirection dir = from.getToNode()->getDirection(&from, c.toEdge, OptionsCont::getOptions().getBool("lefthand"));
575  assert(dir != LINKDIR_NODIR);
576  into.writeAttr(SUMO_ATTR_DIR, toString(dir));
577  // write the state information
578  const LinkState linkState = from.getToNode()->getLinkState(
579  &from, c.toEdge, c.fromLane, c.toLane, c.mayDefinitelyPass, c.tlID);
580  into.writeAttr(SUMO_ATTR_STATE, linkState);
581  }
582  }
583  into.closeTag();
584 }
585 
586 
587 bool
589  bool ret = false;
590  const std::vector<NBEdge*>& incoming = n.getIncomingEdges();
591  for (std::vector<NBEdge*>::const_iterator i = incoming.begin(); i != incoming.end(); ++i) {
592  NBEdge* from = *i;
593  const std::vector<NBEdge::Connection>& connections = from->getConnections();
594  for (std::vector<NBEdge::Connection>::const_iterator j = connections.begin(); j != connections.end(); ++j) {
595  const NBEdge::Connection& c = *j;
596  assert(c.toEdge != 0);
597  if (c.haveVia) {
598  // internal split
599  writeInternalConnection(into, c.id, c.toEdge->getID(), c.internalLaneIndex, c.toLane, c.viaID + "_0");
600  writeInternalConnection(into, c.viaID, c.toEdge->getID(), 0, c.toLane, "");
601  } else {
602  // no internal split
604  }
605  ret = true;
606  }
607  }
608  return ret;
609 }
610 
611 
612 void
614  const std::string& from, const std::string& to,
615  int fromLane, int toLane, const std::string& via) {
617  into.writeAttr(SUMO_ATTR_FROM, from);
618  into.writeAttr(SUMO_ATTR_TO, to);
619  into.writeAttr(SUMO_ATTR_FROM_LANE, fromLane);
620  into.writeAttr(SUMO_ATTR_TO_LANE, toLane);
621  if (via != "") {
622  into.writeAttr(SUMO_ATTR_VIA, via);
623  }
624  into.writeAttr(SUMO_ATTR_DIR, "s");
625  into.writeAttr(SUMO_ATTR_STATE, (via != "" ? "m" : "M"));
626  into.closeTag();
627 }
628 
629 
630 void
631 NWWriter_SUMO::writeRoundabouts(OutputDevice& into, const std::set<EdgeSet>& roundabouts,
632  const NBEdgeCont& ec) {
633  // make output deterministic
634  std::vector<std::vector<std::string> > edgeIDs;
635  for (std::set<EdgeSet>::const_iterator i = roundabouts.begin(); i != roundabouts.end(); ++i) {
636  std::vector<std::string> tEdgeIDs;
637  for (EdgeSet::const_iterator j = (*i).begin(); j != (*i).end(); ++j) {
638  // the edges may have been erased from NBEdgeCont but their pointers are still valid
639  // we verify their existance in writeRoundabout()
640  tEdgeIDs.push_back((*j)->getID());
641  }
642  std::sort(tEdgeIDs.begin(), tEdgeIDs.end());
643  edgeIDs.push_back(tEdgeIDs);
644  }
645  std::sort(edgeIDs.begin(), edgeIDs.end());
646  // write
647  for (std::vector<std::vector<std::string> >::const_iterator i = edgeIDs.begin(); i != edgeIDs.end(); ++i) {
648  writeRoundabout(into, *i, ec);
649  }
650  if (roundabouts.size() != 0) {
651  into.lf();
652  }
653 }
654 
655 
656 void
657 NWWriter_SUMO::writeRoundabout(OutputDevice& into, const std::vector<std::string>& edgeIDs,
658  const NBEdgeCont& ec) {
659  std::vector<std::string> validEdgeIDs;
660  std::vector<std::string> invalidEdgeIDs;
661  std::vector<std::string> nodeIDs;
662  for (std::vector<std::string>::const_iterator i = edgeIDs.begin(); i != edgeIDs.end(); ++i) {
663  const NBEdge* edge = ec.retrieve(*i);
664  if (edge != 0) {
665  nodeIDs.push_back(edge->getToNode()->getID());
666  validEdgeIDs.push_back(edge->getID());
667  } else {
668  invalidEdgeIDs.push_back(*i);
669  }
670  }
671  std::sort(nodeIDs.begin(), nodeIDs.end());
672  if (validEdgeIDs.size() > 0) {
674  into.writeAttr(SUMO_ATTR_NODES, joinToString(nodeIDs, " "));
675  into.writeAttr(SUMO_ATTR_EDGES, joinToString(validEdgeIDs, " "));
676  into.closeTag();
677  if (invalidEdgeIDs.size() > 0) {
678  WRITE_WARNING("Writing incomplete roundabout. Edges: '"
679  + joinToString(invalidEdgeIDs, " ") + "' no longer exist'");
680  }
681  }
682 }
683 
684 
685 void
687  std::vector<SUMOReal> sourceW = d.getSourceWeights();
689  std::vector<SUMOReal> sinkW = d.getSinkWeights();
691  // write the head and the id of the district
693  if (d.getShape().size() > 0) {
695  }
696  size_t i;
697  // write all sources
698  const std::vector<NBEdge*>& sources = d.getSourceEdges();
699  for (i = 0; i < sources.size(); i++) {
700  // write the head and the id of the source
701  into.openTag(SUMO_TAG_TAZSOURCE).writeAttr(SUMO_ATTR_ID, sources[i]->getID()).writeAttr(SUMO_ATTR_WEIGHT, sourceW[i]);
702  into.closeTag();
703  }
704  // write all sinks
705  const std::vector<NBEdge*>& sinks = d.getSinkEdges();
706  for (i = 0; i < sinks.size(); i++) {
707  // write the head and the id of the sink
708  into.openTag(SUMO_TAG_TAZSINK).writeAttr(SUMO_ATTR_ID, sinks[i]->getID()).writeAttr(SUMO_ATTR_WEIGHT, sinkW[i]);
709  into.closeTag();
710  }
711  // write the tail
712  into.closeTag();
713 }
714 
715 
716 std::string
718  SUMOReal time = STEPS2TIME(steps);
719  if (time == std::floor(time)) {
720  return toString(int(time));
721  } else {
722  return toString(time);
723  }
724 }
725 
726 
727 void
729  for (NBConnectionProhibits::const_iterator j = prohibitions.begin(); j != prohibitions.end(); j++) {
730  NBConnection prohibited = (*j).first;
731  const NBConnectionVector& prohibiting = (*j).second;
732  for (NBConnectionVector::const_iterator k = prohibiting.begin(); k != prohibiting.end(); k++) {
733  NBConnection prohibitor = *k;
737  into.closeTag();
738  }
739  }
740 }
741 
742 
743 std::string
745  return c.getFrom()->getID() + "->" + c.getTo()->getID();
746 }
747 
748 
749 void
751  std::vector<NBTrafficLightLogic*> logics = tllCont.getComputed();
752  for (std::vector<NBTrafficLightLogic*>::iterator it = logics.begin(); it != logics.end(); it++) {
754  into.writeAttr(SUMO_ATTR_ID, (*it)->getID());
755  into.writeAttr(SUMO_ATTR_TYPE, (*it)->getType());
756  into.writeAttr(SUMO_ATTR_PROGRAMID, (*it)->getProgramID());
757  into.writeAttr(SUMO_ATTR_OFFSET, writeSUMOTime((*it)->getOffset()));
758  // write params
759  const std::map<std::string, std::string>& params = (*it)->getMap();
760  for (std::map<std::string, std::string>::const_iterator i = params.begin(); i != params.end(); ++i) {
761  into.openTag(SUMO_TAG_PARAM);
762  into.writeAttr(SUMO_ATTR_KEY, (*i).first);
763  into.writeAttr(SUMO_ATTR_VALUE, (*i).second);
764  into.closeTag();
765  }
766  // write the phases
767  const std::vector<NBTrafficLightLogic::PhaseDefinition>& phases = (*it)->getPhases();
768  for (std::vector<NBTrafficLightLogic::PhaseDefinition>::const_iterator j = phases.begin(); j != phases.end(); ++j) {
769  into.openTag(SUMO_TAG_PHASE);
770  into.writeAttr(SUMO_ATTR_DURATION, writeSUMOTime(j->duration));
771  into.writeAttr(SUMO_ATTR_STATE, j->state);
772  into.closeTag();
773  }
774  into.closeTag();
775  }
776  if (logics.size() > 0) {
777  into.lf();
778  }
779 }
780 /****************************************************************************/
781 
static void writeRoundabout(OutputDevice &into, const std::vector< std::string > &r, const NBEdgeCont &ec)
Writes a roundabout.
std::string id
Definition: NBEdge.h:181
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:441
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:240
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:905
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:278
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:2438
long long int SUMOTime
Definition: SUMOTime.h:43
a list of node ids, used for controlling joining
static const SUMOReal UNSPECIFIED_WIDTH
unspecified lane width
Definition: NBEdge.h:201
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:186
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:414
SUMOReal getRadius() const
Returns the turning radius of this node.
Definition: NBNode.h:272
const std::set< EdgeSet > getRoundabouts() const
Returns the determined roundabouts.
Definition: NBEdgeCont.cpp:933
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:171
SUMOReal width
This lane&#39;s width.
Definition: NBNode.h:169
bool isTLControlled() const
Returns whether this node is controlled by any tls.
Definition: NBNode.h:304
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:500
const Crossing & getCrossing(const std::string &id) const
return the crossing with the given id
Definition: NBNode.cpp:2414
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:70
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:640
T MAX2(T a, T b)
Definition: StdDefs.h:79
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:520
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:167
static const SUMOReal UNSPECIFIED_CONTPOS
unspecified internal junction position
Definition: NBEdge.h:207
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:203
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:151
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:82
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:413
An (internal) definition of a single lane of an edge.
Definition: NBEdge.h:122
const std::string & getID() const
Returns the id.
Definition: Named.h:65
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:228
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:318
the edges of a route
int getPriority() const
Returns the priority of the edge.
Definition: NBEdge.h:353
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:2135
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:79
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 (<lane ...) of an edge.
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:265
static void writeEdge(OutputDevice &into, const NBEdge &e, bool noNames, bool origNames)
Writes an edge (<edge ...)
bool hasLoadedLength() const
Returns whether a length was set explicitly.
Definition: NBEdge.h:421
#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.
SUMOReal contPos
custom position for internal junction on this connection
Definition: NBEdge.h:176
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
#define POSITION_EPS
Definition: config.h:188
const CustomShapeMap & getCustomLaneShapes() const
sets a custom shape for an internal lane
Definition: NBNode.h:505
LinkState getLinkState(const NBEdge *incoming, NBEdge *outgoing, int fromLane, int toLane, bool mayDefinitelyPass, const std::string &tlID) const
Definition: NBNode.cpp:1491
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.
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
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:1427
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:1591
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 (<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:369
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:246
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:153
std::vector< NBEdge * > EdgeVector
Definition: NBCont.h:41
static bool writeInternalNodes(OutputDevice &into, const NBNode &n)
Writes internal junctions (<junction with id[0]==&#39;:&#39; ...) of the given node.
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:521
A definition of a pedestrian walking area.
Definition: NBNode.h:160
bool isMacroscopicConnector() const
Returns whether this edge was marked as a macroscopic connector.
Definition: NBEdge.h:859
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:894
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:601
Represents a single node (junction) during network building.
Definition: NBNode.h:74
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:132
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:214
static const SUMOReal UNSPECIFIED_RADIUS
unspecified lane width
Definition: NBNode.h:189
static void writeJunction(OutputDevice &into, const NBNode &n, const bool checkLaneFoes)
Writes a junction (<junction ...)
bool writeLogic(OutputDevice &into, const bool checkLaneFoes) const
Definition: NBNode.cpp:694
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:194
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
const std::vector< WalkingArea > & getWalkingAreas() const
return this junctions pedestrian walking areas
Definition: NBNode.h:645
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:463
const std::vector< Connection > & getConnections() const
Returns the connections.
Definition: NBEdge.h:761
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:173
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:361