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-2020 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, NBEdge::UNSPECIFIED_WIDTH, NBEdge::UNSPECIFIED_OFFSET);
213  if (!myNetBuilder.getEdgeCont().insert(edge)) {
214  delete edge;
215  WRITE_ERROR("Could not add edge '" + ls->id + "'. Probably declared twice.");
216  }
217  delete ls;
218  }
219  delete *i;
220  }
221  }
222  break;
223  case ITSUMO_TAG_NODE: {
224  try {
225  std::string id = myParameter["id"];
226  double x = StringUtils::toDouble(myParameter["x"]);
227  double y = StringUtils::toDouble(myParameter["y"]);
228  Position pos(x, y);
230  WRITE_ERROR("Unable to project coordinates for node '" + id + "'.");
231  }
232  NBNode* node = new NBNode(id, pos);
233  if (!myNetBuilder.getNodeCont().insert(node)) {
234  delete node;
235  WRITE_ERROR("Could not add node '" + id + "'. Probably declared twice.");
236  }
237  } catch (NumberFormatException&) {
238  WRITE_ERROR("Not numeric position information for node '" + myParameter["id"] + "'.");
239  } catch (EmptyData&) {
240  WRITE_ERROR("Missing data in node '" + myParameter["id"] + "'.");
241  }
242  }
243  break;
244  case ITSUMO_TAG_SECTION: {
245  mySections.push_back(new Section(myParameter["sectionID"], myCurrentLaneSets));
246  myCurrentLaneSets.clear();
247  }
248  break;
249  case ITSUMO_TAG_LANESET: {
250  try {
251  std::string id = myParameter["lanesetID"];
252  int i = StringUtils::toInt(myParameter["i"]);
253  std::string fromID = myParameter["from"];
254  std::string toID = myParameter["to"];
255  NBNode* from = myNetBuilder.getNodeCont().retrieve(fromID);
256  NBNode* to = myNetBuilder.getNodeCont().retrieve(toID);
257  if (from == nullptr || to == nullptr) {
258  WRITE_ERROR("Missing node in laneset '" + myParameter["lanesetID"] + "'.");
259  } else {
260  if (myLaneSets.find(id) != myLaneSets.end()) {
261  WRITE_ERROR("Fond laneset-id '" + id + "' twice.");
262  } else {
263  double vSum = 0;
264  for (std::vector<Lane>::iterator j = myCurrentLanes.begin(); j != myCurrentLanes.end(); ++j) {
265  vSum += (*j).v;
266  }
267  vSum /= (double) myCurrentLanes.size();
268  LaneSet* ls = new LaneSet(id, myCurrentLanes, vSum, i, from, to);
269  myLaneSets[id] = ls;
270  myCurrentLaneSets.push_back(ls);
271  myCurrentLanes.clear();
272  }
273  }
274  } catch (NumberFormatException&) {
275  WRITE_ERROR("Not numeric value in laneset '" + myParameter["lanesetID"] + "'.");
276  } catch (EmptyData&) {
277  WRITE_ERROR("Missing data in laneset '" + myParameter["lanesetID"] + "'.");
278  }
279  }
280  break;
281  case ITSUMO_TAG_LANE: {
282  try {
283  std::string id = myParameter["laneID"];
284  int i = StringUtils::toInt(myParameter["i"]);
285  double v = StringUtils::toDouble(myParameter["v"]);
286  myCurrentLanes.push_back(Lane(id, (int) i, v));
287  } catch (NumberFormatException&) {
288  WRITE_ERROR("Not numeric value in lane '" + myParameter["laneID"] + "'.");
289  } catch (EmptyData&) {
290  WRITE_ERROR("Missing data in lane '" + myParameter["laneID"] + "'.");
291  }
292  }
293  break;
294  default:
295  break;
296  }
297 }
298 
299 
300 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:284
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:280
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:279
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:48
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:324
static const double UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:327
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:36
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:47
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:148