SUMO - Simulation of Urban MObility
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-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 <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  i->second->writeXML(out, useGeo, zOffset);
179  }
180  out.close();
181 }
182 
183 
184 int
185 PCPolyContainer::getEnumIDFor(const std::string& key) {
186  if (myIDEnums.find(key) == myIDEnums.end()) {
187  myIDEnums[key] = 0;
188  return 0;
189  } else {
190  myIDEnums[key] = myIDEnums[key] + 1;
191  return myIDEnums[key];
192  }
193 }
194 
195 
196 
197 /****************************************************************************/
198 
void clear()
Removes all stored objects (polygons and pois)
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
unsigned int getNoPOIs()
Returns the number of stored pois.
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.
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:191
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)
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
Boundary myPruningBoundary
The boundary that described the rectangle within which an object must be in order to be kept...
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
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:54
std::map< std::string, int > myIDEnums
An id to int map for proper enumeration.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
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.
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...
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
#define SUMOReal
Definition: config.h:213
A point-of-interest.
PolyCont myPolyCont
The polygon container, accessed by the polygons&#39; ids.
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.