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-2022 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // Importer for networks stored in ITSUMO format
21 /****************************************************************************/
22 #include <config.h>
23 #include <set>
24 #include <functional>
25 #include <sstream>
28 #include <netbuild/NBEdge.h>
29 #include <netbuild/NBEdgeCont.h>
30 #include <netbuild/NBNode.h>
31 #include <netbuild/NBNodeCont.h>
32 #include <netbuild/NBNetBuilder.h>
39 #include <utils/xml/XMLSubSys.h>
40 #include "NILoader.h"
41 #include "NIImporter_ITSUMO.h"
42 
43 
44 
45 // ===========================================================================
46 // static variables
47 // ===========================================================================
69  { "is_preferencial", NIImporter_ITSUMO::ITSUMO_TAG_IS_PREFERENCIAL },
70  { "delimiting_node", NIImporter_ITSUMO::ITSUMO_TAG_DELIMITING_NODE },
74  { "laneset_position", NIImporter_ITSUMO::ITSUMO_TAG_LANESET_POSITION },
77  { "turning_probabilities", NIImporter_ITSUMO::ITSUMO_TAG_TURNING_PROBABILITIES },
79  { "destination_laneset", NIImporter_ITSUMO::ITSUMO_TAG_DESTINATION_LANESET },
86  { "deceleration_prob", NIImporter_ITSUMO::ITSUMO_TAG_DECELERATION_PROB },
88 };
89 
90 
93 };
94 
95 
96 // ===========================================================================
97 // method definitions
98 // ===========================================================================
99 // ---------------------------------------------------------------------------
100 // static methods
101 // ---------------------------------------------------------------------------
102 void
104  // check whether the option is set (properly)
105  if (!oc.isSet("itsumo-files")) {
106  return;
107  }
108  /* Parse file(s)
109  * Each file is parsed twice: first for nodes, second for edges. */
110  std::vector<std::string> files = oc.getStringVector("itsumo-files");
111  // load nodes, first
112  Handler Handler(nb);
113  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
114  // nodes
115  if (!FileHelpers::isReadable(*file)) {
116  WRITE_ERROR("Could not open itsumo-file '" + *file + "'.");
117  return;
118  }
119  Handler.setFileName(*file);
120  PROGRESS_BEGIN_MESSAGE("Parsing nodes from itsumo-file '" + *file + "'");
121  if (!XMLSubSys::runParser(Handler, *file)) {
122  return;
123  }
125  }
126 }
127 
128 
129 // ---------------------------------------------------------------------------
130 // definitions of NIImporter_ITSUMO::Handler-methods
131 // ---------------------------------------------------------------------------
133  : GenericSAXHandler(itsumoTags, ITSUMO_TAG_NOTHING, itsumoAttrs, ITSUMO_ATTR_NOTHING, "itsumo - file"), myNetBuilder(toFill) {
134 }
135 
136 
138 
139 
140 void
142  switch (element) {
143  case ITSUMO_TAG_NODE:
144  myParameter.clear();
145  break;
146  case ITSUMO_TAG_LANESET:
147  myParameter.clear();
148  break;
149  default:
150  break;
151  }
152 }
153 
154 
155 void
156 NIImporter_ITSUMO::Handler::myCharacters(int element, const std::string& chars) {
157  std::string mc = StringUtils::prune(chars);
158  switch (element) {
159  // node parsing
160  case ITSUMO_TAG_NODE_ID:
161  myParameter["id"] = mc;
162  break;
164  myParameter["name"] = mc;
165  break;
166  case ITSUMO_TAG_X_COORD:
167  myParameter["x"] = mc;
168  break;
169  case ITSUMO_TAG_Y_COORD:
170  myParameter["y"] = mc;
171  break;
172  // section parsing
174  myParameter["sectionID"] = mc;
175  break;
176  // laneset parsing
178  myParameter["lanesetID"] = mc;
179  break;
181  myParameter["pos"] = mc;
182  break;
184  myParameter["from"] = mc;
185  break;
186  case ITSUMO_TAG_END_NODE:
187  myParameter["to"] = mc;
188  break;
189  // lane parsing
190  case ITSUMO_TAG_LANE_ID:
191  myParameter["laneID"] = mc;
192  break;
194  myParameter["i"] = mc;
195  break;
197  myParameter["v"] = mc;
198  break;
199  default:
200  break;
201  }
202 }
203 
204 
205 void
207  switch (element) {
208  case ITSUMO_TAG_SIMULATION: {
209  for (std::vector<Section*>::iterator i = mySections.begin(); i != mySections.end(); ++i) {
210  for (std::vector<LaneSet*>::iterator j = (*i)->laneSets.begin(); j != (*i)->laneSets.end(); ++j) {
211  LaneSet* ls = (*j);
212  NBEdge* edge = new NBEdge(ls->id, ls->from, ls->to, "", ls->v, (int)ls->lanes.size(), -1,
214  if (!myNetBuilder.getEdgeCont().insert(edge)) {
215  delete edge;
216  WRITE_ERROR("Could not add edge '" + ls->id + "'. Probably declared twice.");
217  }
218  delete ls;
219  }
220  delete *i;
221  }
222  }
223  break;
224  case ITSUMO_TAG_NODE: {
225  try {
226  std::string id = myParameter["id"];
227  double x = StringUtils::toDouble(myParameter["x"]);
228  double y = StringUtils::toDouble(myParameter["y"]);
229  Position pos(x, y);
231  WRITE_ERROR("Unable to project coordinates for node '" + id + "'.");
232  }
233  NBNode* node = new NBNode(id, pos);
234  if (!myNetBuilder.getNodeCont().insert(node)) {
235  delete node;
236  WRITE_ERROR("Could not add node '" + id + "'. Probably declared twice.");
237  }
238  } catch (NumberFormatException&) {
239  WRITE_ERROR("Not numeric position information for node '" + myParameter["id"] + "'.");
240  } catch (EmptyData&) {
241  WRITE_ERROR("Missing data in node '" + myParameter["id"] + "'.");
242  }
243  }
244  break;
245  case ITSUMO_TAG_SECTION: {
246  mySections.push_back(new Section(myParameter["sectionID"], myCurrentLaneSets));
247  myCurrentLaneSets.clear();
248  }
249  break;
250  case ITSUMO_TAG_LANESET: {
251  try {
252  std::string id = myParameter["lanesetID"];
253  int i = StringUtils::toInt(myParameter["i"]);
254  std::string fromID = myParameter["from"];
255  std::string toID = myParameter["to"];
256  NBNode* from = myNetBuilder.getNodeCont().retrieve(fromID);
257  NBNode* to = myNetBuilder.getNodeCont().retrieve(toID);
258  if (from == nullptr || to == nullptr) {
259  WRITE_ERROR("Missing node in laneset '" + myParameter["lanesetID"] + "'.");
260  } else {
261  if (myLaneSets.find(id) != myLaneSets.end()) {
262  WRITE_ERROR("Fond laneset-id '" + id + "' twice.");
263  } else {
264  double vSum = 0;
265  for (std::vector<Lane>::iterator j = myCurrentLanes.begin(); j != myCurrentLanes.end(); ++j) {
266  vSum += (*j).v;
267  }
268  vSum /= (double) myCurrentLanes.size();
269  LaneSet* ls = new LaneSet(id, myCurrentLanes, vSum, i, from, to);
270  myLaneSets[id] = ls;
271  myCurrentLaneSets.push_back(ls);
272  myCurrentLanes.clear();
273  }
274  }
275  } catch (NumberFormatException&) {
276  WRITE_ERROR("Not numeric value in laneset '" + myParameter["lanesetID"] + "'.");
277  } catch (EmptyData&) {
278  WRITE_ERROR("Missing data in laneset '" + myParameter["lanesetID"] + "'.");
279  }
280  }
281  break;
282  case ITSUMO_TAG_LANE: {
283  try {
284  std::string id = myParameter["laneID"];
285  int i = StringUtils::toInt(myParameter["i"]);
286  double v = StringUtils::toDouble(myParameter["v"]);
287  myCurrentLanes.push_back(Lane(id, (int) i, v));
288  } catch (NumberFormatException&) {
289  WRITE_ERROR("Not numeric value in lane '" + myParameter["laneID"] + "'.");
290  } catch (EmptyData&) {
291  WRITE_ERROR("Missing data in lane '" + myParameter["laneID"] + "'.");
292  }
293  }
294  break;
295  default:
296  break;
297  }
298 }
299 
300 
301 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:288
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:284
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:283
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:49
A handler which converts occuring elements and attributes into enums.
void setFileName(const std::string &name)
Sets the current file name.
The representation of a single edge during network building.
Definition: NBEdge.h:91
static const double UNSPECIFIED_WIDTH
unspecified lane width
Definition: NBEdge.h:349
static const double UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:352
Instance responsible for building networks.
Definition: NBNetBuilder.h:107
static bool transformCoordinate(Position &from, bool includeInBoundary=true, GeoConvHelper *from_srs=0)
transforms loaded coordinates handles projections, offsets (using GeoConvHelper) and import of height...
Represents a single node (junction) during network building.
Definition: NBNode.h:66
void myCharacters(int element, const std::string &chars)
Callback method for characters to implement by derived classes.
Handler(NBNetBuilder &toFill)
Contructor.
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
void myEndElement(int element)
Callback method for a closing tag to implement by derived classes.
static StringBijection< int >::Entry itsumoAttrs[]
The names of MATSIM-XML attributes (for passing to GenericSAXHandler)
static void loadNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Loads content of the optionally given ITSUMO network files.
static StringBijection< int >::Entry itsumoTags[]
The names of MATSIM-XML elements (for passing to GenericSAXHandler)
A storage for options typed value containers)
Definition: OptionsCont.h:89
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
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
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:48
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false, const bool isRoute=false)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:149