SUMO - Simulation of Urban MObility
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-2017 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 
54 // ===========================================================================
55 // method definitions
56 // ===========================================================================
57 // ---------------------------------------------------------------------------
58 // static interface
59 // ---------------------------------------------------------------------------
60 void
62  PCTypeMap& tm) {
63  if (!oc.isSet("xml-files")) {
64  return;
65  }
66  PCLoaderXML handler(toFill, tm, oc);
67  // parse file(s)
68  std::vector<std::string> files = oc.getStringVector("xml");
69  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
70  if (!FileHelpers::isReadable(*file)) {
71  throw ProcessError("Could not open xml-file '" + *file + "'.");
72  }
73  PROGRESS_BEGIN_MESSAGE("Parsing XML from '" + *file + "'");
74  if (!XMLSubSys::runParser(handler, *file)) {
75  throw ProcessError();
76  }
78  }
79 }
80 
81 
82 
83 // ---------------------------------------------------------------------------
84 // handler methods
85 // ---------------------------------------------------------------------------
87  PCTypeMap& tm, OptionsCont& oc)
88  : ShapeHandler("xml-poi-definition", toFill),
89  myTypeMap(tm), myOptions(oc) {}
90 
91 
93 
94 
95 void
97  const SUMOSAXAttributes& attrs) {
98  if (element != SUMO_TAG_POI && element != SUMO_TAG_POLY) {
99  return;
100  }
101  bool ok = true;
102  // get the id, report an error if not given or empty...
103  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
104  std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, myOptions.getString("type"));
105  if (!ok) {
106  return;
107  }
108  // patch the values
109  bool discard = myOptions.getBool("discard");
110  if (myTypeMap.has(type)) {
111  const PCTypeMap::TypeDef& def = myTypeMap.get(type);
112  discard = def.discard;
113  setDefaults(def.prefix, def.color, def.layer, def.allowFill);
114  } else {
116  myOptions.getFloat("layer"), myOptions.getBool("fill"));
117  }
118  if (!discard) {
119  if (element == SUMO_TAG_POI) {
120  addPOI(attrs, myOptions.isInStringVector("prune.keep-list", id), true);
121  }
122  if (element == SUMO_TAG_POLY) {
123  addPoly(attrs, myOptions.isInStringVector("prune.keep-list", id), true);
124  }
125  }
126 }
127 
128 
129 Position
130 PCLoaderXML::getLanePos(const std::string& poiID, const std::string& laneID, double lanePos) {
131  static_cast<PCPolyContainer&>(myShapeContainer).addLanePos(poiID, laneID, lanePos);
132  return Position::INVALID;
133 }
134 
135 
136 /****************************************************************************/
137 
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:168
PCLoaderXML(PCPolyContainer &toFill, PCTypeMap &tm, OptionsCont &oc)
Constructor.
Definition: PCLoaderXML.cpp:86
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.
void setDefaults(const std::string &prefix, const RGBColor &color, const double layer, const bool fill=false)
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:54
PCTypeMap & myTypeMap
The type map to use.
Definition: PCLoaderXML.h:108
OptionsCont & myOptions
Settings to use.
Definition: PCLoaderXML.h:111
double layer
The layer to use.
Definition: PCTypeMap.h:74
begin/end of the description of a polygon
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:110
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
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:52
const TypeDef & get(const std::string &id)
Returns a type definition.
Definition: PCTypeMap.cpp:75
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:46
bool has(const std::string &id)
Returns the information whether the named type is known.
Definition: PCTypeMap.cpp:81
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:202
~PCLoaderXML()
Destructor.
Definition: PCLoaderXML.cpp:92
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:96
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:72
Position getLanePos(const std::string &poiID, const std::string &laneID, double lanePos)
get position for a given laneID
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:61
#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
used to indicate that a position is valid
Definition: Position.h:278
begin/end of the description of a polygon