Eclipse SUMO - Simulation of Urban MObility
NIImporter_MATSim.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-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 MATSim format
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 #include <set>
25 #include <functional>
26 #include <sstream>
28 #include <utils/common/ToString.h>
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>
40 #include <utils/xml/XMLSubSys.h>
41 #include "NILoader.h"
42 #include "NIImporter_MATSim.h"
43 
44 
45 
46 // ===========================================================================
47 // static variables
48 // ===========================================================================
55 };
56 
57 
73 
75 };
76 
77 
78 // ===========================================================================
79 // method definitions
80 // ===========================================================================
81 // ---------------------------------------------------------------------------
82 // static methods
83 // ---------------------------------------------------------------------------
84 void
86  // check whether the option is set (properly)
87  if (!oc.isSet("matsim-files")) {
88  return;
89  }
90  /* Parse file(s)
91  * Each file is parsed twice: first for nodes, second for edges. */
92  std::vector<std::string> files = oc.getStringVector("matsim-files");
93  // load nodes, first
94  NodesHandler nodesHandler(nb.getNodeCont());
95  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
96  // nodes
97  if (!FileHelpers::isReadable(*file)) {
98  WRITE_ERROR("Could not open matsim-file '" + *file + "'.");
99  return;
100  }
101  nodesHandler.setFileName(*file);
102  PROGRESS_BEGIN_MESSAGE("Parsing nodes from matsim-file '" + *file + "'");
103  if (!XMLSubSys::runParser(nodesHandler, *file)) {
104  return;
105  }
107  }
108  // load edges, then
109  EdgesHandler edgesHandler(nb.getNodeCont(), nb.getEdgeCont(), oc.getBool("matsim.keep-length"),
110  oc.getBool("matsim.lanes-from-capacity"), NBCapacity2Lanes(oc.getFloat("lanes-from-capacity.norm")));
111  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
112  // edges
113  edgesHandler.setFileName(*file);
114  PROGRESS_BEGIN_MESSAGE("Parsing edges from matsim-file '" + *file + "'");
115  XMLSubSys::runParser(edgesHandler, *file);
117  }
118 }
119 
120 
121 // ---------------------------------------------------------------------------
122 // definitions of NIImporter_MATSim::NodesHandler-methods
123 // ---------------------------------------------------------------------------
125  : GenericSAXHandler(matsimTags, MATSIM_TAG_NOTHING,
126  matsimAttrs, MATSIM_ATTR_NOTHING,
127  "matsim - file"), myNodeCont(toFill) {
128 }
129 
130 
132 
133 
134 void
136  if (element != MATSIM_TAG_NODE) {
137  return;
138  }
139  // get the id, report a warning if not given or empty...
140  bool ok = true;
141  std::string id = attrs.get<std::string>(MATSIM_ATTR_ID, nullptr, ok);
142  double x = attrs.get<double>(MATSIM_ATTR_X, id.c_str(), ok);
143  double y = attrs.get<double>(MATSIM_ATTR_Y, id.c_str(), ok);
144  if (!ok) {
145  return;
146  }
147  Position pos(x, y);
149  WRITE_ERROR("Unable to project coordinates for node '" + id + "'.");
150  }
151  NBNode* node = new NBNode(id, pos);
152  if (!myNodeCont.insert(node)) {
153  delete node;
154  WRITE_ERROR("Could not add node '" + id + "'. Probably declared twice.");
155  }
156 }
157 
158 
159 
160 // ---------------------------------------------------------------------------
161 // definitions of NIImporter_MATSim::EdgesHandler-methods
162 // ---------------------------------------------------------------------------
164  bool keepEdgeLengths, bool lanesFromCapacity,
165  NBCapacity2Lanes capacity2Lanes)
167  matsimAttrs, MATSIM_ATTR_NOTHING, "matsim - file"),
168  myNodeCont(nc), myEdgeCont(toFill), myCapacityNorm(3600),
169  myKeepEdgeLengths(keepEdgeLengths), myLanesFromCapacity(lanesFromCapacity),
170  myCapacity2Lanes(capacity2Lanes) {
171 }
172 
173 
175 }
176 
177 
178 void
180  const SUMOSAXAttributes& attrs) {
181  bool ok = true;
182  if (element == MATSIM_TAG_NETWORK) {
184  int capDivider = attrs.get<int>(MATSIM_ATTR_CAPDIVIDER, "network", ok);
185  if (ok) {
186  myCapacityNorm = (double)(capDivider * 3600);
187  }
188  }
189  }
190  if (element == MATSIM_TAG_LINKS) {
191  bool ok = true;
192  std::string capperiod = attrs.get<std::string>(MATSIM_ATTR_CAPPERIOD, "links", ok);
193  StringTokenizer st(capperiod, ":");
194  if (st.size() != 3) {
195  WRITE_ERROR("Bogus capacity period format; requires 'hh:mm:ss'.");
196  return;
197  }
198  try {
199  int hours = StringUtils::toInt(st.next());
200  int minutes = StringUtils::toInt(st.next());
201  int seconds = StringUtils::toInt(st.next());
202  myCapacityNorm = (double)(hours * 3600 + minutes * 60 + seconds);
203  } catch (NumberFormatException&) {
204  } catch (EmptyData&) {
205  }
206  return;
207  }
208 
209  // parse "link" elements
210  if (element != MATSIM_TAG_LINK) {
211  return;
212  }
213  std::string id = attrs.get<std::string>(MATSIM_ATTR_ID, nullptr, ok);
214  std::string fromNodeID = attrs.get<std::string>(MATSIM_ATTR_FROM, id.c_str(), ok);
215  std::string toNodeID = attrs.get<std::string>(MATSIM_ATTR_TO, id.c_str(), ok);
216  double length = attrs.get<double>(MATSIM_ATTR_LENGTH, id.c_str(), ok); // override computed?
217  double freeSpeed = attrs.get<double>(MATSIM_ATTR_FREESPEED, id.c_str(), ok); //
218  double capacity = attrs.get<double>(MATSIM_ATTR_CAPACITY, id.c_str(), ok); // override permLanes?
219  double permLanes = attrs.get<double>(MATSIM_ATTR_PERMLANES, id.c_str(), ok);
220  //bool oneWay = attrs.getOpt<bool>(MATSIM_ATTR_ONEWAY, id.c_str(), ok, true); // mandatory?
221  std::string modes = attrs.getOpt<std::string>(MATSIM_ATTR_MODES, id.c_str(), ok, ""); // which values?
222  std::string origid = attrs.getOpt<std::string>(MATSIM_ATTR_ORIGID, id.c_str(), ok, "");
223  NBNode* fromNode = myNodeCont.retrieve(fromNodeID);
224  NBNode* toNode = myNodeCont.retrieve(toNodeID);
225  if (fromNode == nullptr) {
226  WRITE_ERROR("Could not find from-node for edge '" + id + "'.");
227  }
228  if (toNode == nullptr) {
229  WRITE_ERROR("Could not find to-node for edge '" + id + "'.");
230  }
231  if (fromNode == nullptr || toNode == nullptr) {
232  return;
233  }
234  if (myLanesFromCapacity) {
235  permLanes = myCapacity2Lanes.get(capacity);
236  }
237  NBEdge* edge = new NBEdge(id, fromNode, toNode, "", freeSpeed, (int) permLanes, -1, NBEdge::UNSPECIFIED_WIDTH, NBEdge::UNSPECIFIED_OFFSET);
238  edge->setParameter("capacity", toString(capacity));
239  if (myKeepEdgeLengths) {
240  edge->setLoadedLength(length);
241  }
242  if (!myEdgeCont.insert(edge)) {
243  delete edge;
244  WRITE_ERROR("Could not add edge '" + id + "'. Probably declared twice.");
245  }
246 }
247 
248 
249 /****************************************************************************/
250 
NIImporter_MATSim::EdgesHandler::myStartElement
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
Definition: NIImporter_MATSim.cpp:179
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
ToString.h
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
SUMOSAXAttributes::hasAttribute
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
NIImporter_MATSim::MATSIM_ATTR_CAPACITY
Definition: NIImporter_MATSim.h:214
NIImporter_MATSim::MATSIM_TAG_NOTHING
Definition: NIImporter_MATSim.h:192
NBEdgeCont
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:60
NBNetBuilder
Instance responsible for building networks.
Definition: NBNetBuilder.h:109
NIImporter_MATSim::MATSIM_ATTR_Y
Definition: NIImporter_MATSim.h:209
OptionsCont.h
SUMOSAXAttributes::get
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
Definition: SUMOSAXAttributes.h:492
MsgHandler.h
NIImporter_MATSim::NodesHandler::NodesHandler
NodesHandler(NBNodeCont &toFill)
Contructor.
Definition: NIImporter_MATSim.cpp:124
NIImporter_MATSim::NodesHandler::myStartElement
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
Definition: NIImporter_MATSim.cpp:135
NIImporter_MATSim::matsimAttrs
static StringBijection< int >::Entry matsimAttrs[]
The names of MATSIM-XML attributes (for passing to GenericSAXHandler)
Definition: NIImporter_MATSim.h:227
SUMOSAXHandler.h
FileHelpers.h
NIImporter_MATSim::MATSIM_ATTR_PERMLANES
Definition: NIImporter_MATSim.h:215
NBEdgeCont.h
GeoConvHelper.h
OptionsCont::getBool
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
Definition: OptionsCont.cpp:222
EmptyData
Definition: UtilExceptions.h:68
NIImporter_MATSim::MATSIM_ATTR_CAPPERIOD
Definition: NIImporter_MATSim.h:219
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
NBEdge::setLoadedLength
void setLoadedLength(double val)
set loaded length
Definition: NBEdge.cpp:3419
NIImporter_MATSim::loadNetwork
static void loadNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Loads content of the optionally given MATSIM network files.
Definition: NIImporter_MATSim.cpp:85
NIImporter_MATSim::MATSIM_ATTR_TO
Definition: NIImporter_MATSim.h:211
NIImporter_MATSim.h
NBNodeCont
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:59
NBNetBuilder::getEdgeCont
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:150
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_MATSim::MATSIM_TAG_LINKS
Definition: NIImporter_MATSim.h:196
NIImporter_MATSim::MATSIM_ATTR_MODES
Definition: NIImporter_MATSim.h:217
NumberFormatException
Definition: UtilExceptions.h:95
StringBijection
Definition: StringBijection.h:43
NIImporter_MATSim::MATSIM_ATTR_NOTHING
Definition: NIImporter_MATSim.h:206
NIImporter_MATSim::matsimTags
static StringBijection< int >::Entry matsimTags[]
The names of MATSIM-XML elements (for passing to GenericSAXHandler)
Definition: NIImporter_MATSim.h:224
NIImporter_MATSim::MATSIM_ATTR_LENGTH
Definition: NIImporter_MATSim.h:212
NIImporter_MATSim::MATSIM_TAG_NODE
Definition: NIImporter_MATSim.h:194
NIImporter_MATSim::MATSIM_ATTR_ORIGID
Definition: NIImporter_MATSim.h:218
StringTokenizer
Definition: StringTokenizer.h:61
NIImporter_MATSim::MATSIM_TAG_LINK
Definition: NIImporter_MATSim.h:195
NBNetBuilder.h
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
NIImporter_MATSim::NodesHandler::~NodesHandler
~NodesHandler()
Destructor.
Definition: NIImporter_MATSim.cpp:131
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
SUMOSAXAttributes::getOpt
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue, bool report=true) const
Tries to read given attribute assuming it is an int.
Definition: SUMOSAXAttributes.h:518
NBCapacity2Lanes
A helper class which computes the lane number from given capacity.
Definition: NBCapacity2Lanes.h:39
NIImporter_MATSim::MATSIM_ATTR_FREESPEED
Definition: NIImporter_MATSim.h:213
OptionsCont::getFloat
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
Definition: OptionsCont.cpp:208
NBNodeCont.h
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
StringUtils.h
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
PROGRESS_DONE_MESSAGE
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:279
NIImporter_MATSim::EdgesHandler::~EdgesHandler
~EdgesHandler()
Destructor.
Definition: NIImporter_MATSim.cpp:174
NIImporter_MATSim::NodesHandler
A class which extracts MATSIM-nodes from a parsed MATSIM-file.
Definition: NIImporter_MATSim.h:75
NBEdge::UNSPECIFIED_WIDTH
static const double UNSPECIFIED_WIDTH
unspecified lane width
Definition: NBEdge.h:315
Parameterised::setParameter
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
Definition: Parameterised.cpp:46
FileHelpers::isReadable
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:49
NIImporter_MATSim::MATSIM_ATTR_CAPDIVIDER
Definition: NIImporter_MATSim.h:220
config.h
NIImporter_MATSim::MATSIM_ATTR_FROM
Definition: NIImporter_MATSim.h:210
StringTokenizer.h
NBNode
Represents a single node (junction) during network building.
Definition: NBNode.h:67
NIImporter_MATSim::EdgesHandler::EdgesHandler
EdgesHandler(const NBNodeCont &nc, NBEdgeCont &toFill, bool keepEdgeLengths, bool lanesFromCapacity, NBCapacity2Lanes capacity2Lanes)
Constructor.
Definition: NIImporter_MATSim.cpp:163
NIImporter_MATSim::EdgesHandler
A class which extracts MATSIM-edges from a parsed MATSIM-file.
Definition: NIImporter_MATSim.h:122
NIImporter_MATSim::MATSIM_ATTR_X
Definition: NIImporter_MATSim.h:208
NBNetBuilder::getNodeCont
NBNodeCont & getNodeCont()
Returns a reference to the node container.
Definition: NBNetBuilder.h:155
GenericSAXHandler
A handler which converts occuring elements and attributes into enums.
Definition: GenericSAXHandler.h:67
NIImporter_MATSim::MATSIM_TAG_NETWORK
Definition: NIImporter_MATSim.h:193
NBNode.h
NIImporter_MATSim::MATSIM_ATTR_ONEWAY
Definition: NIImporter_MATSim.h:216
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:56
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
NIImporter_MATSim::MATSIM_ATTR_ID
Definition: NIImporter_MATSim.h:207
NBEdge.h
GenericSAXHandler::setFileName
void setFileName(const std::string &name)
Sets the current file name.
Definition: GenericSAXHandler.cpp:68
XMLSubSys.h