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>
63 #include <utils/xml/XMLSubSys.h>
64 #include "NILoader.h"
67 
68 #ifdef HAVE_INTERNAL
69 #include <internal/HeightMapper.h>
70 #endif
71 
72 #ifdef CHECK_MEMORY_LEAKS
73 #include <foreign/nvwa/debug_new.h>
74 #endif // CHECK_MEMORY_LEAKS
75 
76 
77 // ===========================================================================
78 // method definitions
79 // ===========================================================================
81  : myNetBuilder(nb) {}
82 
83 
85 
86 
87 void
89  // build the projection
90  if (!GeoConvHelper::init(oc)) {
91  throw ProcessError("Could not build projection!");
92  }
93  // load types first
94  NIXMLTypesHandler* handler =
96  loadXMLType(handler, oc.getStringVector("type-files"), "types");
97  // try to load height data so it is ready for use by other importers
98 #ifdef HAVE_INTERNAL
99  HeightMapper::loadIfSet(oc);
100 #endif
101  // try to load using different methods
112  loadXML(oc);
113  // check the loaded structures
114  if (myNetBuilder.getNodeCont().size() == 0) {
115  throw ProcessError("No nodes loaded.");
116  }
117  if (myNetBuilder.getEdgeCont().size() == 0) {
118  throw ProcessError("No edges loaded.");
119  }
120  // report loaded structures
121  WRITE_MESSAGE(" Import done:");
122  if (myNetBuilder.getDistrictCont().size() > 0) {
123  WRITE_MESSAGE(" " + toString(myNetBuilder.getDistrictCont().size()) + " districts loaded.");
124  }
125  WRITE_MESSAGE(" " + toString(myNetBuilder.getNodeCont().size()) + " nodes loaded.");
126  if (myNetBuilder.getTypeCont().size() > 0) {
127  WRITE_MESSAGE(" " + toString(myNetBuilder.getTypeCont().size()) + " types loaded.");
128  }
129  WRITE_MESSAGE(" " + toString(myNetBuilder.getEdgeCont().size()) + " edges loaded.");
131  WRITE_MESSAGE("The split of edges was performed " + toString(myNetBuilder.getEdgeCont().getNoEdgeSplits()) + " times.");
132  }
133  if (GeoConvHelper::getProcessing().usingGeoProjection()) {
134  WRITE_MESSAGE("Proj projection parameters used: '" + GeoConvHelper::getProcessing().getProjString() + "'.");
135  }
136 }
137 
138 
139 /* -------------------------------------------------------------------------
140  * file loading methods
141  * ----------------------------------------------------------------------- */
142 void
144  // load nodes
147  oc.getStringVector("node-files"), "nodes");
148  // load the edges
153  oc.getStringVector("edge-files"), "edges");
154  // load the connections
156  oc.getStringVector("connection-files"), "connections");
157  // load traffic lights (needs to come last, references loaded edges and connections)
160  oc.getStringVector("tllogic-files"), "traffic lights");
161 }
162 
163 
164 void
165 NILoader::loadXMLType(SUMOSAXHandler* handler, const std::vector<std::string>& files,
166  const std::string& type) {
167  // build parser
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  PROGRESS_BEGIN_MESSAGE("Parsing " + type + " from '" + *file + "'");
178  XMLSubSys::runParser(*handler, *file);
180  }
181  } catch (const XERCES_CPP_NAMESPACE::XMLException& toCatch) {
182  exceptMsg = TplConvert::_2str(toCatch.getMessage())
183  + "\n The " + type + " could not be loaded from '" + handler->getFileName() + "'.";
184  } catch (const ProcessError& toCatch) {
185  exceptMsg = std::string(toCatch.what()) + "\n The " + type + " could not be loaded from '" + handler->getFileName() + "'.";
186  } catch (...) {
187  exceptMsg = "The " + type + " could not be loaded from '" + handler->getFileName() + "'.";
188  }
189  delete handler;
190  if (exceptMsg != "") {
191  throw ProcessError(exceptMsg);
192  }
193 }
194 
195 
196 bool
197 NILoader::transformCoordinates(Position& from, bool includeInBoundary, GeoConvHelper* from_srs) {
198  Position orig(from);
199  bool ok = GeoConvHelper::getProcessing().x2cartesian(from, includeInBoundary);
200 #ifdef HAVE_INTERNAL
201  if (ok) {
202  const HeightMapper& hm = HeightMapper::get();
203  if (hm.ready()) {
204  if (from_srs != 0 && from_srs->usingGeoProjection()) {
205  from_srs->cartesian2geo(orig);
206  }
207  SUMOReal z = hm.getZ(orig);
208  from = Position(from.x(), from.y(), z);
209  }
210  }
211 #else
212  UNUSED_PARAMETER(from_srs);
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 /****************************************************************************/