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.dev/sumo
3 // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // A reader for a SUMO network's projection description
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <string>
25 #include <map>
26 #include <fstream>
28 #include <utils/options/Option.h>
31 #include <utils/common/RGBColor.h>
32 #include <utils/common/StdDefs.h>
33 #include <utils/common/SysUtils.h>
34 #include <utils/geom/GeomHelper.h>
35 #include <utils/geom/Boundary.h>
36 #include <utils/geom/Position.h>
38 #include <utils/xml/XMLSubSys.h>
43 #include "PCNetProjectionLoader.h"
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
49 // ---------------------------------------------------------------------------
50 // static interface
51 // ---------------------------------------------------------------------------
52 void
53 PCNetProjectionLoader::load(const std::string& file, double scale) {
54  if (!FileHelpers::isReadable(file)) {
55  throw ProcessError(TLF("Could not open net-file '%'.", file));
56  }
57  // build handler and parser
58  PCNetProjectionLoader handler(scale);
59  handler.setFileName(file);
60  SUMOSAXReader* parser = XMLSubSys::getSAXReader(handler, true);
61  const long before = PROGRESS_BEGIN_TIME_MESSAGE("Parsing network projection from '" + file + "'");
62  if (!parser->parseFirst(file)) {
63  delete parser;
64  throw ProcessError(TLF("Can not read XML-file '%'.", handler.getFileName()));
65  }
66  // parse
67  while (parser->parseNext() && !handler.hasReadAll());
68  // clean up
69  PROGRESS_TIME_MESSAGE(before);
70  if (!handler.hasReadAll()) {
71  throw ProcessError(TL("Could not find projection parameter in net."));
72  }
73  delete parser;
74 }
75 
76 
77 // ---------------------------------------------------------------------------
78 // handler methods
79 // ---------------------------------------------------------------------------
81  SUMOSAXHandler("sumo-network"),
82  myFoundLocation(false),
83  myScale(scale) {
84 }
85 
86 
88 
89 
90 void
92  const SUMOSAXAttributes& attrs) {
93  if (element != SUMO_TAG_LOCATION) {
94  return;
95  }
96  // @todo refactor parsing of location since its duplicated in NLHandler and PCNetProjectionLoader
97  myFoundLocation = true;
99  Boundary convBoundary = attrs.get<Boundary>(SUMO_ATTR_CONV_BOUNDARY, nullptr, myFoundLocation);
100  Boundary origBoundary = attrs.get<Boundary>(SUMO_ATTR_ORIG_BOUNDARY, nullptr, myFoundLocation);
101  std::string proj = attrs.get<std::string>(SUMO_ATTR_ORIG_PROJ, nullptr, myFoundLocation);
102  if (myFoundLocation) {
104  Position networkOffset = s[0] + Position(oc.getFloat("offset.x"), oc.getFloat("offset.y"));
105  GeoConvHelper::init(proj, networkOffset, origBoundary, convBoundary, myScale);
106  }
107 }
108 
109 
110 bool
112  return myFoundLocation;
113 }
114 
115 
116 /****************************************************************************/
#define PROGRESS_BEGIN_TIME_MESSAGE(msg)
Definition: MsgHandler.h:301
#define TL(string)
Definition: MsgHandler.h:315
#define PROGRESS_TIME_MESSAGE(before)
Definition: MsgHandler.h:302
#define TLF(string,...)
Definition: MsgHandler.h:317
@ SUMO_TAG_LOCATION
@ SUMO_ATTR_CONV_BOUNDARY
@ SUMO_ATTR_NET_OFFSET
@ SUMO_ATTR_ORIG_BOUNDARY
@ SUMO_ATTR_ORIG_PROJ
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:51
void setFileName(const std::string &name)
Sets the current file name.
const std::string & getFileName() const
returns the current file name
static bool init(OptionsCont &oc)
Initialises the processing and the final instance using the given options.
A storage for options typed value containers)
Definition: OptionsCont.h:89
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:60
A reader for a SUMO network's projection description.
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
bool hasReadAll() const
Returns whether all needed values were read.
double myScale
scaling of input coordinates (not given in the location element)
PCNetProjectionLoader(double scale)
Constructor.
bool myFoundLocation
Information whether the parameter was read.
static void load(const std::string &file, double scale)
Loads network projection if wished.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
A list of positions.
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.
SAX-handler base for SUMO-files.
SAX-reader encapsulation containing binary reader.
Definition: SUMOSAXReader.h:53
bool parseFirst(std::string systemID)
Start parsing the given file using parseFirst of myXMLReader.
bool parseNext()
Continue a progressive parse started by parseFirst.
static SUMOSAXReader * getSAXReader(SUMOSAXHandler &handler, const bool isNet=false, const bool isRoute=false)
Builds a reader and assigns the handler to it.
Definition: XMLSubSys.cpp:132