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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Importer for networks stored in MATSim format
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 #include <set>
32 #include <functional>
33 #include <sstream>
35 #include <utils/common/ToString.h>
37 #include <netbuild/NBEdge.h>
38 #include <netbuild/NBEdgeCont.h>
39 #include <netbuild/NBNode.h>
40 #include <netbuild/NBNodeCont.h>
41 #include <netbuild/NBNetBuilder.h>
47 #include <utils/xml/XMLSubSys.h>
48 #include "NILoader.h"
49 #include "NIImporter_MATSim.h"
50 
51 
52 
53 // ===========================================================================
54 // static variables
55 // ===========================================================================
62 };
63 
64 
80 
82 };
83 
84 
85 // ===========================================================================
86 // method definitions
87 // ===========================================================================
88 // ---------------------------------------------------------------------------
89 // static methods
90 // ---------------------------------------------------------------------------
91 void
93  // check whether the option is set (properly)
94  if (!oc.isSet("matsim-files")) {
95  return;
96  }
97  /* Parse file(s)
98  * Each file is parsed twice: first for nodes, second for edges. */
99  std::vector<std::string> files = oc.getStringVector("matsim-files");
100  // load nodes, first
101  NodesHandler nodesHandler(nb.getNodeCont());
102  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
103  // nodes
104  if (!FileHelpers::isReadable(*file)) {
105  WRITE_ERROR("Could not open matsim-file '" + *file + "'.");
106  return;
107  }
108  nodesHandler.setFileName(*file);
109  PROGRESS_BEGIN_MESSAGE("Parsing nodes from matsim-file '" + *file + "'");
110  if (!XMLSubSys::runParser(nodesHandler, *file)) {
111  return;
112  }
114  }
115  // load edges, then
116  EdgesHandler edgesHandler(nb.getNodeCont(), nb.getEdgeCont(), oc.getBool("matsim.keep-length"),
117  oc.getBool("matsim.lanes-from-capacity"), NBCapacity2Lanes(oc.getFloat("lanes-from-capacity.norm")));
118  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
119  // edges
120  edgesHandler.setFileName(*file);
121  PROGRESS_BEGIN_MESSAGE("Parsing edges from matsim-file '" + *file + "'");
122  XMLSubSys::runParser(edgesHandler, *file);
124  }
125 }
126 
127 
128 // ---------------------------------------------------------------------------
129 // definitions of NIImporter_MATSim::NodesHandler-methods
130 // ---------------------------------------------------------------------------
134  "matsim - file"), myNodeCont(toFill) {
135 }
136 
137 
139 
140 
141 void
143  if (element != MATSIM_TAG_NODE) {
144  return;
145  }
146  // get the id, report a warning if not given or empty...
147  bool ok = true;
148  std::string id = attrs.get<std::string>(MATSIM_ATTR_ID, 0, ok);
149  double x = attrs.get<double>(MATSIM_ATTR_X, id.c_str(), ok);
150  double y = attrs.get<double>(MATSIM_ATTR_Y, id.c_str(), ok);
151  if (!ok) {
152  return;
153  }
154  Position pos(x, y);
156  WRITE_ERROR("Unable to project coordinates for node '" + id + "'.");
157  }
158  NBNode* node = new NBNode(id, pos);
159  if (!myNodeCont.insert(node)) {
160  delete node;
161  WRITE_ERROR("Could not add node '" + id + "'. Probably declared twice.");
162  }
163 }
164 
165 
166 
167 // ---------------------------------------------------------------------------
168 // definitions of NIImporter_MATSim::EdgesHandler-methods
169 // ---------------------------------------------------------------------------
171  bool keepEdgeLengths, bool lanesFromCapacity,
172  NBCapacity2Lanes capacity2Lanes)
174  matsimAttrs, MATSIM_ATTR_NOTHING, "matsim - file"),
175  myNodeCont(nc), myEdgeCont(toFill), myCapacityNorm(3600),
176  myKeepEdgeLengths(keepEdgeLengths), myLanesFromCapacity(lanesFromCapacity),
177  myCapacity2Lanes(capacity2Lanes) {
178 }
179 
180 
182 }
183 
184 
185 void
187  const SUMOSAXAttributes& attrs) {
188  bool ok = true;
189  if (element == MATSIM_TAG_NETWORK) {
191  int capDivider = attrs.get<int>(MATSIM_ATTR_CAPDIVIDER, "network", ok);
192  if (ok) {
193  myCapacityNorm = (double)(capDivider * 3600);
194  }
195  }
196  }
197  if (element == MATSIM_TAG_LINKS) {
198  bool ok = true;
199  std::string capperiod = attrs.get<std::string>(MATSIM_ATTR_CAPPERIOD, "links", ok);
200  StringTokenizer st(capperiod, ":");
201  if (st.size() != 3) {
202  WRITE_ERROR("Bogus capacity period format; requires 'hh:mm:ss'.");
203  return;
204  }
205  try {
206  int hours = TplConvert::_2int(st.next().c_str());
207  int minutes = TplConvert::_2int(st.next().c_str());
208  int seconds = TplConvert::_2int(st.next().c_str());
209  myCapacityNorm = (double)(hours * 3600 + minutes * 60 + seconds);
210  } catch (NumberFormatException&) {
211  } catch (EmptyData&) {
212  }
213  return;
214  }
215 
216  // parse "link" elements
217  if (element != MATSIM_TAG_LINK) {
218  return;
219  }
220  std::string id = attrs.get<std::string>(MATSIM_ATTR_ID, 0, ok);
221  std::string fromNodeID = attrs.get<std::string>(MATSIM_ATTR_FROM, id.c_str(), ok);
222  std::string toNodeID = attrs.get<std::string>(MATSIM_ATTR_TO, id.c_str(), ok);
223  double length = attrs.get<double>(MATSIM_ATTR_LENGTH, id.c_str(), ok); // override computed?
224  double freeSpeed = attrs.get<double>(MATSIM_ATTR_FREESPEED, id.c_str(), ok); //
225  double capacity = attrs.get<double>(MATSIM_ATTR_CAPACITY, id.c_str(), ok); // override permLanes?
226  double permLanes = attrs.get<double>(MATSIM_ATTR_PERMLANES, id.c_str(), ok);
227  //bool oneWay = attrs.getOpt<bool>(MATSIM_ATTR_ONEWAY, id.c_str(), ok, true); // mandatory?
228  std::string modes = attrs.getOpt<std::string>(MATSIM_ATTR_MODES, id.c_str(), ok, ""); // which values?
229  std::string origid = attrs.getOpt<std::string>(MATSIM_ATTR_ORIGID, id.c_str(), ok, "");
230  NBNode* fromNode = myNodeCont.retrieve(fromNodeID);
231  NBNode* toNode = myNodeCont.retrieve(toNodeID);
232  if (fromNode == 0) {
233  WRITE_ERROR("Could not find from-node for edge '" + id + "'.");
234  }
235  if (toNode == 0) {
236  WRITE_ERROR("Could not find to-node for edge '" + id + "'.");
237  }
238  if (fromNode == 0 || toNode == 0) {
239  return;
240  }
241  if (myLanesFromCapacity) {
242  permLanes = myCapacity2Lanes.get(capacity);
243  }
244  NBEdge* edge = new NBEdge(id, fromNode, toNode, "", freeSpeed, (int) permLanes, -1, NBEdge::UNSPECIFIED_WIDTH, NBEdge::UNSPECIFIED_OFFSET);
245  edge->setParameter("capacity", toString(capacity));
246  if (myKeepEdgeLengths) {
247  edge->setLoadedLength(length);
248  }
249  if (!myEdgeCont.insert(edge)) {
250  delete edge;
251  WRITE_ERROR("Could not add edge '" + id + "'. Probably declared twice.");
252  }
253 }
254 
255 
256 /****************************************************************************/
257 
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:53
A helper class which computes the lane number from given capacity.
The representation of a single edge during network building.
Definition: NBEdge.h:70
static const double UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:257
void setLoadedLength(double val)
set loaded lenght
Definition: NBEdge.cpp:3040
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:109
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:254
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:157
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:55
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:45
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:156
A class which extracts MATSIM-nodes from a parsed MATSIM-file.
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
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:201
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.
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:205
NBCapacity2Lanes myCapacity2Lanes
The converter from flow to lanes.
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
Definition: TplConvert.h:155
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:161
Instance responsible for building networks.
Definition: NBNetBuilder.h:115
A storage for options typed value containers)
Definition: OptionsCont.h:98
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:74
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:66
static StringBijection< int >::Entry matsimTags[]
The names of MATSIM-XML elements (for passing to GenericSAXHandler)
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:202
NBEdgeCont & myEdgeCont
The edge container to fill.