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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // A reader for polygons and pois stored in XML-format
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include <map>
35 #include <fstream>
37 #include <utils/options/Option.h>
38 #include <utils/common/StdDefs.h>
40 #include <utils/common/RGBColor.h>
41 #include <utils/geom/GeomHelper.h>
42 #include <utils/geom/Boundary.h>
43 #include <utils/geom/Position.h>
45 #include <utils/xml/XMLSubSys.h>
50 #include "PCLoaderXML.h"
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
56 // ---------------------------------------------------------------------------
57 // static interface
58 // ---------------------------------------------------------------------------
59 void
61  PCTypeMap& tm) {
62  if (!oc.isSet("xml-files")) {
63  return;
64  }
65  PCLoaderXML handler(toFill, tm, oc);
66  // parse file(s)
67  std::vector<std::string> files = oc.getStringVector("xml");
68  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
69  if (!FileHelpers::isReadable(*file)) {
70  throw ProcessError("Could not open xml-file '" + *file + "'.");
71  }
72  PROGRESS_BEGIN_MESSAGE("Parsing XML from '" + *file + "'");
73  if (!XMLSubSys::runParser(handler, *file)) {
74  throw ProcessError();
75  }
77  }
78 }
79 
80 
81 
82 // ---------------------------------------------------------------------------
83 // handler methods
84 // ---------------------------------------------------------------------------
86  PCTypeMap& tm, OptionsCont& oc)
87  : ShapeHandler("xml-poi-definition", toFill),
88  myTypeMap(tm), myOptions(oc) {}
89 
90 
92 
93 
94 void
96  const SUMOSAXAttributes& attrs) {
97  if (element != SUMO_TAG_POI && element != SUMO_TAG_POLY) {
98  return;
99  }
100  bool ok = true;
101  // get the id, report an error if not given or empty...
102  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
103  std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, myOptions.getString("type"));
104  if (!ok) {
105  return;
106  }
107  // patch the values
108  bool discard = myOptions.getBool("discard");
109  if (myTypeMap.has(type)) {
110  const PCTypeMap::TypeDef& def = myTypeMap.get(type);
111  discard = def.discard;
112  setDefaults(def.prefix, def.color, def.layer, def.allowFill);
113  } else {
115  myOptions.getFloat("layer"), myOptions.getBool("fill"));
116  }
117  if (!discard) {
118  if (element == SUMO_TAG_POI) {
119  addPOI(attrs, myOptions.isInStringVector("prune.keep-list", id), true);
120  }
121  if (element == SUMO_TAG_POLY) {
122  addPoly(attrs, myOptions.isInStringVector("prune.keep-list", id), true);
123  }
124  }
125 }
126 
127 
128 Position
129 PCLoaderXML::getLanePos(const std::string& poiID, const std::string& laneID, double lanePos, double lanePosLat) {
130  static_cast<PCPolyContainer&>(myShapeContainer).addLanePos(poiID, laneID, lanePos, lanePosLat);
131  return Position::INVALID;
132 }
133 
134 
135 /****************************************************************************/
136 
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:179
PCLoaderXML(PCPolyContainer &toFill, PCTypeMap &tm, OptionsCont &oc)
Constructor.
Definition: PCLoaderXML.cpp:85
A single definition of values that shall be used for a given type.
Definition: PCTypeMap.h:65
A reader for polygons and pois stored in XML-format.
Definition: PCLoaderXML.h:57
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:53
PCTypeMap & myTypeMap
The type map to use.
Definition: PCLoaderXML.h:107
OptionsCont & myOptions
Settings to use.
Definition: PCLoaderXML.h:110
double layer
The layer to use.
Definition: PCTypeMap.h:73
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:109
bool discard
Information whether polygons of this type shall be discarded.
Definition: PCTypeMap.h:75
A storage for loaded polygons and pois.
RGBColor color
The color to use.
Definition: PCTypeMap.h:69
The XML-Handler for network loading.
Definition: ShapeHandler.h:55
Position getLanePos(const std::string &poiID, const std::string &laneID, double lanePos, double lanePosLat)
get position for a given laneID
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
A storage for type mappings.
Definition: PCTypeMap.h:51
const TypeDef & get(const std::string &id)
Returns a type definition.
Definition: PCTypeMap.cpp:74
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:45
bool has(const std::string &id)
Returns the information whether the named type is known.
Definition: PCTypeMap.cpp:80
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:201
~PCLoaderXML()
Destructor.
Definition: PCLoaderXML.cpp:91
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:95
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:71
A storage for options typed value containers)
Definition: OptionsCont.h:98
ShapeContainer & myShapeContainer
Definition: ShapeHandler.h:116
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as XML.
Definition: PCLoaderXML.cpp:60
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:202
bool allowFill
Information whether polygons of this type can be filled.
Definition: PCTypeMap.h:77
static const Position INVALID
used to indicate that a position is valid
Definition: Position.h:277
begin/end of the description of a polygon