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-2012 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>
43 #include <netbuild/NBNetBuilder.h>
44 #include <netbuild/NBNode.h>
45 #include <netbuild/NBNodeCont.h>
46 #include <netbuild/NBEdge.h>
47 #include <netbuild/NBEdgeCont.h>
48 #include <netbuild/NBTypeCont.h>
49 #include <netbuild/NBOwnTLDef.h>
51 #include "NILoader.h"
52 #include "NIImporter_DlrNavteq.h"
53 
54 
55 #ifdef CHECK_MEMORY_LEAKS
56 #include <foreign/nvwa/debug_new.h>
57 #endif // CHECK_MEMORY_LEAKS
58 
59 
60 // ===========================================================================
61 // method definitions
62 // ===========================================================================
63 // ---------------------------------------------------------------------------
64 // static methods
65 // ---------------------------------------------------------------------------
66 void
68  // check whether the option is set (properly)
69  if (!oc.isSet("dlr-navteq-prefix")) {
70  return;
71  }
72  // parse file(s)
73  LineReader lr;
74  // load nodes
75  std::map<std::string, PositionVector> myGeoms;
76  PROGRESS_BEGIN_MESSAGE("Loading nodes");
77  std::string file = oc.getString("dlr-navteq-prefix") + "_nodes_unsplitted.txt";
78  NodesHandler handler1(nb.getNodeCont(), file, myGeoms);
79  if (!lr.setFile(file)) {
80  throw ProcessError("The file '" + file + "' could not be opened.");
81  }
82  lr.readAll(handler1);
84 
85  // load traffic lights
86  file = oc.getString("dlr-navteq-prefix") + "_traffic_signals.txt";
87  if (lr.setFile(file)) {
88  PROGRESS_BEGIN_MESSAGE("Loading traffic lights");
89  TrafficlightsHandler handler3(nb.getNodeCont(), nb.getTLLogicCont(), file);
90  lr.readAll(handler3);
92  }
93 
94  // load edges
95  PROGRESS_BEGIN_MESSAGE("Loading edges");
96  file = oc.getString("dlr-navteq-prefix") + "_links_unsplitted.txt";
97  // parse the file
98  EdgesHandler handler2(nb.getNodeCont(), nb.getEdgeCont(), file, myGeoms);
99  if (!lr.setFile(file)) {
100  throw ProcessError("The file '" + file + "' could not be opened.");
101  }
102  lr.readAll(handler2);
105 }
106 
107 
108 // ---------------------------------------------------------------------------
109 // definitions of NIImporter_DlrNavteq::NodesHandler-methods
110 // ---------------------------------------------------------------------------
112  const std::string& file,
113  std::map<std::string, PositionVector> &geoms)
114  : myNodeCont(nc), myGeoms(geoms) {
115  UNUSED_PARAMETER(file);
116 }
117 
118 
120 
121 
122 bool
123 NIImporter_DlrNavteq::NodesHandler::report(const std::string& result) {
124  if (result[0] == '#') {
125  return true;
126  }
127  std::string id;
128  double x, y;
129  int no_geoms, intermediate;
130  // parse
131  std::istringstream stream(result);
132  // id
133  stream >> id;
134  if (stream.fail()) {
135  throw ProcessError("Something is wrong with the following data line\n" + result);
136  }
137  // intermediate?
138  stream >> intermediate;
139  if (stream.fail()) {
140  if (myNodeCont.size() == 0) { // be generous with extra data at beginning of file
141  return true;
142  }
143  throw ProcessError("Non-numerical value for intermediate status in node " + id + ".");
144  }
145  // number of geometrical information
146  stream >> no_geoms;
147  if (stream.fail()) {
148  throw ProcessError("Non-numerical value for number of geometries in node " + id + ".");
149  }
150  // geometrical information
151  PositionVector geoms;
152  for (int i = 0; i < no_geoms; i++) {
153  stream >> x;
154  if (stream.fail()) {
155  throw ProcessError("Non-numerical value for x-position in node " + id + ".");
156  }
157  stream >> y;
158  if (stream.fail()) {
159  throw ProcessError("Non-numerical value for y-position in node " + id + ".");
160  }
161  Position pos(x, y);
162  if (!NILoader::transformCoordinates(pos, true)) {
163  throw ProcessError("Unable to project coordinates for node " + id + ".");
164  }
165  geoms.push_back(pos);
166  }
167 
168  if (intermediate == 0) {
169  NBNode* n = new NBNode(id, geoms[0]);
170  if (!myNodeCont.insert(n)) {
171  delete n;
172  throw ProcessError("Could not add node '" + id + "'.");
173  }
174  } else {
175  myGeoms[id] = geoms;
176  }
177  return true;
178 }
179 
180 
181 // ---------------------------------------------------------------------------
182 // definitions of NIImporter_DlrNavteq::EdgesHandler-methods
183 // ---------------------------------------------------------------------------
185  const std::string& file,
186  std::map < std::string,
187  PositionVector > &geoms)
188  : myNodeCont(nc), myEdgeCont(ec), myGeoms(geoms) {
189  UNUSED_PARAMETER(file);
190 }
191 
192 
194 
195 
196 bool
197 NIImporter_DlrNavteq::EdgesHandler::report(const std::string& result) {
198 // 0: LINK_ID NODE_ID_FROM NODE_ID_TO BETWEEN_NODE_ID
199 // 4: length vehicle_type form_of_way brunnel_type
200 // 7: street_type speed_category number_of_lanes average_speed
201 // 10: NAME_ID1 NAME_ID2 housenumbers_right housenumbers_left
202 // ZIP_CODE AREA_ID SUBAREA_ID through_traffic special_restrictions
203 // extended_number_of_lanes isRamp (these two only exist in networks extracted since 05/2009)
204 // connection (this may be omitted)
205 
206  if (result[0] == '#') {
207  return true;
208  }
209  std::string id, fromID, toID, interID;
210  SUMOReal length;
211  SUMOReal speed = (SUMOReal) 30.0 / (SUMOReal) 3.6;
212  int nolanes = 1;
213  int priority = -1;
214  // parse
216  // id
217  id = st.next();
218  // from node id
219  fromID = st.next();
220  // to node id
221  toID = st.next();
222  // intermediate node id
223  interID = st.next();
224  // length
225  try {
226  length = TplConvert<char>::_2SUMOReal(st.next().c_str());
227  } catch (NumberFormatException&) {
228  throw ProcessError("Non-numerical value for an edge's length occured (edge '" + id + "'.");
229  }
230  // vehicle_type
231  std::string veh_type = st.next();
232  // form_of_way
233  std::string form_of_way = st.next();
234  // brunnel_type
235  std::string brunnel_type = st.next();
236  // street_type
237  std::string street_type = st.next();
238  speed = NINavTeqHelper::getSpeed(id, st.next());
239  // number of lanes
240  nolanes = NINavTeqHelper::getLaneNumber(id, st.next(), speed);
241  std::vector<std::string> theRest = st.getVector();
242  bool connection = (theRest.size() == 11) && (theRest[10] == "1");
243  if (theRest.size() > 11) {
244  // post 05/2009 network
245  if (theRest[11] != "-1") {
246  try {
247  nolanes = TplConvert<char>::_2int(theRest[11].c_str());
248  } catch (NumberFormatException&) {
249  throw ProcessError("Non-numerical value for the extended number of lanes (edge '" + id + "'.");
250  }
251  }
252  connection = (theRest.size() == 13) && (theRest[12] == "1");
253  }
254  // try to get the nodes
255  NBNode* from = myNodeCont.retrieve(fromID);
256  NBNode* to = myNodeCont.retrieve(toID);
257  if (from == 0) {
258  throw ProcessError("The from-node '" + fromID + "' of edge '" + id + "' could not be found");
259  }
260  if (to == 0) {
261  throw ProcessError("The to-node '" + toID + "' of edge '" + id + "' could not be found");
262  }
263  // build the edge
264  NBEdge* e = 0;
265  if (interID == "-1") {
266  e = new NBEdge(id, from, to, "", speed, nolanes, priority, -1, -1);
267  } else {
268  PositionVector geoms = myGeoms[interID];
269  if (connection) {
270  geoms = geoms.reverse();
271  geoms.push_front(from->getPosition());
272  geoms.push_back(to->getPosition());
273  e = new NBEdge(id, from, to, "", speed, nolanes, priority, -1, -1, geoms, "", LANESPREAD_CENTER);
274  } else {
275  geoms.push_front(from->getPosition());
276  geoms.push_back(to->getPosition());
277  e = new NBEdge(id, from, to, "", speed, nolanes, priority, -1, -1, geoms, "", LANESPREAD_CENTER);
278  }
279  }
280  // add vehicle type information to the edge
281  NINavTeqHelper::addVehicleClasses(*e, veh_type);
282  // insert the edge to the network
283  if (!myEdgeCont.insert(e)) {
284  delete e;
285  throw ProcessError("Could not add edge '" + id + "'.");
286  }
287  return true;
288 }
289 
290 
291 // ---------------------------------------------------------------------------
292 // definitions of NIImporter_DlrNavteq::TrafficlightsHandler-methods
293 // ---------------------------------------------------------------------------
296  const std::string& file)
297  : myNodeCont(nc), myTLLogicCont(tlc) {
298  UNUSED_PARAMETER(file);
299 }
300 
301 
303 
304 
305 bool
307 // #ID POICOL-TYPE DESCRIPTION LONGITUDE LATITUDE NAVTEQ_LINK_ID NODEID
308 
309  if (result[0] == '#') {
310  return true;
311  }
313  std::string nodeID = st.getVector().back();
314  NBNode* node = myNodeCont.retrieve(nodeID);
315  if (node == 0) {
316  WRITE_WARNING("The traffic light node '" + nodeID + "' could not be found");
317  } else {
318  if (node->getType() != NODETYPE_TRAFFIC_LIGHT) {
319  node->reinit(node->getPosition(), NODETYPE_TRAFFIC_LIGHT);
320  NBTrafficLightDefinition* tlDef = new NBOwnTLDef(nodeID, node);
321  if (!myTLLogicCont.insert(tlDef)) {
322  // actually, nothing should fail here
323  delete tlDef;
324  throw ProcessError("Could not allocate tls for '" + nodeID + "'.");
325  }
326  }
327  }
328  return true;
329 }
330 
331 /****************************************************************************/