SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PCPolyContainer.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A storage for loaded polygons and pois
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2005-2015 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 <algorithm>
36 #include <map>
38 #include <utils/common/ToString.h>
42 #include <utils/shapes/Polygon.h>
46 #include "PCPolyContainer.h"
47 
48 #ifdef CHECK_MEMORY_LEAKS
49 #include <foreign/nvwa/debug_new.h>
50 #endif // CHECK_MEMORY_LEAKS
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
57  const Boundary& pruningBoundary,
58  const std::vector<std::string>& removeByNames)
59  : myPruningBoundary(pruningBoundary), myDoPrune(prune),
60  myRemoveByNames(removeByNames) {}
61 
62 
64  clear();
65 }
66 
67 
68 bool
69 PCPolyContainer::insert(const std::string& id, Polygon* poly,
70  int layer, bool ignorePruning) {
71  // check whether the polygon lies within the wished area
72  // - if such an area was given
73  if (myDoPrune && !ignorePruning) {
74  Boundary b = poly->getShape().getBoxBoundary();
76  delete poly;
77  return false;
78  }
79  }
80  // check whether the polygon was named to be a removed one
81  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), id) != myRemoveByNames.end()) {
82  delete poly;
83  return false;
84  }
85  //
86  PolyCont::iterator i = myPolyCont.find(id);
87  if (i != myPolyCont.end()) {
88  WRITE_ERROR("Polygon '" + id + "' could not be added.");
89  delete poly;
90  return false;
91  }
92  myPolyCont[id] = poly;
93  myPolyLayerMap[poly] = layer;
94  return true;
95 }
96 
97 
98 bool
99 PCPolyContainer::insert(const std::string& id, PointOfInterest* poi,
100  int layer, bool ignorePruning) {
101  // check whether the poi lies within the wished area
102  // - if such an area was given
103  if (myDoPrune && !ignorePruning) {
104  if (!myPruningBoundary.around(*poi)) {
105  delete poi;
106  return false;
107  }
108  }
109  // check whether the polygon was named to be a removed one
110  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), id) != myRemoveByNames.end()) {
111  delete poi;
112  return false;
113  }
114  //
115  POICont::iterator i = myPOICont.find(id);
116  if (i != myPOICont.end()) {
117  WRITE_ERROR("POI '" + id + "' could not be added.");
118  delete poi;
119  return false;
120  }
121  myPOICont[id] = poi;
122  myPOILayerMap[poi] = layer;
123  return true;
124 }
125 
126 
127 bool
128 PCPolyContainer::containsPolygon(const std::string& id) {
129  return myPolyCont.find(id) != myPolyCont.end();
130 }
131 
132 
133 void
135  // polys
136  for (PolyCont::iterator i = myPolyCont.begin(); i != myPolyCont.end(); i++) {
137  delete(*i).second;
138  }
139  myPolyCont.clear();
140  myPolyLayerMap.clear();
141  // pois
142  for (POICont::iterator i = myPOICont.begin(); i != myPOICont.end(); i++) {
143  delete(*i).second;
144  }
145  myPOICont.clear();
146  myPOILayerMap.clear();
147 }
148 
149 
150 void
152  WRITE_MESSAGE(" " + toString(getNoPolygons()) + " polygons loaded.");
153  WRITE_MESSAGE(" " + toString(getNoPOIs()) + " pois loaded.");
154 }
155 
156 
157 void
158 PCPolyContainer::save(const std::string& file, bool useGeo) {
159  const GeoConvHelper& gch = GeoConvHelper::getFinal();
160  if (useGeo && !gch.usingGeoProjection()) {
161  WRITE_WARNING("Ignoring option \"proj.plain-geo\" because no geo-conversion has been defined");
162  useGeo = false;
163  }
165  out.writeXMLHeader("additional", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.dlr.de/xsd/additional_file.xsd\"");
166  if (useGeo) {
168  } else {
170  }
171  // write polygons
172  for (PolyCont::iterator i = myPolyCont.begin(); i != myPolyCont.end(); ++i) {
173  i->second->writeXML(out, useGeo);
174  }
175  // write pois
176  const SUMOReal zOffset = OptionsCont::getOptions().getFloat("poi-layer-offset");
177  for (POICont::iterator i = myPOICont.begin(); i != myPOICont.end(); ++i) {
178  PointOfInterest* p = i->second;
179  out.openTag(SUMO_TAG_POI);
183  out.writeAttr(SUMO_ATTR_LAYER, p->getLayer() + zOffset);
184  if (useGeo) {
185  Position pos(*p);
186  gch.cartesian2geo(pos);
187  out.writeAttr(SUMO_ATTR_LON, pos.x());
188  out.writeAttr(SUMO_ATTR_LAT, pos.y());
189  } else {
190  out.writeAttr(SUMO_ATTR_X, p->x());
191  out.writeAttr(SUMO_ATTR_Y, p->y());
192  }
193  if (p->getAngle() != Shape::DEFAULT_ANGLE) {
195  }
196  if (p->getImgFile() != Shape::DEFAULT_IMG_FILE) {
198  }
199  if (p->getWidth() != Shape::DEFAULT_IMG_WIDTH) {
201  }
202  if (p->getHeight() != Shape::DEFAULT_IMG_HEIGHT) {
204  }
205  for (std::map<std::string, std::string>::const_iterator j = p->getMap().begin(); j != p->getMap().end(); ++j) {
206  out.openTag(SUMO_TAG_PARAM);
207  out.writeAttr(SUMO_ATTR_KEY, (*j).first);
208  out.writeAttr(SUMO_ATTR_VALUE, (*j).second);
209  out.closeTag();
210  }
211  out.closeTag();
212  }
213  out.close();
214 }
215 
216 
217 int
218 PCPolyContainer::getEnumIDFor(const std::string& key) {
219  if (myIDEnums.find(key) == myIDEnums.end()) {
220  myIDEnums[key] = 0;
221  return 0;
222  } else {
223  myIDEnums[key] = myIDEnums[key] + 1;
224  return myIDEnums[key];
225  }
226 }
227 
228 
229 
230 /****************************************************************************/
231 
void clear()
Removes all stored objects (polygons and pois)
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
bool containsPolygon(const std::string &kidey)
Returns the information whether a polygon with the given key is in the container. ...
void close()
Closes the device and removes it from the dictionary.
static void writeLocation(OutputDevice &into)
writes the location element
const std::string & getImgFile() const
Returns the imgFile of the Shape.
Definition: Shape.h:101
void cartesian2geo(Position &cartesian) const
Converts the given cartesian (shifted) position to its geo (lat/long) representation.
unsigned int getNoPOIs()
Returns the number of stored pois.
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:152
bool around(const Position &p, SUMOReal offset=0) const
Returns whether the boundary contains the given coordinate.
Definition: Boundary.cpp:148
POICont myPOICont
The poi container, accessed by the pois&#39; ids.
PCPolyContainer(bool prune, const Boundary &pruningBoundary, const std::vector< std::string > &removeByNames)
Constructor.
A layer number.
const PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: Polygon.h:82
#define GEO_OUTPUT_ACCURACY
Definition: config.h:16
bool partialWithin(const AbstractPoly &poly, SUMOReal offset=0) const
Returns whether the boundary is partially within the given polygon.
Definition: Boundary.cpp:190
static std::string escapeXML(const std::string &orig)
Replaces the standard escapes by their XML entities.
bool insert(const std::string &id, Polygon *poly, int layer, bool ignorePruning=false)
Adds a polygon to the storage.
void save(const std::string &file, bool useGeo)
Saves the stored polygons into the given file.
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
const std::map< std::string, std::string > & getMap() const
Returns the inner key/value map.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
bool writeXMLHeader(const std::string &rootElement, const std::string &attrs="", const std::string &comment="")
Writes an XML header with optional configuration.
A 2D- or 3D-polygon.
Definition: Polygon.h:52
static const SUMOReal DEFAULT_ANGLE
Definition: Shape.h:151
Boundary myPruningBoundary
The boundary that described the rectangle within which an object must be in order to be kept...
const std::string & getID() const
Returns the id.
Definition: Named.h:60
void setPrecision(unsigned int precision=OUTPUT_ACCURACY)
Sets the precison or resets it to default.
bool usingGeoProjection() const
Returns whether a transformation from geo to metric coordinates will be performed.
static methods for processing the coordinates conversion for the current net
Definition: GeoConvHelper.h:60
static const SUMOReal DEFAULT_IMG_HEIGHT
Definition: Shape.h:154
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
unsigned int getNoPolygons()
Returns the number of stored polygons.
~PCPolyContainer()
Destructor.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
std::map< std::string, int > myIDEnums
An id to int map for proper enumeration.
const RGBColor & getColor() const
Returns the color of the Shape.
Definition: Shape.h:79
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
void report()
Reports how many polygons and pois were added.
std::map< PointOfInterest *, int > myPOILayerMap
A map from pois to the layers they are located in.
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
std::map< Polygon *, int > myPolyLayerMap
A map from polygons to the layers they are located in.
int getEnumIDFor(const std::string &key)
Retuns a unique id for a given name.
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
std::vector< std::string > myRemoveByNames
List of names of polygons/pois that shall be removed.
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
SUMOReal getWidth() const
Returns whether the image width of the POI.
SUMOReal getHeight() const
Returns whether the image hidth of the POI.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:215
A point-of-interest.
SUMOReal getLayer() const
Returns the layer of the Shape.
Definition: Shape.h:87
PolyCont myPolyCont
The polygon container, accessed by the polygons&#39; ids.
const std::string & getType() const
Returns the (abstract) type of the Shape.
Definition: Shape.h:71
bool myDoPrune
Information whether the pruning boundary shall be used.
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
Boundary getBoxBoundary() const
Returns a boundary enclosing this list of lines.
A color information.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
SUMOReal getAngle() const
Returns the angle of the Shape.
Definition: Shape.h:94
static const SUMOReal DEFAULT_IMG_WIDTH
Definition: Shape.h:153