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-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 <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  myPolygons.clear();
65  myPOIs.clear();
66 }
67 
68 
69 bool
70 PCPolyContainer::add(SUMO::Polygon* poly, 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(), poly->getID()) != myRemoveByNames.end()) {
82  delete poly;
83  return false;
84  }
85  return ShapeContainer::add(poly);
86 }
87 
88 
89 bool
90 PCPolyContainer::add(PointOfInterest* poi, bool ignorePruning) {
91  // check whether the poi lies within the wished area
92  // - if such an area was given
93  if (myDoPrune && !ignorePruning) {
94  if (!myPruningBoundary.around(*poi)) {
95  delete poi;
96  return false;
97  }
98  }
99  // check whether the polygon was named to be a removed one
100  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), poi->getID()) != myRemoveByNames.end()) {
101  delete poi;
102  return false;
103  }
104  return ShapeContainer::add(poi);
105 }
106 
107 
108 void
109 PCPolyContainer::addLanePos(const std::string& poiID, const std::string& laneID, SUMOReal lanePos) {
110  myLanePosPois[poiID] = std::make_pair(laneID, lanePos);
111 }
112 
113 
114 void
115 PCPolyContainer::save(const std::string& file, bool useGeo) {
116  const GeoConvHelper& gch = GeoConvHelper::getFinal();
117  if (useGeo && !gch.usingGeoProjection()) {
118  WRITE_WARNING("Ignoring option \"proj.plain-geo\" because no geo-conversion has been defined");
119  useGeo = false;
120  }
122  out.writeXMLHeader("additional", "additional_file.xsd");
123  if (useGeo) {
125  } else if (gch.usingGeoProjection()) {
127  }
128  // write polygons
129  for (std::map<std::string, SUMO::Polygon*>::const_iterator i = myPolygons.getMyMap().begin(); i != myPolygons.getMyMap().end(); ++i) {
130  i->second->writeXML(out, useGeo);
131  }
132  // write pois
133  const SUMOReal zOffset = OptionsCont::getOptions().getFloat("poi-layer-offset");
134  for (std::map<std::string, PointOfInterest*>::const_iterator i = myPOIs.getMyMap().begin(); i != myPOIs.getMyMap().end(); ++i) {
135  std::map<std::string, std::pair<std::string, SUMOReal> >::const_iterator it = myLanePosPois.find(i->first);
136  if (it == myLanePosPois.end()) {
137  i->second->writeXML(out, useGeo, zOffset);
138  } else {
139  i->second->writeXML(out, useGeo, zOffset, it->second.first, it->second.second);
140  }
141  }
142  out.close();
143 }
144 
145 
147  // XXX duplicate of NWWriter_DlrNavteq::writeHeader()
148  const std::string OUTPUT_VERSION = "6.5"; // XXX duplicate of NWWriter_DlrNavteq OUTPUT_VERSION
149  time_t rawtime;
150  time(&rawtime);
151  char buffer [80];
152  strftime(buffer, 80, "on %c", localtime(&rawtime));
153  device << "# Generated " << buffer << " by " << oc.getFullName() << "\n";
154  device << "# Format matches Extraction version: V" << OUTPUT_VERSION << " \n";
155  std::stringstream tmp;
156  oc.writeConfiguration(tmp, true, false, false);
157  tmp.seekg(std::ios_base::beg);
158  std::string line;
159  while (!tmp.eof()) {
160  std::getline(tmp, line);
161  device << "# " << line << "\n";
162  }
163  device << "#\n";
164 }
165 
166 
167 void
168 PCPolyContainer::saveDlrTDP(const std::string& prefix) {
169  const OptionsCont& oc = OptionsCont::getOptions();
170  const GeoConvHelper& gch = GeoConvHelper::getFinal();
171  const bool haveGeo = gch.usingGeoProjection();
172  const SUMOReal geoScale = pow(10.0f, haveGeo ? 5 : 2); // see NIImporter_DlrNavteq::GEO_SCALE
173  // write pois
174  OutputDevice& out = OutputDevice::getDevice(prefix + "_points_of_interest.txt");
175  out.setPrecision(0);
176  writeDlrTDPHeader(out, oc);
177  // write format specifier
178  out << "# ID\tCITY\tTYPE\tNAME\tgeo_x\tgeo_y\n";
179  int id = 0;
180  for (std::map<std::string, PointOfInterest*>::const_iterator i = myPOIs.getMyMap().begin(); i != myPOIs.getMyMap().end(); ++i) {
181  Position pos(*(i->second));
182  gch.cartesian2geo(pos);
183  pos.mul(geoScale);
184  out << id << "\t";
185  out << "" << "\t";
186  out << i->second->getType() << "\t";
187  out << i->first << "\t";
188  out << pos.x() << "\t";
189  out << pos.y() << "\t";
190  id++;
191  }
192  out.close();
193  // write polygons
194  OutputDevice& out2 = OutputDevice::getDevice(prefix + "_polygons.txt");
195  out2.setPrecision(0);
196  writeDlrTDPHeader(out2, oc);
197  // write format specifier
198  out2 << "# ID\tCITY\tTYPE\tNAME\tgeo_x1\tgeo_y1\t[geo_x2 geo_y2 ...]\n";
199  id = 0;
200  for (std::map<std::string, SUMO::Polygon*>::const_iterator i = myPolygons.getMyMap().begin(); i != myPolygons.getMyMap().end(); ++i) {
201  out2 << id << "\t";
202  out2 << "" << "\t";
203  out2 << i->second->getType() << "\t";
204  out2 << i->first << "\t";
205 
206  PositionVector shape(i->second->getShape());
207  for (int i = 0; i < (int) shape.size(); i++) {
208  Position pos = shape[i];
209  gch.cartesian2geo(pos);
210  pos.mul(geoScale);
211  out2 << pos.x() << "\t";
212  out2 << pos.y() << "\t";
213  }
214  id++;
215  }
216  out2.close();
217 }
218 
219 
220 int
221 PCPolyContainer::getEnumIDFor(const std::string& key) {
222  return myIDEnums[key]++;
223 }
224 
225 
226 
227 /****************************************************************************/
228 
void close()
Closes the device and removes it from the dictionary.
static void writeLocation(OutputDevice &into)
writes the location element
void cartesian2geo(Position &cartesian) const
Converts the given cartesian (shifted) position to its geo (lat/long) representation.
bool around(const Position &p, SUMOReal offset=0) const
Returns whether the boundary contains the given coordinate.
Definition: Boundary.cpp:184
PCPolyContainer(bool prune, const Boundary &pruningBoundary, const std::vector< std::string > &removeByNames)
Constructor.
void saveDlrTDP(const std::string &prefix)
Saves the stored polygons and pois into the given file in dlrTDP format.
const PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: Polygon.h:87
#define OUTPUT_VERSION
bool partialWithin(const AbstractPoly &poly, SUMOReal offset=0) const
Returns whether the boundary is partially within the given polygon.
Definition: Boundary.cpp:228
POIs myPOIs
stored POIs
Polygons myPolygons
stored Polygons
void save(const std::string &file, bool useGeo)
Saves the stored polygons and pois into the given file.
void setPrecision(int precision=gPrecision)
Sets the precison or resets it to default.
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
const std::string & getFullName() const
Definition: OptionsCont.h:658
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
void writeConfiguration(std::ostream &os, const bool filled, const bool complete, const bool addComments, const bool maskDoubleHyphen=false) const
Writes the configuration.
A 2D- or 3D-polygon.
Definition: Polygon.h:57
void clear()
Removes all items from the container (deletes them, too)
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:66
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
bool writeXMLHeader(const std::string &rootElement, const std::string &schemaFile, std::map< SumoXMLAttr, std::string > attrs=std::map< SumoXMLAttr, std::string >())
Writes an XML header with optional configuration.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
virtual bool add(SUMO::Polygon *poly, bool ignorePruning=false)
int gPrecisionGeo
Definition: StdDefs.cpp:35
A list of positions.
~PCPolyContainer()
Destructor.
std::map< std::string, int > myIDEnums
An id to int map for proper enumeration.
bool add(SUMO::Polygon *poly, bool ignorePruning=false)
Adds a polygon to the storage.
const IDMap & getMyMap() const
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
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
void addLanePos(const std::string &poiID, const std::string &laneID, SUMOReal lanePos)
A storage for options typed value containers)
Definition: OptionsCont.h:99
std::map< std::string, std::pair< std::string, SUMOReal > > myLanePosPois
An id to pos map for lane pos specs.
std::vector< std::string > myRemoveByNames
List of names of polygons/pois that shall be removed.
void mul(SUMOReal val)
Multiplies both positions with the given value.
Definition: Position.h:113
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:208
A point-of-interest.
bool myDoPrune
Information whether the pruning boundary shall be used.
Boundary getBoxBoundary() const
Returns a boundary enclosing this list of lines.
static void writeDlrTDPHeader(OutputDevice &device, const OptionsCont &oc)