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