SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NIImporter_ITSUMO.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // Importer for networks stored in ITSUMO format
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
11 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 #include <set>
32 #include <functional>
33 #include <sstream>
36 #include <netbuild/NBEdge.h>
37 #include <netbuild/NBEdgeCont.h>
38 #include <netbuild/NBNode.h>
39 #include <netbuild/NBNodeCont.h>
40 #include <netbuild/NBNetBuilder.h>
47 #include <utils/xml/XMLSubSys.h>
48 #include "NILoader.h"
49 #include "NIImporter_ITSUMO.h"
50 
51 #ifdef CHECK_MEMORY_LEAKS
52 #include <foreign/nvwa/debug_new.h>
53 #endif // CHECK_MEMORY_LEAKS
54 
55 
56 
57 // ===========================================================================
58 // static variables
59 // ===========================================================================
81  { "is_preferencial", NIImporter_ITSUMO::ITSUMO_TAG_IS_PREFERENCIAL },
82  { "delimiting_node", NIImporter_ITSUMO::ITSUMO_TAG_DELIMITING_NODE },
86  { "laneset_position", NIImporter_ITSUMO::ITSUMO_TAG_LANESET_POSITION },
89  { "turning_probabilities", NIImporter_ITSUMO::ITSUMO_TAG_TURNING_PROBABILITIES },
91  { "destination_laneset", NIImporter_ITSUMO::ITSUMO_TAG_DESTINATION_LANESET },
98  { "deceleration_prob", NIImporter_ITSUMO::ITSUMO_TAG_DECELERATION_PROB },
100 };
101 
102 
105 };
106 
107 
108 // ===========================================================================
109 // method definitions
110 // ===========================================================================
111 // ---------------------------------------------------------------------------
112 // static methods
113 // ---------------------------------------------------------------------------
114 void
116  // check whether the option is set (properly)
117  if (!oc.isSet("itsumo-files")) {
118  return;
119  }
120  /* Parse file(s)
121  * Each file is parsed twice: first for nodes, second for edges. */
122  std::vector<std::string> files = oc.getStringVector("itsumo-files");
123  // load nodes, first
124  Handler Handler(nb);
125  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
126  // nodes
127  if (!FileHelpers::exists(*file)) {
128  WRITE_ERROR("Could not open itsumo-file '" + *file + "'.");
129  return;
130  }
131  Handler.setFileName(*file);
132  PROGRESS_BEGIN_MESSAGE("Parsing nodes from itsumo-file '" + *file + "'");
133  if (!XMLSubSys::runParser(Handler, *file)) {
134  return;
135  }
137  }
138 }
139 
140 
141 // ---------------------------------------------------------------------------
142 // definitions of NIImporter_ITSUMO::Handler-methods
143 // ---------------------------------------------------------------------------
145  : GenericSAXHandler(itsumoTags, ITSUMO_TAG_NOTHING, itsumoAttrs, ITSUMO_ATTR_NOTHING, "itsumo - file"), myNetBuilder(toFill) {
146 }
147 
148 
150 
151 
152 void
154  switch (element) {
155  case ITSUMO_TAG_NODE:
156  myParameter.clear();
157  break;
158  case ITSUMO_TAG_LANESET:
159  myParameter.clear();
160  break;
161  default:
162  break;
163  }
164 }
165 
166 
167 void
168 NIImporter_ITSUMO::Handler::myCharacters(int element, const std::string& chars) {
169  std::string mc = StringUtils::prune(chars);
170  switch (element) {
171  // node parsing
172  case ITSUMO_TAG_NODE_ID:
173  myParameter["id"] = mc;
174  break;
176  myParameter["name"] = mc;
177  break;
178  case ITSUMO_TAG_X_COORD:
179  myParameter["x"] = mc;
180  break;
181  case ITSUMO_TAG_Y_COORD:
182  myParameter["y"] = mc;
183  break;
184  // section parsing
186  myParameter["sectionID"] = mc;
187  break;
188  // laneset parsing
190  myParameter["lanesetID"] = mc;
191  break;
193  myParameter["pos"] = mc;
194  break;
196  myParameter["from"] = mc;
197  break;
198  case ITSUMO_TAG_END_NODE:
199  myParameter["to"] = mc;
200  break;
201  // lane parsing
202  case ITSUMO_TAG_LANE_ID:
203  myParameter["laneID"] = mc;
204  break;
206  myParameter["i"] = mc;
207  break;
209  myParameter["v"] = mc;
210  break;
211  default:
212  break;
213  }
214 }
215 
216 
217 void
219  switch (element) {
220  case ITSUMO_TAG_SIMULATION: {
221  for (std::vector<Section*>::iterator i = mySections.begin(); i != mySections.end(); ++i) {
222  for (std::vector<LaneSet*>::iterator j = (*i)->laneSets.begin(); j != (*i)->laneSets.end(); ++j) {
223  LaneSet* ls = (*j);
224  NBEdge* edge = new NBEdge(ls->id, ls->from, ls->to, "", ls->v, (unsigned int)ls->lanes.size(), -1, NBEdge::UNSPECIFIED_WIDTH, NBEdge::UNSPECIFIED_OFFSET);
225  if (!myNetBuilder.getEdgeCont().insert(edge)) {
226  delete edge;
227  WRITE_ERROR("Could not add edge '" + ls->id + "'. Probably declared twice.");
228  }
229  delete ls;
230  }
231  delete *i;
232  }
233  }
234  break;
235  case ITSUMO_TAG_NODE: {
236  try {
237  std::string id = myParameter["id"];
238  SUMOReal x = TplConvert::_2SUMOReal(myParameter["x"].c_str());
239  SUMOReal y = TplConvert::_2SUMOReal(myParameter["y"].c_str());
240  Position pos(x, y);
242  WRITE_ERROR("Unable to project coordinates for node '" + id + "'.");
243  }
244  NBNode* node = new NBNode(id, pos);
245  if (!myNetBuilder.getNodeCont().insert(node)) {
246  delete node;
247  WRITE_ERROR("Could not add node '" + id + "'. Probably declared twice.");
248  }
249  } catch (NumberFormatException&) {
250  WRITE_ERROR("Not numeric position information for node '" + myParameter["id"] + "'.");
251  } catch (EmptyData&) {
252  WRITE_ERROR("Missing data in node '" + myParameter["id"] + "'.");
253  }
254  }
255  break;
256  case ITSUMO_TAG_SECTION: {
257  mySections.push_back(new Section(myParameter["sectionID"], myCurrentLaneSets));
258  myCurrentLaneSets.clear();
259  }
260  break;
261  case ITSUMO_TAG_LANESET: {
262  try {
263  std::string id = myParameter["lanesetID"];
264  int i = TplConvert::_2int(myParameter["i"].c_str());
265  std::string fromID = myParameter["from"];
266  std::string toID = myParameter["to"];
267  NBNode* from = myNetBuilder.getNodeCont().retrieve(fromID);
268  NBNode* to = myNetBuilder.getNodeCont().retrieve(toID);
269  if (from == 0 || to == 0) {
270  WRITE_ERROR("Missing node in laneset '" + myParameter["lanesetID"] + "'.");
271  } else {
272  if (myLaneSets.find(id) != myLaneSets.end()) {
273  WRITE_ERROR("Fond laneset-id '" + id + "' twice.");
274  } else {
275  SUMOReal vSum = 0;
276  for (std::vector<Lane>::iterator j = myCurrentLanes.begin(); j != myCurrentLanes.end(); ++j) {
277  vSum += (*j).v;
278  }
279  vSum /= (SUMOReal) myCurrentLanes.size();
280  LaneSet* ls = new LaneSet(id, myCurrentLanes, vSum, i, from, to);
281  myLaneSets[id] = ls;
282  myCurrentLaneSets.push_back(ls);
283  myCurrentLanes.clear();
284  }
285  }
286  } catch (NumberFormatException&) {
287  WRITE_ERROR("Not numeric value in laneset '" + myParameter["lanesetID"] + "'.");
288  } catch (EmptyData&) {
289  WRITE_ERROR("Missing data in laneset '" + myParameter["lanesetID"] + "'.");
290  }
291  }
292  break;
293  case ITSUMO_TAG_LANE: {
294  try {
295  std::string id = myParameter["laneID"];
296  int i = TplConvert::_2int(myParameter["i"].c_str());
297  SUMOReal v = TplConvert::_2SUMOReal(myParameter["v"].c_str());
298  myCurrentLanes.push_back(Lane(id, (unsigned int) i, v));
299  } catch (NumberFormatException&) {
300  WRITE_ERROR("Not numeric value in lane '" + myParameter["laneID"] + "'.");
301  } catch (EmptyData&) {
302  WRITE_ERROR("Missing data in lane '" + myParameter["laneID"] + "'.");
303  }
304  }
305  break;
306  default:
307  break;
308  }
309 }
310 
311 
312 /****************************************************************************/
313 
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) ...
static const SUMOReal UNSPECIFIED_WIDTH
unspecified lane width
Definition: NBEdge.h:201
static bool transformCoordinates(Position &from, bool includeInBoundary=true, GeoConvHelper *from_srs=0)
transforms loaded coordinates handles projections, offsets (using GeoConvHelper) and import of height...
static SUMOReal _2SUMOReal(const E *const data)
Definition: TplConvert.h:223
The representation of a single edge during network building.
Definition: NBEdge.h:71
static const SUMOReal UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:203
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
static StringBijection< int >::Entry itsumoAttrs[]
The names of MATSIM-XML attributes (for passing to GenericSAXHandler)
void myEndElement(int element)
Callback method for a closing tag to implement by derived classes.
A handler which converts occuring elements and attributes into enums.
static StringBijection< int >::Entry itsumoTags[]
The names of MATSIM-XML elements (for passing to GenericSAXHandler)
void setFileName(const std::string &name)
Sets the current file name.
Encapsulated SAX-Attributes.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
static bool exists(std::string path)
Checks whether the given file exists.
Definition: FileHelpers.cpp:57
void myCharacters(int element, const std::string &chars)
Callback method for characters to implement by derived classes.
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:198
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:201
static int _2int(const E *const data)
Definition: TplConvert.h:114
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:56
Instance responsible for building networks.
Definition: NBNetBuilder.h:113
A storage for options typed value containers)
Definition: OptionsCont.h:108
static void loadNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Loads content of the optionally given ITSUMO network files.
Represents a single node (junction) during network building.
Definition: NBNode.h:74
#define SUMOReal
Definition: config.h:215
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:199
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
Handler(NBNetBuilder &toFill)
Contructor.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.