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-2017 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 
53 
54 // ===========================================================================
55 // static variables
56 // ===========================================================================
63 };
64 
65 
81 
83 };
84 
85 
86 // ===========================================================================
87 // method definitions
88 // ===========================================================================
89 // ---------------------------------------------------------------------------
90 // static methods
91 // ---------------------------------------------------------------------------
92 void
94  // check whether the option is set (properly)
95  if (!oc.isSet("matsim-files")) {
96  return;
97  }
98  /* Parse file(s)
99  * Each file is parsed twice: first for nodes, second for edges. */
100  std::vector<std::string> files = oc.getStringVector("matsim-files");
101  // load nodes, first
102  NodesHandler nodesHandler(nb.getNodeCont());
103  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
104  // nodes
105  if (!FileHelpers::isReadable(*file)) {
106  WRITE_ERROR("Could not open matsim-file '" + *file + "'.");
107  return;
108  }
109  nodesHandler.setFileName(*file);
110  PROGRESS_BEGIN_MESSAGE("Parsing nodes from matsim-file '" + *file + "'");
111  if (!XMLSubSys::runParser(nodesHandler, *file)) {
112  return;
113  }
115  }
116  // load edges, then
117  EdgesHandler edgesHandler(nb.getNodeCont(), nb.getEdgeCont(), oc.getBool("matsim.keep-length"),
118  oc.getBool("matsim.lanes-from-capacity"), NBCapacity2Lanes(oc.getFloat("lanes-from-capacity.norm")));
119  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
120  // edges
121  edgesHandler.setFileName(*file);
122  PROGRESS_BEGIN_MESSAGE("Parsing edges from matsim-file '" + *file + "'");
123  XMLSubSys::runParser(edgesHandler, *file);
125  }
126 }
127 
128 
129 // ---------------------------------------------------------------------------
130 // definitions of NIImporter_MATSim::NodesHandler-methods
131 // ---------------------------------------------------------------------------
135  "matsim - file"), myNodeCont(toFill) {
136 }
137 
138 
140 
141 
142 void
144  if (element != MATSIM_TAG_NODE) {
145  return;
146  }
147  // get the id, report a warning if not given or empty...
148  bool ok = true;
149  std::string id = attrs.get<std::string>(MATSIM_ATTR_ID, 0, ok);
150  double x = attrs.get<double>(MATSIM_ATTR_X, id.c_str(), ok);
151  double y = attrs.get<double>(MATSIM_ATTR_Y, id.c_str(), ok);
152  if (!ok) {
153  return;
154  }
155  Position pos(x, y);
157  WRITE_ERROR("Unable to project coordinates for node '" + id + "'.");
158  }
159  NBNode* node = new NBNode(id, pos);
160  if (!myNodeCont.insert(node)) {
161  delete node;
162  WRITE_ERROR("Could not add node '" + id + "'. Probably declared twice.");
163  }
164 }
165 
166 
167 
168 // ---------------------------------------------------------------------------
169 // definitions of NIImporter_MATSim::EdgesHandler-methods
170 // ---------------------------------------------------------------------------
172  bool keepEdgeLengths, bool lanesFromCapacity,
173  NBCapacity2Lanes capacity2Lanes)
175  matsimAttrs, MATSIM_ATTR_NOTHING, "matsim - file"),
176  myNodeCont(nc), myEdgeCont(toFill), myCapacityNorm(3600),
177  myKeepEdgeLengths(keepEdgeLengths), myLanesFromCapacity(lanesFromCapacity),
178  myCapacity2Lanes(capacity2Lanes) {
179 }
180 
181 
183 }
184 
185 
186 void
188  const SUMOSAXAttributes& attrs) {
189  bool ok = true;
190  if (element == MATSIM_TAG_NETWORK) {
192  int capDivider = attrs.get<int>(MATSIM_ATTR_CAPDIVIDER, "network", ok);
193  if (ok) {
194  myCapacityNorm = (double)(capDivider * 3600);
195  }
196  }
197  }
198  if (element == MATSIM_TAG_LINKS) {
199  bool ok = true;
200  std::string capperiod = attrs.get<std::string>(MATSIM_ATTR_CAPPERIOD, "links", ok);
201  StringTokenizer st(capperiod, ":");
202  if (st.size() != 3) {
203  WRITE_ERROR("Bogus capacity period format; requires 'hh:mm:ss'.");
204  return;
205  }
206  try {
207  int hours = TplConvert::_2int(st.next().c_str());
208  int minutes = TplConvert::_2int(st.next().c_str());
209  int seconds = TplConvert::_2int(st.next().c_str());
210  myCapacityNorm = (double)(hours * 3600 + minutes * 60 + seconds);
211  } catch (NumberFormatException&) {
212  } catch (EmptyData&) {
213  }
214  return;
215  }
216 
217  // parse "link" elements
218  if (element != MATSIM_TAG_LINK) {
219  return;
220  }
221  std::string id = attrs.get<std::string>(MATSIM_ATTR_ID, 0, ok);
222  std::string fromNodeID = attrs.get<std::string>(MATSIM_ATTR_FROM, id.c_str(), ok);
223  std::string toNodeID = attrs.get<std::string>(MATSIM_ATTR_TO, id.c_str(), ok);
224  double length = attrs.get<double>(MATSIM_ATTR_LENGTH, id.c_str(), ok); // override computed?
225  double freeSpeed = attrs.get<double>(MATSIM_ATTR_FREESPEED, id.c_str(), ok); //
226  double capacity = attrs.get<double>(MATSIM_ATTR_CAPACITY, id.c_str(), ok); // override permLanes?
227  double permLanes = attrs.get<double>(MATSIM_ATTR_PERMLANES, id.c_str(), ok);
228  //bool oneWay = attrs.getOpt<bool>(MATSIM_ATTR_ONEWAY, id.c_str(), ok, true); // mandatory?
229  std::string modes = attrs.getOpt<std::string>(MATSIM_ATTR_MODES, id.c_str(), ok, ""); // which values?
230  std::string origid = attrs.getOpt<std::string>(MATSIM_ATTR_ORIGID, id.c_str(), ok, "");
231  NBNode* fromNode = myNodeCont.retrieve(fromNodeID);
232  NBNode* toNode = myNodeCont.retrieve(toNodeID);
233  if (fromNode == 0) {
234  WRITE_ERROR("Could not find from-node for edge '" + id + "'.");
235  }
236  if (toNode == 0) {
237  WRITE_ERROR("Could not find to-node for edge '" + id + "'.");
238  }
239  if (fromNode == 0 || toNode == 0) {
240  return;
241  }
242  if (myLanesFromCapacity) {
243  permLanes = myCapacity2Lanes.get(capacity);
244  }
245  NBEdge* edge = new NBEdge(id, fromNode, toNode, "", freeSpeed, (int) permLanes, -1, NBEdge::UNSPECIFIED_WIDTH, NBEdge::UNSPECIFIED_OFFSET);
246  edge->addParameter("capacity", toString(capacity));
247  if (myKeepEdgeLengths) {
248  edge->setLoadedLength(length);
249  }
250  if (!myEdgeCont.insert(edge)) {
251  delete edge;
252  WRITE_ERROR("Could not add edge '" + id + "'. Probably declared twice.");
253  }
254 }
255 
256 
257 /****************************************************************************/
258 
NBNode * retrieve(const std::string &id) const
Returns the node with the given name.
Definition: NBNodeCont.cpp:106
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:54
A helper class which computes the lane number from given capacity.
The representation of a single edge during network building.
Definition: NBEdge.h:71
static const double UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:258
void setLoadedLength(double val)
set loaded lenght
Definition: NBEdge.cpp:2928
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:110
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:255
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:158
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:56
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:46
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:155
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:202
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
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:206
void addParameter(const std::string &key, const std::string &value)
Adds a parameter.
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:149
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:160
Instance responsible for building networks.
Definition: NBNetBuilder.h:114
A storage for options typed value containers)
Definition: OptionsCont.h:99
bool insert(const std::string &id, const Position &position, NBDistrict *district=0)
Inserts a node into the map.
Definition: NBNodeCont.cpp:77
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:75
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:63
static StringBijection< int >::Entry matsimTags[]
The names of MATSIM-XML elements (for passing to GenericSAXHandler)
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
NBEdgeCont & myEdgeCont
The edge container to fill.