Eclipse 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-2019 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 
PCNetProjectionLoader
A reader for a SUMO network's projection description.
Definition: PCNetProjectionLoader.h:47
Boundary.h
SUMOSAXHandler
SAX-handler base for SUMO-files.
Definition: SUMOSAXHandler.h:42
GeomConvHelper.h
SUMO_ATTR_NET_OFFSET
Definition: SUMOXMLDefinitions.h:824
OptionsCont.h
PCNetProjectionLoader::PCNetProjectionLoader
PCNetProjectionLoader(double scale)
Constructor.
Definition: PCNetProjectionLoader.cpp:84
SUMOSAXAttributes::get
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
Definition: SUMOSAXAttributes.h:493
MsgHandler.h
SUMOSAXReader
SAX-reader encapsulation containing binary reader.
Definition: SUMOSAXReader.h:56
PCNetProjectionLoader::hasReadAll
bool hasReadAll() const
Returns whether all needed values were read.
Definition: PCNetProjectionLoader.cpp:115
SUMOSAXReader::parseNext
bool parseNext()
Definition: SUMOSAXReader.cpp:173
FileHelpers.h
GeoConvHelper.h
PCNetProjectionLoader::~PCNetProjectionLoader
~PCNetProjectionLoader()
Destructor.
Definition: PCNetProjectionLoader.cpp:91
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
PCNetProjectionLoader::myFoundLocation
bool myFoundLocation
Information whether the parameter was read.
Definition: PCNetProjectionLoader.h:91
PROGRESS_TIME_MESSAGE
#define PROGRESS_TIME_MESSAGE(before)
Definition: MsgHandler.h:243
PositionVector
A list of positions.
Definition: PositionVector.h:46
GeoConvHelper::init
static bool init(OptionsCont &oc)
Initialises the processing and the final instance using the given options.
Definition: GeoConvHelper.cpp:201
RGBColor.h
SUMO_ATTR_ORIG_BOUNDARY
Definition: SUMOXMLDefinitions.h:826
SysUtils::getCurrentMillis
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:39
SUMO_TAG_LOCATION
Definition: SUMOXMLDefinitions.h:264
PCNetProjectionLoader::myScale
double myScale
scaling of input coordinates (not given in the location element)
Definition: PCNetProjectionLoader.h:94
SysUtils.h
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
SUMO_ATTR_ORIG_PROJ
Definition: SUMOXMLDefinitions.h:827
PCPolyContainer.h
ProcessError
Definition: UtilExceptions.h:40
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:90
SUMOSAXReader::parseFirst
bool parseFirst(std::string systemID)
Definition: SUMOSAXReader.cpp:125
OptionsCont::getFloat
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
Definition: OptionsCont.cpp:209
Position.h
PROGRESS_BEGIN_MESSAGE
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:241
Option.h
SUMO_ATTR_CONV_BOUNDARY
Definition: SUMOXMLDefinitions.h:825
PCNetProjectionLoader::load
static void load(const std::string &file, double scale)
Loads network projection if wished.
Definition: PCNetProjectionLoader.cpp:55
GenericSAXHandler::getFileName
const std::string & getFileName() const
returns the current file name
Definition: GenericSAXHandler.cpp:75
FileHelpers::isReadable
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:49
PCNetProjectionLoader::myStartElement
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
Definition: PCNetProjectionLoader.cpp:95
config.h
GeomHelper.h
StdDefs.h
SUMOSAXReader.h
PCNetProjectionLoader.h
XMLSubSys::getSAXReader
static SUMOSAXReader * getSAXReader(SUMOSAXHandler &handler)
Builds a reader and assigns the handler to it.
Definition: XMLSubSys.cpp:101
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:57
SUMOXMLDefinitions.h
GenericSAXHandler::setFileName
void setFileName(const std::string &name)
Sets the current file name.
Definition: GenericSAXHandler.cpp:69
XMLSubSys.h