Eclipse 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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Importer for networks stored in ITSUMO format
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 #include <set>
26 #include <functional>
27 #include <sstream>
30 #include <netbuild/NBEdge.h>
31 #include <netbuild/NBEdgeCont.h>
32 #include <netbuild/NBNode.h>
33 #include <netbuild/NBNodeCont.h>
34 #include <netbuild/NBNetBuilder.h>
41 #include <utils/xml/XMLSubSys.h>
42 #include "NILoader.h"
43 #include "NIImporter_ITSUMO.h"
44 
45 
46 
47 // ===========================================================================
48 // static variables
49 // ===========================================================================
71  { "is_preferencial", NIImporter_ITSUMO::ITSUMO_TAG_IS_PREFERENCIAL },
72  { "delimiting_node", NIImporter_ITSUMO::ITSUMO_TAG_DELIMITING_NODE },
76  { "laneset_position", NIImporter_ITSUMO::ITSUMO_TAG_LANESET_POSITION },
79  { "turning_probabilities", NIImporter_ITSUMO::ITSUMO_TAG_TURNING_PROBABILITIES },
81  { "destination_laneset", NIImporter_ITSUMO::ITSUMO_TAG_DESTINATION_LANESET },
88  { "deceleration_prob", NIImporter_ITSUMO::ITSUMO_TAG_DECELERATION_PROB },
90 };
91 
92 
95 };
96 
97 
98 // ===========================================================================
99 // method definitions
100 // ===========================================================================
101 // ---------------------------------------------------------------------------
102 // static methods
103 // ---------------------------------------------------------------------------
104 void
106  // check whether the option is set (properly)
107  if (!oc.isSet("itsumo-files")) {
108  return;
109  }
110  /* Parse file(s)
111  * Each file is parsed twice: first for nodes, second for edges. */
112  std::vector<std::string> files = oc.getStringVector("itsumo-files");
113  // load nodes, first
114  Handler Handler(nb);
115  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
116  // nodes
117  if (!FileHelpers::isReadable(*file)) {
118  WRITE_ERROR("Could not open itsumo-file '" + *file + "'.");
119  return;
120  }
121  Handler.setFileName(*file);
122  PROGRESS_BEGIN_MESSAGE("Parsing nodes from itsumo-file '" + *file + "'");
123  if (!XMLSubSys::runParser(Handler, *file)) {
124  return;
125  }
127  }
128 }
129 
130 
131 // ---------------------------------------------------------------------------
132 // definitions of NIImporter_ITSUMO::Handler-methods
133 // ---------------------------------------------------------------------------
135  : GenericSAXHandler(itsumoTags, ITSUMO_TAG_NOTHING, itsumoAttrs, ITSUMO_ATTR_NOTHING, "itsumo - file"), myNetBuilder(toFill) {
136 }
137 
138 
140 
141 
142 void
144  switch (element) {
145  case ITSUMO_TAG_NODE:
146  myParameter.clear();
147  break;
148  case ITSUMO_TAG_LANESET:
149  myParameter.clear();
150  break;
151  default:
152  break;
153  }
154 }
155 
156 
157 void
158 NIImporter_ITSUMO::Handler::myCharacters(int element, const std::string& chars) {
159  std::string mc = StringUtils::prune(chars);
160  switch (element) {
161  // node parsing
162  case ITSUMO_TAG_NODE_ID:
163  myParameter["id"] = mc;
164  break;
166  myParameter["name"] = mc;
167  break;
168  case ITSUMO_TAG_X_COORD:
169  myParameter["x"] = mc;
170  break;
171  case ITSUMO_TAG_Y_COORD:
172  myParameter["y"] = mc;
173  break;
174  // section parsing
176  myParameter["sectionID"] = mc;
177  break;
178  // laneset parsing
180  myParameter["lanesetID"] = mc;
181  break;
183  myParameter["pos"] = mc;
184  break;
186  myParameter["from"] = mc;
187  break;
188  case ITSUMO_TAG_END_NODE:
189  myParameter["to"] = mc;
190  break;
191  // lane parsing
192  case ITSUMO_TAG_LANE_ID:
193  myParameter["laneID"] = mc;
194  break;
196  myParameter["i"] = mc;
197  break;
199  myParameter["v"] = mc;
200  break;
201  default:
202  break;
203  }
204 }
205 
206 
207 void
209  switch (element) {
210  case ITSUMO_TAG_SIMULATION: {
211  for (std::vector<Section*>::iterator i = mySections.begin(); i != mySections.end(); ++i) {
212  for (std::vector<LaneSet*>::iterator j = (*i)->laneSets.begin(); j != (*i)->laneSets.end(); ++j) {
213  LaneSet* ls = (*j);
214  NBEdge* edge = new NBEdge(ls->id, ls->from, ls->to, "", ls->v, (int)ls->lanes.size(), -1, NBEdge::UNSPECIFIED_WIDTH, NBEdge::UNSPECIFIED_OFFSET);
215  if (!myNetBuilder.getEdgeCont().insert(edge)) {
216  delete edge;
217  WRITE_ERROR("Could not add edge '" + ls->id + "'. Probably declared twice.");
218  }
219  delete ls;
220  }
221  delete *i;
222  }
223  }
224  break;
225  case ITSUMO_TAG_NODE: {
226  try {
227  std::string id = myParameter["id"];
228  double x = StringUtils::toDouble(myParameter["x"]);
229  double y = StringUtils::toDouble(myParameter["y"]);
230  Position pos(x, y);
232  WRITE_ERROR("Unable to project coordinates for node '" + id + "'.");
233  }
234  NBNode* node = new NBNode(id, pos);
235  if (!myNetBuilder.getNodeCont().insert(node)) {
236  delete node;
237  WRITE_ERROR("Could not add node '" + id + "'. Probably declared twice.");
238  }
239  } catch (NumberFormatException&) {
240  WRITE_ERROR("Not numeric position information for node '" + myParameter["id"] + "'.");
241  } catch (EmptyData&) {
242  WRITE_ERROR("Missing data in node '" + myParameter["id"] + "'.");
243  }
244  }
245  break;
246  case ITSUMO_TAG_SECTION: {
247  mySections.push_back(new Section(myParameter["sectionID"], myCurrentLaneSets));
248  myCurrentLaneSets.clear();
249  }
250  break;
251  case ITSUMO_TAG_LANESET: {
252  try {
253  std::string id = myParameter["lanesetID"];
254  int i = StringUtils::toInt(myParameter["i"]);
255  std::string fromID = myParameter["from"];
256  std::string toID = myParameter["to"];
257  NBNode* from = myNetBuilder.getNodeCont().retrieve(fromID);
258  NBNode* to = myNetBuilder.getNodeCont().retrieve(toID);
259  if (from == nullptr || to == nullptr) {
260  WRITE_ERROR("Missing node in laneset '" + myParameter["lanesetID"] + "'.");
261  } else {
262  if (myLaneSets.find(id) != myLaneSets.end()) {
263  WRITE_ERROR("Fond laneset-id '" + id + "' twice.");
264  } else {
265  double vSum = 0;
266  for (std::vector<Lane>::iterator j = myCurrentLanes.begin(); j != myCurrentLanes.end(); ++j) {
267  vSum += (*j).v;
268  }
269  vSum /= (double) myCurrentLanes.size();
270  LaneSet* ls = new LaneSet(id, myCurrentLanes, vSum, i, from, to);
271  myLaneSets[id] = ls;
272  myCurrentLaneSets.push_back(ls);
273  myCurrentLanes.clear();
274  }
275  }
276  } catch (NumberFormatException&) {
277  WRITE_ERROR("Not numeric value in laneset '" + myParameter["lanesetID"] + "'.");
278  } catch (EmptyData&) {
279  WRITE_ERROR("Missing data in laneset '" + myParameter["lanesetID"] + "'.");
280  }
281  }
282  break;
283  case ITSUMO_TAG_LANE: {
284  try {
285  std::string id = myParameter["laneID"];
286  int i = StringUtils::toInt(myParameter["i"]);
287  double v = StringUtils::toDouble(myParameter["v"]);
288  myCurrentLanes.push_back(Lane(id, (int) i, v));
289  } catch (NumberFormatException&) {
290  WRITE_ERROR("Not numeric value in lane '" + myParameter["laneID"] + "'.");
291  } catch (EmptyData&) {
292  WRITE_ERROR("Missing data in lane '" + myParameter["laneID"] + "'.");
293  }
294  }
295  break;
296  default:
297  break;
298  }
299 }
300 
301 
302 /****************************************************************************/
303 
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:49
std::vector< Section * > mySections
std::vector< Lane > myCurrentLanes
The representation of a single edge during network building.
Definition: NBEdge.h:86
static const double UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:306
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:113
static const double UNSPECIFIED_WIDTH
unspecified lane width
Definition: NBEdge.h:303
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:152
Encapsulated SAX-Attributes.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:151
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:241
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter...
std::map< std::string, std::string > myParameter
A temporary parameter map.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:47
NBNodeCont & getNodeCont()
Returns a reference to the node container.
Definition: NBNetBuilder.h:156
Instance responsible for building networks.
Definition: NBNetBuilder.h:110
A storage for options typed value containers)
Definition: OptionsCont.h:90
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:68
NBNetBuilder & myNetBuilder
The container to fill.
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:242
std::map< std::string, LaneSet * > myLaneSets
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
Handler(NBNetBuilder &toFill)
Contructor.