SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NILoader.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Perfoms network import
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
13 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <string>
38 #include <utils/options/Option.h>
41 #include <utils/common/ToString.h>
42 #include <netbuild/NBTypeCont.h>
43 #include <netbuild/NBNodeCont.h>
44 #include <netbuild/NBEdgeCont.h>
45 #include <netbuild/NBNetBuilder.h>
62 #include <utils/xml/XMLSubSys.h>
63 #include "NILoader.h"
66 
67 #ifdef HAVE_MESOSIM
68 #include <internal/HeightMapper.h>
69 #endif
70 
71 #ifdef CHECK_MEMORY_LEAKS
72 #include <foreign/nvwa/debug_new.h>
73 #endif // CHECK_MEMORY_LEAKS
74 
75 
76 // ===========================================================================
77 // method definitions
78 // ===========================================================================
80  : myNetBuilder(nb) {}
81 
82 
84 
85 
86 void
88  // build the projection
89  if (!GeoConvHelper::init(oc)) {
90  throw ProcessError("Could not build projection!");
91  }
92  // load types first
93  NIXMLTypesHandler* handler =
95  loadXMLType(handler, oc.getStringVector("type-files"), "types");
96  // try to load height data so it is ready for use by other importers
97 #ifdef HAVE_MESOSIM
98  HeightMapper::loadIfSet(oc);
99 #endif
100  // try to load using different methods
111  loadXML(oc);
112  // check the loaded structures
113  if (myNetBuilder.getNodeCont().size() == 0) {
114  throw ProcessError("No nodes loaded.");
115  }
116  if (myNetBuilder.getEdgeCont().size() == 0) {
117  throw ProcessError("No edges loaded.");
118  }
119  // report loaded structures
120  WRITE_MESSAGE(" Import done:");
121  if (myNetBuilder.getDistrictCont().size() > 0) {
122  WRITE_MESSAGE(" " + toString(myNetBuilder.getDistrictCont().size()) + " districts loaded.");
123  }
124  WRITE_MESSAGE(" " + toString(myNetBuilder.getNodeCont().size()) + " nodes loaded.");
125  if (myNetBuilder.getTypeCont().size() > 0) {
126  WRITE_MESSAGE(" " + toString(myNetBuilder.getTypeCont().size()) + " types loaded.");
127  }
128  WRITE_MESSAGE(" " + toString(myNetBuilder.getEdgeCont().size()) + " edges loaded.");
130  WRITE_MESSAGE("The split of edges was performed " + toString(myNetBuilder.getEdgeCont().getNoEdgeSplits()) + " times.");
131  }
132  if (GeoConvHelper::getProcessing().usingGeoProjection()) {
133  WRITE_MESSAGE("Proj projection parameters used: '" + GeoConvHelper::getProcessing().getProjString() + "'.");
134  }
135 }
136 
137 
138 /* -------------------------------------------------------------------------
139  * file loading methods
140  * ----------------------------------------------------------------------- */
141 void
143  // load nodes
146  oc.getStringVector("node-files"), "nodes");
147  // load the edges
152  oc.getStringVector("edge-files"), "edges");
153  // load the connections
155  oc.getStringVector("connection-files"), "connections");
156  // load traffic lights (needs to come last, references loaded edges and connections)
159  oc.getStringVector("tllogic-files"), "traffic lights");
160 }
161 
162 
163 void
164 NILoader::loadXMLType(SUMOSAXHandler* handler, const std::vector<std::string> &files,
165  const std::string& type) {
166  // build parser
167  SAX2XMLReader* parser = XMLSubSys::getSAXReader(*handler);
168  std::string exceptMsg = "";
169  // start the parsing
170  try {
171  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
172  if (!FileHelpers::exists(*file)) {
173  WRITE_ERROR("Could not open " + type + "-file '" + *file + "'.");
174  exceptMsg = "Process Error";
175  continue;
176  }
177  handler->setFileName(*file);
178  PROGRESS_BEGIN_MESSAGE("Parsing " + type + " from '" + *file + "'");
179  parser->parse(file->c_str());
181  }
182  } catch (const XMLException& toCatch) {
183  exceptMsg = TplConvert<XMLCh>::_2str(toCatch.getMessage())
184  + "\n The " + type + " could not be loaded from '" + handler->getFileName() + "'.";
185  } catch (const ProcessError& toCatch) {
186  exceptMsg = std::string(toCatch.what()) + "\n The " + type + " could not be loaded from '" + handler->getFileName() + "'.";
187  } catch (...) {
188  exceptMsg = "The " + type + " could not be loaded from '" + handler->getFileName() + "'.";
189  }
190  delete parser;
191  delete handler;
192  if (exceptMsg != "") {
193  throw ProcessError(exceptMsg);
194  }
195 }
196 
197 
198 bool
199 NILoader::transformCoordinates(Position& from, bool includeInBoundary, GeoConvHelper* from_srs) {
200  Position orig(from);
201  bool ok = GeoConvHelper::getProcessing().x2cartesian(from, includeInBoundary);
202 #ifdef HAVE_MESOSIM
203  if (ok) {
204  const HeightMapper& hm = HeightMapper::get();
205  if (hm.ready()) {
206  if (from_srs != 0 && from_srs->usingGeoProjection()) {
207  from_srs->cartesian2geo(orig);
208  }
209  SUMOReal z = hm.getZ(orig);
210  from = Position(from.x(), from.y(), z);
211  }
212  }
213 #endif
214  return ok;
215 }
216 
217 
218 bool
219 NILoader::transformCoordinates(PositionVector& from, bool includeInBoundary, GeoConvHelper* from_srs) {
220  const SUMOReal maxLength = OptionsCont::getOptions().getFloat("geometry.max-segment-length");
221  if (maxLength > 0 && from.size() > 1) {
222  // transformation to cartesian coordinates must happen before we can check segment length
223  PositionVector copy = from;
224  for (int i = 0; i < (int) from.size(); i++) {
225  transformCoordinates(copy[i], false);
226  }
227  // check lengths and insert new points where needed (in the original
228  // coordinate system)
229  int inserted = 0;
230  for (int i = 0; i < (int)copy.size() - 1; i++) {
231  Position start = from[i + inserted];
232  Position end = from[i + inserted + 1];
233  SUMOReal length = copy[i].distanceTo(copy[i + 1]);
234  const Position step = (end - start) * (maxLength / length);
235  int steps = 0;
236  while (length > maxLength) {
237  length -= maxLength;
238  steps++;
239  from.insertAt(i + inserted + 1, start + (step * steps));
240  inserted++;
241  }
242  }
243  // now perform the transformation again so that height mapping can be
244  // performed for the new points
245  }
246  bool ok = true;
247  for (int i = 0; i < (int) from.size(); i++) {
248  ok = ok && transformCoordinates(from[i], includeInBoundary, from_srs);
249  }
250  return ok;
251 }
252 /****************************************************************************/