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-sim.org/
13 // Copyright (C) 2005-2014 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>
41 #include <utils/shapes/Polygon.h>
44 #include "PCPolyContainer.h"
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
55  const Boundary& pruningBoundary,
56  const std::vector<std::string>& removeByNames)
57  : myPruningBoundary(pruningBoundary), myDoPrune(prune),
58  myRemoveByNames(removeByNames) {}
59 
60 
62  clear();
63 }
64 
65 
66 bool
67 PCPolyContainer::insert(const std::string& id, Polygon* poly,
68  int layer, bool ignorePruning) {
69  // check whether the polygon lies within the wished area
70  // - if such an area was given
71  if (myDoPrune && !ignorePruning) {
72  Boundary b = poly->getShape().getBoxBoundary();
74  delete poly;
75  return false;
76  }
77  }
78  // check whether the polygon was named to be a removed one
79  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), id) != myRemoveByNames.end()) {
80  delete poly;
81  return false;
82  }
83  //
84  PolyCont::iterator i = myPolyCont.find(id);
85  if (i != myPolyCont.end()) {
86  WRITE_ERROR("Polygon '" + id + "' could not be added.");
87  delete poly;
88  return false;
89  }
90  myPolyCont[id] = poly;
91  myPolyLayerMap[poly] = layer;
92  return true;
93 }
94 
95 
96 bool
97 PCPolyContainer::insert(const std::string& id, PointOfInterest* poi,
98  int layer, bool ignorePruning) {
99  // check whether the poi lies within the wished area
100  // - if such an area was given
101  if (myDoPrune && !ignorePruning) {
102  if (!myPruningBoundary.around(*poi)) {
103  delete poi;
104  return false;
105  }
106  }
107  // check whether the polygon was named to be a removed one
108  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), id) != myRemoveByNames.end()) {
109  delete poi;
110  return false;
111  }
112  //
113  POICont::iterator i = myPOICont.find(id);
114  if (i != myPOICont.end()) {
115  WRITE_ERROR("POI '" + id + "' could not be added.");
116  delete poi;
117  return false;
118  }
119  myPOICont[id] = poi;
120  myPOILayerMap[poi] = layer;
121  return true;
122 }
123 
124 
125 bool
126 PCPolyContainer::containsPolygon(const std::string& id) {
127  return myPolyCont.find(id) != myPolyCont.end();
128 }
129 
130 
131 void
133  // polys
134  for (PolyCont::iterator i = myPolyCont.begin(); i != myPolyCont.end(); i++) {
135  delete(*i).second;
136  }
137  myPolyCont.clear();
138  myPolyLayerMap.clear();
139  // pois
140  for (POICont::iterator i = myPOICont.begin(); i != myPOICont.end(); i++) {
141  delete(*i).second;
142  }
143  myPOICont.clear();
144  myPOILayerMap.clear();
145 }
146 
147 
148 void
150  WRITE_MESSAGE(" " + toString(getNoPolygons()) + " polygons loaded.");
151  WRITE_MESSAGE(" " + toString(getNoPOIs()) + " pois loaded.");
152 }
153 
154 
155 void
156 PCPolyContainer::save(const std::string& file) {
158  out.writeXMLHeader("additional", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo-sim.org/xsd/additional_file.xsd\"");
159  // write polygons
160  for (PolyCont::iterator i = myPolyCont.begin(); i != myPolyCont.end(); ++i) {
161  Polygon* p = i->second;
162  out.openTag(SUMO_TAG_POLY);
166  out.writeAttr(SUMO_ATTR_FILL, p->getFill());
169  if (p->getAngle() != Shape::DEFAULT_ANGLE) {
171  }
172  if (p->getImgFile() != Shape::DEFAULT_IMG_FILE) {
174  }
175  const std::map<std::string, std::string>& attrs = p->getMap();
176  if (attrs.size() != 0) {
177  for (std::map<std::string, std::string>::const_iterator j = attrs.begin(); j != attrs.end(); ++j) {
178  out.openTag(SUMO_TAG_PARAM);
179  out.writeAttr(SUMO_ATTR_KEY, (*j).first);
180  out.writeAttr(SUMO_ATTR_VALUE, (*j).second);
181  out.closeTag();
182  }
183  }
184  out.closeTag();
185  }
186  // write pois
187  for (POICont::iterator i = myPOICont.begin(); i != myPOICont.end(); ++i) {
188  PointOfInterest* p = i->second;
189  out.openTag(SUMO_TAG_POI);
194  out.writeAttr(SUMO_ATTR_X, p->x());
195  out.writeAttr(SUMO_ATTR_Y, p->y());
196  if (p->getAngle() != Shape::DEFAULT_ANGLE) {
198  }
199  if (p->getImgFile() != Shape::DEFAULT_IMG_FILE) {
201  }
202  if (p->getWidth() != Shape::DEFAULT_IMG_WIDTH) {
204  }
205  if (p->getHeight() != Shape::DEFAULT_IMG_HEIGHT) {
207  }
208  const std::map<std::string, std::string>& attrs = p->getMap();
209  if (attrs.size() != 0) {
210  for (std::map<std::string, std::string>::const_iterator j = attrs.begin(); j != attrs.end(); ++j) {
211  out.openTag(SUMO_TAG_PARAM);
212  out.writeAttr(SUMO_ATTR_KEY, (*j).first);
213  out.writeAttr(SUMO_ATTR_VALUE, (*j).second);
214  out.closeTag();
215  }
216  }
217  out.closeTag();
218  }
219  out.close();
220 }
221 
222 
223 int
224 PCPolyContainer::getEnumIDFor(const std::string& key) {
225  if (myIDEnums.find(key) == myIDEnums.end()) {
226  myIDEnums[key] = 0;
227  return 0;
228  } else {
229  myIDEnums[key] = myIDEnums[key] + 1;
230  return myIDEnums[key];
231  }
232 }
233 
234 
235 
236 /****************************************************************************/
237