SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NIImporter_DlrNavteq.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Importer for networks stored in Elmar's format
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2013 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 
33 #include <string>
34 #include <sstream>
40 #include <utils/common/ToString.h>
44 #include <netbuild/NBNetBuilder.h>
45 #include <netbuild/NBNode.h>
46 #include <netbuild/NBNodeCont.h>
47 #include <netbuild/NBEdge.h>
48 #include <netbuild/NBEdgeCont.h>
49 #include <netbuild/NBTypeCont.h>
50 #include <netbuild/NBOwnTLDef.h>
52 #include "NILoader.h"
53 #include "NIImporter_DlrNavteq.h"
54 
55 
56 #ifdef CHECK_MEMORY_LEAKS
57 #include <foreign/nvwa/debug_new.h>
58 #endif // CHECK_MEMORY_LEAKS
59 
60 // ---------------------------------------------------------------------------
61 // static members
62 // ---------------------------------------------------------------------------
64 
65 // ===========================================================================
66 // method definitions
67 // ===========================================================================
68 // ---------------------------------------------------------------------------
69 // static methods
70 // ---------------------------------------------------------------------------
71 void
73  // check whether the option is set (properly)
74  if (!oc.isSet("dlr-navteq-prefix")) {
75  return;
76  }
77  // parse file(s)
78  LineReader lr;
79  // load nodes
80  std::map<std::string, PositionVector> myGeoms;
81  PROGRESS_BEGIN_MESSAGE("Loading nodes");
82  std::string file = oc.getString("dlr-navteq-prefix") + "_nodes_unsplitted.txt";
83  NodesHandler handler1(nb.getNodeCont(), file, myGeoms);
84  if (!lr.setFile(file)) {
85  throw ProcessError("The file '" + file + "' could not be opened.");
86  }
87  lr.readAll(handler1);
89 
90  // load street names if given and wished
91  std::map<std::string, std::string> streetNames; // nameID : name
92  if (oc.getBool("output.street-names")) {
93  file = oc.getString("dlr-navteq-prefix") + "_names.txt";
94  if (lr.setFile(file)) {
95  PROGRESS_BEGIN_MESSAGE("Loading Street Names");
96  NamesHandler handler4(file, streetNames);
97  lr.readAll(handler4);
99  } else {
100  WRITE_WARNING("Output will not contain street names because the file '" + file + "' was not found");
101  }
102  }
103 
104  // load edges
105  PROGRESS_BEGIN_MESSAGE("Loading edges");
106  file = oc.getString("dlr-navteq-prefix") + "_links_unsplitted.txt";
107  // parse the file
108  EdgesHandler handler2(nb.getNodeCont(), nb.getEdgeCont(), file, myGeoms, streetNames);
109  if (!lr.setFile(file)) {
110  throw ProcessError("The file '" + file + "' could not be opened.");
111  }
112  lr.readAll(handler2);
115 
116  // load traffic lights if given
117  file = oc.getString("dlr-navteq-prefix") + "_traffic_signals.txt";
118  if (lr.setFile(file)) {
119  PROGRESS_BEGIN_MESSAGE("Loading traffic lights");
120  TrafficlightsHandler handler3(nb.getNodeCont(), nb.getTLLogicCont(), nb.getEdgeCont(), file);
121  lr.readAll(handler3);
123  }
124 }
125 
126 
127 // ---------------------------------------------------------------------------
128 // definitions of NIImporter_DlrNavteq::NodesHandler-methods
129 // ---------------------------------------------------------------------------
131  const std::string& file,
132  std::map<std::string, PositionVector>& geoms)
133  : myNodeCont(nc), myGeoms(geoms) {
134  UNUSED_PARAMETER(file);
135 }
136 
137 
139 
140 
141 bool
142 NIImporter_DlrNavteq::NodesHandler::report(const std::string& result) {
143  if (result[0] == '#') {
144  return true;
145  }
146  std::string id;
147  double x, y;
148  int no_geoms, intermediate;
149  // parse
150  std::istringstream stream(result);
151  // id
152  stream >> id;
153  if (stream.fail()) {
154  throw ProcessError("Something is wrong with the following data line\n" + result);
155  }
156  // intermediate?
157  stream >> intermediate;
158  if (stream.fail()) {
159  if (myNodeCont.size() == 0) { // be generous with extra data at beginning of file
160  return true;
161  }
162  throw ProcessError("Non-numerical value for intermediate status in node " + id + ".");
163  }
164  // number of geometrical information
165  stream >> no_geoms;
166  if (stream.fail()) {
167  throw ProcessError("Non-numerical value for number of geometries in node " + id + ".");
168  }
169  // geometrical information
170  PositionVector geoms;
171  for (int i = 0; i < no_geoms; i++) {
172  stream >> x;
173  if (stream.fail()) {
174  throw ProcessError("Non-numerical value for x-position in node " + id + ".");
175  }
176  stream >> y;
177  if (stream.fail()) {
178  throw ProcessError("Non-numerical value for y-position in node " + id + ".");
179  }
180  Position pos(x, y);
181  if (!NILoader::transformCoordinates(pos, true)) {
182  throw ProcessError("Unable to project coordinates for node " + id + ".");
183  }
184  geoms.push_back(pos);
185  }
186 
187  if (intermediate == 0) {
188  NBNode* n = new NBNode(id, geoms[0]);
189  if (!myNodeCont.insert(n)) {
190  delete n;
191  throw ProcessError("Could not add node '" + id + "'.");
192  }
193  } else {
194  myGeoms[id] = geoms;
195  }
196  return true;
197 }
198 
199 
200 // ---------------------------------------------------------------------------
201 // definitions of NIImporter_DlrNavteq::EdgesHandler-methods
202 // ---------------------------------------------------------------------------
204  const std::string& file,
205  std::map<std::string, PositionVector>& geoms,
206  std::map<std::string, std::string>& streetNames):
207  myNodeCont(nc), myEdgeCont(ec), myGeoms(geoms), myStreetNames(streetNames) {
208  UNUSED_PARAMETER(file);
209 }
210 
211 
213 
214 
215 bool
216 NIImporter_DlrNavteq::EdgesHandler::report(const std::string& result) {
217 // 0: LINK_ID NODE_ID_FROM NODE_ID_TO BETWEEN_NODE_ID
218 // 4: length vehicle_type form_of_way brunnel_type
219 // 7: street_type speed_category number_of_lanes average_speed
220 // 10: NAME_ID1 NAME_ID2 housenumbers_right housenumbers_left
221 // ZIP_CODE AREA_ID SUBAREA_ID through_traffic special_restrictions
222 // extended_number_of_lanes isRamp (these two only exist in networks extracted since 05/2009)
223 // connection (this may be omitted)
224 
225  if (result[0] == '#') {
226  return true;
227  }
228  std::string id, fromID, toID, interID;
229  SUMOReal length;
230  SUMOReal speed = (SUMOReal) 30.0 / (SUMOReal) 3.6;
231  int nolanes = 1;
232  int priority;
233  int form_of_way;
234  // parse
236  // id
237  id = st.next();
238  // from node id
239  fromID = st.next();
240  // to node id
241  toID = st.next();
242  // intermediate node id
243  interID = st.next();
244  // length
245  try {
246  length = TplConvert::_2SUMOReal(st.next().c_str());
247  } catch (NumberFormatException&) {
248  throw ProcessError("Non-numerical value for an edge's length occured (edge '" + id + "'.");
249  }
250  // vehicle_type
251  std::string veh_type = st.next();
252  // form_of_way
253  try {
254  form_of_way = TplConvert::_2int(st.next().c_str());
255  } catch (NumberFormatException&) {
256  throw ProcessError("Non-numerical value for an edge's form_of_way occured (edge '" + id + "'.");
257  }
258  // brunnel_type
259  std::string brunnel_type = st.next();
260  // street_type used for priority
261  try {
262  priority = -TplConvert::_2int(st.next().c_str());
263  } catch (NumberFormatException&) {
264  throw ProcessError("Non-numerical value for an edge's street_type occured (edge '" + id + "'.");
265  }
266  // modify priority using form_of_way
267  if (form_of_way == 11) {
268  priority -= 1; // frontage road, very often with lowered curb
269  } else if (form_of_way > 11) {
270  priority -= 2; // parking/service access assume lowered curb
271  }
272  speed = NINavTeqHelper::getSpeed(id, st.next());
273  // number of lanes
274  nolanes = NINavTeqHelper::getLaneNumber(id, st.next(), speed);
275  // average_speed (reportedly this is simply the speed from speed_category minus 10km/h)
276  std::string average_speed = st.next();
277  // regional street name id
278  std::string nameID_regional = st.next();
279  // local street name id
280  std::string nameID_local = st.next();
281  std::string streetName = getStreetNameFromIDs(nameID_regional, nameID_local);
282  std::vector<std::string> theRest = st.getVector();
283  bool connection = (theRest.size() == 8) && (theRest[7] == "1");
284  if (theRest.size() > 8) {
285  // post 05/2009 network
286  if (theRest[8] != "-1") {
287  try {
288  nolanes = TplConvert::_2int(theRest[8].c_str());
289  } catch (NumberFormatException&) {
290  throw ProcessError("Non-numerical value for the extended number of lanes (edge '" + id + "'.");
291  }
292  }
293  connection = (theRest.size() == 10) && (theRest[9] == "1");
294  }
295  // try to get the nodes
296  NBNode* from = myNodeCont.retrieve(fromID);
297  NBNode* to = myNodeCont.retrieve(toID);
298  if (from == 0) {
299  throw ProcessError("The from-node '" + fromID + "' of edge '" + id + "' could not be found");
300  }
301  if (to == 0) {
302  throw ProcessError("The to-node '" + toID + "' of edge '" + id + "' could not be found");
303  }
304  // build the edge
305  NBEdge* e = 0;
306  if (interID == "-1") {
307  e = new NBEdge(id, from, to, "", speed, nolanes, priority,
309  } else {
310  PositionVector geoms = myGeoms[interID];
311  if (connection) {
312  geoms = geoms.reverse();
313  }
314  geoms.push_front(from->getPosition());
315  geoms.push_back(to->getPosition());
316  e = new NBEdge(id, from, to, "", speed, nolanes, priority,
318  }
319  // add vehicle type information to the edge
320  NINavTeqHelper::addVehicleClasses(*e, veh_type);
321  // permission modifications based on form_of_way
322  if (form_of_way == 14) { // pedestrian area (fussgaengerzone)
323  // unfortunately, the veh_type string is misleading in this case
325  }
326  // insert the edge to the network
327  if (!myEdgeCont.insert(e)) {
328  delete e;
329  throw ProcessError("Could not add edge '" + id + "'.");
330  }
331  return true;
332 }
333 
334 
335 std::string
337  const std::string& regionalID, const std::string& localID) const {
338  std::string result = "";
339  bool hadRegional = false;
340  if (myStreetNames.count(regionalID) > 0) {
341  hadRegional = true;
342  result += myStreetNames[regionalID];
343  }
344  if (myStreetNames.count(localID) > 0) {
345  if (hadRegional) {
346  result += " / ";
347  }
348  result += myStreetNames[localID];
349  }
350  return result;
351 }
352 
353 // ---------------------------------------------------------------------------
354 // definitions of NIImporter_DlrNavteq::TrafficlightsHandler-methods
355 // ---------------------------------------------------------------------------
358  NBEdgeCont& ne,
359  const std::string& file) :
360  myNodeCont(nc),
361  myTLLogicCont(tlc),
362  myEdgeCont(ne) {
363  UNUSED_PARAMETER(file);
364 }
365 
366 
368 
369 
370 bool
372 // #ID POICOL-TYPE DESCRIPTION LONGITUDE LATITUDE NAVTEQ_LINK_ID NODEID
373 
374  if (result[0] == '#') {
375  return true;
376  }
378  const std::string edgeID = st.get(5);
379  NBEdge* edge = myEdgeCont.retrieve(edgeID);
380  if (edge == 0) {
381  WRITE_WARNING("The traffic light edge '" + edgeID + "' could not be found");
382  } else {
383  NBNode* node = edge->getToNode();
384  if (node->getType() != NODETYPE_TRAFFIC_LIGHT) {
385  node->reinit(node->getPosition(), NODETYPE_TRAFFIC_LIGHT);
386  // @note. There may be additional information somewhere in the GDF files about traffic light type ...
388  // @note actually we could use the navteq node ID here
389  NBTrafficLightDefinition* tlDef = new NBOwnTLDef(node->getID(), node, 0, type);
390  if (!myTLLogicCont.insert(tlDef)) {
391  // actually, nothing should fail here
392  delete tlDef;
393  throw ProcessError("Could not allocate tls for '" + node->getID() + "'.");
394  }
395  }
396  }
397  return true;
398 }
399 
400 
401 // ---------------------------------------------------------------------------
402 // definitions of NIImporter_DlrNavteq::NamesHandler-methods
403 // ---------------------------------------------------------------------------
405  const std::string& file, std::map<std::string, std::string>& streetNames) :
406  myStreetNames(streetNames) {
407  UNUSED_PARAMETER(file);
408 }
409 
410 
412 
413 
414 bool
415 NIImporter_DlrNavteq::NamesHandler::report(const std::string& result) {
416 // # NAME_ID Name
417  if (result[0] == '#') {
418  return true;
419  }
421  if (st.size() == 1) {
422  return true; // one line with the number of data containing lines in it
423  }
424  assert(st.size() >= 2);
425  const std::string id = st.next();
426  myStreetNames[id] = joinToString(st.getVector(), " ");
427  return true;
428 }
429 /****************************************************************************/