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