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 // ---------------------------------------------------------------------------
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 
NBNode * retrieve(const std::string &id) const
Returns the node with the given name.
Definition: NBNodeCont.cpp:108
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
static bool transformCoordinate(Position &from, bool includeInBoundary=true, GeoConvHelper *from_srs=0)
transforms loaded coordinates handles projections, offsets (using GeoConvHelper) and import of height...
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:49
A helper class which computes the lane number from given capacity.
The representation of a single edge during network building.
Definition: NBEdge.h:86
static const double UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:306
void setLoadedLength(double val)
set loaded lenght
Definition: NBEdge.cpp:3456
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything&#39;s ok.
Definition: XMLSubSys.cpp:113
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
NodesHandler(NBNodeCont &toFill)
Contructor.
static const double UNSPECIFIED_WIDTH
unspecified lane width
Definition: NBEdge.h:303
NBNodeCont & myNodeCont
The nodes container to fill.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
A handler which converts occuring elements and attributes into enums.
void setFileName(const std::string &name)
Sets the current file name.
bool myLanesFromCapacity
Whether the lane number shall be computed from the capacity.
bool insert(NBEdge *edge, bool ignorePrunning=false)
Adds an edge to the dictionary.
Definition: NBEdgeCont.cpp:152
static StringBijection< int >::Entry matsimAttrs[]
The names of MATSIM-XML attributes (for passing to GenericSAXHandler)
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
Encapsulated SAX-Attributes.
int get(double capacity) const
Returns the number of lanes computed from the given capacity.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:151
A class which extracts MATSIM-nodes from a parsed MATSIM-file.
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:61
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) ...
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:241
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter...
bool myKeepEdgeLengths
Whether the loaded lengths shal be used.
EdgesHandler(const NBNodeCont &nc, NBEdgeCont &toFill, bool keepEdgeLengths, bool lanesFromCapacity, NBCapacity2Lanes capacity2Lanes)
Constructor.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
double myCapacityNorm
The capacity norming.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
NBCapacity2Lanes myCapacity2Lanes
The converter from flow to lanes.
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.
A class which extracts MATSIM-edges from a parsed MATSIM-file.
const NBNodeCont & myNodeCont
The previously parsed nodes.
NBNodeCont & getNodeCont()
Returns a reference to the node container.
Definition: NBNetBuilder.h:156
Instance responsible for building networks.
Definition: NBNetBuilder.h:110
A storage for options typed value containers)
Definition: OptionsCont.h:90
bool insert(const std::string &id, const Position &position, NBDistrict *district=0)
Inserts a node into the map.
Definition: NBNodeCont.cpp:79
static void loadNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Loads content of the optionally given MATSIM network files.
Represents a single node (junction) during network building.
Definition: NBNode.h:68
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:60
static StringBijection< int >::Entry matsimTags[]
The names of MATSIM-XML elements (for passing to GenericSAXHandler)
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:242
NBEdgeCont & myEdgeCont
The edge container to fill.