SUMO - Simulation of Urban MObility
NIImporter_MATSim.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Importer for networks stored in MATSim format
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 #include <set>
33 #include <functional>
34 #include <sstream>
36 #include <utils/common/ToString.h>
38 #include <netbuild/NBEdge.h>
39 #include <netbuild/NBEdgeCont.h>
40 #include <netbuild/NBNode.h>
41 #include <netbuild/NBNodeCont.h>
42 #include <netbuild/NBNetBuilder.h>
48 #include <utils/xml/XMLSubSys.h>
49 #include "NILoader.h"
50 #include "NIImporter_MATSim.h"
51 
52 #ifdef CHECK_MEMORY_LEAKS
53 #include <foreign/nvwa/debug_new.h>
54 #endif // CHECK_MEMORY_LEAKS
55 
56 
57 
58 // ===========================================================================
59 // static variables
60 // ===========================================================================
67 };
68 
69 
85 
87 };
88 
89 
90 // ===========================================================================
91 // method definitions
92 // ===========================================================================
93 // ---------------------------------------------------------------------------
94 // static methods
95 // ---------------------------------------------------------------------------
96 void
98  // check whether the option is set (properly)
99  if (!oc.isSet("matsim-files")) {
100  return;
101  }
102  /* Parse file(s)
103  * Each file is parsed twice: first for nodes, second for edges. */
104  std::vector<std::string> files = oc.getStringVector("matsim-files");
105  // load nodes, first
106  NodesHandler nodesHandler(nb.getNodeCont());
107  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
108  // nodes
109  if (!FileHelpers::isReadable(*file)) {
110  WRITE_ERROR("Could not open matsim-file '" + *file + "'.");
111  return;
112  }
113  nodesHandler.setFileName(*file);
114  PROGRESS_BEGIN_MESSAGE("Parsing nodes from matsim-file '" + *file + "'");
115  if (!XMLSubSys::runParser(nodesHandler, *file)) {
116  return;
117  }
119  }
120  // load edges, then
121  EdgesHandler edgesHandler(nb.getNodeCont(), nb.getEdgeCont(), oc.getBool("matsim.keep-length"),
122  oc.getBool("matsim.lanes-from-capacity"), NBCapacity2Lanes(oc.getFloat("lanes-from-capacity.norm")));
123  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
124  // edges
125  edgesHandler.setFileName(*file);
126  PROGRESS_BEGIN_MESSAGE("Parsing edges from matsim-file '" + *file + "'");
127  XMLSubSys::runParser(edgesHandler, *file);
129  }
130 }
131 
132 
133 // ---------------------------------------------------------------------------
134 // definitions of NIImporter_MATSim::NodesHandler-methods
135 // ---------------------------------------------------------------------------
139  "matsim - file"), myNodeCont(toFill) {
140 }
141 
142 
144 
145 
146 void
148  if (element != MATSIM_TAG_NODE) {
149  return;
150  }
151  // get the id, report a warning if not given or empty...
152  bool ok = true;
153  std::string id = attrs.get<std::string>(MATSIM_ATTR_ID, 0, ok);
154  SUMOReal x = attrs.get<SUMOReal>(MATSIM_ATTR_X, id.c_str(), ok);
155  SUMOReal y = attrs.get<SUMOReal>(MATSIM_ATTR_Y, id.c_str(), ok);
156  if (!ok) {
157  return;
158  }
159  Position pos(x, y);
161  WRITE_ERROR("Unable to project coordinates for node '" + id + "'.");
162  }
163  NBNode* node = new NBNode(id, pos);
164  if (!myNodeCont.insert(node)) {
165  delete node;
166  WRITE_ERROR("Could not add node '" + id + "'. Probably declared twice.");
167  }
168 }
169 
170 
171 
172 // ---------------------------------------------------------------------------
173 // definitions of NIImporter_MATSim::EdgesHandler-methods
174 // ---------------------------------------------------------------------------
176  bool keepEdgeLengths, bool lanesFromCapacity,
177  NBCapacity2Lanes capacity2Lanes)
179  matsimAttrs, MATSIM_ATTR_NOTHING, "matsim - file"),
180  myNodeCont(nc), myEdgeCont(toFill), myCapacityNorm(3600),
181  myKeepEdgeLengths(keepEdgeLengths), myLanesFromCapacity(lanesFromCapacity),
182  myCapacity2Lanes(capacity2Lanes) {
183 }
184 
185 
187 }
188 
189 
190 void
192  const SUMOSAXAttributes& attrs) {
193  bool ok = true;
194  if (element == MATSIM_TAG_NETWORK) {
196  int capDivider = attrs.get<int>(MATSIM_ATTR_CAPDIVIDER, "network", ok);
197  if (ok) {
198  myCapacityNorm = (SUMOReal)(capDivider * 3600);
199  }
200  }
201  }
202  if (element == MATSIM_TAG_LINKS) {
203  bool ok = true;
204  std::string capperiod = attrs.get<std::string>(MATSIM_ATTR_CAPPERIOD, "links", ok);
205  StringTokenizer st(capperiod, ":");
206  if (st.size() != 3) {
207  WRITE_ERROR("Bogus capacity period format; requires 'hh:mm:ss'.");
208  return;
209  }
210  try {
211  int hours = TplConvert::_2int(st.next().c_str());
212  int minutes = TplConvert::_2int(st.next().c_str());
213  int seconds = TplConvert::_2int(st.next().c_str());
214  myCapacityNorm = (SUMOReal)(hours * 3600 + minutes * 60 + seconds);
215  } catch (NumberFormatException&) {
216  } catch (EmptyData&) {
217  }
218  return;
219  }
220 
221  // parse "link" elements
222  if (element != MATSIM_TAG_LINK) {
223  return;
224  }
225  std::string id = attrs.get<std::string>(MATSIM_ATTR_ID, 0, ok);
226  std::string fromNodeID = attrs.get<std::string>(MATSIM_ATTR_FROM, id.c_str(), ok);
227  std::string toNodeID = attrs.get<std::string>(MATSIM_ATTR_TO, id.c_str(), ok);
228  SUMOReal length = attrs.get<SUMOReal>(MATSIM_ATTR_LENGTH, id.c_str(), ok); // override computed?
229  SUMOReal freeSpeed = attrs.get<SUMOReal>(MATSIM_ATTR_FREESPEED, id.c_str(), ok); //
230  SUMOReal capacity = attrs.get<SUMOReal>(MATSIM_ATTR_CAPACITY, id.c_str(), ok); // override permLanes?
231  SUMOReal permLanes = attrs.get<SUMOReal>(MATSIM_ATTR_PERMLANES, id.c_str(), ok);
232  //bool oneWay = attrs.getOpt<bool>(MATSIM_ATTR_ONEWAY, id.c_str(), ok, true); // mandatory?
233  std::string modes = attrs.getOpt<std::string>(MATSIM_ATTR_MODES, id.c_str(), ok, ""); // which values?
234  std::string origid = attrs.getOpt<std::string>(MATSIM_ATTR_ORIGID, id.c_str(), ok, "");
235  NBNode* fromNode = myNodeCont.retrieve(fromNodeID);
236  NBNode* toNode = myNodeCont.retrieve(toNodeID);
237  if (fromNode == 0) {
238  WRITE_ERROR("Could not find from-node for edge '" + id + "'.");
239  }
240  if (toNode == 0) {
241  WRITE_ERROR("Could not find to-node for edge '" + id + "'.");
242  }
243  if (fromNode == 0 || toNode == 0) {
244  return;
245  }
246  if (myLanesFromCapacity) {
247  permLanes = myCapacity2Lanes.get(capacity);
248  }
249  NBEdge* edge = new NBEdge(id, fromNode, toNode, "", freeSpeed, (unsigned int) permLanes, -1, NBEdge::UNSPECIFIED_WIDTH, NBEdge::UNSPECIFIED_OFFSET);
250  edge->addParameter("capacity", toString(capacity));
251  if (myKeepEdgeLengths) {
252  edge->setLoadedLength(length);
253  }
254  if (!myEdgeCont.insert(edge)) {
255  delete edge;
256  WRITE_ERROR("Could not add edge '" + id + "'. Probably declared twice.");
257  }
258 }
259 
260 
261 /****************************************************************************/
262 
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
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) ...
static const SUMOReal UNSPECIFIED_WIDTH
unspecified lane width
Definition: NBEdge.h:201
static bool transformCoordinates(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:58
A helper class which computes the lane number from given capacity.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
The representation of a single edge during network building.
Definition: NBEdge.h:70
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
static const SUMOReal UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:203
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:114
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.
NBNodeCont & myNodeCont
The nodes container to fill.
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:161
static StringBijection< int >::Entry matsimAttrs[]
The names of MATSIM-XML attributes (for passing to GenericSAXHandler)
Encapsulated SAX-Attributes.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
NBEdgeCont & getEdgeCont()
Returns the edge container.
Definition: NBNetBuilder.h:154
SUMOReal myCapacityNorm
The capacity norming.
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
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
void setLoadedLength(SUMOReal val)
Definition: NBEdge.cpp:2408
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:54
bool myKeepEdgeLengths
Whether the loaded lengths shal be used.
EdgesHandler(const NBNodeCont &nc, NBEdgeCont &toFill, bool keepEdgeLengths, bool lanesFromCapacity, NBCapacity2Lanes capacity2Lanes)
Constructor.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
void addParameter(const std::string &key, const std::string &value)
Adds a parameter.
NBCapacity2Lanes myCapacity2Lanes
The converter from flow to lanes.
int get(SUMOReal capacity) const
Returns the number of lanes computed from the given capacity.
static int _2int(const E *const data)
Definition: TplConvert.h:114
A class which extracts MATSIM-edges from a parsed MATSIM-file.
const NBNodeCont & myNodeCont
The previously parsed nodes.
NBNodeCont & getNodeCont()
Returns the node container.
Definition: NBNetBuilder.h:162
Instance responsible for building networks.
Definition: NBNetBuilder.h:113
A storage for options typed value containers)
Definition: OptionsCont.h:108
bool insert(const std::string &id, const Position &position, NBDistrict *district=0)
Inserts a node into the map.
Definition: NBNodeCont.cpp:80
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
#define SUMOReal
Definition: config.h:213
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.
NBNode * retrieve(const std::string &id) const
Returns the node with the given name.
Definition: NBNodeCont.cpp:109
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:64
static StringBijection< int >::Entry matsimTags[]
The names of MATSIM-XML elements (for passing to GenericSAXHandler)
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
NBEdgeCont & myEdgeCont
The edge container to fill.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.