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-sim.org/
12 // Copyright (C) 2001-2014 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-sim.org/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
78  writeLocation(device);
79 
80  // write inner lanes
81  bool origNames = oc.getBool("output.original-names");
82  if (!oc.getBool("no-internal-links")) {
83  bool hadAny = false;
84  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
85  hadAny |= writeInternalEdges(device, *(*i).second, origNames);
86  }
87  if (hadAny) {
88  device.lf();
89  }
90  }
91 
92  // write edges with lanes and connected edges
93  bool noNames = !oc.getBool("output.street-names");
94  for (std::map<std::string, NBEdge*>::const_iterator i = ec.begin(); i != ec.end(); ++i) {
95  writeEdge(device, *(*i).second, noNames, origNames);
96  }
97  device.lf();
98 
99  // write tls logics
100  writeTrafficLights(device, nb.getTLLogicCont());
101 
102  // write the nodes (junctions)
103  std::set<NBNode*> roundaboutNodes;
104  const bool checkLaneFoesAll = oc.getBool("check-lane-foes.all");
105  const bool checkLaneFoesRoundabout = !checkLaneFoesAll && oc.getBool("check-lane-foes.roundabout");
106  if (checkLaneFoesRoundabout) {
107  const std::vector<EdgeVector>& roundabouts = nb.getRoundabouts();
108  for (std::vector<EdgeVector>::const_iterator i = roundabouts.begin(); i != roundabouts.end(); ++i) {
109  for (EdgeVector::const_iterator j = (*i).begin(); j != (*i).end(); ++j) {
110  roundaboutNodes.insert((*j)->getToNode());
111  }
112  }
113  }
114  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
115  const bool checkLaneFoes = checkLaneFoesAll || (checkLaneFoesRoundabout && roundaboutNodes.count((*i).second) > 0);
116  writeJunction(device, *(*i).second, checkLaneFoes);
117  }
118  device.lf();
119  const bool includeInternal = !oc.getBool("no-internal-links");
120  if (includeInternal) {
121  // ... internal nodes if not unwanted
122  bool hadAny = false;
123  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
124  hadAny |= writeInternalNodes(device, *(*i).second);
125  }
126  if (hadAny) {
127  device.lf();
128  }
129  }
130 
131  // write the successors of lanes
132  unsigned int numConnections = 0;
133  for (std::map<std::string, NBEdge*>::const_iterator it_edge = ec.begin(); it_edge != ec.end(); it_edge++) {
134  NBEdge* from = it_edge->second;
136  const std::vector<NBEdge::Connection> connections = from->getConnections();
137  numConnections += (unsigned int)connections.size();
138  for (std::vector<NBEdge::Connection>::const_iterator it_c = connections.begin(); it_c != connections.end(); it_c++) {
139  writeConnection(device, *from, *it_c, includeInternal);
140  }
141  }
142  if (numConnections > 0) {
143  device.lf();
144  }
145  if (includeInternal) {
146  // ... internal successors if not unwanted
147  bool hadAny = false;
148  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
149  hadAny |= writeInternalConnections(device, *(*i).second);
150  }
151  if (hadAny) {
152  device.lf();
153  }
154  }
155  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
156  NBNode* node = (*i).second;
157  // write connections from pedestrian crossings
158  const std::vector<NBNode::Crossing>& crossings = node->getCrossings();
159  for (std::vector<NBNode::Crossing>::const_iterator it = crossings.begin(); it != crossings.end(); it++) {
160  NWWriter_SUMO::writeInternalConnection(device, (*it).id, (*it).nextWalkingArea, 0, 0, "");
161  }
162  // write connections from pedestrian walking areas
163  const std::vector<NBNode::WalkingArea>& WalkingAreas = node->getWalkingAreas();
164  for (std::vector<NBNode::WalkingArea>::const_iterator it = WalkingAreas.begin(); it != WalkingAreas.end(); it++) {
165  if ((*it).nextCrossing != "") {
166  const NBNode::Crossing& nextCrossing = node->getCrossing((*it).nextCrossing);
167  // connection to next crossing (may be tls-controlled)
169  device.writeAttr(SUMO_ATTR_FROM, (*it).id);
170  device.writeAttr(SUMO_ATTR_TO, (*it).nextCrossing);
171  device.writeAttr(SUMO_ATTR_FROM_LANE, 0);
172  device.writeAttr(SUMO_ATTR_TO_LANE, 0);
173  if ((*it).tlID != "") {
174  device.writeAttr(SUMO_ATTR_TLID, (*it).tlID);
175  assert(nextCrossing.tlLinkNo >= 0);
176  device.writeAttr(SUMO_ATTR_TLLINKINDEX, nextCrossing.tlLinkNo);
177  }
180  device.closeTag();
181  }
182  // optional connections from/to sidewalk
183  for (std::vector<std::string>::const_iterator it_sw = (*it).nextSidewalks.begin(); it_sw != (*it).nextSidewalks.end(); ++it_sw) {
184  NWWriter_SUMO::writeInternalConnection(device, (*it).id, (*it_sw), 0, 0, "");
185  }
186  for (std::vector<std::string>::const_iterator it_sw = (*it).prevSidewalks.begin(); it_sw != (*it).prevSidewalks.end(); ++it_sw) {
187  NWWriter_SUMO::writeInternalConnection(device, (*it_sw), (*it).id, 0, 0, "");
188  }
189  }
190  }
191 
192  // write loaded prohibitions
193  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
194  writeProhibitions(device, i->second->getProhibitions());
195  }
196 
197  // write roundabout information
198  const std::vector<EdgeVector>& roundabouts = nb.getRoundabouts();
199  // make output deterministic
200  std::vector<std::vector<std::string> > edgeIDs;
201  for (std::vector<EdgeVector>::const_iterator i = roundabouts.begin(); i != roundabouts.end(); ++i) {
202  std::vector<std::string> tEdgeIDs;
203  for (EdgeVector::const_iterator j = (*i).begin(); j != (*i).end(); ++j) {
204  tEdgeIDs.push_back((*j)->getID());
205  }
206  std::sort(tEdgeIDs.begin(), tEdgeIDs.end());
207  edgeIDs.push_back(tEdgeIDs);
208  }
209  std::sort(edgeIDs.begin(), edgeIDs.end());
210  // write
211  for (std::vector<std::vector<std::string> >::const_iterator i = edgeIDs.begin(); i != edgeIDs.end(); ++i) {
212  writeRoundabout(device, *i, ec);
213  }
214  if (roundabouts.size() != 0) {
215  device.lf();
216  }
217 
218  // write the districts
219  for (std::map<std::string, NBDistrict*>::const_iterator i = dc.begin(); i != dc.end(); i++) {
220  writeDistrict(device, *(*i).second);
221  }
222  if (dc.size() != 0) {
223  device.lf();
224  }
225  device.close();
226 }
227 
228 
229 bool
230 NWWriter_SUMO::writeInternalEdges(OutputDevice& into, const NBNode& n, bool origNames) {
231  bool ret = false;
232  const EdgeVector& incoming = n.getIncomingEdges();
233  for (EdgeVector::const_iterator i = incoming.begin(); i != incoming.end(); i++) {
234  const std::vector<NBEdge::Connection>& elv = (*i)->getConnections();
235  if (elv.size() > 0) {
236  bool haveVia = false;
237  NBEdge* toEdge = 0;
238  std::string internalEdgeID = "";
239  // first pass: compute average lengths of non-via edges
240  std::map<NBEdge*, SUMOReal> lengthSum;
241  std::map<NBEdge*, int> numLanes;
242  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
243  lengthSum[(*k).toEdge] += MAX2((*k).shape.length(), POSITION_EPS);
244  numLanes[(*k).toEdge] += 1;
245  }
246  // second pass: write non-via edges
247  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
248  if ((*k).toEdge == 0) {
249  assert(false); // should never happen. tell me when it does
250  continue;
251  }
252  if (toEdge != (*k).toEdge) {
253  internalEdgeID = (*k).id;
254  if (toEdge != 0) {
255  // close the previous edge
256  into.closeTag();
257  }
258  toEdge = (*k).toEdge;
259  into.openTag(SUMO_TAG_EDGE);
260  into.writeAttr(SUMO_ATTR_ID, internalEdgeID);
262  // open a new edge
263  }
264  // to avoid changing to an internal lane which has a successor
265  // with the wrong permissions we need to inherit them from the successor
266  const NBEdge::Lane& successor = (*k).toEdge->getLanes()[(*k).toLane];
267  const SUMOReal length = lengthSum[toEdge] / numLanes[toEdge];
268  // @note the actual length should be used once sumo supports lanes of
269  // varying length within the same edge
270  //const SUMOReal length = MAX2((*k).shape.length(), POSITION_EPS);
271  writeLane(into, internalEdgeID, (*k).getInternalLaneID(), (*k).vmax,
272  successor.permissions, successor.preferred,
273  NBEdge::UNSPECIFIED_OFFSET, successor.width, (*k).shape, (*k).origID,
274  length, (*k).internalLaneIndex, origNames);
275  haveVia = haveVia || (*k).haveVia;
276  }
277  ret = true;
278  into.closeTag(); // close the last edge
279  // third pass: write via edges
280  if (haveVia) {
281  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
282  if (!(*k).haveVia) {
283  continue;
284  }
285  if ((*k).toEdge == 0) {
286  assert(false); // should never happen. tell me when it does
287  continue;
288  }
289  const NBEdge::Lane& successor = (*k).toEdge->getLanes()[(*k).toLane];
290  into.openTag(SUMO_TAG_EDGE);
291  into.writeAttr(SUMO_ATTR_ID, (*k).viaID);
293  writeLane(into, (*k).viaID, (*k).viaID + "_0", (*k).viaVmax, SVCAll, SVCAll,
294  NBEdge::UNSPECIFIED_OFFSET, successor.width, (*k).viaShape, (*k).origID,
295  MAX2((*k).viaShape.length(), POSITION_EPS), // microsim needs positive length
296  0, origNames);
297  into.closeTag();
298  }
299  }
300  }
301  }
302  // write pedestrian crossings
303  const std::vector<NBNode::Crossing>& crossings = n.getCrossings();
304  for (std::vector<NBNode::Crossing>::const_iterator it = crossings.begin(); it != crossings.end(); it++) {
305  into.openTag(SUMO_TAG_EDGE);
306  into.writeAttr(SUMO_ATTR_ID, (*it).id);
308  into.writeAttr(SUMO_ATTR_CROSSING_EDGES, (*it).edges);
309  writeLane(into, (*it).id, (*it).id + "_0", 1, SVC_PEDESTRIAN, 0,
310  NBEdge::UNSPECIFIED_OFFSET, (*it).width, (*it).shape, "", (*it).shape.length(), 0, false);
311  into.closeTag();
312  }
313  // write pedestrian walking areas
314  const std::vector<NBNode::WalkingArea>& WalkingAreas = n.getWalkingAreas();
315  for (std::vector<NBNode::WalkingArea>::const_iterator it = WalkingAreas.begin(); it != WalkingAreas.end(); it++) {
316  const NBNode::WalkingArea& wa = *it;
317  into.openTag(SUMO_TAG_EDGE);
318  into.writeAttr(SUMO_ATTR_ID, wa.id);
320  writeLane(into, wa.id, wa.id + "_0", 1, SVC_PEDESTRIAN, 0,
321  NBEdge::UNSPECIFIED_OFFSET, wa.width, wa.shape, "", wa.length, 0, false);
322  into.closeTag();
323  }
324  return ret;
325 }
326 
327 
328 void
329 NWWriter_SUMO::writeEdge(OutputDevice& into, const NBEdge& e, bool noNames, bool origNames) {
330  // write the edge's begin
333  into.writeAttr(SUMO_ATTR_TO, e.getToNode()->getID());
334  if (!noNames && e.getStreetName() != "") {
336  }
338  if (e.getTypeName() != "") {
340  }
341  if (e.isMacroscopicConnector()) {
343  }
344  // write the spread type if not default ("right")
347  }
348  if (e.hasLoadedLength()) {
350  }
351  if (!e.hasDefaultGeometry()) {
353  }
354  // write the lanes
355  const std::vector<NBEdge::Lane>& lanes = e.getLanes();
356 
357  SUMOReal length = e.getLoadedLength();
358  if (OptionsCont::getOptions().getBool("no-internal-links") && !e.hasLoadedLength()) {
359  // use length to junction center even if a modified geometry was given
360  PositionVector geom = e.getGeometry();
363  length = geom.length();
364  }
365  if (length <= 0) {
366  length = POSITION_EPS;
367  }
368  for (unsigned int i = 0; i < (unsigned int) lanes.size(); i++) {
369  const NBEdge::Lane& l = lanes[i];
370  writeLane(into, e.getID(), e.getLaneID(i), l.speed,
371  l.permissions, l.preferred, l.endOffset, l.width, l.shape, l.origID,
372  length, i, origNames);
373  }
374  // close the edge
375  into.closeTag();
376 }
377 
378 
379 void
380 NWWriter_SUMO::writeLane(OutputDevice& into, const std::string& eID, const std::string& lID,
381  SUMOReal speed, SVCPermissions permissions, SVCPermissions preferred,
382  SUMOReal endOffset, SUMOReal width, const PositionVector& shape,
383  const std::string& origID, SUMOReal length, unsigned int index, bool origNames) {
384  // output the lane's attributes
386  // the first lane of an edge will be the depart lane
387  into.writeAttr(SUMO_ATTR_INDEX, index);
388  // write the list of allowed/disallowed vehicle classes
389  writePermissions(into, permissions);
390  writePreferences(into, preferred);
391  // some further information
392  if (speed == 0) {
393  WRITE_WARNING("Lane #" + toString(index) + " of edge '" + eID + "' has a maximum velocity of 0.");
394  } else if (speed < 0) {
395  throw ProcessError("Negative velocity (" + toString(speed) + " on edge '" + eID + "' lane#" + toString(index) + ".");
396  }
397  if (endOffset > 0) {
398  length = length - endOffset;
399  }
400  into.writeAttr(SUMO_ATTR_SPEED, speed);
401  into.writeAttr(SUMO_ATTR_LENGTH, length);
402  if (endOffset != NBEdge::UNSPECIFIED_OFFSET) {
403  into.writeAttr(SUMO_ATTR_ENDOFFSET, endOffset);
404  }
405  if (width != NBEdge::UNSPECIFIED_WIDTH) {
406  into.writeAttr(SUMO_ATTR_WIDTH, width);
407  }
408  into.writeAttr(SUMO_ATTR_SHAPE, endOffset > 0 ?
409  shape.getSubpart(0, shape.length() - endOffset) : shape);
410  if (origNames && origID != "") {
411  into.openTag(SUMO_TAG_PARAM);
412  into.writeAttr(SUMO_ATTR_KEY, "origId");
413  into.writeAttr(SUMO_ATTR_VALUE, origID);
414  into.closeTag();
415  into.closeTag();
416  } else {
417  into.closeTag();
418  }
419 }
420 
421 
422 void
423 NWWriter_SUMO::writeJunction(OutputDevice& into, const NBNode& n, const bool checkLaneFoes) {
424  // write the attributes
426  into.writeAttr(SUMO_ATTR_TYPE, n.getType());
428  // write the incoming lanes
429  std::string incLanes;
430  const std::vector<NBEdge*>& incoming = n.getIncomingEdges();
431  for (std::vector<NBEdge*>::const_iterator i = incoming.begin(); i != incoming.end(); ++i) {
432  unsigned int noLanes = (*i)->getNumLanes();
433  for (unsigned int j = 0; j < noLanes; j++) {
434  incLanes += (*i)->getLaneID(j);
435  if (i != incoming.end() - 1 || j < noLanes - 1) {
436  incLanes += ' ';
437  }
438  }
439  }
440  const std::vector<NBNode::Crossing>& crossings = n.getCrossings();
441  for (std::vector<NBNode::Crossing>::const_iterator it = crossings.begin(); it != crossings.end(); it++) {
442  incLanes += ' ' + (*it).prevWalkingArea + "_0";
443  }
444  into.writeAttr(SUMO_ATTR_INCLANES, incLanes);
445  // write the internal lanes
446  std::string intLanes;
447  if (!OptionsCont::getOptions().getBool("no-internal-links")) {
448  unsigned int l = 0;
449  for (EdgeVector::const_iterator i = incoming.begin(); i != incoming.end(); i++) {
450  const std::vector<NBEdge::Connection>& elv = (*i)->getConnections();
451  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
452  if ((*k).toEdge == 0) {
453  continue;
454  }
455  if (l != 0) {
456  intLanes += ' ';
457  }
458  if (!(*k).haveVia) {
459  intLanes += (*k).getInternalLaneID();
460  } else {
461  intLanes += (*k).viaID + "_0";
462  }
463  l++;
464  }
465  }
466  }
467  for (std::vector<NBNode::Crossing>::const_iterator it = crossings.begin(); it != crossings.end(); it++) {
468  intLanes += ' ' + (*it).id + "_0";
469  }
470  into.writeAttr(SUMO_ATTR_INTLANES, intLanes);
471  // close writing
473  if (n.getType() == NODETYPE_DEAD_END) {
474  into.closeTag();
475  } else {
476  // write right-of-way logics
477  n.writeLogic(into, checkLaneFoes);
478  into.closeTag();
479  }
480 }
481 
482 
483 bool
485  bool ret = false;
486  const std::vector<NBEdge*>& incoming = n.getIncomingEdges();
487  // build the list of internal lane ids
488  std::vector<std::string> internalLaneIDs;
489  for (EdgeVector::const_iterator i = incoming.begin(); i != incoming.end(); i++) {
490  const std::vector<NBEdge::Connection>& elv = (*i)->getConnections();
491  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
492  if ((*k).toEdge != 0) {
493  internalLaneIDs.push_back((*k).getInternalLaneID());
494  }
495  }
496  }
497  const std::vector<NBNode::Crossing>& crossings = n.getCrossings();
498  for (std::vector<NBNode::Crossing>::const_iterator it_c = crossings.begin(); it_c != crossings.end(); ++it_c) {
499  internalLaneIDs.push_back((*it_c).id + "_0");
500  }
501  // write the internal nodes
502  for (std::vector<NBEdge*>::const_iterator i = incoming.begin(); i != incoming.end(); i++) {
503  const std::vector<NBEdge::Connection>& elv = (*i)->getConnections();
504  for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
505  if ((*k).toEdge == 0 || !(*k).haveVia) {
506  continue;
507  }
508  Position pos = (*k).shape[-1];
509  into.openTag(SUMO_TAG_JUNCTION).writeAttr(SUMO_ATTR_ID, (*k).viaID + "_0");
511  NWFrame::writePositionLong(pos, into);
512  std::string incLanes = (*k).getInternalLaneID();
513  if ((*k).foeIncomingLanes.length() != 0) {
514  incLanes += " " + (*k).foeIncomingLanes;
515  }
516  into.writeAttr(SUMO_ATTR_INCLANES, incLanes);
517  const std::vector<unsigned int>& foes = (*k).foeInternalLinks;
518  std::vector<std::string> foeIDs;
519  for (std::vector<unsigned int>::const_iterator it = foes.begin(); it != foes.end(); ++it) {
520  foeIDs.push_back(internalLaneIDs[*it]);
521  }
522  into.writeAttr(SUMO_ATTR_INTLANES, joinToString(foeIDs, " "));
523  into.closeTag();
524  ret = true;
525  }
526  }
527  return ret;
528 }
529 
530 
531 void
533  bool includeInternal, ConnectionStyle style) {
534  assert(c.toEdge != 0);
536  into.writeAttr(SUMO_ATTR_FROM, from.getID());
537  into.writeAttr(SUMO_ATTR_TO, c.toEdge->getID());
540  if (c.mayDefinitelyPass && style != TLL) {
542  }
543  if (style != PLAIN) {
544  if (includeInternal) {
546  }
547  // set information about the controlling tl if any
548  if (c.tlID != "") {
549  into.writeAttr(SUMO_ATTR_TLID, c.tlID);
551  }
552  if (style == SUMONET) {
553  // write the direction information
554  LinkDirection dir = from.getToNode()->getDirection(&from, c.toEdge);
555  assert(dir != LINKDIR_NODIR);
556  into.writeAttr(SUMO_ATTR_DIR, toString(dir));
557  // write the state information
558  const LinkState linkState = from.getToNode()->getLinkState(
559  &from, c.toEdge, c.toLane, c.mayDefinitelyPass, c.tlID);
560  into.writeAttr(SUMO_ATTR_STATE, linkState);
561  }
562  }
563  into.closeTag();
564 }
565 
566 
567 bool
569  bool ret = false;
570  const std::vector<NBEdge*>& incoming = n.getIncomingEdges();
571  for (std::vector<NBEdge*>::const_iterator i = incoming.begin(); i != incoming.end(); ++i) {
572  NBEdge* from = *i;
573  const std::vector<NBEdge::Connection>& connections = from->getConnections();
574  for (std::vector<NBEdge::Connection>::const_iterator j = connections.begin(); j != connections.end(); ++j) {
575  const NBEdge::Connection& c = *j;
576  assert(c.toEdge != 0);
577  if (c.haveVia) {
578  // internal split
579  writeInternalConnection(into, c.id, c.toEdge->getID(), c.internalLaneIndex, c.toLane, c.viaID + "_0");
580  writeInternalConnection(into, c.viaID, c.toEdge->getID(), 0, c.toLane, "");
581  } else {
582  // no internal split
584  }
585  ret = true;
586  }
587  }
588  return ret;
589 }
590 
591 
592 void
594  const std::string& from, const std::string& to,
595  int fromLane, int toLane, const std::string& via) {
597  into.writeAttr(SUMO_ATTR_FROM, from);
598  into.writeAttr(SUMO_ATTR_TO, to);
599  into.writeAttr(SUMO_ATTR_FROM_LANE, fromLane);
600  into.writeAttr(SUMO_ATTR_TO_LANE, toLane);
601  if (via != "") {
602  into.writeAttr(SUMO_ATTR_VIA, via);
603  }
604  into.writeAttr(SUMO_ATTR_DIR, "s");
605  into.writeAttr(SUMO_ATTR_STATE, (via != "" ? "m" : "M"));
606  into.closeTag();
607 }
608 
609 
610 void
611 NWWriter_SUMO::writeRoundabout(OutputDevice& into, const std::vector<std::string>& edgeIDs,
612  const NBEdgeCont& ec) {
613  std::vector<std::string> nodeIDs;
614  for (std::vector<std::string>::const_iterator i = edgeIDs.begin(); i != edgeIDs.end(); ++i) {
615  nodeIDs.push_back(ec.retrieve(*i)->getToNode()->getID());
616  }
617  std::sort(nodeIDs.begin(), nodeIDs.end());
619  into.writeAttr(SUMO_ATTR_NODES, joinToString(nodeIDs, " "));
620  into.writeAttr(SUMO_ATTR_EDGES, joinToString(edgeIDs, " "));
621  into.closeTag();
622 }
623 
624 
625 void
627  std::vector<SUMOReal> sourceW = d.getSourceWeights();
629  std::vector<SUMOReal> sinkW = d.getSinkWeights();
631  // write the head and the id of the district
633  if (d.getShape().size() > 0) {
635  }
636  size_t i;
637  // write all sources
638  const std::vector<NBEdge*>& sources = d.getSourceEdges();
639  for (i = 0; i < sources.size(); i++) {
640  // write the head and the id of the source
641  into.openTag(SUMO_TAG_TAZSOURCE).writeAttr(SUMO_ATTR_ID, sources[i]->getID()).writeAttr(SUMO_ATTR_WEIGHT, sourceW[i]);
642  into.closeTag();
643  }
644  // write all sinks
645  const std::vector<NBEdge*>& sinks = d.getSinkEdges();
646  for (i = 0; i < sinks.size(); i++) {
647  // write the head and the id of the sink
648  into.openTag(SUMO_TAG_TAZSINK).writeAttr(SUMO_ATTR_ID, sinks[i]->getID()).writeAttr(SUMO_ATTR_WEIGHT, sinkW[i]);
649  into.closeTag();
650  }
651  // write the tail
652  into.closeTag();
653 }
654 
655 
656 std::string
658  SUMOReal time = STEPS2TIME(steps);
659  if (time == std::floor(time)) {
660  return toString(int(time));
661  } else {
662  return toString(time);
663  }
664 }
665 
666 
667 void
669  for (NBConnectionProhibits::const_iterator j = prohibitions.begin(); j != prohibitions.end(); j++) {
670  NBConnection prohibited = (*j).first;
671  const NBConnectionVector& prohibiting = (*j).second;
672  for (NBConnectionVector::const_iterator k = prohibiting.begin(); k != prohibiting.end(); k++) {
673  NBConnection prohibitor = *k;
677  into.closeTag();
678  }
679  }
680 }
681 
682 
683 std::string
685  return c.getFrom()->getID() + "->" + c.getTo()->getID();
686 }
687 
688 
689 void
691  std::vector<NBTrafficLightLogic*> logics = tllCont.getComputed();
692  for (std::vector<NBTrafficLightLogic*>::iterator it = logics.begin(); it != logics.end(); it++) {
694  into.writeAttr(SUMO_ATTR_ID, (*it)->getID());
695  into.writeAttr(SUMO_ATTR_TYPE, (*it)->getType());
696  into.writeAttr(SUMO_ATTR_PROGRAMID, (*it)->getProgramID());
697  into.writeAttr(SUMO_ATTR_OFFSET, writeSUMOTime((*it)->getOffset()));
698  // write params
699  const std::map<std::string, std::string>& params = (*it)->getMap();
700  for (std::map<std::string, std::string>::const_iterator i = params.begin(); i != params.end(); ++i) {
701  into.openTag(SUMO_TAG_PARAM);
702  into.writeAttr(SUMO_ATTR_KEY, (*i).first);
703  into.writeAttr(SUMO_ATTR_VALUE, (*i).second);
704  into.closeTag();
705  }
706  // write the phases
707  const std::vector<NBTrafficLightLogic::PhaseDefinition>& phases = (*it)->getPhases();
708  for (std::vector<NBTrafficLightLogic::PhaseDefinition>::const_iterator j = phases.begin(); j != phases.end(); ++j) {
709  into.openTag(SUMO_TAG_PHASE);
710  into.writeAttr(SUMO_ATTR_DURATION, writeSUMOTime(j->duration));
711  into.writeAttr(SUMO_ATTR_STATE, j->state);
712  into.closeTag();
713  }
714  into.closeTag();
715  }
716  if (logics.size() > 0) {
717  into.lf();
718  }
719 }
720 
721 
722 void
724  const GeoConvHelper& geoConvHelper = GeoConvHelper::getFinal();
726  into.writeAttr(SUMO_ATTR_NET_OFFSET, geoConvHelper.getOffsetBase());
727  into.writeAttr(SUMO_ATTR_CONV_BOUNDARY, geoConvHelper.getConvBoundary());
728  if (geoConvHelper.usingGeoProjection()) {
730  }
731  into.writeAttr(SUMO_ATTR_ORIG_BOUNDARY, geoConvHelper.getOrigBoundary());
732  if (geoConvHelper.usingGeoProjection()) {
733  into.setPrecision();
734  }
735  into.writeAttr(SUMO_ATTR_ORIG_PROJ, geoConvHelper.getProjString());
736  into.closeTag();
737  into.lf();
738 }
739 
740 
741 void
743  if (permissions == SVCAll) {
744  return;
745  } else if (permissions == 0) {
746  into.writeAttr(SUMO_ATTR_DISALLOW, "all");
747  return;
748  } else {
749  size_t num_allowed = 0;
750  for (int mask = 1; mask <= SUMOVehicleClass_MAX; mask = mask << 1) {
751  if ((mask & permissions) == mask) {
752  ++num_allowed;
753  }
754  }
755  if (num_allowed <= (SumoVehicleClassStrings.size() - num_allowed) && num_allowed > 0) {
756  into.writeAttr(SUMO_ATTR_ALLOW, getVehicleClassNames(permissions));
757  } else {
759  }
760  }
761 }
762 
763 
764 void
766  if (preferred == SVCAll || preferred == 0) {
767  return;
768  } else {
770  }
771 }
772 /****************************************************************************/
773 
static void writeRoundabout(OutputDevice &into, const std::vector< std::string > &r, const NBEdgeCont &ec)
Writes a roundabout.
std::string id
Definition: NBEdge.h:177
The information about how to spread the lanes from the given position.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
const EdgeVector & getIncomingEdges() const
Returns this node's incoming edges.
Definition: NBNode.h:244
void close()
Closes the device and removes it from the dictionary.
SUMOReal endOffset
This lane's offset to the intersection begin.
Definition: NBEdge.h:136
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:2030
a list of node ids, used for controlling joining
static const SUMOReal UNSPECIFIED_WIDTH
unspecified lane width
Definition: NBEdge.h:201
PositionVector shape
The lane's shape.
Definition: NBEdge.h:128
is a pedestrian
static void writeDistrict(OutputDevice &into, const NBDistrict &d)
Writes a district.
const std::string & getTypeName() const
Returns the type name.
Definition: NBEdge.h:495
std::string viaID
Definition: NBEdge.h:182
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:379
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's width.
Definition: NBNode.h:172
static void writeLocation(OutputDevice &into)
writes the location element
SUMOReal width
This lane's width.
Definition: NBNode.h:170
const std::vector< NBEdge::Lane > & getLanes() const
Returns the lane definitions.
Definition: NBEdge.h:503
const Crossing & getCrossing(const std::string &id) const
return the crossing with the given id
Definition: NBNode.cpp:2006
#define GEO_OUTPUT_ACCURACY
Definition: config.h:16
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:572
T MAX2(T a, T b)
Definition: StdDefs.h:72
the weight of a district's source or sink
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: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)
static void writePermissions(OutputDevice &into, SVCPermissions permissions)
writes allowed disallowed attributes if needed;
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
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
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
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't set.
Definition: NBEdge.h:412
An (internal) definition of a single lane of an edge.
Definition: NBEdge.h:123
const std::vector< EdgeVector > & getRoundabouts() const
Returns the determined roundabouts.
Definition: NBNetBuilder.h:194
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
void setPrecision(unsigned int precision=OUTPUT_ACCURACY)
Sets the precison or resets it to default.
static void writeNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Writes the network into a SUMO-file.
void push_front_noDoublePos(const Position &p)
const int SUMOVehicleClass_MAX
const Position & getPosition() const
Returns the position of this node.
Definition: NBNode.h:232
std::map< std::string, NBEdge * >::const_iterator end() const
Returns the pointer to the end of the stored edges.
Definition: NBEdgeCont.h:198
bool usingGeoProjection() const
Returns whether a transformation from geo to metric coordinates will be performed.
static methods for processing the coordinates conversion for the current net
Definition: GeoConvHelper.h:59
the edges of a route
int getPriority() const
Returns the priority of the edge.
Definition: NBEdge.h:352
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:1958
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:74
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.
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:269
static void writeEdge(OutputDevice &into, const NBEdge &e, bool noNames, bool origNames)
Writes an edge (
bool hasLoadedLength() const
Returns whether a length was set explicitly.
Definition: NBEdge.h:420
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
const PositionVector & getShape() const
Returns the shape.
Definition: NBDistrict.h:222
const std::string & getProjString() const
Returns the network offset.
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:186
static void writePositionLong(const Position &pos, OutputDevice &dev)
Writes the given position to device in long format (one attribute per dimension)
Definition: NWFrame.cpp:152
const Position getOffsetBase() const
Returns the network base.
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:142
StringBijection< SUMOVehicleClass > SumoVehicleClassStrings(sumoVehicleClassStringInitializer, SVC_CUSTOM2, false)
const Boundary & getConvBoundary() const
Returns the converted boundary.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
static void writeLane(OutputDevice &into, const std::string &eID, const std::string &lID, SUMOReal speed, SVCPermissions permissions, SVCPermissions preferred, SUMOReal endOffset, SUMOReal width, const PositionVector &shape, const std::string &origID, SUMOReal length, unsigned int index, bool origNames)
Writes a lane (
const PositionVector & getShape() const
retrieve the junction shape
Definition: NBNode.cpp:1397
std::string getVehicleClassNames(SVCPermissions permissions)
Returns the ids of the given classes, divided using a ' '.
std::vector< NBConnection > NBConnectionVector
Definition of a connection vector.
static void normaliseSum(std::vector< T > &v, T msum=1.0)
Definition: VectorHelper.h:57
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 (
SUMOReal length() const
Returns the length.
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:368
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:245
LinkState getLinkState(const NBEdge *incoming, NBEdge *outgoing, int fromLane, bool mayDefinitelyPass, const std::string &tlID) const
Definition: NBNode.cpp:1293
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:38
static bool writeInternalNodes(OutputDevice &into, const NBNode &n)
Writes internal junctions (
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:839
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:817
NBTrafficLightLogicCont & getTLLogicCont()
Returns the traffic light logics container.
Definition: NBNetBuilder.h:178
static void writePreferences(OutputDevice &into, SVCPermissions preferred)
writes allowed disallowed attributes if needed;
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
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's lanes' lateral offset is computed.
Definition: NBEdge.h:597
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:153
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:215
static void writeJunction(OutputDevice &into, const NBNode &n, const bool checkLaneFoes)
Writes a junction (
bool writeLogic(OutputDevice &into, const bool checkLaneFoes) const
Definition: NBNode.cpp:640
const std::vector< SUMOReal > & getSourceWeights() const
Returns the weights of the sources.
Definition: NBDistrict.h:190
const Boundary & getOrigBoundary() const
Returns the original boundary.
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:190
const std::vector< WalkingArea > & getWalkingAreas() const
return this junctions pedestrian walking areas
Definition: NBNode.h:577
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:458
const std::vector< Connection > & getConnections() const
Returns the connections.
Definition: NBEdge.h:747
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:134
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
SUMOReal width
This lane'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)
LinkDirection getDirection(const NBEdge *const incoming, const NBEdge *const outgoing) const
Returns the representation of the described stream's direction.
Definition: NBNode.cpp:1247
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:360