SUMO - Simulation of Urban MObility
NIImporter_ITSUMO.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2011-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Importer for networks stored in ITSUMO format
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 
52 
53 // ===========================================================================
54 // static variables
55 // ===========================================================================
77  { "is_preferencial", NIImporter_ITSUMO::ITSUMO_TAG_IS_PREFERENCIAL },
78  { "delimiting_node", NIImporter_ITSUMO::ITSUMO_TAG_DELIMITING_NODE },
82  { "laneset_position", NIImporter_ITSUMO::ITSUMO_TAG_LANESET_POSITION },
85  { "turning_probabilities", NIImporter_ITSUMO::ITSUMO_TAG_TURNING_PROBABILITIES },
87  { "destination_laneset", NIImporter_ITSUMO::ITSUMO_TAG_DESTINATION_LANESET },
94  { "deceleration_prob", NIImporter_ITSUMO::ITSUMO_TAG_DECELERATION_PROB },
96 };
97 
98 
101 };
102 
103 
104 // ===========================================================================
105 // method definitions
106 // ===========================================================================
107 // ---------------------------------------------------------------------------
108 // static methods
109 // ---------------------------------------------------------------------------
110 void
112  // check whether the option is set (properly)
113  if (!oc.isSet("itsumo-files")) {
114  return;
115  }
116  /* Parse file(s)
117  * Each file is parsed twice: first for nodes, second for edges. */
118  std::vector<std::string> files = oc.getStringVector("itsumo-files");
119  // load nodes, first
120  Handler Handler(nb);
121  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
122  // nodes
123  if (!FileHelpers::isReadable(*file)) {
124  WRITE_ERROR("Could not open itsumo-file '" + *file + "'.");
125  return;
126  }
127  Handler.setFileName(*file);
128  PROGRESS_BEGIN_MESSAGE("Parsing nodes from itsumo-file '" + *file + "'");
129  if (!XMLSubSys::runParser(Handler, *file)) {
130  return;
131  }
133  }
134 }
135 
136 
137 // ---------------------------------------------------------------------------
138 // definitions of NIImporter_ITSUMO::Handler-methods
139 // ---------------------------------------------------------------------------
141  : GenericSAXHandler(itsumoTags, ITSUMO_TAG_NOTHING, itsumoAttrs, ITSUMO_ATTR_NOTHING, "itsumo - file"), myNetBuilder(toFill) {
142 }
143 
144 
146 
147 
148 void
150  switch (element) {
151  case ITSUMO_TAG_NODE:
152  myParameter.clear();
153  break;
154  case ITSUMO_TAG_LANESET:
155  myParameter.clear();
156  break;
157  default:
158  break;
159  }
160 }
161 
162 
163 void
164 NIImporter_ITSUMO::Handler::myCharacters(int element, const std::string& chars) {
165  std::string mc = StringUtils::prune(chars);
166  switch (element) {
167  // node parsing
168  case ITSUMO_TAG_NODE_ID:
169  myParameter["id"] = mc;
170  break;
172  myParameter["name"] = mc;
173  break;
174  case ITSUMO_TAG_X_COORD:
175  myParameter["x"] = mc;
176  break;
177  case ITSUMO_TAG_Y_COORD:
178  myParameter["y"] = mc;
179  break;
180  // section parsing
182  myParameter["sectionID"] = mc;
183  break;
184  // laneset parsing
186  myParameter["lanesetID"] = mc;
187  break;
189  myParameter["pos"] = mc;
190  break;
192  myParameter["from"] = mc;
193  break;
194  case ITSUMO_TAG_END_NODE:
195  myParameter["to"] = mc;
196  break;
197  // lane parsing
198  case ITSUMO_TAG_LANE_ID:
199  myParameter["laneID"] = mc;
200  break;
202  myParameter["i"] = mc;
203  break;
205  myParameter["v"] = mc;
206  break;
207  default:
208  break;
209  }
210 }
211 
212 
213 void
215  switch (element) {
216  case ITSUMO_TAG_SIMULATION: {
217  for (std::vector<Section*>::iterator i = mySections.begin(); i != mySections.end(); ++i) {
218  for (std::vector<LaneSet*>::iterator j = (*i)->laneSets.begin(); j != (*i)->laneSets.end(); ++j) {
219  LaneSet* ls = (*j);
220  NBEdge* edge = new NBEdge(ls->id, ls->from, ls->to, "", ls->v, (int)ls->lanes.size(), -1, NBEdge::UNSPECIFIED_WIDTH, NBEdge::UNSPECIFIED_OFFSET);
221  if (!myNetBuilder.getEdgeCont().insert(edge)) {
222  delete edge;
223  WRITE_ERROR("Could not add edge '" + ls->id + "'. Probably declared twice.");
224  }
225  delete ls;
226  }
227  delete *i;
228  }
229  }
230  break;
231  case ITSUMO_TAG_NODE: {
232  try {
233  std::string id = myParameter["id"];
234  double x = TplConvert::_2double(myParameter["x"].c_str());
235  double y = TplConvert::_2double(myParameter["y"].c_str());
236  Position pos(x, y);
238  WRITE_ERROR("Unable to project coordinates for node '" + id + "'.");
239  }
240  NBNode* node = new NBNode(id, pos);
241  if (!myNetBuilder.getNodeCont().insert(node)) {
242  delete node;
243  WRITE_ERROR("Could not add node '" + id + "'. Probably declared twice.");
244  }
245  } catch (NumberFormatException&) {
246  WRITE_ERROR("Not numeric position information for node '" + myParameter["id"] + "'.");
247  } catch (EmptyData&) {
248  WRITE_ERROR("Missing data in node '" + myParameter["id"] + "'.");
249  }
250  }
251  break;
252  case ITSUMO_TAG_SECTION: {
253  mySections.push_back(new Section(myParameter["sectionID"], myCurrentLaneSets));
254  myCurrentLaneSets.clear();
255  }
256  break;
257  case ITSUMO_TAG_LANESET: {
258  try {
259  std::string id = myParameter["lanesetID"];
260  int i = TplConvert::_2int(myParameter["i"].c_str());
261  std::string fromID = myParameter["from"];
262  std::string toID = myParameter["to"];
263  NBNode* from = myNetBuilder.getNodeCont().retrieve(fromID);
264  NBNode* to = myNetBuilder.getNodeCont().retrieve(toID);
265  if (from == 0 || to == 0) {
266  WRITE_ERROR("Missing node in laneset '" + myParameter["lanesetID"] + "'.");
267  } else {
268  if (myLaneSets.find(id) != myLaneSets.end()) {
269  WRITE_ERROR("Fond laneset-id '" + id + "' twice.");
270  } else {
271  double vSum = 0;
272  for (std::vector<Lane>::iterator j = myCurrentLanes.begin(); j != myCurrentLanes.end(); ++j) {
273  vSum += (*j).v;
274  }
275  vSum /= (double) myCurrentLanes.size();
276  LaneSet* ls = new LaneSet(id, myCurrentLanes, vSum, i, from, to);
277  myLaneSets[id] = ls;
278  myCurrentLaneSets.push_back(ls);
279  myCurrentLanes.clear();
280  }
281  }
282  } catch (NumberFormatException&) {
283  WRITE_ERROR("Not numeric value in laneset '" + myParameter["lanesetID"] + "'.");
284  } catch (EmptyData&) {
285  WRITE_ERROR("Missing data in laneset '" + myParameter["lanesetID"] + "'.");
286  }
287  }
288  break;
289  case ITSUMO_TAG_LANE: {
290  try {
291  std::string id = myParameter["laneID"];
292  int i = TplConvert::_2int(myParameter["i"].c_str());
293  double v = TplConvert::_2double(myParameter["v"].c_str());
294  myCurrentLanes.push_back(Lane(id, (int) i, v));
295  } catch (NumberFormatException&) {
296  WRITE_ERROR("Not numeric value in lane '" + myParameter["laneID"] + "'.");
297  } catch (EmptyData&) {
298  WRITE_ERROR("Missing data in lane '" + myParameter["laneID"] + "'.");
299  }
300  }
301  break;
302  default:
303  break;
304  }
305 }
306 
307 
308 /****************************************************************************/
309 
NBNode * retrieve(const std::string &id) const
Returns the node with the given name.
Definition: NBNodeCont.cpp:108
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:53
std::vector< Section * > mySections
std::vector< Lane > myCurrentLanes
The representation of a single edge during network building.
Definition: NBEdge.h:70
static const double UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:257
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:109
static const double UNSPECIFIED_WIDTH
unspecified lane width
Definition: NBEdge.h:254
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:157
Encapsulated SAX-Attributes.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:156
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:201
std::map< std::string, std::string > myParameter
A temporary parameter map.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
Definition: TplConvert.h:155
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:51
NBNodeCont & getNodeCont()
Returns a reference to the node container.
Definition: NBNetBuilder.h:161
Instance responsible for building networks.
Definition: NBNetBuilder.h:115
A storage for options typed value containers)
Definition: OptionsCont.h:98
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:311
bool insert(const std::string &id, const Position &position, NBDistrict *district=0)
Inserts a node into the map.
Definition: NBNodeCont.cpp:79
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
NBNetBuilder & myNetBuilder
The container to fill.
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:202
std::map< std::string, LaneSet * > myLaneSets
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
Handler(NBNetBuilder &toFill)
Contructor.