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 /****************************************************************************/
16 // A reader for a SUMO network's projection description
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <string>
26 #include <map>
27 #include <fstream>
29 #include <utils/options/Option.h>
32 #include <utils/common/RGBColor.h>
33 #include <utils/common/StdDefs.h>
34 #include <utils/common/SysUtils.h>
35 #include <utils/geom/GeomHelper.h>
36 #include <utils/geom/Boundary.h>
37 #include <utils/geom/Position.h>
39 #include <utils/xml/XMLSubSys.h>
44 #include "PCNetProjectionLoader.h"
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
50 // ---------------------------------------------------------------------------
51 // static interface
52 // ---------------------------------------------------------------------------
53 void
54 PCNetProjectionLoader::load(const std::string& file, double scale) {
55  if (!FileHelpers::isReadable(file)) {
56  throw ProcessError("Could not open net-file '" + file + "'.");
57  }
58  // build handler and parser
59  PCNetProjectionLoader handler(scale);
60  handler.setFileName(file);
61  SUMOSAXReader* parser = XMLSubSys::getSAXReader(handler);
62  const long before = PROGRESS_BEGIN_TIME_MESSAGE("Parsing network projection from '" + file + "'");
63  if (!parser->parseFirst(file)) {
64  delete parser;
65  throw ProcessError("Can not read XML-file '" + handler.getFileName() + "'.");
66  }
67  // parse
68  while (parser->parseNext() && !handler.hasReadAll());
69  // clean up
70  PROGRESS_TIME_MESSAGE(before);
71  if (!handler.hasReadAll()) {
72  throw ProcessError("Could not find projection parameter in net.");
73  }
74  delete parser;
75 }
76 
77 
78 // ---------------------------------------------------------------------------
79 // handler methods
80 // ---------------------------------------------------------------------------
82  SUMOSAXHandler("sumo-network"),
83  myFoundLocation(false),
84  myScale(scale) {
85 }
86 
87 
89 
90 
91 void
93  const SUMOSAXAttributes& attrs) {
94  if (element != SUMO_TAG_LOCATION) {
95  return;
96  }
97  // @todo refactor parsing of location since its duplicated in NLHandler and PCNetProjectionLoader
98  myFoundLocation = true;
100  Boundary convBoundary = attrs.get<Boundary>(SUMO_ATTR_CONV_BOUNDARY, nullptr, myFoundLocation);
101  Boundary origBoundary = attrs.get<Boundary>(SUMO_ATTR_ORIG_BOUNDARY, nullptr, myFoundLocation);
102  std::string proj = attrs.get<std::string>(SUMO_ATTR_ORIG_PROJ, nullptr, myFoundLocation);
103  if (myFoundLocation) {
105  Position networkOffset = s[0] + Position(oc.getFloat("offset.x"), oc.getFloat("offset.y"));
106  GeoConvHelper::init(proj, networkOffset, origBoundary, convBoundary, myScale);
107  }
108 }
109 
110 
111 bool
113  return myFoundLocation;
114 }
115 
116 
117 /****************************************************************************/
118 
PCNetProjectionLoader
A reader for a SUMO network's projection description.
Definition: PCNetProjectionLoader.h:46
Boundary.h
PROGRESS_BEGIN_TIME_MESSAGE
#define PROGRESS_BEGIN_TIME_MESSAGE(msg)
Definition: MsgHandler.h:280
SUMOSAXHandler
SAX-handler base for SUMO-files.
Definition: SUMOSAXHandler.h:41
GeomConvHelper.h
SUMO_ATTR_NET_OFFSET
Definition: SUMOXMLDefinitions.h:827
OptionsCont.h
PCNetProjectionLoader::PCNetProjectionLoader
PCNetProjectionLoader(double scale)
Constructor.
Definition: PCNetProjectionLoader.cpp:81
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:492
MsgHandler.h
SUMOSAXReader
SAX-reader encapsulation containing binary reader.
Definition: SUMOSAXReader.h:55
PCNetProjectionLoader::hasReadAll
bool hasReadAll() const
Returns whether all needed values were read.
Definition: PCNetProjectionLoader.cpp:112
SUMOSAXReader::parseNext
bool parseNext()
Definition: SUMOSAXReader.cpp:175
FileHelpers.h
GeoConvHelper.h
PCNetProjectionLoader::~PCNetProjectionLoader
~PCNetProjectionLoader()
Destructor.
Definition: PCNetProjectionLoader.cpp:88
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
PCNetProjectionLoader::myFoundLocation
bool myFoundLocation
Information whether the parameter was read.
Definition: PCNetProjectionLoader.h:90
PROGRESS_TIME_MESSAGE
#define PROGRESS_TIME_MESSAGE(before)
Definition: MsgHandler.h:281
PositionVector
A list of positions.
Definition: PositionVector.h:45
GeoConvHelper::init
static bool init(OptionsCont &oc)
Initialises the processing and the final instance using the given options.
Definition: GeoConvHelper.cpp:200
RGBColor.h
SUMO_ATTR_ORIG_BOUNDARY
Definition: SUMOXMLDefinitions.h:829
SUMO_TAG_LOCATION
Definition: SUMOXMLDefinitions.h:263
PCNetProjectionLoader::myScale
double myScale
scaling of input coordinates (not given in the location element)
Definition: PCNetProjectionLoader.h:93
SysUtils.h
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:41
SUMO_ATTR_ORIG_PROJ
Definition: SUMOXMLDefinitions.h:830
PCPolyContainer.h
ProcessError
Definition: UtilExceptions.h:39
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
SUMOSAXReader::parseFirst
bool parseFirst(std::string systemID)
Definition: SUMOSAXReader.cpp:127
OptionsCont::getFloat
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
Definition: OptionsCont.cpp:208
Position.h
Option.h
SUMO_ATTR_CONV_BOUNDARY
Definition: SUMOXMLDefinitions.h:828
PCNetProjectionLoader::load
static void load(const std::string &file, double scale)
Loads network projection if wished.
Definition: PCNetProjectionLoader.cpp:54
GenericSAXHandler::getFileName
const std::string & getFileName() const
returns the current file name
Definition: GenericSAXHandler.cpp:74
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:92
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:100
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:56
SUMOXMLDefinitions.h
GenericSAXHandler::setFileName
void setFileName(const std::string &name)
Sets the current file name.
Definition: GenericSAXHandler.cpp:68
XMLSubSys.h