SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
NIImporter_OpenStreetMap.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Importer for networks stored in OpenStreetMap format
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 #include <algorithm>
34 #include <set>
35 #include <functional>
36 #include <sstream>
37 #include <limits>
41 #include <utils/common/ToString.h>
45 #include <netbuild/NBEdge.h>
46 #include <netbuild/NBEdgeCont.h>
47 #include <netbuild/NBNode.h>
48 #include <netbuild/NBNodeCont.h>
49 #include <netbuild/NBNetBuilder.h>
50 #include <netbuild/NBOwnTLDef.h>
56 #include <utils/xml/XMLSubSys.h>
57 #include "NILoader.h"
59 
60 #ifdef CHECK_MEMORY_LEAKS
61 #include <foreign/nvwa/debug_new.h>
62 #endif // CHECK_MEMORY_LEAKS
63 
64 //#define DEBUG_LAYER_ELEVATION
65 
66 // ---------------------------------------------------------------------------
67 // static members
68 // ---------------------------------------------------------------------------
70 
72 
73 // ===========================================================================
74 // Private classes
75 // ===========================================================================
76 
80 public:
81  bool operator()(const Edge* e1, const Edge* e2) const {
82  if (e1->myHighWayType != e2->myHighWayType) {
83  return e1->myHighWayType > e2->myHighWayType;
84  }
85  if (e1->myNoLanes != e2->myNoLanes) {
86  return e1->myNoLanes > e2->myNoLanes;
87  }
88  if (e1->myNoLanesForward != e2->myNoLanesForward) {
89  return e1->myNoLanesForward > e2->myNoLanesForward;
90  }
91  if (e1->myMaxSpeed != e2->myMaxSpeed) {
92  return e1->myMaxSpeed > e2->myMaxSpeed;
93  }
94  if (e1->myIsOneWay != e2->myIsOneWay) {
95  return e1->myIsOneWay > e2->myIsOneWay;
96  }
97  return e1->myCurrentNodes > e2->myCurrentNodes;
98  }
99 };
100 
101 // ===========================================================================
102 // method definitions
103 // ===========================================================================
104 // ---------------------------------------------------------------------------
105 // static methods
106 // ---------------------------------------------------------------------------
108 
109 
110 void
112  NIImporter_OpenStreetMap importer;
113  importer.load(oc, nb);
114 }
115 
116 
118 
119 
121  // delete nodes
122  for (std::set<NIOSMNode*, CompareNodes>::iterator i = myUniqueNodes.begin(); i != myUniqueNodes.end(); i++) {
123  delete *i;
124  }
125  // delete edges
126  for (std::map<long long int, Edge*>::iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
127  delete(*i).second;
128  }
129 }
130 
131 
132 void
134  // check whether the option is set (properly)
135  if (!oc.isSet("osm-files")) {
136  return;
137  }
138  /* Parse file(s)
139  * Each file is parsed twice: first for nodes, second for edges. */
140  std::vector<std::string> files = oc.getStringVector("osm-files");
141  // load nodes, first
142  NodesHandler nodesHandler(myOSMNodes, myUniqueNodes, oc.getBool("osm.elevation"));
143  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
144  // nodes
145  if (!FileHelpers::isReadable(*file)) {
146  WRITE_ERROR("Could not open osm-file '" + *file + "'.");
147  return;
148  }
149  nodesHandler.setFileName(*file);
150  PROGRESS_BEGIN_MESSAGE("Parsing nodes from osm-file '" + *file + "'");
151  if (!XMLSubSys::runParser(nodesHandler, *file)) {
152  return;
153  }
155  }
156  // load edges, then
157  EdgesHandler edgesHandler(myOSMNodes, myEdges);
158  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
159  // edges
160  edgesHandler.setFileName(*file);
161  PROGRESS_BEGIN_MESSAGE("Parsing edges from osm-file '" + *file + "'");
162  XMLSubSys::runParser(edgesHandler, *file);
164  }
165 
166  /* Remove duplicate edges with the same shape and attributes */
167  if (!oc.getBool("osm.skip-duplicates-check")) {
168  PROGRESS_BEGIN_MESSAGE("Removing duplicate edges");
169  if (myEdges.size() > 1) {
170  std::set<const Edge*, CompareEdges> dupsFinder;
171  for (std::map<long long int, Edge*>::iterator it = myEdges.begin(); it != myEdges.end();) {
172  if (dupsFinder.count(it->second) > 0) {
173  WRITE_MESSAGE("Found duplicate edges. Removing " + toString(it->first));
174  delete it->second;
175  myEdges.erase(it++);
176  } else {
177  dupsFinder.insert(it->second);
178  it++;
179  }
180  }
181  }
183  }
184 
185  /* Mark which nodes are used (by edges or traffic lights).
186  * This is necessary to detect which OpenStreetMap nodes are for
187  * geometry only */
188  std::map<long long int, int> nodeUsage;
189  // Mark which nodes are used by edges (begin and end)
190  for (std::map<long long int, Edge*>::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
191  Edge* e = (*i).second;
192  assert(e->myCurrentIsRoad);
193  for (std::vector<long long int>::const_iterator j = e->myCurrentNodes.begin(); j != e->myCurrentNodes.end(); ++j) {
194  if (nodeUsage.find(*j) == nodeUsage.end()) {
195  nodeUsage[*j] = 0;
196  }
197  nodeUsage[*j] = nodeUsage[*j] + 1;
198  }
199  }
200  // Mark which nodes are used by traffic lights
201  for (std::map<long long int, NIOSMNode*>::const_iterator nodesIt = myOSMNodes.begin(); nodesIt != myOSMNodes.end(); ++nodesIt) {
202  if (nodesIt->second->tlsControlled /* || nodesIt->second->railwayCrossing*/) {
203  // If the key is not found in the map, the value is automatically
204  // initialized with 0.
205  nodeUsage[nodesIt->first] += 1;
206  }
207  }
208 
209  /* Instantiate edges
210  * Only those nodes in the middle of an edge which are used by more than
211  * one edge are instantiated. Other nodes are considered as geometry nodes. */
212  NBNodeCont& nc = nb.getNodeCont();
214  for (std::map<long long int, Edge*>::iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
215  Edge* e = (*i).second;
216  assert(e->myCurrentIsRoad);
217  if (e->myCurrentNodes.size() < 2) {
218  WRITE_WARNING("Discarding way '" + toString(e->id) + "' because it has only " +
219  toString(e->myCurrentNodes.size()) + " node(s)");
220  continue;
221  }
222  // build nodes;
223  // - the from- and to-nodes must be built in any case
224  // - the in-between nodes are only built if more than one edge references them
225  NBNode* currentFrom = insertNodeChecking(*e->myCurrentNodes.begin(), nc, tlsc);
226  NBNode* last = insertNodeChecking(*(e->myCurrentNodes.end() - 1), nc, tlsc);
227  int running = 0;
228  std::vector<long long int> passed;
229  for (std::vector<long long int>::iterator j = e->myCurrentNodes.begin(); j != e->myCurrentNodes.end(); ++j) {
230  passed.push_back(*j);
231  if (nodeUsage[*j] > 1 && j != e->myCurrentNodes.end() - 1 && j != e->myCurrentNodes.begin()) {
232  NBNode* currentTo = insertNodeChecking(*j, nc, tlsc);
233  running = insertEdge(e, running, currentFrom, currentTo, passed, nb);
234  currentFrom = currentTo;
235  passed.clear();
236  }
237  }
238  if (running == 0) {
239  running = -1;
240  }
241  insertEdge(e, running, currentFrom, last, passed, nb);
242  }
243 
244  const SUMOReal layerElevation = oc.getFloat("osm.layer-elevation");
245  if (layerElevation > 0) {
246  reconstructLayerElevation(layerElevation, nb);
247  }
248 
249  // load relations (after edges are built since we want to apply
250  // turn-restrictions directly to NBEdges)
251  RelationHandler relationHandler(myOSMNodes, myEdges);
252  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
253  // relations
254  relationHandler.setFileName(*file);
255  PROGRESS_BEGIN_MESSAGE("Parsing relations from osm-file '" + *file + "'");
256  XMLSubSys::runParser(relationHandler, *file);
258  }
259 }
260 
261 
262 NBNode*
264  NBNode* node = nc.retrieve(toString(id));
265  if (node == 0) {
266  NIOSMNode* n = myOSMNodes.find(id)->second;
267  Position pos(n->lon, n->lat, n->ele);
268  if (!NBNetBuilder::transformCoordinates(pos, true)) {
269  WRITE_ERROR("Unable to project coordinates for junction '" + toString(id) + "'.");
270  return 0;
271  }
272  node = new NBNode(toString(id), pos);
273  if (!nc.insert(node)) {
274  WRITE_ERROR("Could not insert junction '" + toString(id) + "'.");
275  delete node;
276  return 0;
277  }
278  n->node = node;
279  if (n->railwayCrossing) {
280  node->reinit(pos, NODETYPE_RAIL_CROSSING);
281  } else if (n->tlsControlled) {
282  // ok, this node is a traffic light node where no other nodes
283  // participate
284  // @note: The OSM-community has not settled on a schema for differentiating between fixed and actuated lights
286  NBOwnTLDef* tlDef = new NBOwnTLDef(toString(id), node, 0, type);
287  if (!tlsc.insert(tlDef)) {
288  // actually, nothing should fail here
289  delete tlDef;
290  throw ProcessError("Could not allocate tls '" + toString(id) + "'.");
291  }
292  }
293  }
294  return node;
295 }
296 
297 
298 int
300  const std::vector<long long int>& passed, NBNetBuilder& nb) {
301  NBNodeCont& nc = nb.getNodeCont();
302  NBEdgeCont& ec = nb.getEdgeCont();
303  NBTypeCont& tc = nb.getTypeCont();
305  // patch the id
306  std::string id = toString(e->id);
307  if (from == 0 || to == 0) {
308  WRITE_ERROR("Discarding edge '" + id + "' because the nodes could not be built.");
309  return index;
310  }
311  if (index >= 0) {
312  id = id + "#" + toString(index);
313  } else {
314  index = 0;
315  }
316  if (from == to) {
317  assert(passed.size() >= 2);
318  if (passed.size() == 2) {
319  WRITE_WARNING("Discarding edge '" + id + "' which connects two identical nodes without geometry.");
320  return index;
321  }
322  // in the special case of a looped way split again using passed
323  int intermediateIndex = (int)passed.size() / 2;
324  NBNode* intermediate = insertNodeChecking(passed[intermediateIndex], nc, tlsc);
325  std::vector<long long int> part1(passed.begin(), passed.begin() + intermediateIndex);
326  std::vector<long long int> part2(passed.begin() + intermediateIndex + 1, passed.end());
327  index = insertEdge(e, index, from, intermediate, part1, nb);
328  return insertEdge(e, index, intermediate, to, part2, nb);
329  }
330  const int newIndex = index + 1;
331 
332  // convert the shape
333  PositionVector shape;
334  shape.push_back(from->getPosition());
335  for (std::vector<long long int>::const_iterator i = passed.begin(); i != passed.end(); ++i) {
336  NIOSMNode* n = myOSMNodes.find(*i)->second;
337  Position pos(n->lon, n->lat, n->ele);
338  if (!NBNetBuilder::transformCoordinates(pos, true)) {
339  WRITE_ERROR("Unable to project coordinates for edge '" + id + "'.");
340  }
341  shape.push_back_noDoublePos(pos);
342  }
343  shape.push_back_noDoublePos(to->getPosition());
344 
345  std::string type = e->myHighWayType;
346  if (!tc.knows(type)) {
347  if (myUnusableTypes.count(type) > 0) {
348  return newIndex;
349  } else if (myKnownCompoundTypes.count(type) > 0) {
350  type = myKnownCompoundTypes[type];
351  } else {
352  // this edge has a type which does not yet exist in the TypeContainer
354  std::vector<std::string> types;
355  while (tok.hasNext()) {
356  std::string t = tok.next();
357  if (tc.knows(t)) {
358  if (std::find(types.begin(), types.end(), t) == types.end()) {
359  types.push_back(t);
360  }
361  } else if (tok.size() > 1) {
362  WRITE_WARNING("Discarding unknown compound '" + t + "' in type '" + type + "' (first occurence for edge '" + id + "').");
363  }
364  }
365  if (types.size() == 0) {
366  WRITE_WARNING("Discarding unusable type '" + type + "' (first occurence for edge '" + id + "').");
367  myUnusableTypes.insert(type);
368  return newIndex;
369  } else {
370  const std::string newType = joinToString(types, "|");
371  if (tc.knows(newType)) {
372  myKnownCompoundTypes[type] = newType;
373  type = newType;
374  } else if (myKnownCompoundTypes.count(newType) > 0) {
375  type = myKnownCompoundTypes[newType];
376  } else {
377  // build a new type by merging all values
378  int numLanes = 0;
379  SUMOReal maxSpeed = 0;
380  int prio = 0;
382  SUMOReal sidewalkWidth = NBEdge::UNSPECIFIED_WIDTH;
383  SUMOReal bikelaneWidth = NBEdge::UNSPECIFIED_WIDTH;
384  bool defaultIsOneWay = false;
385  SVCPermissions permissions = 0;
386  bool discard = true;
387  for (std::vector<std::string>::iterator it = types.begin(); it != types.end(); it++) {
388  if (!tc.getShallBeDiscarded(*it)) {
389  numLanes = MAX2(numLanes, tc.getNumLanes(*it));
390  maxSpeed = MAX2(maxSpeed, tc.getSpeed(*it));
391  prio = MAX2(prio, tc.getPriority(*it));
392  defaultIsOneWay &= tc.getIsOneWay(*it);
393  permissions |= tc.getPermissions(*it);
394  width = MAX2(width, tc.getWidth(*it));
395  sidewalkWidth = MAX2(sidewalkWidth, tc.getSidewalkWidth(*it));
396  bikelaneWidth = MAX2(bikelaneWidth, tc.getBikeLaneWidth(*it));
397  discard = false;
398  }
399  }
400  if (width != NBEdge::UNSPECIFIED_WIDTH) {
401  width = MAX2(width, SUMO_const_laneWidth);
402  }
403  if (discard) {
404  WRITE_WARNING("Discarding compound type '" + newType + "' (first occurence for edge '" + id + "').");
405  myUnusableTypes.insert(newType);
406  return newIndex;
407  } else {
408  WRITE_MESSAGE("Adding new type '" + type + "' (first occurence for edge '" + id + "').");
409  tc.insert(newType, numLanes, maxSpeed, prio, permissions, width, defaultIsOneWay, sidewalkWidth, bikelaneWidth);
410  for (std::vector<std::string>::iterator it = types.begin(); it != types.end(); it++) {
411  if (!tc.getShallBeDiscarded(*it)) {
412  tc.copyRestrictionsAndAttrs(*it, newType);
413  }
414  }
415  myKnownCompoundTypes[type] = newType;
416  type = newType;
417  }
418  }
419  }
420  }
421  }
422 
423  // otherwise it is not an edge and will be ignored
424  bool ok = true;
425  int numLanesForward = tc.getNumLanes(type);
426  int numLanesBackward = tc.getNumLanes(type);
427  SUMOReal speed = tc.getSpeed(type);
428  bool defaultsToOneWay = tc.getIsOneWay(type);
429  SVCPermissions forwardPermissions = tc.getPermissions(type);
430  SVCPermissions backwardPermissions = tc.getPermissions(type);
431  SUMOReal forwardWidth = tc.getWidth(type);
432  SUMOReal backwardWidth = tc.getWidth(type);
433  const bool addSidewalk = (tc.getSidewalkWidth(type) != NBEdge::UNSPECIFIED_WIDTH);
434  const bool addBikeLane = (tc.getBikeLaneWidth(type) != NBEdge::UNSPECIFIED_WIDTH);
435  // check directions
436  bool addForward = true;
437  bool addBackward = true;
438  if (e->myIsOneWay == "true" || e->myIsOneWay == "yes" || e->myIsOneWay == "1" || (defaultsToOneWay && e->myIsOneWay != "no" && e->myIsOneWay != "false" && e->myIsOneWay != "0")) {
439  addBackward = false;
440  }
441  if (e->myIsOneWay == "-1" || e->myIsOneWay == "reverse") {
442  // one-way in reversed direction of way
443  addForward = false;
444  addBackward = true;
445  }
446  if (e->myIsOneWay != "" && e->myIsOneWay != "false" && e->myIsOneWay != "no" && e->myIsOneWay != "true" && e->myIsOneWay != "yes" && e->myIsOneWay != "-1" && e->myIsOneWay != "1" && e->myIsOneWay != "reverse") {
447  WRITE_WARNING("New value for oneway found: " + e->myIsOneWay);
448  }
449  // if we had been able to extract the number of lanes, override the highway type default
450  if (e->myNoLanes > 0) {
451  if (addForward && !addBackward) {
452  numLanesForward = e->myNoLanes;
453  } else if (!addForward && addBackward) {
454  numLanesBackward = e->myNoLanes;
455  } else {
456  if (e->myNoLanesForward > 0) {
457  numLanesForward = e->myNoLanesForward;
458  } else if (e->myNoLanesForward < 0) {
459  numLanesForward = e->myNoLanes + e->myNoLanesForward;
460  } else {
461  numLanesForward = (int)std::ceil(e->myNoLanes / 2.0);
462  }
463  numLanesBackward = e->myNoLanes - numLanesForward;
464  // sometimes ways are tagged according to their physical width of a single
465  // lane but they are intended for traffic in both directions
466  numLanesForward = MAX2(1, numLanesForward);
467  numLanesBackward = MAX2(1, numLanesBackward);
468  }
469  } else if (e->myNoLanes == 0) {
470  WRITE_WARNING("Skipping edge '" + id + "' because it has zero lanes.");
471  ok = false;
472  }
473  // if we had been able to extract the maximum speed, override the type's default
474  if (e->myMaxSpeed != MAXSPEED_UNGIVEN) {
475  speed = (SUMOReal)(e->myMaxSpeed / 3.6);
476  }
477  if (speed <= 0) {
478  WRITE_WARNING("Skipping edge '" + id + "' because it has speed " + toString(speed));
479  ok = false;
480  }
481  // deal with cycleways that run in the opposite direction of a one-way street
482  if (addBikeLane) {
483  if (!addForward && (e->myCyclewayType & WAY_FORWARD) != 0) {
484  addForward = true;
485  forwardPermissions = SVC_BICYCLE;
486  forwardWidth = tc.getBikeLaneWidth(type);
487  numLanesForward = 1;
488  // do not add an additional cycle lane
490  }
491  if (!addBackward && (e->myCyclewayType & WAY_BACKWARD) != 0) {
492  addBackward = true;
493  backwardPermissions = SVC_BICYCLE;
494  backwardWidth = tc.getBikeLaneWidth(type);
495  numLanesBackward = 1;
496  // do not add an additional cycle lane
498  }
499  }
500  // deal with busways that run in the opposite direction of a one-way street
501  if (!addForward && (e->myBuswayType & WAY_FORWARD) != 0) {
502  addForward = true;
503  forwardPermissions = SVC_BUS;
504  numLanesForward = 1;
505  }
506  if (!addBackward && (e->myBuswayType & WAY_BACKWARD) != 0) {
507  addBackward = true;
508  backwardPermissions = SVC_BUS;
509  numLanesBackward = 1;
510  }
511 
512  if (ok) {
514  id = StringUtils::escapeXML(id);
515  if (addForward) {
516  assert(numLanesForward > 0);
517  NBEdge* nbe = new NBEdge(id, from, to, type, speed, numLanesForward, tc.getPriority(type),
518  forwardWidth, NBEdge::UNSPECIFIED_OFFSET, shape,
519  StringUtils::escapeXML(e->streetName), toString(e->id), lsf, true);
520  nbe->setPermissions(forwardPermissions);
521  if ((e->myBuswayType & WAY_FORWARD) != 0) {
522  nbe->setPermissions(SVC_BUS, 0);
523  }
524  if (addBikeLane && (e->myCyclewayType == WAY_UNKNOWN || (e->myCyclewayType & WAY_FORWARD) != 0)) {
525  nbe->addBikeLane(tc.getBikeLaneWidth(type));
526  }
527  if (addSidewalk) {
528  nbe->addSidewalk(tc.getSidewalkWidth(type));
529  }
530  if (!ec.insert(nbe)) {
531  delete nbe;
532  throw ProcessError("Could not add edge '" + id + "'.");
533  }
534  }
535  if (addBackward) {
536  assert(numLanesBackward > 0);
537  NBEdge* nbe = new NBEdge("-" + id, to, from, type, speed, numLanesBackward, tc.getPriority(type),
538  backwardWidth, NBEdge::UNSPECIFIED_OFFSET, shape.reverse(),
539  StringUtils::escapeXML(e->streetName), toString(e->id), lsf, true);
540  nbe->setPermissions(backwardPermissions);
541  if ((e->myBuswayType & WAY_BACKWARD) != 0) {
542  nbe->setPermissions(SVC_BUS, 0);
543  }
544  if (addBikeLane && (e->myCyclewayType == WAY_UNKNOWN || (e->myCyclewayType & WAY_BACKWARD) != 0)) {
545  nbe->addBikeLane(tc.getBikeLaneWidth(type));
546  }
547  if (addSidewalk) {
548  nbe->addSidewalk(tc.getSidewalkWidth(type));
549  }
550  if (!ec.insert(nbe)) {
551  delete nbe;
552  throw ProcessError("Could not add edge '-" + id + "'.");
553  }
554  }
555  }
556  return newIndex;
557 }
558 
559 
560 // ---------------------------------------------------------------------------
561 // definitions of NIImporter_OpenStreetMap::NodesHandler-methods
562 // ---------------------------------------------------------------------------
564  std::map<long long int, NIOSMNode*>& toFill,
565  std::set<NIOSMNode*, CompareNodes>& uniqueNodes,
566  bool importElevation) :
567  SUMOSAXHandler("osm - file"),
568  myToFill(toFill),
569  myLastNodeID(-1),
570  myIsInValidNodeTag(false),
571  myHierarchyLevel(0),
572  myUniqueNodes(uniqueNodes),
573  myImportElevation(importElevation) {
574 }
575 
576 
578 
579 
580 void
582  ++myHierarchyLevel;
583  if (element == SUMO_TAG_NODE) {
584  bool ok = true;
585  if (myHierarchyLevel != 2) {
586  WRITE_ERROR("Node element on wrong XML hierarchy level (id='" + toString(attrs.get<long long int>(SUMO_ATTR_ID, 0, ok)) + "', level='" + toString(myHierarchyLevel) + "').");
587  return;
588  }
589  long long int id = attrs.get<long long int>(SUMO_ATTR_ID, 0, ok);
590  std::string action = attrs.hasAttribute("action") ? attrs.getStringSecure("action", "") : "";
591  if (action == "delete") {
592  return;
593  }
594  if (!ok) {
595  return;
596  }
597  myLastNodeID = -1;
598  if (myToFill.find(id) == myToFill.end()) {
599  myLastNodeID = id;
600  // assume we are loading multiple files...
601  // ... so we won't report duplicate nodes
602  bool ok = true;
603  double tlat, tlon;
604  std::istringstream lon(attrs.get<std::string>(SUMO_ATTR_LON, toString(id).c_str(), ok));
605  if (!ok) {
606  return;
607  }
608  lon >> tlon;
609  if (lon.fail()) {
610  WRITE_ERROR("Node's '" + toString(id) + "' lon information is not numeric.");
611  return;
612  }
613  std::istringstream lat(attrs.get<std::string>(SUMO_ATTR_LAT, toString(id).c_str(), ok));
614  if (!ok) {
615  return;
616  }
617  lat >> tlat;
618  if (lat.fail()) {
619  WRITE_ERROR("Node's '" + toString(id) + "' lat information is not numeric.");
620  return;
621  }
622  NIOSMNode* toAdd = new NIOSMNode(id, tlon, tlat);
623  myIsInValidNodeTag = true;
624 
625  std::set<NIOSMNode*, CompareNodes>::iterator similarNode = myUniqueNodes.find(toAdd);
626  if (similarNode == myUniqueNodes.end()) {
627  myUniqueNodes.insert(toAdd);
628  } else {
629  delete toAdd;
630  toAdd = *similarNode;
631  WRITE_MESSAGE("Found duplicate nodes. Substituting " + toString(id) + " with " + toString(toAdd->id));
632  }
633  myToFill[id] = toAdd;
634  }
635  }
636  if (element == SUMO_TAG_TAG && myIsInValidNodeTag) {
637  if (myHierarchyLevel != 3) {
638  WRITE_ERROR("Tag element on wrong XML hierarchy level.");
639  return;
640  }
641  bool ok = true;
642  std::string key = attrs.get<std::string>(SUMO_ATTR_K, toString(myLastNodeID).c_str(), ok, false);
643  // we check whether the key is relevant (and we really need to transcode the value) to avoid hitting #1636
644  if (key == "highway" || key == "ele" || key == "crossing" || key == "railway") {
645  std::string value = attrs.get<std::string>(SUMO_ATTR_V, toString(myLastNodeID).c_str(), ok, false);
646  if (key == "highway" && value.find("traffic_signal") != std::string::npos) {
647  myToFill[myLastNodeID]->tlsControlled = true;
648  } else if (key == "crossing" && value.find("traffic_signals") != std::string::npos) {
649  myToFill[myLastNodeID]->tlsControlled = true;
650  } else if (key == "railway" && value.find("crossing") != std::string::npos) {
651  myToFill[myLastNodeID]->railwayCrossing = true;
652  } else if (myImportElevation && key == "ele") {
653  try {
654  myToFill[myLastNodeID]->ele = TplConvert::_2SUMOReal(value.c_str());
655  } catch (...) {
656  WRITE_WARNING("Value of key '" + key + "' is not numeric ('" + value + "') in node '" +
657  toString(myLastNodeID) + "'.");
658  }
659  }
660  }
661  }
662 }
663 
664 
665 void
667  if (element == SUMO_TAG_NODE && myHierarchyLevel == 2) {
668  myLastNodeID = -1;
669  myIsInValidNodeTag = false;
670  }
671  --myHierarchyLevel;
672 }
673 
674 
675 // ---------------------------------------------------------------------------
676 // definitions of NIImporter_OpenStreetMap::EdgesHandler-methods
677 // ---------------------------------------------------------------------------
679  const std::map<long long int, NIOSMNode*>& osmNodes,
680  std::map<long long int, Edge*>& toFill) :
681  SUMOSAXHandler("osm - file"),
682  myOSMNodes(osmNodes),
683  myEdgeMap(toFill) {
684  mySpeedMap["signals"] = MAXSPEED_UNGIVEN;
685  mySpeedMap["none"] = 300.;
686  mySpeedMap["no"] = 300.;
687  mySpeedMap["walk"] = 5.;
688  mySpeedMap["DE:rural"] = 100.;
689  mySpeedMap["DE:urban"] = 50.;
690  mySpeedMap["DE:living_street"] = 10.;
691 
692 }
693 
694 
696 }
697 
698 
699 void
701  const SUMOSAXAttributes& attrs) {
702  myParentElements.push_back(element);
703  // parse "way" elements
704  if (element == SUMO_TAG_WAY) {
705  bool ok = true;
706  long long int id = attrs.get<long long int>(SUMO_ATTR_ID, 0, ok);
707  std::string action = attrs.hasAttribute("action") ? attrs.getStringSecure("action", "") : "";
708  if (action == "delete") {
709  myCurrentEdge = 0;
710  return;
711  }
712  if (!ok) {
713  myCurrentEdge = 0;
714  return;
715  }
716  myCurrentEdge = new Edge(id);
717  }
718  // parse "nd" (node) elements
719  if (element == SUMO_TAG_ND) {
720  bool ok = true;
721  long long int ref = attrs.get<long long int>(SUMO_ATTR_REF, 0, ok);
722  if (ok) {
723  std::map<long long int, NIOSMNode*>::const_iterator node = myOSMNodes.find(ref);
724  if (node == myOSMNodes.end()) {
725  WRITE_WARNING("The referenced geometry information (ref='" + toString(ref) + "') is not known");
726  return;
727  } else {
728  ref = node->second->id; // node may have been substituted
729  if (myCurrentEdge->myCurrentNodes.size() == 0 ||
730  myCurrentEdge->myCurrentNodes.back() != ref) { // avoid consecutive duplicates
731  myCurrentEdge->myCurrentNodes.push_back(ref);
732  }
733  }
734  }
735  }
736  // parse values
737  if (element == SUMO_TAG_TAG && myParentElements.size() > 2 && myParentElements[myParentElements.size() - 2] == SUMO_TAG_WAY) {
738  if (myCurrentEdge == 0) {
739  return;
740  }
741  bool ok = true;
742  std::string key = attrs.get<std::string>(SUMO_ATTR_K, toString(myCurrentEdge->id).c_str(), ok, false);
743  if (key.size() > 8 && StringUtils::startsWith(key, "cycleway:")) {
744  // handle special busway keys
745  const std::string cyclewaySpec = key.substr(9);
746  key = "cycleway";
747  if (cyclewaySpec == "right") {
748  myCurrentEdge->myCyclewayType = (WayType)(myCurrentEdge->myCyclewayType | WAY_FORWARD);
749  } else if (cyclewaySpec == "left") {
750  myCurrentEdge->myCyclewayType = (WayType)(myCurrentEdge->myCyclewayType | WAY_BACKWARD);
751  } else if (cyclewaySpec == "both") {
752  myCurrentEdge->myCyclewayType = (WayType)(myCurrentEdge->myCyclewayType | WAY_BOTH);
753  } else {
754  key = "ignore";
755  }
756  if ((myCurrentEdge->myCyclewayType & WAY_BOTH) != 0) {
757  // now we have some info on directionality
758  myCurrentEdge->myCyclewayType = (WayType)(myCurrentEdge->myCyclewayType & ~WAY_UNKNOWN);
759  }
760  } else if (key.size() > 6 && StringUtils::startsWith(key, "busway:")) {
761  // handle special busway keys
762  const std::string buswaySpec = key.substr(7);
763  key = "busway";
764  if (buswaySpec == "right") {
765  myCurrentEdge->myBuswayType = (WayType)(myCurrentEdge->myBuswayType | WAY_FORWARD);
766  } else if (buswaySpec == "left") {
767  myCurrentEdge->myBuswayType = (WayType)(myCurrentEdge->myBuswayType | WAY_BACKWARD);
768  } else if (buswaySpec == "both") {
769  myCurrentEdge->myBuswayType = (WayType)(myCurrentEdge->myBuswayType | WAY_BOTH);
770  } else {
771  key = "ignore";
772  }
773  }
774 
775  // we check whether the key is relevant (and we really need to transcode the value) to avoid hitting #1636
776  if (!StringUtils::endsWith(key, "way") && !StringUtils::startsWith(key, "lanes") && key != "maxspeed" && key != "junction" && key != "name" && key != "tracks" && key != "layer" && key != "route") {
777  return;
778  }
779  std::string value = attrs.get<std::string>(SUMO_ATTR_V, toString(myCurrentEdge->id).c_str(), ok, false);
780 
781  if (key == "highway" || key == "railway" || key == "waterway" || key == "cycleway" || key == "busway" || key == "route") {
782  myCurrentEdge->myCurrentIsRoad = true;
783  // special cycleway stuff
784  if (key == "cycleway") {
785  if (value == "no") {
786  return;
787  } else if (value == "opposite_track") {
788  myCurrentEdge->myCyclewayType = WAY_BACKWARD;
789  } else if (value == "opposite_lane") {
790  myCurrentEdge->myCyclewayType = WAY_BACKWARD;
791  }
792  }
793  // special busway stuff
794  if (key == "busway") {
795  if (value == "no") {
796  return;
797  } else if (value == "opposite_track") {
798  myCurrentEdge->myBuswayType = WAY_BACKWARD;
799  } else if (value == "opposite_lane") {
800  myCurrentEdge->myBuswayType = WAY_BACKWARD;
801  }
802  // no need to extend the type id
803  return;
804  }
805  // build type id
806  const std::string singleTypeID = key + "." + value;
807  if (myCurrentEdge->myHighWayType != "") {
808  // osm-ways may be used by more than one mode (eg railway.tram + highway.residential. this is relevant for multimodal traffic)
809  // we create a new type for this kind of situation which must then be resolved in insertEdge()
810  std::vector<std::string> types = StringTokenizer(myCurrentEdge->myHighWayType, compoundTypeSeparator).getVector();
811  types.push_back(singleTypeID);
812  myCurrentEdge->myHighWayType = joinToStringSorting(types, compoundTypeSeparator);
813  } else {
814  myCurrentEdge->myHighWayType = singleTypeID;
815  }
816  } else if (key == "lanes") {
817  try {
818  myCurrentEdge->myNoLanes = TplConvert::_2int(value.c_str());
819  } catch (NumberFormatException&) {
820  // might be a list of values
821  StringTokenizer st(value, ";", true);
822  std::vector<std::string> list = st.getVector();
823  if (list.size() >= 2) {
824  int minLanes = std::numeric_limits<int>::max();
825  try {
826  for (std::vector<std::string>::iterator i = list.begin(); i != list.end(); ++i) {
827  int numLanes = TplConvert::_2int(StringUtils::prune(*i).c_str());
828  minLanes = MIN2(minLanes, numLanes);
829  }
830  myCurrentEdge->myNoLanes = minLanes;
831  WRITE_WARNING("Using minimum lane number from list (" + value + ") for edge '" + toString(myCurrentEdge->id) + "'.");
832  } catch (NumberFormatException&) {
833  WRITE_WARNING("Value of key '" + key + "' is not numeric ('" + value + "') in edge '" +
834  toString(myCurrentEdge->id) + "'.");
835  }
836  }
837  } catch (EmptyData&) {
838  WRITE_WARNING("Value of key '" + key + "' is not numeric ('" + value + "') in edge '" +
839  toString(myCurrentEdge->id) + "'.");
840  }
841  } else if (key == "lanes:forward") {
842  try {
843  myCurrentEdge->myNoLanesForward = TplConvert::_2int(value.c_str());
844  } catch (...) {
845  WRITE_WARNING("Value of key '" + key + "' is not numeric ('" + value + "') in edge '" +
846  toString(myCurrentEdge->id) + "'.");
847  }
848  } else if (key == "lanes:backward") {
849  try {
850  // denote backwards count with a negative sign
851  myCurrentEdge->myNoLanesForward = -TplConvert::_2int(value.c_str());
852  } catch (...) {
853  WRITE_WARNING("Value of key '" + key + "' is not numeric ('" + value + "') in edge '" +
854  toString(myCurrentEdge->id) + "'.");
855  }
856  } else if (key == "maxspeed") {
857  if (mySpeedMap.find(value) != mySpeedMap.end()) {
858  myCurrentEdge->myMaxSpeed = mySpeedMap[value];
859  } else {
860  SUMOReal conversion = 1; // OSM default is km/h
861  if (StringUtils::to_lower_case(value).find("km/h") != std::string::npos) {
862  value = StringUtils::prune(value.substr(0, value.find_first_not_of("0123456789")));
863  } else if (StringUtils::to_lower_case(value).find("mph") != std::string::npos) {
864  value = StringUtils::prune(value.substr(0, value.find_first_not_of("0123456789")));
865  conversion = 1.609344; // kilometers per mile
866  }
867  try {
868  myCurrentEdge->myMaxSpeed = TplConvert::_2SUMOReal(value.c_str()) * conversion;
869  } catch (...) {
870  WRITE_WARNING("Value of key '" + key + "' is not numeric ('" + value + "') in edge '" +
871  toString(myCurrentEdge->id) + "'.");
872  }
873  }
874  } else if (key == "junction") {
875  if ((value == "roundabout") && (myCurrentEdge->myIsOneWay == "")) {
876  myCurrentEdge->myIsOneWay = "yes";
877  }
878  } else if (key == "oneway") {
879  myCurrentEdge->myIsOneWay = value;
880  } else if (key == "name") {
881  myCurrentEdge->streetName = value;
882  } else if (key == "layer") {
883  try {
884  myCurrentEdge->myLayer = TplConvert::_2int(value.c_str());
885  } catch (...) {
886  WRITE_WARNING("Value of key '" + key + "' is not numeric ('" + value + "') in edge '" +
887  toString(myCurrentEdge->id) + "'.");
888  }
889  } else if (key == "tracks") {
890  try {
891  if (TplConvert::_2int(value.c_str()) > 1) {
892  myCurrentEdge->myIsOneWay = "false";
893  } else {
894  myCurrentEdge->myIsOneWay = "true";
895  }
896  } catch (...) {
897  WRITE_WARNING("Value of key '" + key + "' is not numeric ('" + value + "') in edge '" +
898  toString(myCurrentEdge->id) + "'.");
899  }
900  }
901  }
902 }
903 
904 
905 void
907  myParentElements.pop_back();
908  if (element == SUMO_TAG_WAY) {
909  if (myCurrentEdge != 0 && myCurrentEdge->myCurrentIsRoad) {
910  myEdgeMap[myCurrentEdge->id] = myCurrentEdge;
911  } else {
912  delete myCurrentEdge;
913  }
914  myCurrentEdge = 0;
915  }
916 }
917 
918 
919 // ---------------------------------------------------------------------------
920 // definitions of NIImporter_OpenStreetMap::RelationHandler-methods
921 // ---------------------------------------------------------------------------
923  const std::map<long long int, NIOSMNode*>& osmNodes,
924  const std::map<long long int, Edge*>& osmEdges) :
925  SUMOSAXHandler("osm - file"),
926  myOSMNodes(osmNodes),
927  myOSMEdges(osmEdges) {
928  resetValues();
929 }
930 
931 
933 }
934 
935 void
937  myCurrentRelation = INVALID_ID;
938  myIsRestriction = false;
939  myFromWay = INVALID_ID;
940  myToWay = INVALID_ID;
941  myViaNode = INVALID_ID;
942  myViaWay = INVALID_ID;
943  myRestrictionType = RESTRICTION_UNKNOWN;
944 }
945 
946 void
948  const SUMOSAXAttributes& attrs) {
949  myParentElements.push_back(element);
950  // parse "way" elements
951  if (element == SUMO_TAG_RELATION) {
952  bool ok = true;
953  myCurrentRelation = attrs.get<long long int>(SUMO_ATTR_ID, 0, ok);
954  std::string action = attrs.hasAttribute("action") ? attrs.getStringSecure("action", "") : "";
955  if (action == "delete" || !ok) {
956  myCurrentRelation = INVALID_ID;
957  }
958  return;
959  } else if (myCurrentRelation == INVALID_ID) {
960  return;
961  }
962  // parse member elements
963  if (element == SUMO_TAG_MEMBER) {
964  bool ok = true;
965  std::string role = attrs.hasAttribute("role") ? attrs.getStringSecure("role", "") : "";
966  long long int ref = attrs.get<long long int>(SUMO_ATTR_REF, 0, ok);
967  if (role == "via") {
968  // u-turns for divided ways may be given with 2 via-nodes or 1 via-way
969  std::string memberType = attrs.get<std::string>(SUMO_ATTR_TYPE, 0, ok);
970  if (memberType == "way" && checkEdgeRef(ref)) {
971  myViaWay = ref;
972  } else if (memberType == "node") {
973  if (myOSMNodes.find(ref) != myOSMNodes.end()) {
974  myViaNode = ref;
975  } else {
976  WRITE_WARNING("No node found for reference '" + toString(ref) + "' in relation '" + toString(myCurrentRelation) + "'");
977  }
978  }
979  } else if (role == "from" && checkEdgeRef(ref)) {
980  myFromWay = ref;
981  } else if (role == "to" && checkEdgeRef(ref)) {
982  myToWay = ref;
983  }
984  return;
985  }
986  // parse values
987  if (element == SUMO_TAG_TAG) {
988  bool ok = true;
989  std::string key = attrs.get<std::string>(SUMO_ATTR_K, toString(myCurrentRelation).c_str(), ok, false);
990  // we check whether the key is relevant (and we really need to transcode the value) to avoid hitting #1636
991  if (key == "type" || key == "restriction") {
992  std::string value = attrs.get<std::string>(SUMO_ATTR_V, toString(myCurrentRelation).c_str(), ok, false);
993  if (key == "type" && value == "restriction") {
994  myIsRestriction = true;
995  return;
996  }
997  if (key == "restriction") {
998  // @note: the 'right/left/straight' part is ignored since the information is
999  // redundantly encoded in the 'from', 'to' and 'via' members
1000  if (value.substr(0, 5) == "only_") {
1001  myRestrictionType = RESTRICTION_ONLY;
1002  } else if (value.substr(0, 3) == "no_") {
1003  myRestrictionType = RESTRICTION_NO;
1004  } else {
1005  WRITE_WARNING("Found unknown restriction type '" + value + "' in relation '" + toString(myCurrentRelation) + "'");
1006  }
1007  return;
1008  }
1009  }
1010  }
1011 }
1012 
1013 
1014 bool
1016  if (myOSMEdges.find(ref) != myOSMEdges.end()) {
1017  return true;
1018  } else {
1019  WRITE_WARNING("No way found for reference '" + toString(ref) + "' in relation '" + toString(myCurrentRelation) + "'");
1020  return false;
1021  }
1022 }
1023 
1024 
1025 void
1027  myParentElements.pop_back();
1028  if (element == SUMO_TAG_RELATION) {
1029  if (myIsRestriction) {
1030  assert(myCurrentRelation != INVALID_ID);
1031  bool ok = true;
1032  if (myRestrictionType == RESTRICTION_UNKNOWN) {
1033  WRITE_WARNING("Ignoring restriction relation '" + toString(myCurrentRelation) + "' with unknown type.");
1034  ok = false;
1035  }
1036  if (myFromWay == INVALID_ID) {
1037  WRITE_WARNING("Ignoring restriction relation '" + toString(myCurrentRelation) + "' with unknown from-way.");
1038  ok = false;
1039  }
1040  if (myToWay == INVALID_ID) {
1041  WRITE_WARNING("Ignoring restriction relation '" + toString(myCurrentRelation) + "' with unknown to-way.");
1042  ok = false;
1043  }
1044  if (myViaNode == INVALID_ID && myViaWay == INVALID_ID) {
1045  WRITE_WARNING("Ignoring restriction relation '" + toString(myCurrentRelation) + "' with unknown via.");
1046  ok = false;
1047  }
1048  if (ok && !applyRestriction()) {
1049  WRITE_WARNING("Ignoring restriction relation '" + toString(myCurrentRelation) + "'.");
1050  }
1051  }
1052  // other relations might use similar subelements so reset in any case
1053  resetValues();
1054  }
1055 }
1056 
1057 
1058 bool
1060  // since OSM ways are bidirectional we need the via to figure out which direction was meant
1061  if (myViaNode != INVALID_ID) {
1062  NBNode* viaNode = myOSMNodes.find(myViaNode)->second->node;
1063  if (viaNode == 0) {
1064  WRITE_WARNING("Via-node '" + toString(myViaNode) + "' was not instantiated");
1065  return false;
1066  }
1067  NBEdge* from = findEdgeRef(myFromWay, viaNode->getIncomingEdges());
1068  NBEdge* to = findEdgeRef(myToWay, viaNode->getOutgoingEdges());
1069  if (from == 0) {
1070  WRITE_WARNING("from-edge of restriction relation could not be determined");
1071  return false;
1072  }
1073  if (to == 0) {
1074  WRITE_WARNING("to-edge of restriction relation could not be determined");
1075  return false;
1076  }
1077  if (myRestrictionType == RESTRICTION_ONLY) {
1078  from->addEdge2EdgeConnection(to);
1079  } else {
1080  from->removeFromConnections(to, -1, -1, true);
1081  }
1082  } else {
1083  // XXX interpreting via-ways or via-node lists not yet implemented
1084  WRITE_WARNING("direction of restriction relation could not be determined");
1085  return false;
1086  }
1087  return true;
1088 }
1089 
1090 
1091 NBEdge*
1092 NIImporter_OpenStreetMap::RelationHandler::findEdgeRef(long long int wayRef, const std::vector<NBEdge*>& candidates) const {
1093  const std::string prefix = toString(wayRef);
1094  const std::string backPrefix = "-" + prefix;
1095  NBEdge* result = 0;
1096  int found = 0;
1097  for (EdgeVector::const_iterator it = candidates.begin(); it != candidates.end(); ++it) {
1098  if (((*it)->getID().substr(0, prefix.size()) == prefix) ||
1099  ((*it)->getID().substr(0, backPrefix.size()) == backPrefix)) {
1100  result = *it;
1101  found++;
1102  }
1103  }
1104  if (found > 1) {
1105  WRITE_WARNING("Ambigous way reference '" + prefix + "' in restriction relation");
1106  result = 0;
1107  }
1108  return result;
1109 }
1110 
1111 
1112 void
1114  NBNodeCont& nc = nb.getNodeCont();
1115  NBEdgeCont& ec = nb.getEdgeCont();
1116  // reconstruct elevation from layer info
1117  // build a map of raising and lowering forces (attractor and distance)
1118  // for all nodes unknownElevation
1119  std::map<NBNode*, std::vector<std::pair<SUMOReal, SUMOReal> > > layerForces;
1120 
1121  // collect all nodes that belong to a way with layer information
1122  std::set<NBNode*> knownElevation;
1123  for (std::map<long long int, Edge*>::iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
1124  Edge* e = (*i).second;
1125  if (e->myLayer != 0) {
1126  for (std::vector<long long int>::iterator j = e->myCurrentNodes.begin(); j != e->myCurrentNodes.end(); ++j) {
1127  NBNode* node = nc.retrieve(toString(*j));
1128  if (node != 0) {
1129  knownElevation.insert(node);
1130  layerForces[node].push_back(std::make_pair(e->myLayer * layerElevation, POSITION_EPS));
1131  }
1132  }
1133  }
1134  }
1135 #ifdef DEBUG_LAYER_ELEVATION
1136  std::cout << "known elevations:\n";
1137  for (std::set<NBNode*>::iterator it = knownElevation.begin(); it != knownElevation.end(); ++it) {
1138  const std::vector<std::pair<SUMOReal, SUMOReal> >& primaryLayers = layerForces[*it];
1139  std::cout << " node=" << (*it)->getID() << " ele=";
1140  for (std::vector<std::pair<SUMOReal, SUMOReal> >::const_iterator it_ele = primaryLayers.begin(); it_ele != primaryLayers.end(); ++it_ele) {
1141  std::cout << it_ele->first << " ";
1142  }
1143  std::cout << "\n";
1144  }
1145 #endif
1146  // collect all nodes within a grade-dependent range around knownElevation-nodes
1147  std::set<NBNode*> unknownElevation;
1148  for (std::set<NBNode*>::iterator it = knownElevation.begin(); it != knownElevation.end(); ++it) {
1150  SUMOReal eleSum = 0;
1151  const std::vector<std::pair<SUMOReal, SUMOReal> >& primaryLayers = layerForces[*it];
1152  for (std::vector<std::pair<SUMOReal, SUMOReal> >::const_iterator it_ele = primaryLayers.begin(); it_ele != primaryLayers.end(); ++it_ele) {
1153  eleMax = MAX2(eleMax, it_ele->first);
1154  eleSum += it_ele->first;
1155  }
1156  const SUMOReal eleAvg = eleSum / primaryLayers.size();
1157  const SUMOReal maxDist = fabs(eleMax) * 100 / layerElevation;
1158  std::map<NBNode*, SUMOReal> neighbors = getNeighboringNodes(*it, maxDist);
1159  for (std::map<NBNode*, SUMOReal>::iterator it_neigh = neighbors.begin(); it_neigh != neighbors.end(); ++it_neigh) {
1160  unknownElevation.insert(it_neigh->first);
1161  layerForces[it_neigh->first].push_back(std::make_pair(eleAvg, it_neigh->second));
1162  }
1163  }
1164 
1165  // collect forces from ground-level nodes (neither in knownElevation nor unknownElevation)
1166  for (std::set<NBNode*>::iterator it = unknownElevation.begin(); it != unknownElevation.end(); ++it) {
1168  const std::vector<std::pair<SUMOReal, SUMOReal> >& primaryLayers = layerForces[*it];
1169  for (std::vector<std::pair<SUMOReal, SUMOReal> >::const_iterator it_ele = primaryLayers.begin(); it_ele != primaryLayers.end(); ++it_ele) {
1170  eleMax = MAX2(eleMax, it_ele->first);
1171  }
1172  const SUMOReal maxDist = fabs(eleMax) * 100 / layerElevation;
1173  std::map<NBNode*, SUMOReal> neighbors = getNeighboringNodes(*it, maxDist);
1174  for (std::map<NBNode*, SUMOReal>::iterator it_neigh = neighbors.begin(); it_neigh != neighbors.end(); ++it_neigh) {
1175  if (knownElevation.count(it_neigh->first) == 0 && unknownElevation.count(it_neigh->first) == 0) {
1176  layerForces[*it].push_back(std::make_pair(0, it_neigh->second));
1177  }
1178  }
1179  }
1180  // compute the elevation for each node as the weighted average of all forces
1181 #ifdef DEBUG_LAYER_ELEVATION
1182  std::cout << "summation of forces\n";
1183 #endif
1184  std::map<NBNode*, SUMOReal> nodeElevation;
1185  for (std::map<NBNode*, std::vector<std::pair<SUMOReal, SUMOReal> > >::iterator it = layerForces.begin(); it != layerForces.end(); ++it) {
1186  const std::vector<std::pair<SUMOReal, SUMOReal> >& forces = it->second;
1187  if (forces.size() == 1) {
1188  nodeElevation[it->first] = forces.front().first;
1189  } else if (knownElevation.count(it->first) != 0) {
1190  // use the maximum value
1192  for (std::vector<std::pair<SUMOReal, SUMOReal> >::const_iterator it_force = forces.begin(); it_force != forces.end(); ++it_force) {
1193  eleMax = MAX2(eleMax, it_force->first);
1194  }
1195  nodeElevation[it->first] = eleMax;
1196  } else {
1197  // use the weighted sum
1198  SUMOReal distSum = 0;
1199  for (std::vector<std::pair<SUMOReal, SUMOReal> >::const_iterator it_force = forces.begin(); it_force != forces.end(); ++it_force) {
1200  distSum += it_force->second;
1201  }
1202  SUMOReal weightSum = 0;
1203  SUMOReal elevation = 0;
1204 #ifdef DEBUG_LAYER_ELEVATION
1205  std::cout << " node=" << it->first->getID() << " distSum=" << distSum << "\n";
1206 #endif
1207  for (std::vector<std::pair<SUMOReal, SUMOReal> >::const_iterator it_force = forces.begin(); it_force != forces.end(); ++it_force) {
1208  const SUMOReal weight = (distSum - it_force->second) / distSum;
1209  weightSum += weight;
1210  elevation += it_force->first * weight;
1211 
1212 #ifdef DEBUG_LAYER_ELEVATION
1213  std::cout << " force=" << it_force->first << " dist=" << it_force->second << " weight=" << weight << " ele=" << elevation << "\n";
1214 #endif
1215  }
1216  nodeElevation[it->first] = elevation / weightSum;
1217  }
1218  }
1219 #ifdef DEBUG_LAYER_ELEVATION
1220  std::cout << "final elevations:\n";
1221  for (std::map<NBNode*, SUMOReal>::iterator it = nodeElevation.begin(); it != nodeElevation.end(); ++it) {
1222  std::cout << " node=" << (it->first)->getID() << " ele=" << it->second << "\n";;
1223  }
1224 #endif
1225  // apply node elevations and interpolate edge shapes in z-direction
1226  for (std::map<NBNode*, SUMOReal>::iterator it = nodeElevation.begin(); it != nodeElevation.end(); ++it) {
1227  NBNode* n = it->first;
1228  Position pos = n->getPosition();
1229  n->reinit(n->getPosition() + Position(0, 0, it->second), n->getType());
1230  }
1231 
1232  // apply way elevation to all edges that had layer information
1233  for (std::map<std::string, NBEdge*>::const_iterator it = ec.begin(); it != ec.end(); ++it) {
1234  NBEdge* edge = it->second;
1235  const PositionVector& geom = edge->getGeometry();
1236  const SUMOReal length = geom.length2D();
1237  const SUMOReal zFrom = nodeElevation[edge->getFromNode()];
1238  const SUMOReal zTo = nodeElevation[edge->getToNode()];
1239  // XXX if the from- or to-node was part of multiple ways with
1240  // different layers, reconstruct the layer value from origID
1241  SUMOReal dist = 0;
1242  PositionVector newGeom;
1243  for (PositionVector::const_iterator it_pos = geom.begin(); it_pos != geom.end(); ++it_pos) {
1244  if (it_pos != geom.begin()) {
1245  dist += (*it_pos).distanceTo2D(*(it_pos - 1));
1246  }
1247  newGeom.push_back((*it_pos) + Position(0, 0, zFrom + (zTo - zFrom) * dist / length));
1248  }
1249  edge->setGeometry(newGeom);
1250  }
1251 }
1252 
1253 
1254 std::map<NBNode*, SUMOReal>
1256  std::map<NBNode*, SUMOReal> result;
1257  std::set<NBNode*> visited;
1258  std::vector<NBNode*> open;
1259  open.push_back(node);
1260  while (open.size() > 0) {
1261  NBNode* n = open.back();
1262  open.pop_back();
1263  if (visited.count(n) != 0) {
1264  continue;
1265  }
1266  visited.insert(n);
1267  const EdgeVector& edges = n->getEdges();
1268  for (EdgeVector::const_iterator j = edges.begin(); j != edges.end(); ++j) {
1269  NBEdge* e = *j;
1270  NBNode* s = 0;
1271  if (n->hasIncoming(e)) {
1272  s = e->getFromNode();
1273  } else {
1274  s = e->getToNode();
1275  }
1276  const SUMOReal dist = result[n] + e->getGeometry().length2D();
1277  if (result.count(s) == 0) {
1278  result[s] = dist;
1279  } else {
1280  result[s] = MIN2(dist, result[s]);
1281  }
1282  if (dist < maxDist) {
1283  open.push_back(s);
1284  }
1285  }
1286  }
1287  return result;
1288 }
1289 
1290 
1291 /****************************************************************************/
1292 
const SUMOReal lat
The latitude the node is located at.
void reconstructLayerElevation(SUMOReal layerElevation, NBNetBuilder &nb)
reconstruct elevation from layer info
An internal definition of a loaded edge.
An internal representation of an OSM-node.
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
const EdgeVector & getIncomingEdges() const
Returns this node's incoming edges.
Definition: NBNode.h:240
const long long int id
The edge's id.
static const SUMOReal UNSPECIFIED_WIDTH
unspecified lane width
Definition: NBEdge.h:237
std::string streetName
The edge's street name.
NBTypeCont & getTypeCont()
Returns the type container.
Definition: NBNetBuilder.h:169
const SUMOReal SUMO_const_laneWidth
Definition: StdDefs.h:49
std::string next()
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
static bool transformCoordinates(Position &from, bool includeInBoundary=true, GeoConvHelper *from_srs=0)
transforms loaded coordinates handles projections, offsets (using GeoConvHelper) and import of height...
const long long int id
The node's id.
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:58
static bool endsWith(const std::string &str, const std::string suffix)
Checks whether a given string ends with the suffix.
WayType myBuswayType
Information about the kind of busway along this road.
static SUMOReal _2SUMOReal(const E *const data)
converts a char-type array into the SUMOReal value described by it
Definition: TplConvert.h:290
A container for traffic light definitions and built programs.
void reinit(const Position &position, SumoXMLNodeType type, bool updateEdgeGeometries=false)
Resets initial values.
Definition: NBNode.cpp:266
bool applyRestriction() const
try to apply the parsed restriction and return whether successful
void addSidewalk(SUMOReal width)
add a pedestrian sidewalk of the given width and shift existing connctions
Definition: NBEdge.cpp:2824
vehicle is a bicycle
void myEndElement(int element)
Called when a closing tag occurs.
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
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
static std::string escapeXML(const std::string &orig)
Replaces the standard escapes by their XML entities.
bool getIsOneWay(const std::string &type) const
Returns whether edges are one-way per default for the given type.
Definition: NBTypeCont.cpp:197
T MAX2(T a, T b)
Definition: StdDefs.h:75
void setPermissions(SVCPermissions permissions, int lane=-1)
set allowed/disallowed classes for the given lane or for all lanes if -1 is given ...
Definition: NBEdge.cpp:2698
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
void myEndElement(int element)
Called when a closing tag occurs.
static const SUMOReal UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:240
SAX-handler base for SUMO-files.
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:114
bool hasIncoming(const NBEdge *const e) const
Returns whether the given edge ends at this node.
Definition: NBNode.cpp:1224
std::vector< long long int > myCurrentNodes
The list of nodes this edge is made of.
void setGeometry(const PositionVector &g, bool inner=false)
(Re)sets the edge's geometry
Definition: NBEdge.cpp:493
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
SUMOReal getWidth(const std::string &type) const
Returns the lane width for the given type [m].
Definition: NBTypeCont.cpp:221
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
std::string joinToStringSorting(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:204
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
std::map< NBNode *, SUMOReal > getNeighboringNodes(NBNode *node, SUMOReal maxDist)
collect neighboring nodes with their road distance
SUMOReal ele
The elevation of this node.
NBNode * node
the NBNode that was instantiated
PositionVector reverse() const
reverse position vector
static const SUMOReal MAXSPEED_UNGIVEN
SUMOReal getSidewalkWidth(const std::string &type) const
Returns the lane width for a sidewalk to be added [m].
Definition: NBTypeCont.cpp:227
static void loadNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Loads content of the optionally given OSM file.
Functor which compares two Edges.
WayType myCyclewayType
Information about the kind of cycleway along this road.
const EdgeVector & getOutgoingEdges() const
Returns this node's outgoing edges.
Definition: NBNode.h:248
const std::string & getID() const
Returns the id.
Definition: Named.h:66
SUMOReal length2D() const
Returns the length.
int myNoLanesForward
number of lanes in forward direction or 0 if unknown, negative if backwards lanes are meant ...
bool addEdge2EdgeConnection(NBEdge *dest)
Adds a connection to another edge.
Definition: NBEdge.cpp:778
const Position & getPosition() const
Returns the position of this node.
Definition: NBNode.h:228
RelationHandler(const std::map< long long int, NIOSMNode * > &osmNodes, const std::map< long long int, Edge * > &osmEdges)
Constructor.
#define max(a, b)
Definition: polyfonts.c:65
void load(const OptionsCont &oc, NBNetBuilder &nb)
SUMOReal getSpeed(const std::string &type) const
Returns the maximal velocity for the given type [m/s].
Definition: NBTypeCont.cpp:185
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
void setFileName(const std::string &name)
Sets the current file name.
SUMOReal getBikeLaneWidth(const std::string &type) const
Returns the lane width for a bike lane to be added [m].
Definition: NBTypeCont.cpp:233
std::map< std::string, NBEdge * >::const_iterator end() const
Returns the pointer to the end of the stored edges.
Definition: NBEdgeCont.h:198
A class which extracts OSM-edges from a parsed OSM-file.
int insertEdge(Edge *e, int index, NBNode *from, NBNode *to, const std::vector< long long int > &passed, NBNetBuilder &nb)
Builds an NBEdge.
bool insert(NBEdge *edge, bool ignorePrunning=false)
Adds an edge to the dictionary.
Definition: NBEdgeCont.cpp:162
Encapsulated SAX-Attributes.
static StringBijection< TrafficLightType > TrafficLightTypes
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
int getNumLanes(const std::string &type) const
Returns the number of lanes for the given type.
Definition: NBTypeCont.cpp:179
NBEdgeCont & getEdgeCont()
Returns the edge container.
Definition: NBNetBuilder.h:153
A list of positions.
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:265
int getPriority(const std::string &type) const
Returns the priority for the given type.
Definition: NBTypeCont.cpp:191
void myEndElement(int element)
Called when a closing tag occurs.
const EdgeVector & getEdges() const
Returns all edges which participate in this node.
Definition: NBNode.h:256
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
T MIN2(T a, T b)
Definition: StdDefs.h:69
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
#define POSITION_EPS
Definition: config.h:188
NodesHandler(std::map< long long int, NIOSMNode * > &toFill, std::set< NIOSMNode *, CompareNodes > &uniqueNodes, bool importElevation)
Contructor.
bool knows(const std::string &type) const
Returns whether the named type is in the container.
Definition: NBTypeCont.cpp:79
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
void removeFromConnections(NBEdge *toEdge, int fromLane=-1, int toLane=-1, bool tryLater=false)
Removes the specified connection(s)
Definition: NBEdge.cpp:1063
bool railwayCrossing
Whether this is a railway crossing.
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
double myMaxSpeed
maximum speed in km/h, or MAXSPEED_UNGIVEN
void insert(const std::string &id, int numLanes, SUMOReal maxSpeed, int prio, SVCPermissions permissions, SUMOReal width, bool oneWayIsDefault, SUMOReal sidewalkWidth, SUMOReal bikeLaneWidth)
Adds a type into the list.
Definition: NBTypeCont.cpp:65
bool checkEdgeRef(long long int ref) const
check whether a referenced way has a corresponding edge
std::map< long long int, Edge * > myEdges
the map from OSM way ids to edge objects
std::vector< std::string > getVector()
std::map< std::string, NBEdge * >::const_iterator begin() const
Returns the pointer to the begin of the stored edges.
Definition: NBEdgeCont.h:190
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
int myNoLanes
number of lanes, or -1 if unknown
vehicle is a bus
void addBikeLane(SUMOReal width)
add a bicycle lane of the given width and shift existing connctions
Definition: NBEdge.cpp:2846
static std::string to_lower_case(std::string str)
Transfers the content to lower case.
Definition: StringUtils.cpp:67
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
Definition: TplConvert.h:149
bool tlsControlled
Whether this is a tls controlled junction.
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:416
std::map< std::string, std::string > myKnownCompoundTypes
The compound types that have already been mapped to other known types.
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:56
EdgesHandler(const std::map< long long int, NIOSMNode * > &osmNodes, std::map< long long int, Edge * > &toFill)
Constructor.
const SUMOReal lon
The longitude the node is located at.
NBNodeCont & getNodeCont()
Returns the node container.
Definition: NBNetBuilder.h:161
int myLayer
Information about the relative z-ordering of ways.
Instance responsible for building networks.
Definition: NBNetBuilder.h:112
std::vector< NBEdge * > EdgeVector
Definition: NBCont.h:41
bool getShallBeDiscarded(const std::string &type) const
Returns the information whether edges of this type shall be discarded.
Definition: NBTypeCont.cpp:203
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:577
static const std::string compoundTypeSeparator
The separator within newly created compound type names.
std::map< std::string, SUMOReal > mySpeedMap
A map of non-numeric speed descriptions to their numeric values.
virtual std::string getStringSecure(int id, const std::string &def) const =0
Returns the string-value of the named (by its enum-value) attribute.
A storage for options typed value containers)
Definition: OptionsCont.h:99
bool copyRestrictionsAndAttrs(const std::string &fromId, const std::string &toId)
Copy restrictions to a type.
Definition: NBTypeCont.cpp:118
bool insert(const std::string &id, const Position &position, NBDistrict *district=0)
Inserts a node into the map.
Definition: NBNodeCont.cpp:81
NBTrafficLightLogicCont & getTLLogicCont()
Returns the traffic light logics container.
Definition: NBNetBuilder.h:177
LaneSpreadFunction
Numbers representing special SUMO-XML-attribute values Information how the edge's lateral offset shal...
NBEdge * findEdgeRef(long long int wayRef, const std::vector< NBEdge * > &candidates) const
try to find the way segment among candidates
A class which extracts OSM-nodes from a parsed OSM-file.
Represents a single node (junction) during network building.
Definition: NBNode.h:74
void resetValues()
reset members to their defaults for parsing a new relation
NBNode * insertNodeChecking(long long int id, NBNodeCont &nc, NBTrafficLightLogicCont &tlsc)
Builds an NBNode.
T get(const std::string &str) const
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:188
std::string myHighWayType
The type, stored in "highway" key.
bool insert(NBTrafficLightDefinition *logic, bool forceInsert=false)
Adds a logic definition to the dictionary.
Importer for networks stored in OpenStreetMap format.
static const long long int INVALID_ID
#define SUMOReal
Definition: config.h:214
bool myCurrentIsRoad
Information whether this is a road.
bool operator()(const Edge *e1, const Edge *e2) const
std::set< std::string > myUnusableTypes
The compounds types that do not contain known types.
void push_back_noDoublePos(const Position &p)
insert in back a non double position
NBNode * retrieve(const std::string &id) const
Returns the node with the given name.
Definition: NBNodeCont.cpp:110
SVCPermissions getPermissions(const std::string &type) const
Returns allowed vehicle classes for the given type.
Definition: NBTypeCont.cpp:215
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:63
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
std::map< long long int, NIOSMNode * > myOSMNodes
the map from OSM node ids to actual nodes
A traffic light logics which must be computed (only nodes/edges are given)
Definition: NBOwnTLDef.h:54
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
std::set< NIOSMNode *, CompareNodes > myUniqueNodes
the set of unique nodes used in NodesHandler, used when freeing memory
A class which extracts relevant relation information from a parsed OSM-file.
std::string myIsOneWay
Information whether this is an one-way road.
TrafficLightType
A storage for available types of edges.
Definition: NBTypeCont.h:62
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:409