Eclipse SUMO - Simulation of Urban MObility
PCTypeDefHandler.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 handler for loading polygon type maps
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <string>
28 #include <utils/common/RGBColor.h>
32 #include <utils/shapes/Shape.h>
33 #include "PCTypeMap.h"
34 #include "PCTypeDefHandler.h"
35 
36 
37 // ===========================================================================
38 // method definitions
39 // ===========================================================================
42  myOptions(oc),
43  myContainer(con),
44  myOverwriteType(!oc.isDefault("type"))
45 {}
46 
47 
49 
50 
51 void
53  const SUMOSAXAttributes& attrs) {
54  if (element == SUMO_TAG_POLYTYPE) {
55  bool ok = true;
56  // get the id, report an error if not given or empty...
57  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
58  if (!ok) {
59  return;
60  }
61  const std::string icon = attrs.getOpt<std::string>(SUMO_ATTR_ICON, id.c_str(), ok, myOptions.getString("icon"));
62  const double layer = attrs.getOpt<double>(SUMO_ATTR_LAYER, id.c_str(), ok, myOptions.getFloat("layer"));
63  const bool discard = attrs.getOpt<bool>(SUMO_ATTR_DISCARD, id.c_str(), ok, false);
65  if (attrs.hasAttribute(SUMO_ATTR_FILL)) {
66  const std::string allowFillS = attrs.get<std::string>(SUMO_ATTR_FILL, id.c_str(), ok);
67  if (allowFillS == "force") {
68  allowFill = PCTypeMap::Filltype::FORCE;
69  } else {
71  }
72  }
73  const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_NAME, id.c_str(), ok, myOverwriteType ? myOptions.getString("type") : id);
74  const std::string prefix = attrs.getOpt<std::string>(SUMO_ATTR_PREFIX, id.c_str(), ok, myOptions.getString("prefix"));
75  const std::string color = attrs.getOpt<std::string>(SUMO_ATTR_COLOR, id.c_str(), ok, myOptions.getString("color"));
76  const double angle = attrs.getOpt<double>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
77  const std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
78  // !!! what about error handling?
79  if (!myContainer.add(id, type, color, prefix, icon, layer, angle, imgFile, discard, allowFill)) {
80  WRITE_ERRORF(TL("Could not add polygon type '%' (probably the id is already used)."), id);
81  }
82  }
83 }
84 
85 
86 /****************************************************************************/
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:305
#define TL(string)
Definition: MsgHandler.h:315
@ SUMO_TAG_POLYTYPE
type of polygon
@ SUMO_ATTR_ICON
icon
@ SUMO_ATTR_FILL
Fill the polygon.
@ SUMO_ATTR_NAME
@ SUMO_ATTR_LAYER
A layer number.
@ SUMO_ATTR_ANGLE
@ SUMO_ATTR_PREFIX
@ SUMO_ATTR_COLOR
A color information.
@ SUMO_ATTR_ID
@ SUMO_ATTR_DISCARD
@ SUMO_ATTR_IMGFILE
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)
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)
virtual ~PCTypeDefHandler()
Destructor.
OptionsCont & myOptions
The options (program settings)
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
bool myOverwriteType
whether to replace the loaded type with a custom value
PCTypeDefHandler(OptionsCont &oc, PCTypeMap &con)
Constructor.
PCTypeMap & myContainer
The type map to fill.
A storage for type mappings.
Definition: PCTypeMap.h:42
bool add(const std::string &id, const std::string &newid, const std::string &color, const std::string &prefix, const std::string &icon, double layer, double angle, const std::string &imgFile, bool discard, Filltype allowFill)
Adds a type definition.
Definition: PCTypeMap.cpp:48
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.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
SAX-handler base for SUMO-files.
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:47
static const double DEFAULT_ANGLE
Definition: Shape.h:46
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter