Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
PCLoaderXML.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/****************************************************************************/
21// A reader for polygons and pois stored in XML-format
22/****************************************************************************/
23#include <config.h>
24
25#include <string>
26#include <map>
27#include <fstream>
37#include <utils/geom/Boundary.h>
38#include <utils/geom/Position.h>
40#include <utils/xml/XMLSubSys.h>
43#include "PCLoaderXML.h"
44
45
46// ===========================================================================
47// method definitions
48// ===========================================================================
49// ---------------------------------------------------------------------------
50// static interface
51// ---------------------------------------------------------------------------
52void
54 PCTypeMap& tm) {
55 if (!oc.isSet("xml-files")) {
56 return;
57 }
58 PCLoaderXML handler(toFill, tm, oc);
59 // parse file(s)
60 std::vector<std::string> files = oc.getStringVector("xml");
61 for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
62 if (!FileHelpers::isReadable(*file)) {
63 throw ProcessError(TLF("Could not open xml-file '%'.", *file));
64 }
65 const long before = PROGRESS_BEGIN_TIME_MESSAGE("Parsing XML from '" + *file + "'");
66 if (!XMLSubSys::runParser(handler, *file)) {
67 throw ProcessError();
68 }
70 }
71}
72
73
74
75// ---------------------------------------------------------------------------
76// handler methods
77// ---------------------------------------------------------------------------
79 PCTypeMap& tm, OptionsCont& oc)
80 : ShapeHandler("xml-poi-definition", toFill),
81 myTypeMap(tm), myOptions(oc) {}
82
83
85
86
87void
89 const SUMOSAXAttributes& attrs) {
90 if (element != SUMO_TAG_POI && element != SUMO_TAG_POLY) {
91 return;
92 }
93 bool ok = true;
94 // get the id, report an error if not given or empty...
95 std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
96 std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, myOptions.getString("type"));
97 if (!ok) {
98 return;
99 }
100 // patch the values
101 bool discard = myOptions.getBool("discard");
102 if (myTypeMap.has(type)) {
103 const PCTypeMap::TypeDef& def = myTypeMap.get(type);
104 discard = def.discard;
106 } else {
108 myOptions.getString("icon"), myOptions.getFloat("layer"), myOptions.getBool("fill"));
109 }
110 if (!discard) {
111 if (element == SUMO_TAG_POI) {
112 addPOI(attrs, myOptions.isInStringVector("prune.keep-list", id), true);
113 }
114 if (element == SUMO_TAG_POLY) {
115 addPoly(attrs, myOptions.isInStringVector("prune.keep-list", id), true);
116 }
117 }
118}
119
120
122PCLoaderXML::getLanePos(const std::string& poiID, const std::string& laneID, double lanePos, bool friendlyPos, double lanePosLat) {
123 static_cast<PCPolyContainer&>(myShapeContainer).addLanePos(poiID, laneID, lanePos, friendlyPos, lanePosLat);
124 return Position::INVALID;
125}
126
127
128/****************************************************************************/
#define PROGRESS_BEGIN_TIME_MESSAGE(msg)
Definition MsgHandler.h:301
#define PROGRESS_TIME_MESSAGE(before)
Definition MsgHandler.h:302
#define TLF(string,...)
Definition MsgHandler.h:317
@ SUMO_TAG_POI
begin/end of the description of a Point of interest
@ SUMO_TAG_POLY
begin/end of the description of a polygon
@ SUMO_ATTR_TYPE
@ SUMO_ATTR_ID
static bool isReadable(std::string path)
Checks whether the given file is readable.
A storage for options typed value containers)
Definition OptionsCont.h:89
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
bool isInStringVector(const std::string &optionName, const std::string &itemName) const
Returns the named option is a list of string values containing the specified item.
A reader for polygons and pois stored in XML-format.
Definition PCLoaderXML.h:48
~PCLoaderXML()
Destructor.
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
PCTypeMap & myTypeMap
The type map to use.
Definition PCLoaderXML.h:98
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as XML.
PCLoaderXML(PCPolyContainer &toFill, PCTypeMap &tm, OptionsCont &oc)
Constructor.
Position getLanePos(const std::string &poiID, const std::string &laneID, double lanePos, bool friendlyPos, double lanePosLat)
get position for a given laneID (Has to be implemented in all child)
OptionsCont & myOptions
Settings to use.
A storage for loaded polygons and pois.
A storage for type mappings.
Definition PCTypeMap.h:42
const TypeDef & get(const std::string &id)
Returns a type definition.
Definition PCTypeMap.cpp:70
bool has(const std::string &id)
Returns the information whether the named type is known.
Definition PCTypeMap.cpp:76
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
static const Position INVALID
used to indicate that a position is valid
Definition Position.h:322
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition RGBColor.cpp:239
Encapsulated SAX-Attributes.
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue=T(), bool report=true) const
Tries to read given attribute assuming it is an int.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
The XML-Handler for network loading.
void addPOI(const SUMOSAXAttributes &attrs, const bool ignorePruning, const bool useProcessing)
adds a POI
void addPoly(const SUMOSAXAttributes &attrs, const bool ignorePruning, const bool useProcessing)
adds a polygon
ShapeContainer & myShapeContainer
reference to shape container in which all Shares are being added
void setDefaults(const std::string &prefix, const RGBColor &color, const std::string &icon, const double layer, const bool fill=false)
set default values
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false, const bool isRoute=false, const bool isExternal=false, const bool catchExceptions=true)
Runs the given handler on the given file; returns if everything's ok.
A single definition of values that shall be used for a given type.
Definition PCTypeMap.h:61
std::string icon
the icon to use
Definition PCTypeMap.h:69
bool discard
Information whether polygons of this type shall be discarded.
Definition PCTypeMap.h:77
std::string prefix
The prefix to use.
Definition PCTypeMap.h:67
double layer
The layer to use.
Definition PCTypeMap.h:71
RGBColor color
The color to use.
Definition PCTypeMap.h:65
Filltype allowFill
Information whether polygons of this type can be filled.
Definition PCTypeMap.h:79