SUMO - Simulation of Urban MObility
PCPolyContainer.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2005-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // A storage for loaded polygons and pois
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include <algorithm>
35 #include <map>
37 #include <utils/common/ToString.h>
45 #include "PCPolyContainer.h"
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
52  const Boundary& pruningBoundary,
53  const std::vector<std::string>& removeByNames)
54  : myPruningBoundary(pruningBoundary), myDoPrune(prune),
55  myRemoveByNames(removeByNames) {}
56 
57 
59  myPolygons.clear();
60  myPOIs.clear();
61 }
62 
63 
64 bool
65 PCPolyContainer::add(SUMOPolygon* poly, bool ignorePruning) {
66  // check whether the polygon lies within the wished area
67  // - if such an area was given
68  if (myDoPrune && !ignorePruning) {
69  Boundary b = poly->getShape().getBoxBoundary();
71  delete poly;
72  return false;
73  }
74  }
75  // check whether the polygon was named to be a removed one
76  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), poly->getID()) != myRemoveByNames.end()) {
77  delete poly;
78  return false;
79  }
80  return ShapeContainer::add(poly);
81 }
82 
83 
84 bool
85 PCPolyContainer::add(PointOfInterest* poi, bool ignorePruning) {
86  // check whether the poi lies within the wished area
87  // - if such an area was given
88  if (myDoPrune && !ignorePruning) {
89  if (!myPruningBoundary.around(*poi)) {
90  delete poi;
91  return false;
92  }
93  }
94  // check whether the polygon was named to be a removed one
95  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), poi->getID()) != myRemoveByNames.end()) {
96  delete poi;
97  return false;
98  }
99  return ShapeContainer::add(poi);
100 }
101 
102 
103 void
104 PCPolyContainer::addLanePos(const std::string& poiID, const std::string& laneID, double lanePos, double lanePosLat) {
105  myLanePosPois[poiID] = LanePos(laneID, lanePos, lanePosLat);
106 }
107 
108 
109 void
110 PCPolyContainer::save(const std::string& file, bool useGeo) {
111  const GeoConvHelper& gch = GeoConvHelper::getFinal();
112  if (useGeo && !gch.usingGeoProjection()) {
113  WRITE_WARNING("Ignoring option \"proj.plain-geo\" because no geo-conversion has been defined");
114  useGeo = false;
115  }
117  out.writeXMLHeader("additional", "additional_file.xsd");
118  if (useGeo) {
120  } else if (gch.usingGeoProjection()) {
122  }
123  // write polygons
124  for (auto i : myPolygons) {
125  i.second->writeXML(out, useGeo);
126  }
127  // write pois
128  const double zOffset = OptionsCont::getOptions().getFloat("poi-layer-offset");
129  for (auto i : myPOIs) {
130  std::map<std::string, LanePos>::const_iterator it = myLanePosPois.find(i.first);
131  if (it == myLanePosPois.end()) {
132  i.second->writeXML(out, useGeo, zOffset);
133  } else {
134  i.second->writeXML(out, useGeo, zOffset, it->second.laneID, it->second.pos, it->second.posLat);
135  }
136  }
137  out.close();
138 }
139 
140 
142  // XXX duplicate of NWWriter_DlrNavteq::writeHeader()
143  const std::string OUTPUT_VERSION = "6.5"; // XXX duplicate of NWWriter_DlrNavteq OUTPUT_VERSION
144  time_t rawtime;
145  time(&rawtime);
146  char buffer [80];
147  strftime(buffer, 80, "on %c", localtime(&rawtime));
148  device << "# Generated " << buffer << " by " << oc.getFullName() << "\n";
149  device << "# Format matches Extraction version: V" << OUTPUT_VERSION << " \n";
150  std::stringstream tmp;
151  oc.writeConfiguration(tmp, true, false, false);
152  tmp.seekg(std::ios_base::beg);
153  std::string line;
154  while (!tmp.eof()) {
155  std::getline(tmp, line);
156  device << "# " << line << "\n";
157  }
158  device << "#\n";
159 }
160 
161 
162 void
163 PCPolyContainer::saveDlrTDP(const std::string& prefix) {
164  const OptionsCont& oc = OptionsCont::getOptions();
165  const GeoConvHelper& gch = GeoConvHelper::getFinal();
166  const bool haveGeo = gch.usingGeoProjection();
167  const double geoScale = pow(10.0f, haveGeo ? 5 : 2); // see NIImporter_DlrNavteq::GEO_SCALE
168  // write pois
169  OutputDevice& out = OutputDevice::getDevice(prefix + "_points_of_interest.txt");
170  out.setPrecision(0);
171  writeDlrTDPHeader(out, oc);
172  // write format specifier
173  out << "# ID\tCITY\tTYPE\tNAME\tgeo_x\tgeo_y\n";
174  int id = 0;
175  for (const auto& i : myPOIs) {
176  Position pos(*i.second);
177  gch.cartesian2geo(pos);
178  pos.mul(geoScale);
179  out << id << "\t";
180  out << "" << "\t";
181  out << i.second->getType() << "\t";
182  out << i.first << "\t";
183  out << pos.x() << "\t";
184  out << pos.y() << "\t";
185  id++;
186  }
187  out.close();
188  // write polygons
189  OutputDevice& out2 = OutputDevice::getDevice(prefix + "_polygons.txt");
190  out2.setPrecision(0);
191  writeDlrTDPHeader(out2, oc);
192  // write format specifier
193  out2 << "# ID\tCITY\tTYPE\tNAME\tgeo_x1\tgeo_y1\t[geo_x2 geo_y2 ...]\n";
194  id = 0;
195  for (const auto& i : myPolygons) {
196  out2 << id << "\t";
197  out2 << "" << "\t";
198  out2 << i.second->getType() << "\t";
199  out2 << i.first << "\t";
200 
201  PositionVector shape(i.second->getShape());
202  for (int i = 0; i < (int) shape.size(); i++) {
203  Position pos = shape[i];
204  gch.cartesian2geo(pos);
205  pos.mul(geoScale);
206  out2 << pos.x() << "\t";
207  out2 << pos.y() << "\t";
208  }
209  id++;
210  }
211  out2.close();
212 }
213 
214 
215 int
216 PCPolyContainer::getEnumIDFor(const std::string& key) {
217  return myIDEnums[key]++;
218 }
219 
220 
221 
222 /****************************************************************************/
223 
void close()
Closes the device and removes it from the dictionary.
static void writeLocation(OutputDevice &into)
writes the location element
PCPolyContainer(bool prune, const Boundary &pruningBoundary, const std::vector< std::string > &removeByNames)
Constructor.
bool add(SUMOPolygon *poly, bool ignorePruning=false)
Adds a polygon to the storage.
void saveDlrTDP(const std::string &prefix)
Saves the stored polygons and pois into the given file in dlrTDP format.
double y() const
Returns the y-position.
Definition: Position.h:67
#define OUTPUT_VERSION
bool usingGeoProjection() const
Returns whether a transformation from geo to metric coordinates will be performed.
POIs myPOIs
stored POIs
Polygons myPolygons
stored Polygons
double x() const
Returns the x-position.
Definition: Position.h:62
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.
const PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: SUMOPolygon.h:84
const std::string & getID() const
Returns the id.
Definition: Named.h:65
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:47
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
void addLanePos(const std::string &poiID, const std::string &laneID, double lanePos, double lanePosLat)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
void clear()
Removes all items from the container (deletes them, too)
void cartesian2geo(Position &cartesian) const
Converts the given cartesian (shifted) position to its geo (lat/long) representation.
Boundary myPruningBoundary
The boundary that described the rectangle within which an object must be in order to be kept...
static methods for processing the coordinates conversion for the current net
Definition: GeoConvHelper.h:59
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:45
int gPrecisionGeo
Definition: StdDefs.cpp:30
A list of positions.
std::map< std::string, LanePos > myLanePosPois
An id to pos map for lane pos specs.
~PCPolyContainer()
Destructor.
std::map< std::string, int > myIDEnums
An id to int map for proper enumeration.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
bool partialWithin(const AbstractPoly &poly, double offset=0) const
Returns whether the boundary is partially within the given polygon.
Definition: Boundary.cpp:291
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.
bool around(const Position &p, double offset=0) const
Returns whether the boundary contains the given coordinate.
Definition: Boundary.cpp:179
A storage for options typed value containers)
Definition: OptionsCont.h:98
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...
const std::string & getFullName() const
Definition: OptionsCont.h:657
virtual bool add(SUMOPolygon *poly, bool ignorePruning=false)
add polygon
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
void writeConfiguration(std::ostream &os, const bool filled, const bool complete, const bool addComments, const bool maskDoubleHyphen=false) const
Writes the configuration.
Boundary getBoxBoundary() const
Returns a boundary enclosing this list of lines.
A point-of-interest.
void mul(double val)
Multiplies both positions with the given value.
Definition: Position.h:112
bool myDoPrune
Information whether the pruning boundary shall be used.
static void writeDlrTDPHeader(OutputDevice &device, const OptionsCont &oc)