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-2013 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  if (oc.getBool("tls.discard-loaded") || oc.getBool("tls.discard-simple")) {
114  size_t removed = myNetBuilder.getTLLogicCont().getNumExtracted();
115  if (removed > 0) {
116  WRITE_MESSAGE(" Removed " + toString(removed) + " traffic lights before loading plain-XML");
117  }
118  }
119  loadXML(oc);
120  // check the loaded structures
121  if (myNetBuilder.getNodeCont().size() == 0) {
122  throw ProcessError("No nodes loaded.");
123  }
124  if (myNetBuilder.getEdgeCont().size() == 0) {
125  throw ProcessError("No edges loaded.");
126  }
127  // report loaded structures
128  WRITE_MESSAGE(" Import done:");
129  if (myNetBuilder.getDistrictCont().size() > 0) {
130  WRITE_MESSAGE(" " + toString(myNetBuilder.getDistrictCont().size()) + " districts loaded.");
131  }
132  WRITE_MESSAGE(" " + toString(myNetBuilder.getNodeCont().size()) + " nodes loaded.");
133  if (myNetBuilder.getTypeCont().size() > 0) {
134  WRITE_MESSAGE(" " + toString(myNetBuilder.getTypeCont().size()) + " types loaded.");
135  }
136  WRITE_MESSAGE(" " + toString(myNetBuilder.getEdgeCont().size()) + " edges loaded.");
138  WRITE_MESSAGE("The split of edges was performed " + toString(myNetBuilder.getEdgeCont().getNoEdgeSplits()) + " times.");
139  }
140  if (GeoConvHelper::getProcessing().usingGeoProjection()) {
141  WRITE_MESSAGE("Proj projection parameters used: '" + GeoConvHelper::getProcessing().getProjString() + "'.");
142  }
143 }
144 
145 
146 /* -------------------------------------------------------------------------
147  * file loading methods
148  * ----------------------------------------------------------------------- */
149 void
151  // load nodes
154  oc.getStringVector("node-files"), "nodes");
155  // load the edges
160  oc.getStringVector("edge-files"), "edges");
161  // load the connections
163  oc.getStringVector("connection-files"), "connections");
164  // load traffic lights (needs to come last, references loaded edges and connections)
167  oc.getStringVector("tllogic-files"), "traffic lights");
168 }
169 
170 
171 void
172 NILoader::loadXMLType(SUMOSAXHandler* handler, const std::vector<std::string>& files,
173  const std::string& type) {
174  // build parser
175  std::string exceptMsg = "";
176  // start the parsing
177  try {
178  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
179  if (!FileHelpers::exists(*file)) {
180  WRITE_ERROR("Could not open " + type + "-file '" + *file + "'.");
181  exceptMsg = "Process Error";
182  continue;
183  }
184  PROGRESS_BEGIN_MESSAGE("Parsing " + type + " from '" + *file + "'");
185  XMLSubSys::runParser(*handler, *file);
187  }
188  } catch (const XERCES_CPP_NAMESPACE::XMLException& toCatch) {
189  exceptMsg = TplConvert::_2str(toCatch.getMessage())
190  + "\n The " + type + " could not be loaded from '" + handler->getFileName() + "'.";
191  } catch (const ProcessError& toCatch) {
192  exceptMsg = std::string(toCatch.what()) + "\n The " + type + " could not be loaded from '" + handler->getFileName() + "'.";
193  } catch (...) {
194  exceptMsg = "The " + type + " could not be loaded from '" + handler->getFileName() + "'.";
195  }
196  delete handler;
197  if (exceptMsg != "") {
198  throw ProcessError(exceptMsg);
199  }
200 }
201 
202 
203 bool
204 NILoader::transformCoordinates(Position& from, bool includeInBoundary, GeoConvHelper* from_srs) {
205  Position orig(from);
206  bool ok = GeoConvHelper::getProcessing().x2cartesian(from, includeInBoundary);
207 #ifdef HAVE_INTERNAL
208  if (ok) {
209  const HeightMapper& hm = HeightMapper::get();
210  if (hm.ready()) {
211  if (from_srs != 0 && from_srs->usingGeoProjection()) {
212  from_srs->cartesian2geo(orig);
213  }
214  SUMOReal z = hm.getZ(orig);
215  from = Position(from.x(), from.y(), z);
216  }
217  }
218 #else
219  UNUSED_PARAMETER(from_srs);
220 #endif
221  return ok;
222 }
223 
224 
225 bool
226 NILoader::transformCoordinates(PositionVector& from, bool includeInBoundary, GeoConvHelper* from_srs) {
227  const SUMOReal maxLength = OptionsCont::getOptions().getFloat("geometry.max-segment-length");
228  if (maxLength > 0 && from.size() > 1) {
229  // transformation to cartesian coordinates must happen before we can check segment length
230  PositionVector copy = from;
231  for (int i = 0; i < (int) from.size(); i++) {
232  transformCoordinates(copy[i], false);
233  }
234  // check lengths and insert new points where needed (in the original
235  // coordinate system)
236  int inserted = 0;
237  for (int i = 0; i < (int)copy.size() - 1; i++) {
238  Position start = from[i + inserted];
239  Position end = from[i + inserted + 1];
240  SUMOReal length = copy[i].distanceTo(copy[i + 1]);
241  const Position step = (end - start) * (maxLength / length);
242  int steps = 0;
243  while (length > maxLength) {
244  length -= maxLength;
245  steps++;
246  from.insertAt(i + inserted + 1, start + (step * steps));
247  inserted++;
248  }
249  }
250  // now perform the transformation again so that height mapping can be
251  // performed for the new points
252  }
253  bool ok = true;
254  for (int i = 0; i < (int) from.size(); i++) {
255  ok = ok && transformCoordinates(from[i], includeInBoundary, from_srs);
256  }
257  return ok;
258 }
259 /****************************************************************************/