Eclipse 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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
21 // A storage for loaded polygons and pois
22 /****************************************************************************/
23 #include <config.h>
24 
25 #include <string>
26 #include <algorithm>
27 #include <map>
29 #include <utils/common/ToString.h>
37 #include "PCPolyContainer.h"
38 
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
44  const Boundary& pruningBoundary,
45  const std::vector<std::string>& removeByNames)
46  : myPruningBoundary(pruningBoundary), myDoPrune(prune),
47  myRemoveByNames(removeByNames) {}
48 
49 
51  myPolygons.clear();
52  myPOIs.clear();
53 }
54 
55 
56 bool
57 PCPolyContainer::add(SUMOPolygon* poly, bool ignorePruning) {
58  // check whether the polygon lies within the wished area
59  // - if such an area was given
60  if (myDoPrune && !ignorePruning) {
61  Boundary b = poly->getShape().getBoxBoundary();
63  delete poly;
64  return false;
65  }
66  }
67  // check whether the polygon was named to be a removed one
68  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), poly->getID()) != myRemoveByNames.end()) {
69  delete poly;
70  return false;
71  }
72  return ShapeContainer::add(poly);
73 }
74 
75 
76 bool
77 PCPolyContainer::add(PointOfInterest* poi, bool ignorePruning) {
78  // check whether the poi lies within the wished area
79  // - if such an area was given
80  if (myDoPrune && !ignorePruning) {
81  if (!myPruningBoundary.around(*poi)) {
82  delete poi;
83  return false;
84  }
85  }
86  // check whether the polygon was named to be a removed one
87  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), poi->getID()) != myRemoveByNames.end()) {
88  delete poi;
89  return false;
90  }
91  return ShapeContainer::add(poi);
92 }
93 
94 
95 void
96 PCPolyContainer::addLanePos(const std::string& poiID, const std::string& laneID, double lanePos, double lanePosLat) {
97  myLanePosPois[poiID] = LanePos(laneID, lanePos, lanePosLat);
98 }
99 
100 
101 void
102 PCPolyContainer::save(const std::string& file, bool useGeo) {
103  const GeoConvHelper& gch = GeoConvHelper::getFinal();
104  if (useGeo && !gch.usingGeoProjection()) {
105  WRITE_WARNING("Ignoring option \"proj.plain-geo\" because no geo-conversion has been defined");
106  useGeo = false;
107  }
109  out.writeXMLHeader("additional", "additional_file.xsd");
110  if (useGeo) {
112  } else if (gch.usingGeoProjection()) {
114  }
115  // write polygons
116  for (auto i : myPolygons) {
117  i.second->writeXML(out, useGeo);
118  }
119  // write pois
120  const double zOffset = OptionsCont::getOptions().getFloat("poi-layer-offset");
121  for (auto i : myPOIs) {
122  std::map<std::string, LanePos>::const_iterator it = myLanePosPois.find(i.first);
123  if (it == myLanePosPois.end()) {
124  i.second->writeXML(out, useGeo, zOffset);
125  } else {
126  i.second->writeXML(out, useGeo, zOffset, it->second.laneID, it->second.pos, it->second.posLat);
127  }
128  }
129  out.close();
130 }
131 
132 
134  // XXX duplicate of NWWriter_DlrNavteq::writeHeader()
135  device << "# Format matches Extraction version: V6.5 \n";
136  std::stringstream tmp;
137  oc.writeConfiguration(tmp, true, false, false);
138  tmp.seekg(std::ios_base::beg);
139  std::string line;
140  while (!tmp.eof()) {
141  std::getline(tmp, line);
142  device << "# " << line << "\n";
143  }
144  device << "#\n";
145 }
146 
147 
148 void
149 PCPolyContainer::saveDlrTDP(const std::string& prefix) {
150  const OptionsCont& oc = OptionsCont::getOptions();
151  const GeoConvHelper& gch = GeoConvHelper::getFinal();
152  const bool haveGeo = gch.usingGeoProjection();
153  const double geoScale = pow(10.0f, haveGeo ? 5 : 2); // see NIImporter_DlrNavteq::GEO_SCALE
154  // write pois
155  OutputDevice& out = OutputDevice::getDevice(prefix + "_points_of_interest.txt");
156  out.setPrecision(0);
157  writeDlrTDPHeader(out, oc);
158  // write format specifier
159  out << "# ID\tCITY\tTYPE\tNAME\tgeo_x\tgeo_y\n";
160  int id = 0;
161  for (const auto& i : myPOIs) {
162  Position pos(*i.second);
163  gch.cartesian2geo(pos);
164  pos.mul(geoScale);
165  out << id << "\t";
166  out << "" << "\t";
167  out << i.second->getShapeType() << "\t";
168  out << i.first << "\t";
169  out << pos.x() << "\t";
170  out << pos.y() << "\t";
171  id++;
172  }
173  out.close();
174  // write polygons
175  OutputDevice& out2 = OutputDevice::getDevice(prefix + "_polygons.txt");
176  out2.setPrecision(0);
177  writeDlrTDPHeader(out2, oc);
178  // write format specifier
179  out2 << "# ID\tCITY\tTYPE\tNAME\tgeo_x1\tgeo_y1\t[geo_x2 geo_y2 ...]\n";
180  id = 0;
181  for (const auto& i : myPolygons) {
182  out2 << id << "\t";
183  out2 << "" << "\t";
184  out2 << i.second->getShapeType() << "\t";
185  out2 << i.first << "\t";
186 
187  PositionVector shape(i.second->getShape());
188  for (int i = 0; i < (int) shape.size(); i++) {
189  Position pos = shape[i];
190  gch.cartesian2geo(pos);
191  pos.mul(geoScale);
192  out2 << pos.x() << "\t";
193  out2 << pos.y() << "\t";
194  }
195  id++;
196  }
197  out2.close();
198 }
199 
200 
201 int
202 PCPolyContainer::getEnumIDFor(const std::string& key) {
203  return myIDEnums[key]++;
204 }
205 
206 
207 /****************************************************************************/
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:276
int gPrecisionGeo
Definition: StdDefs.cpp:26
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
bool partialWithin(const AbstractPoly &poly, double offset=0) const
Returns whether the boundary is partially within the given polygon.
Definition: Boundary.cpp:289
bool around(const Position &p, double offset=0) const
Returns whether the AbstractPoly the given coordinate.
Definition: Boundary.cpp:171
static methods for processing the coordinates conversion for the current net
Definition: GeoConvHelper.h:53
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.
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
bool usingGeoProjection() const
Returns whether a transformation from geo to metric coordinates will be performed.
const std::string & getID() const
Returns the id.
Definition: Named.h:73
void clear()
Removes all items from the container (deletes them, too)
A storage for options typed value containers)
Definition: OptionsCont.h:89
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
void writeConfiguration(std::ostream &os, const bool filled, const bool complete, const bool addComments, const bool inComment=false) const
Writes the configuration.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.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.
void close()
Closes the device and removes it from the dictionary.
void setPrecision(int precision=gPrecision)
Sets the precison or resets it to default.
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
std::map< std::string, LanePos > myLanePosPois
An id to pos map for lane pos specs.
void addLanePos(const std::string &poiID, const std::string &laneID, double lanePos, double lanePosLat)
static void writeDlrTDPHeader(OutputDevice &device, const OptionsCont &oc)
bool myDoPrune
Information whether the pruning boundary shall be used.
~PCPolyContainer()
Destructor.
void save(const std::string &file, bool useGeo)
Saves the stored polygons and pois into the given file.
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.
std::vector< std::string > myRemoveByNames
List of names of polygons/pois that shall be removed.
PCPolyContainer(bool prune, const Boundary &pruningBoundary, const std::vector< std::string > &removeByNames)
Constructor.
std::map< std::string, int > myIDEnums
An id to int map for proper enumeration.
int getEnumIDFor(const std::string &key)
Retuns a unique id for a given name.
Boundary myPruningBoundary
The boundary that described the rectangle within which an object must be in order to be kept.
A point-of-interest.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36
double x() const
Returns the x-position.
Definition: Position.h:54
void mul(double val)
Multiplies both positions with the given value.
Definition: Position.h:104
double y() const
Returns the y-position.
Definition: Position.h:59
A list of positions.
Boundary getBoxBoundary() const
Returns a boundary enclosing this list of lines.
const PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: SUMOPolygon.h:82
POIs myPOIs
stored POIs
virtual bool add(SUMOPolygon *poly, bool ignorePruning=false)
add polygon
Polygons myPolygons
stored Polygons