SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PCLoaderXML.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A reader for polygons and pois stored in XML-format
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <string>
35 #include <map>
36 #include <fstream>
38 #include <utils/options/Option.h>
39 #include <utils/common/StdDefs.h>
41 #include <utils/common/RGBColor.h>
42 #include <utils/geom/GeomHelper.h>
43 #include <utils/geom/Boundary.h>
44 #include <utils/geom/Position.h>
46 #include <utils/xml/XMLSubSys.h>
51 #include "PCLoaderXML.h"
52 
53 #ifdef CHECK_MEMORY_LEAKS
54 #include <foreign/nvwa/debug_new.h>
55 #endif // CHECK_MEMORY_LEAKS
56 
57 
58 // ===========================================================================
59 // method definitions
60 // ===========================================================================
61 // ---------------------------------------------------------------------------
62 // static interface
63 // ---------------------------------------------------------------------------
64 void
66  PCTypeMap& tm) {
67  if (!oc.isSet("xml-files")) {
68  return;
69  }
70  PCLoaderXML handler(toFill, tm, oc);
71  // parse file(s)
72  std::vector<std::string> files = oc.getStringVector("xml");
73  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
74  if (!FileHelpers::isReadable(*file)) {
75  throw ProcessError("Could not open xml-file '" + *file + "'.");
76  }
77  PROGRESS_BEGIN_MESSAGE("Parsing XML from '" + *file + "'");
78  if (!XMLSubSys::runParser(handler, *file)) {
79  throw ProcessError();
80  }
82  }
83 }
84 
85 
86 
87 // ---------------------------------------------------------------------------
88 // handler methods
89 // ---------------------------------------------------------------------------
91  PCTypeMap& tm, OptionsCont& oc)
92  : ShapeHandler("xml-poi-definition", toFill),
93  myTypeMap(tm), myOptions(oc) {}
94 
95 
97 
98 
99 void
101  const SUMOSAXAttributes& attrs) {
102  if (element != SUMO_TAG_POI && element != SUMO_TAG_POLY) {
103  return;
104  }
105  bool ok = true;
106  // get the id, report an error if not given or empty...
107  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
108  std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, myOptions.getString("type"));
109  if (!ok) {
110  return;
111  }
112  // patch the values
113  bool discard = myOptions.getBool("discard");
114  if (myTypeMap.has(type)) {
115  const PCTypeMap::TypeDef& def = myTypeMap.get(type);
116  discard = def.discard;
117  setDefaults(def.prefix, def.color, def.layer, def.allowFill);
118  } else {
120  myOptions.getFloat("layer"), myOptions.getBool("fill"));
121  }
122  if (!discard) {
123  if (element == SUMO_TAG_POI) {
124  addPOI(attrs, myOptions.isInStringVector("prune.keep-list", id), true);
125  }
126  if (element == SUMO_TAG_POLY) {
127  addPoly(attrs, myOptions.isInStringVector("prune.keep-list", id), true);
128  }
129  }
130 }
131 
132 
133 Position
134 PCLoaderXML::getLanePos(const std::string& poiID, const std::string& laneID, SUMOReal lanePos) {
135  static_cast<PCPolyContainer&>(myShapeContainer).addLanePos(poiID, laneID, lanePos);
136  return Position::INVALID;
137 }
138 
139 
140 /****************************************************************************/
141 
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) ...
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:172
PCLoaderXML(PCPolyContainer &toFill, PCTypeMap &tm, OptionsCont &oc)
Constructor.
Definition: PCLoaderXML.cpp:90
A single definition of values that shall be used for a given type.
Definition: PCTypeMap.h:66
A reader for polygons and pois stored in XML-format.
Definition: PCLoaderXML.h:58
bool isInStringVector(const std::string &optionName, const std::string &itemName)
Returns the named option is a list of string values containing the specified item.
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:58
PCTypeMap & myTypeMap
The type map to use.
Definition: PCLoaderXML.h:108
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
OptionsCont & myOptions
Settings to use.
Definition: PCLoaderXML.h:111
SUMOReal layer
The layer to use.
Definition: PCTypeMap.h:74
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
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:114
bool discard
Information whether polygons of this type shall be discarded.
Definition: PCTypeMap.h:76
A storage for loaded polygons and pois.
RGBColor color
The color to use.
Definition: PCTypeMap.h:70
The XML-Handler for network loading.
Definition: ShapeHandler.h:56
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
A storage for type mappings.
Definition: PCTypeMap.h:52
const TypeDef & get(const std::string &id)
Returns a type definition.
Definition: PCTypeMap.cpp:79
Encapsulated SAX-Attributes.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
bool has(const std::string &id)
Returns the information whether the named type is known.
Definition: PCTypeMap.cpp:85
void addPOI(const SUMOSAXAttributes &attrs, const bool ignorePruning, const bool useProcessing)
adds a POI
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
~PCLoaderXML()
Destructor.
Definition: PCLoaderXML.cpp:96
void addPoly(const SUMOSAXAttributes &attrs, const bool ignorePruning, const bool useProcessing)
adds a polygon
Position getLanePos(const std::string &poiID, const std::string &laneID, SUMOReal lanePos)
get position for a given laneID
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
std::string prefix
The prefix to use.
Definition: PCTypeMap.h:72
void setDefaults(const std::string &prefix, const RGBColor &color, const SUMOReal layer, const bool fill=false)
A storage for options typed value containers)
Definition: OptionsCont.h:99
ShapeContainer & myShapeContainer
Definition: ShapeHandler.h:110
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as XML.
Definition: PCLoaderXML.cpp:65
#define SUMOReal
Definition: config.h:214
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.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
bool allowFill
Information whether polygons of this type can be filled.
Definition: PCTypeMap.h:78
static const Position INVALID
Definition: Position.h:261
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.