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