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.dev/sumo
3 // Copyright (C) 2011-2024 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  handler.needsCharacterData();
114  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
115  // nodes
116  if (!FileHelpers::isReadable(*file)) {
117  WRITE_ERRORF(TL("Could not open itsumo-file '%'."), *file);
118  return;
119  }
120  handler.setFileName(*file);
121  PROGRESS_BEGIN_MESSAGE("Parsing nodes from itsumo-file '" + *file + "'");
122  if (!XMLSubSys::runParser(handler, *file)) {
123  return;
124  }
126  }
127 }
128 
129 
130 // ---------------------------------------------------------------------------
131 // definitions of NIImporter_ITSUMO::Handler-methods
132 // ---------------------------------------------------------------------------
134  : GenericSAXHandler(itsumoTags, ITSUMO_TAG_NOTHING, itsumoAttrs, ITSUMO_ATTR_NOTHING, "itsumo - file"), myNetBuilder(toFill) {
135 }
136 
137 
139 
140 
141 void
143  switch (element) {
144  case ITSUMO_TAG_NODE:
145  myParameter.clear();
146  break;
147  case ITSUMO_TAG_LANESET:
148  myParameter.clear();
149  break;
150  default:
151  break;
152  }
153 }
154 
155 
156 void
157 NIImporter_ITSUMO::Handler::myCharacters(int element, const std::string& chars) {
158  std::string mc = StringUtils::prune(chars);
159  switch (element) {
160  // node parsing
161  case ITSUMO_TAG_NODE_ID:
162  myParameter["id"] = mc;
163  break;
165  myParameter["name"] = mc;
166  break;
167  case ITSUMO_TAG_X_COORD:
168  myParameter["x"] = mc;
169  break;
170  case ITSUMO_TAG_Y_COORD:
171  myParameter["y"] = mc;
172  break;
173  // section parsing
175  myParameter["sectionID"] = mc;
176  break;
177  // laneset parsing
179  myParameter["lanesetID"] = mc;
180  break;
182  myParameter["pos"] = mc;
183  break;
185  myParameter["from"] = mc;
186  break;
187  case ITSUMO_TAG_END_NODE:
188  myParameter["to"] = mc;
189  break;
190  // lane parsing
191  case ITSUMO_TAG_LANE_ID:
192  myParameter["laneID"] = mc;
193  break;
195  myParameter["i"] = mc;
196  break;
198  myParameter["v"] = mc;
199  break;
200  default:
201  break;
202  }
203 }
204 
205 
206 void
208  switch (element) {
209  case ITSUMO_TAG_SIMULATION: {
210  for (std::vector<Section*>::iterator i = mySections.begin(); i != mySections.end(); ++i) {
211  for (std::vector<LaneSet*>::iterator j = (*i)->laneSets.begin(); j != (*i)->laneSets.end(); ++j) {
212  LaneSet* ls = (*j);
213  NBEdge* edge = new NBEdge(ls->id, ls->from, ls->to, "", ls->v, NBEdge::UNSPECIFIED_FRICTION, (int)ls->lanes.size(), -1,
215  if (!myNetBuilder.getEdgeCont().insert(edge)) {
216  delete edge;
217  WRITE_ERRORF(TL("Could not add edge '%'. Probably declared twice."), ls->id);
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_ERRORF(TL("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_ERRORF(TL("Could not add node '%'. Probably declared twice."), id);
238  }
239  } catch (NumberFormatException&) {
240  WRITE_ERRORF(TL("Not numeric position information for node '%'."), myParameter["id"]);
241  } catch (EmptyData&) {
242  WRITE_ERRORF(TL("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_ERRORF(TL("Missing node in laneset '%'."), myParameter["lanesetID"]);
261  } else {
262  if (myLaneSets.find(id) != myLaneSets.end()) {
263  WRITE_ERRORF(TL("Fond laneset-id '%' twice."), id);
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_ERRORF(TL("Not numeric value in laneset '%'."), myParameter["lanesetID"]);
278  } catch (EmptyData&) {
279  WRITE_ERRORF(TL("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_ERRORF(TL("Not numeric value in lane '%'."), myParameter["laneID"]);
291  } catch (EmptyData&) {
292  WRITE_ERRORF(TL("Missing data in lane '%'."), myParameter["laneID"]);
293  }
294  }
295  break;
296  default:
297  break;
298  }
299 }
300 
301 
302 /****************************************************************************/
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:305
#define TL(string)
Definition: MsgHandler.h:315
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:300
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:299
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:51
A handler which converts occurring elements and attributes into enums.
void needsCharacterData(const bool value=true)
void setFileName(const std::string &name)
Sets the current file name.
The representation of a single edge during network building.
Definition: NBEdge.h:92
static const double UNSPECIFIED_FRICTION
unspecified lane friction
Definition: NBEdge.h:351
static const double UNSPECIFIED_WIDTH
unspecified lane width
Definition: NBEdge.h:342
static const double UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:345
Instance responsible for building networks.
Definition: NBNetBuilder.h:107
static bool transformCoordinate(Position &from, bool includeInBoundary=true, GeoConvHelper *from_srs=nullptr)
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:56
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, const bool isExternal=false, const bool catchExceptions=true)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:148