SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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>
45 #include "PCPolyContainer.h"
46 
47 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
56  const Boundary& pruningBoundary,
57  const std::vector<std::string>& removeByNames)
58  : myPruningBoundary(pruningBoundary), myDoPrune(prune),
59  myRemoveByNames(removeByNames) {}
60 
61 
63  clear();
64 }
65 
66 
67 bool
68 PCPolyContainer::insert(const std::string& id, Polygon* poly,
69  int layer, bool ignorePruning) {
70  // check whether the polygon lies within the wished area
71  // - if such an area was given
72  if (myDoPrune && !ignorePruning) {
73  Boundary b = poly->getShape().getBoxBoundary();
75  delete poly;
76  return false;
77  }
78  }
79  // check whether the polygon was named to be a removed one
80  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), id) != myRemoveByNames.end()) {
81  delete poly;
82  return false;
83  }
84  //
85  PolyCont::iterator i = myPolyCont.find(id);
86  if (i != myPolyCont.end()) {
87  WRITE_ERROR("Polygon '" + id + "' could not be added.");
88  delete poly;
89  return false;
90  }
91  myPolyCont[id] = poly;
92  myPolyLayerMap[poly] = layer;
93  return true;
94 }
95 
96 
97 bool
98 PCPolyContainer::insert(const std::string& id, PointOfInterest* poi,
99  int layer, bool ignorePruning) {
100  // check whether the poi lies within the wished area
101  // - if such an area was given
102  if (myDoPrune && !ignorePruning) {
103  if (!myPruningBoundary.around(*poi)) {
104  delete poi;
105  return false;
106  }
107  }
108  // check whether the polygon was named to be a removed one
109  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), id) != myRemoveByNames.end()) {
110  delete poi;
111  return false;
112  }
113  //
114  POICont::iterator i = myPOICont.find(id);
115  if (i != myPOICont.end()) {
116  WRITE_ERROR("POI '" + id + "' could not be added.");
117  delete poi;
118  return false;
119  }
120  myPOICont[id] = poi;
121  myPOILayerMap[poi] = layer;
122  return true;
123 }
124 
125 
126 bool
127 PCPolyContainer::containsPolygon(const std::string& id) {
128  return myPolyCont.find(id) != myPolyCont.end();
129 }
130 
131 
132 void
134  // polys
135  for (PolyCont::iterator i = myPolyCont.begin(); i != myPolyCont.end(); i++) {
136  delete(*i).second;
137  }
138  myPolyCont.clear();
139  myPolyLayerMap.clear();
140  // pois
141  for (POICont::iterator i = myPOICont.begin(); i != myPOICont.end(); i++) {
142  delete(*i).second;
143  }
144  myPOICont.clear();
145  myPOILayerMap.clear();
146 }
147 
148 
149 void
151  WRITE_MESSAGE(" " + toString(getNoPolygons()) + " polygons loaded.");
152  WRITE_MESSAGE(" " + toString(getNoPOIs()) + " pois loaded.");
153 }
154 
155 
156 void
157 PCPolyContainer::save(const std::string& file, bool useGeo) {
158  const GeoConvHelper& gch = GeoConvHelper::getFinal();
159  if (useGeo && !gch.usingGeoProjection()) {
160  WRITE_WARNING("Ignoring option \"proj.plain-geo\" because no geo-conversion has been defined");
161  useGeo = false;
162  }
164  out.writeXMLHeader("additional", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.dlr.de/xsd/additional_file.xsd\"");
165  if (useGeo) {
167  } else {
169  }
170  // write polygons
171  for (PolyCont::iterator i = myPolyCont.begin(); i != myPolyCont.end(); ++i) {
172  i->second->writeXML(out, useGeo);
173  }
174  // write pois
175  for (POICont::iterator i = myPOICont.begin(); i != myPOICont.end(); ++i) {
176  PointOfInterest* p = i->second;
177  out.openTag(SUMO_TAG_POI);
182  if (useGeo) {
183  Position pos(*p);
184  gch.cartesian2geo(pos);
185  out.writeAttr(SUMO_ATTR_LON, pos.x());
186  out.writeAttr(SUMO_ATTR_LAT, pos.y());
187  } else {
188  out.writeAttr(SUMO_ATTR_X, p->x());
189  out.writeAttr(SUMO_ATTR_Y, p->y());
190  }
191  if (p->getAngle() != Shape::DEFAULT_ANGLE) {
193  }
194  if (p->getImgFile() != Shape::DEFAULT_IMG_FILE) {
196  }
197  if (p->getWidth() != Shape::DEFAULT_IMG_WIDTH) {
199  }
200  if (p->getHeight() != Shape::DEFAULT_IMG_HEIGHT) {
202  }
203  for (std::map<std::string, std::string>::const_iterator j = p->getMap().begin(); j != p->getMap().end(); ++j) {
204  out.openTag(SUMO_TAG_PARAM);
205  out.writeAttr(SUMO_ATTR_KEY, (*j).first);
206  out.writeAttr(SUMO_ATTR_VALUE, (*j).second);
207  out.closeTag();
208  }
209  out.closeTag();
210  }
211  out.close();
212 }
213 
214 
215 int
216 PCPolyContainer::getEnumIDFor(const std::string& key) {
217  if (myIDEnums.find(key) == myIDEnums.end()) {
218  myIDEnums[key] = 0;
219  return 0;
220  } else {
221  myIDEnums[key] = myIDEnums[key] + 1;
222  return myIDEnums[key];
223  }
224 }
225 
226 
227 
228 /****************************************************************************/
229 
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' 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.
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
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.
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' 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