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