SUMO - Simulation of Urban MObility
PCNetProjectionLoader.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // A reader for a SUMO network's projection description
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <string>
27 #include <map>
28 #include <fstream>
30 #include <utils/options/Option.h>
33 #include <utils/common/RGBColor.h>
34 #include <utils/common/StdDefs.h>
35 #include <utils/common/SysUtils.h>
36 #include <utils/geom/GeomHelper.h>
37 #include <utils/geom/Boundary.h>
38 #include <utils/geom/Position.h>
40 #include <utils/xml/XMLSubSys.h>
45 #include "PCNetProjectionLoader.h"
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
51 // ---------------------------------------------------------------------------
52 // static interface
53 // ---------------------------------------------------------------------------
54 void
55 PCNetProjectionLoader::load(const std::string& file, double scale) {
56  if (!FileHelpers::isReadable(file)) {
57  throw ProcessError("Could not open net-file '" + file + "'.");
58  }
59  // build handler and parser
60  PCNetProjectionLoader handler(scale);
61  handler.setFileName(file);
62  SUMOSAXReader* parser = XMLSubSys::getSAXReader(handler);
63  const long before = SysUtils::getCurrentMillis();
64  PROGRESS_BEGIN_MESSAGE("Parsing network projection from '" + file + "'");
65  if (!parser->parseFirst(file)) {
66  delete parser;
67  throw ProcessError("Can not read XML-file '" + handler.getFileName() + "'.");
68  }
69  // parse
70  while (parser->parseNext() && !handler.hasReadAll());
71  // clean up
72  PROGRESS_TIME_MESSAGE(before);
73  if (!handler.hasReadAll()) {
74  throw ProcessError("Could not find projection parameter in net.");
75  }
76  delete parser;
77 }
78 
79 
80 
81 // ---------------------------------------------------------------------------
82 // handler methods
83 // ---------------------------------------------------------------------------
85  SUMOSAXHandler("sumo-network"),
86  myFoundLocation(false),
87  myScale(scale) {
88 }
89 
90 
92 
93 
94 void
96  const SUMOSAXAttributes& attrs) {
97  if (element != SUMO_TAG_LOCATION) {
98  return;
99  }
100  // @todo refactor parsing of location since its duplicated in NLHandler and PCNetProjectionLoader
101  myFoundLocation = true;
103  Boundary convBoundary = attrs.get<Boundary>(SUMO_ATTR_CONV_BOUNDARY, nullptr, myFoundLocation);
104  Boundary origBoundary = attrs.get<Boundary>(SUMO_ATTR_ORIG_BOUNDARY, nullptr, myFoundLocation);
105  std::string proj = attrs.get<std::string>(SUMO_ATTR_ORIG_PROJ, nullptr, myFoundLocation);
106  if (myFoundLocation) {
108  Position networkOffset = s[0] + Position(oc.getFloat("offset.x"), oc.getFloat("offset.y"));
109  GeoConvHelper::init(proj, networkOffset, origBoundary, convBoundary, myScale);
110  }
111 }
112 
113 
114 bool
116  return myFoundLocation;
117 }
118 
119 
120 /****************************************************************************/
121 
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:47
double myScale
scaling of input coordinates (not given in the location element)
const std::string & getFileName() const
returns the current file name
static void load(const std::string &file, double scale)
Loads network projection if wished.
SAX-reader encapsulation containing binary reader.
Definition: SUMOSAXReader.h:51
bool myFoundLocation
Information whether the parameter was read.
SAX-handler base for SUMO-files.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
#define PROGRESS_TIME_MESSAGE(before)
Definition: MsgHandler.h:245
void setFileName(const std::string &name)
Sets the current file name.
Encapsulated SAX-Attributes.
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:39
A list of positions.
static SUMOSAXReader * getSAXReader(SUMOSAXHandler &handler)
Builds a reader and assigns the handler to it.
Definition: XMLSubSys.cpp:101
static bool init(OptionsCont &oc)
Initialises the processing and the final instance using the given options.
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:243
A reader for a SUMO network&#39;s projection description.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
bool parseFirst(std::string systemID)
A storage for options typed value containers)
Definition: OptionsCont.h:92
PCNetProjectionLoader(double scale)
Constructor.
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:39
bool hasReadAll() const
Returns whether all needed values were read.