SUMO - Simulation of Urban MObility
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-2017 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 
53 
54 // ===========================================================================
55 // static variables
56 // ===========================================================================
78  { "is_preferencial", NIImporter_ITSUMO::ITSUMO_TAG_IS_PREFERENCIAL },
79  { "delimiting_node", NIImporter_ITSUMO::ITSUMO_TAG_DELIMITING_NODE },
83  { "laneset_position", NIImporter_ITSUMO::ITSUMO_TAG_LANESET_POSITION },
86  { "turning_probabilities", NIImporter_ITSUMO::ITSUMO_TAG_TURNING_PROBABILITIES },
88  { "destination_laneset", NIImporter_ITSUMO::ITSUMO_TAG_DESTINATION_LANESET },
95  { "deceleration_prob", NIImporter_ITSUMO::ITSUMO_TAG_DECELERATION_PROB },
97 };
98 
99 
102 };
103 
104 
105 // ===========================================================================
106 // method definitions
107 // ===========================================================================
108 // ---------------------------------------------------------------------------
109 // static methods
110 // ---------------------------------------------------------------------------
111 void
113  // check whether the option is set (properly)
114  if (!oc.isSet("itsumo-files")) {
115  return;
116  }
117  /* Parse file(s)
118  * Each file is parsed twice: first for nodes, second for edges. */
119  std::vector<std::string> files = oc.getStringVector("itsumo-files");
120  // load nodes, first
121  Handler Handler(nb);
122  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
123  // nodes
124  if (!FileHelpers::isReadable(*file)) {
125  WRITE_ERROR("Could not open itsumo-file '" + *file + "'.");
126  return;
127  }
128  Handler.setFileName(*file);
129  PROGRESS_BEGIN_MESSAGE("Parsing nodes from itsumo-file '" + *file + "'");
130  if (!XMLSubSys::runParser(Handler, *file)) {
131  return;
132  }
134  }
135 }
136 
137 
138 // ---------------------------------------------------------------------------
139 // definitions of NIImporter_ITSUMO::Handler-methods
140 // ---------------------------------------------------------------------------
142  : GenericSAXHandler(itsumoTags, ITSUMO_TAG_NOTHING, itsumoAttrs, ITSUMO_ATTR_NOTHING, "itsumo - file"), myNetBuilder(toFill) {
143 }
144 
145 
147 
148 
149 void
151  switch (element) {
152  case ITSUMO_TAG_NODE:
153  myParameter.clear();
154  break;
155  case ITSUMO_TAG_LANESET:
156  myParameter.clear();
157  break;
158  default:
159  break;
160  }
161 }
162 
163 
164 void
165 NIImporter_ITSUMO::Handler::myCharacters(int element, const std::string& chars) {
166  std::string mc = StringUtils::prune(chars);
167  switch (element) {
168  // node parsing
169  case ITSUMO_TAG_NODE_ID:
170  myParameter["id"] = mc;
171  break;
173  myParameter["name"] = mc;
174  break;
175  case ITSUMO_TAG_X_COORD:
176  myParameter["x"] = mc;
177  break;
178  case ITSUMO_TAG_Y_COORD:
179  myParameter["y"] = mc;
180  break;
181  // section parsing
183  myParameter["sectionID"] = mc;
184  break;
185  // laneset parsing
187  myParameter["lanesetID"] = mc;
188  break;
190  myParameter["pos"] = mc;
191  break;
193  myParameter["from"] = mc;
194  break;
195  case ITSUMO_TAG_END_NODE:
196  myParameter["to"] = mc;
197  break;
198  // lane parsing
199  case ITSUMO_TAG_LANE_ID:
200  myParameter["laneID"] = mc;
201  break;
203  myParameter["i"] = mc;
204  break;
206  myParameter["v"] = mc;
207  break;
208  default:
209  break;
210  }
211 }
212 
213 
214 void
216  switch (element) {
217  case ITSUMO_TAG_SIMULATION: {
218  for (std::vector<Section*>::iterator i = mySections.begin(); i != mySections.end(); ++i) {
219  for (std::vector<LaneSet*>::iterator j = (*i)->laneSets.begin(); j != (*i)->laneSets.end(); ++j) {
220  LaneSet* ls = (*j);
221  NBEdge* edge = new NBEdge(ls->id, ls->from, ls->to, "", ls->v, (int)ls->lanes.size(), -1, NBEdge::UNSPECIFIED_WIDTH, NBEdge::UNSPECIFIED_OFFSET);
222  if (!myNetBuilder.getEdgeCont().insert(edge)) {
223  delete edge;
224  WRITE_ERROR("Could not add edge '" + ls->id + "'. Probably declared twice.");
225  }
226  delete ls;
227  }
228  delete *i;
229  }
230  }
231  break;
232  case ITSUMO_TAG_NODE: {
233  try {
234  std::string id = myParameter["id"];
235  double x = TplConvert::_2double(myParameter["x"].c_str());
236  double y = TplConvert::_2double(myParameter["y"].c_str());
237  Position pos(x, y);
239  WRITE_ERROR("Unable to project coordinates for node '" + id + "'.");
240  }
241  NBNode* node = new NBNode(id, pos);
242  if (!myNetBuilder.getNodeCont().insert(node)) {
243  delete node;
244  WRITE_ERROR("Could not add node '" + id + "'. Probably declared twice.");
245  }
246  } catch (NumberFormatException&) {
247  WRITE_ERROR("Not numeric position information for node '" + myParameter["id"] + "'.");
248  } catch (EmptyData&) {
249  WRITE_ERROR("Missing data in node '" + myParameter["id"] + "'.");
250  }
251  }
252  break;
253  case ITSUMO_TAG_SECTION: {
254  mySections.push_back(new Section(myParameter["sectionID"], myCurrentLaneSets));
255  myCurrentLaneSets.clear();
256  }
257  break;
258  case ITSUMO_TAG_LANESET: {
259  try {
260  std::string id = myParameter["lanesetID"];
261  int i = TplConvert::_2int(myParameter["i"].c_str());
262  std::string fromID = myParameter["from"];
263  std::string toID = myParameter["to"];
264  NBNode* from = myNetBuilder.getNodeCont().retrieve(fromID);
265  NBNode* to = myNetBuilder.getNodeCont().retrieve(toID);
266  if (from == 0 || to == 0) {
267  WRITE_ERROR("Missing node in laneset '" + myParameter["lanesetID"] + "'.");
268  } else {
269  if (myLaneSets.find(id) != myLaneSets.end()) {
270  WRITE_ERROR("Fond laneset-id '" + id + "' twice.");
271  } else {
272  double vSum = 0;
273  for (std::vector<Lane>::iterator j = myCurrentLanes.begin(); j != myCurrentLanes.end(); ++j) {
274  vSum += (*j).v;
275  }
276  vSum /= (double) myCurrentLanes.size();
277  LaneSet* ls = new LaneSet(id, myCurrentLanes, vSum, i, from, to);
278  myLaneSets[id] = ls;
279  myCurrentLaneSets.push_back(ls);
280  myCurrentLanes.clear();
281  }
282  }
283  } catch (NumberFormatException&) {
284  WRITE_ERROR("Not numeric value in laneset '" + myParameter["lanesetID"] + "'.");
285  } catch (EmptyData&) {
286  WRITE_ERROR("Missing data in laneset '" + myParameter["lanesetID"] + "'.");
287  }
288  }
289  break;
290  case ITSUMO_TAG_LANE: {
291  try {
292  std::string id = myParameter["laneID"];
293  int i = TplConvert::_2int(myParameter["i"].c_str());
294  double v = TplConvert::_2double(myParameter["v"].c_str());
295  myCurrentLanes.push_back(Lane(id, (int) i, v));
296  } catch (NumberFormatException&) {
297  WRITE_ERROR("Not numeric value in lane '" + myParameter["laneID"] + "'.");
298  } catch (EmptyData&) {
299  WRITE_ERROR("Missing data in lane '" + myParameter["laneID"] + "'.");
300  }
301  }
302  break;
303  default:
304  break;
305  }
306 }
307 
308 
309 /****************************************************************************/
310 
NBNode * retrieve(const std::string &id) const
Returns the node with the given name.
Definition: NBNodeCont.cpp:106
static bool transformCoordinate(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:54
std::vector< Section * > mySections
std::vector< Lane > myCurrentLanes
The representation of a single edge during network building.
Definition: NBEdge.h:71
static const double UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:258
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything&#39;s ok.
Definition: XMLSubSys.cpp:110
static const double UNSPECIFIED_WIDTH
unspecified lane width
Definition: NBEdge.h:255
std::vector< LaneSet * > myCurrentLaneSets
static StringBijection< int >::Entry itsumoAttrs[]
The names of MATSIM-XML attributes (for passing to GenericSAXHandler)
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
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.
bool insert(NBEdge *edge, bool ignorePrunning=false)
Adds an edge to the dictionary.
Definition: NBEdgeCont.cpp:158
Encapsulated SAX-Attributes.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:155
void myCharacters(int element, const std::string &chars)
Callback method for characters to implement by derived classes.
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) ...
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
std::map< std::string, std::string > myParameter
A temporary parameter map.
#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:52
NBNodeCont & getNodeCont()
Returns a reference to the node container.
Definition: NBNetBuilder.h:160
Instance responsible for building networks.
Definition: NBNetBuilder.h:114
A storage for options typed value containers)
Definition: OptionsCont.h:99
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:297
bool insert(const std::string &id, const Position &position, NBDistrict *district=0)
Inserts a node into the map.
Definition: NBNodeCont.cpp:77
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:75
NBNetBuilder & myNetBuilder
The container to fill.
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
std::map< std::string, LaneSet * > myLaneSets
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
Handler(NBNetBuilder &toFill)
Contructor.