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