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