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 /****************************************************************************/
16 // Importer for networks stored in ITSUMO format
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 #include <set>
25 #include <functional>
26 #include <sstream>
29 #include <netbuild/NBEdge.h>
30 #include <netbuild/NBEdgeCont.h>
31 #include <netbuild/NBNode.h>
32 #include <netbuild/NBNodeCont.h>
33 #include <netbuild/NBNetBuilder.h>
40 #include <utils/xml/XMLSubSys.h>
41 #include "NILoader.h"
42 #include "NIImporter_ITSUMO.h"
43 
44 
45 
46 // ===========================================================================
47 // static variables
48 // ===========================================================================
70  { "is_preferencial", NIImporter_ITSUMO::ITSUMO_TAG_IS_PREFERENCIAL },
71  { "delimiting_node", NIImporter_ITSUMO::ITSUMO_TAG_DELIMITING_NODE },
75  { "laneset_position", NIImporter_ITSUMO::ITSUMO_TAG_LANESET_POSITION },
78  { "turning_probabilities", NIImporter_ITSUMO::ITSUMO_TAG_TURNING_PROBABILITIES },
80  { "destination_laneset", NIImporter_ITSUMO::ITSUMO_TAG_DESTINATION_LANESET },
87  { "deceleration_prob", NIImporter_ITSUMO::ITSUMO_TAG_DECELERATION_PROB },
89 };
90 
91 
94 };
95 
96 
97 // ===========================================================================
98 // method definitions
99 // ===========================================================================
100 // ---------------------------------------------------------------------------
101 // static methods
102 // ---------------------------------------------------------------------------
103 void
105  // check whether the option is set (properly)
106  if (!oc.isSet("itsumo-files")) {
107  return;
108  }
109  /* Parse file(s)
110  * Each file is parsed twice: first for nodes, second for edges. */
111  std::vector<std::string> files = oc.getStringVector("itsumo-files");
112  // load nodes, first
113  Handler Handler(nb);
114  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
115  // nodes
116  if (!FileHelpers::isReadable(*file)) {
117  WRITE_ERROR("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, (int)ls->lanes.size(), -1, NBEdge::UNSPECIFIED_WIDTH, NBEdge::UNSPECIFIED_OFFSET);
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 /****************************************************************************/
302 
OptionsCont::isSet
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
Definition: OptionsCont.cpp:135
NBEdge::UNSPECIFIED_OFFSET
static const double UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:318
NIImporter_ITSUMO::Handler::LaneSet::from
NBNode * from
Definition: NIImporter_ITSUMO.h:148
XMLSubSys::runParser
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:112
NIImporter_ITSUMO::ITSUMO_TAG_SECTION_NAME
@ ITSUMO_TAG_SECTION_NAME
Definition: NIImporter_ITSUMO.h:203
NBNetBuilder
Instance responsible for building networks.
Definition: NBNetBuilder.h:109
NIImporter_ITSUMO::ITSUMO_TAG_SECTION_ID
@ ITSUMO_TAG_SECTION_ID
Definition: NIImporter_ITSUMO.h:202
NIImporter_ITSUMO::Handler::Lane
Definition: NIImporter_ITSUMO.h:129
OptionsCont.h
NIImporter_ITSUMO::ITSUMO_TAG_SINKS
@ ITSUMO_TAG_SINKS
Definition: NIImporter_ITSUMO.h:194
StringUtils::toDouble
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
Definition: StringUtils.cpp:345
NIImporter_ITSUMO::ITSUMO_TAG_LANE_ID
@ ITSUMO_TAG_LANE_ID
Definition: NIImporter_ITSUMO.h:218
MsgHandler.h
NIImporter_ITSUMO::ITSUMO_TAG_NODE
@ ITSUMO_TAG_NODE
Definition: NIImporter_ITSUMO.h:188
SUMOSAXHandler.h
NIImporter_ITSUMO::ITSUMO_TAG_IS_PREFERENCIAL
@ ITSUMO_TAG_IS_PREFERENCIAL
Definition: NIImporter_ITSUMO.h:204
FileHelpers.h
NIImporter_ITSUMO::ITSUMO_TAG_SOURCES
@ ITSUMO_TAG_SOURCES
Definition: NIImporter_ITSUMO.h:193
NIImporter_ITSUMO::ITSUMO_TAG_STREETS
@ ITSUMO_TAG_STREETS
Definition: NIImporter_ITSUMO.h:196
NBEdgeCont.h
GeoConvHelper.h
EmptyData
Definition: UtilExceptions.h:68
NIImporter_ITSUMO::Handler::LaneSet::lanes
std::vector< Lane > lanes
Definition: NIImporter_ITSUMO.h:145
NIImporter_ITSUMO::Handler::LaneSet::id
std::string id
Definition: NIImporter_ITSUMO.h:144
NIImporter_ITSUMO::Handler::Handler
Handler(NBNetBuilder &toFill)
Contructor.
Definition: NIImporter_ITSUMO.cpp:133
NBNetBuilder::transformCoordinate
static bool transformCoordinate(Position &from, bool includeInBoundary=true, GeoConvHelper *from_srs=0)
transforms loaded coordinates handles projections, offsets (using GeoConvHelper) and import of height...
Definition: NBNetBuilder.cpp:633
NIImporter_ITSUMO::ITSUMO_TAG_LANESETS
@ ITSUMO_TAG_LANESETS
Definition: NIImporter_ITSUMO.h:206
NIImporter_ITSUMO::ITSUMO_TAG_LANE_POSITION
@ ITSUMO_TAG_LANE_POSITION
Definition: NIImporter_ITSUMO.h:219
NIImporter_ITSUMO::itsumoTags
static StringBijection< int >::Entry itsumoTags[]
The names of MATSIM-XML elements (for passing to GenericSAXHandler)
Definition: NIImporter_ITSUMO.h:235
NBEdge
The representation of a single edge during network building.
Definition: NBEdge.h:91
OptionsCont::getStringVector
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
Definition: OptionsCont.cpp:235
NIImporter_ITSUMO::ITSUMO_TAG_NOTHING
@ ITSUMO_TAG_NOTHING
Definition: NIImporter_ITSUMO.h:183
NIImporter_ITSUMO::ITSUMO_TAG_MAXIMUM_SPEED
@ ITSUMO_TAG_MAXIMUM_SPEED
Definition: NIImporter_ITSUMO.h:220
NIImporter_ITSUMO::ITSUMO_TAG_NODE_NAME
@ ITSUMO_TAG_NODE_NAME
Definition: NIImporter_ITSUMO.h:190
NumberFormatException
Definition: UtilExceptions.h:95
NIImporter_ITSUMO::ITSUMO_TAG_STREET
@ ITSUMO_TAG_STREET
Definition: NIImporter_ITSUMO.h:197
StringUtils::prune
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:48
NIImporter_ITSUMO::Handler::~Handler
~Handler()
Destructor.
Definition: NIImporter_ITSUMO.cpp:138
StringBijection
Definition: StringBijection.h:43
NIImporter_ITSUMO::ITSUMO_TAG_X_COORD
@ ITSUMO_TAG_X_COORD
Definition: NIImporter_ITSUMO.h:191
NIImporter_ITSUMO::ITSUMO_TAG_DELIMITING_NODE
@ ITSUMO_TAG_DELIMITING_NODE
Definition: NIImporter_ITSUMO.h:205
NIImporter_ITSUMO::ITSUMO_ATTR_NOTHING
@ ITSUMO_ATTR_NOTHING
Definition: NIImporter_ITSUMO.h:231
NIImporter_ITSUMO::ITSUMO_TAG_Y_COORD
@ ITSUMO_TAG_Y_COORD
Definition: NIImporter_ITSUMO.h:192
NBNetBuilder.h
NIImporter_ITSUMO::ITSUMO_TAG_DESTINATION_LANESET
@ ITSUMO_TAG_DESTINATION_LANESET
Definition: NIImporter_ITSUMO.h:214
NIImporter_ITSUMO::ITSUMO_TAG_NETWORK_NAME
@ ITSUMO_TAG_NETWORK_NAME
Definition: NIImporter_ITSUMO.h:186
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
NIImporter_ITSUMO.h
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
NIImporter_ITSUMO::loadNetwork
static void loadNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Loads content of the optionally given ITSUMO network files.
Definition: NIImporter_ITSUMO.cpp:104
NIImporter_ITSUMO::ITSUMO_TAG_NETWORK_ID
@ ITSUMO_TAG_NETWORK_ID
Definition: NIImporter_ITSUMO.h:185
NIImporter_ITSUMO::ITSUMO_TAG_SECTIONS
@ ITSUMO_TAG_SECTIONS
Definition: NIImporter_ITSUMO.h:200
NIImporter_ITSUMO::ITSUMO_TAG_LANE
@ ITSUMO_TAG_LANE
Definition: NIImporter_ITSUMO.h:217
NIImporter_ITSUMO::ITSUMO_TAG_SIMULATION
@ ITSUMO_TAG_SIMULATION
Definition: NIImporter_ITSUMO.h:184
NIImporter_ITSUMO::Handler::LaneSet
Definition: NIImporter_ITSUMO.h:140
NIImporter_ITSUMO::ITSUMO_TAG_NODE_ID
@ ITSUMO_TAG_NODE_ID
Definition: NIImporter_ITSUMO.h:189
NIImporter_ITSUMO::Handler::myStartElement
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
Definition: NIImporter_ITSUMO.cpp:142
NIImporter_ITSUMO::ITSUMO_TAG_LANESET_POSITION
@ ITSUMO_TAG_LANESET_POSITION
Definition: NIImporter_ITSUMO.h:209
NIImporter_ITSUMO::ITSUMO_TAG_STREET_ID
@ ITSUMO_TAG_STREET_ID
Definition: NIImporter_ITSUMO.h:198
NBNodeCont.h
NIImporter_ITSUMO::ITSUMO_TAG_DECELERATION_PROB
@ ITSUMO_TAG_DECELERATION_PROB
Definition: NIImporter_ITSUMO.h:221
StringUtils.h
NIImporter_ITSUMO::ITSUMO_TAG_LANES
@ ITSUMO_TAG_LANES
Definition: NIImporter_ITSUMO.h:216
StringUtils::toInt
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
Definition: StringUtils.cpp:278
NILoader.h
PROGRESS_BEGIN_MESSAGE
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:278
NIImporter_ITSUMO::Handler
Definition: NIImporter_ITSUMO.h:74
NIImporter_ITSUMO::Handler::myEndElement
void myEndElement(int element)
Callback method for a closing tag to implement by derived classes.
Definition: NIImporter_ITSUMO.cpp:207
NIImporter_ITSUMO::Handler::Section
Definition: NIImporter_ITSUMO.h:155
NIImporter_ITSUMO::Handler::LaneSet::to
NBNode * to
Definition: NIImporter_ITSUMO.h:149
NIImporter_ITSUMO::ITSUMO_TAG_PROBABILITY
@ ITSUMO_TAG_PROBABILITY
Definition: NIImporter_ITSUMO.h:215
PROGRESS_DONE_MESSAGE
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:279
NBEdge::UNSPECIFIED_WIDTH
static const double UNSPECIFIED_WIDTH
unspecified lane width
Definition: NBEdge.h:315
NIImporter_ITSUMO::Handler::LaneSet::v
double v
Definition: NIImporter_ITSUMO.h:146
FileHelpers::isReadable
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:49
NIImporter_ITSUMO::Handler::myCharacters
void myCharacters(int element, const std::string &chars)
Callback method for characters to implement by derived classes.
Definition: NIImporter_ITSUMO.cpp:157
config.h
NIImporter_ITSUMO::ITSUMO_TAG_LANESET
@ ITSUMO_TAG_LANESET
Definition: NIImporter_ITSUMO.h:207
StringTokenizer.h
NIImporter_ITSUMO::ITSUMO_TAG_SECTION
@ ITSUMO_TAG_SECTION
Definition: NIImporter_ITSUMO.h:201
NBNode
Represents a single node (junction) during network building.
Definition: NBNode.h:67
NIImporter_ITSUMO::ITSUMO_TAG_TRAFFIC_LIGHTS
@ ITSUMO_TAG_TRAFFIC_LIGHTS
Definition: NIImporter_ITSUMO.h:195
NIImporter_ITSUMO::ITSUMO_TAG_DIRECTION
@ ITSUMO_TAG_DIRECTION
Definition: NIImporter_ITSUMO.h:213
NIImporter_ITSUMO::ITSUMO_TAG_START_NODE
@ ITSUMO_TAG_START_NODE
Definition: NIImporter_ITSUMO.h:210
GenericSAXHandler
A handler which converts occuring elements and attributes into enums.
Definition: GenericSAXHandler.h:67
NBNode.h
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:56
NIImporter_ITSUMO::itsumoAttrs
static StringBijection< int >::Entry itsumoAttrs[]
The names of MATSIM-XML attributes (for passing to GenericSAXHandler)
Definition: NIImporter_ITSUMO.h:238
NIImporter_ITSUMO::ITSUMO_TAG_END_NODE
@ ITSUMO_TAG_END_NODE
Definition: NIImporter_ITSUMO.h:211
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
NIImporter_ITSUMO::ITSUMO_TAG_NODES
@ ITSUMO_TAG_NODES
Definition: NIImporter_ITSUMO.h:187
NIImporter_ITSUMO::ITSUMO_TAG_LANESET_ID
@ ITSUMO_TAG_LANESET_ID
Definition: NIImporter_ITSUMO.h:208
NBEdge.h
GenericSAXHandler::setFileName
void setFileName(const std::string &name)
Sets the current file name.
Definition: GenericSAXHandler.cpp:68
NIImporter_ITSUMO::ITSUMO_TAG_TURNING_PROBABILITIES
@ ITSUMO_TAG_TURNING_PROBABILITIES
Definition: NIImporter_ITSUMO.h:212
XMLSubSys.h
NIImporter_ITSUMO::ITSUMO_TAG_STREET_NAME
@ ITSUMO_TAG_STREET_NAME
Definition: NIImporter_ITSUMO.h:199