SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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.sourceforge.net/
12 // Copyright (C) 2001-2012 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>
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 #ifdef CHECK_MEMORY_LEAKS
52 #include <foreign/nvwa/debug_new.h>
53 #endif // CHECK_MEMORY_LEAKS
54 
55 
56 
57 // ===========================================================================
58 // static variables
59 // ===========================================================================
66 };
67 
68 
84 
86 };
87 
88 
89 // ===========================================================================
90 // method definitions
91 // ===========================================================================
92 // ---------------------------------------------------------------------------
93 // static methods
94 // ---------------------------------------------------------------------------
95 void
97  // check whether the option is set (properly)
98  if (!oc.isSet("matsim-files")) {
99  return;
100  }
101  /* Parse file(s)
102  * Each file is parsed twice: first for nodes, second for edges. */
103  std::vector<std::string> files = oc.getStringVector("matsim-files");
104  // load nodes, first
105  NodesHandler nodesHandler(nb.getNodeCont());
106  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
107  // nodes
108  if (!FileHelpers::exists(*file)) {
109  WRITE_ERROR("Could not open matsim-file '" + *file + "'.");
110  return;
111  }
112  nodesHandler.setFileName(*file);
113  PROGRESS_BEGIN_MESSAGE("Parsing nodes from matsim-file '" + *file + "'");
114  if (!XMLSubSys::runParser(nodesHandler, *file)) {
115  return;
116  }
118  }
119  // load edges, then
120  EdgesHandler edgesHandler(nb.getNodeCont(), nb.getEdgeCont(), oc.getBool("matsim.keep-length"),
121  oc.getBool("matsim.lanes-from-capacity"), NBCapacity2Lanes(oc.getFloat("lanes-from-capacity.norm")));
122  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
123  // edges
124  edgesHandler.setFileName(*file);
125  PROGRESS_BEGIN_MESSAGE("Parsing edges from matsim-file '" + *file + "'");
126  XMLSubSys::runParser(edgesHandler, *file);
128  }
129 }
130 
131 
132 // ---------------------------------------------------------------------------
133 // definitions of NIImporter_MATSim::NodesHandler-methods
134 // ---------------------------------------------------------------------------
136  : GenericSAXHandler(matsimTags, MATSIM_TAG_NOTHING,
137  matsimAttrs, MATSIM_ATTR_NOTHING,
138  "matsim - file"), myNodeCont(toFill) {
139 }
140 
141 
143 
144 
145 void
147  if (element != MATSIM_TAG_NODE) {
148  return;
149  }
150  // get the id, report a warning if not given or empty...
151  bool ok = true;
152  std::string id = attrs.getStringReporting(MATSIM_ATTR_ID, 0, ok);
153  SUMOReal x = attrs.getSUMORealReporting(MATSIM_ATTR_X, id.c_str(), ok);
154  SUMOReal y = attrs.getSUMORealReporting(MATSIM_ATTR_Y, id.c_str(), ok);
155  if (!ok) {
156  return;
157  }
158  Position pos(x, y);
159  if (!NILoader::transformCoordinates(pos)) {
160  WRITE_ERROR("Unable to project coordinates for node '" + id + "'.");
161  }
162  NBNode* node = new NBNode(id, pos);
163  if (!myNodeCont.insert(node)) {
164  delete node;
165  WRITE_ERROR("Could not add node '" + id + "'. Probably declared twice.");
166  }
167 }
168 
169 
170 
171 // ---------------------------------------------------------------------------
172 // definitions of NIImporter_MATSim::EdgesHandler-methods
173 // ---------------------------------------------------------------------------
175  bool keepEdgeLengths, bool lanesFromCapacity,
176  NBCapacity2Lanes capacity2Lanes)
178  matsimAttrs, MATSIM_ATTR_NOTHING, "matsim - file"),
179  myNodeCont(nc), myEdgeCont(toFill), myCapacityNorm(3600),
180  myKeepEdgeLengths(keepEdgeLengths), myLanesFromCapacity(lanesFromCapacity),
181  myCapacity2Lanes(capacity2Lanes) {
182 }
183 
184 
186 }
187 
188 
189 void
191  const SUMOSAXAttributes& attrs) {
192  bool ok = true;
193  if (element == MATSIM_TAG_NETWORK) {
195  int capDivider = attrs.getIntReporting(MATSIM_ATTR_CAPDIVIDER, "network", ok);
196  if (ok) {
197  myCapacityNorm = (SUMOReal)(capDivider * 3600);
198  }
199  }
200  }
201  if (element == MATSIM_TAG_LINKS) {
202  bool ok = true;
203  std::string capperiod = attrs.getStringReporting(MATSIM_ATTR_CAPPERIOD, "links", ok);
204  StringTokenizer st(capperiod, ":");
205  if (st.size() != 3) {
206  WRITE_ERROR("Bogus capacity period format; requires 'hh:mm:ss'.");
207  return;
208  }
209  try {
210  int hours = TplConvert<char>::_2int(st.next().c_str());
211  int minutes = TplConvert<char>::_2int(st.next().c_str());
212  int seconds = TplConvert<char>::_2int(st.next().c_str());
213  myCapacityNorm = (SUMOReal)(hours * 3600 + minutes * 60 + seconds);
214  } catch (NumberFormatException&) {
215  } catch (EmptyData&) {
216  }
217  return;
218  }
219 
220  // parse "link" elements
221  if (element != MATSIM_TAG_LINK) {
222  return;
223  }
224  std::string id = attrs.getStringReporting(MATSIM_ATTR_ID, 0, ok);
225  std::string fromNodeID = attrs.getStringReporting(MATSIM_ATTR_FROM, id.c_str(), ok);
226  std::string toNodeID = attrs.getStringReporting(MATSIM_ATTR_TO, id.c_str(), ok);
227  SUMOReal length = attrs.getSUMORealReporting(MATSIM_ATTR_LENGTH, id.c_str(), ok); // override computed?
228  SUMOReal freeSpeed = attrs.getSUMORealReporting(MATSIM_ATTR_FREESPEED, id.c_str(), ok); //
229  SUMOReal capacity = attrs.getSUMORealReporting(MATSIM_ATTR_CAPACITY, id.c_str(), ok); // override permLanes?
230  SUMOReal permLanes = attrs.getSUMORealReporting(MATSIM_ATTR_PERMLANES, id.c_str(), ok);
231  //bool oneWay = attrs.getOptBoolReporting(MATSIM_ATTR_ONEWAY, id.c_str(), ok, true); // mandatory?
232  std::string modes = attrs.getOptStringReporting(MATSIM_ATTR_MODES, id.c_str(), ok, ""); // which values?
233  std::string origid = attrs.getOptStringReporting(MATSIM_ATTR_ORIGID, id.c_str(), ok, "");
234  NBNode* fromNode = myNodeCont.retrieve(fromNodeID);
235  NBNode* toNode = myNodeCont.retrieve(toNodeID);
236  if (fromNode == 0) {
237  WRITE_ERROR("Could not find from-node for edge '" + id + "'.");
238  }
239  if (toNode == 0) {
240  WRITE_ERROR("Could not find to-node for edge '" + id + "'.");
241  }
242  if (fromNode == 0 || toNode == 0) {
243  return;
244  }
245  if (myLanesFromCapacity) {
246  permLanes = myCapacity2Lanes.get(capacity);
247  }
248  NBEdge* edge = new NBEdge(id, fromNode, toNode, "", freeSpeed, (unsigned int) permLanes, -1, -1, -1);
249  if (myKeepEdgeLengths) {
250  edge->setLoadedLength(length);
251  }
252  if (!myEdgeCont.insert(edge)) {
253  delete edge;
254  WRITE_ERROR("Could not add edge '" + id + "'. Probably declared twice.");
255  }
256 }
257 
258 
259 /****************************************************************************/
260