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 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
53  const Boundary& pruningBoundary,
54  const std::vector<std::string>& removeByNames)
55  : myPruningBoundary(pruningBoundary), myDoPrune(prune),
56  myRemoveByNames(removeByNames) {}
57 
58 
60  myPolygons.clear();
61  myPOIs.clear();
62 }
63 
64 
65 bool
66 PCPolyContainer::add(SUMO::Polygon* poly, bool ignorePruning) {
67  // check whether the polygon lies within the wished area
68  // - if such an area was given
69  if (myDoPrune && !ignorePruning) {
70  Boundary b = poly->getShape().getBoxBoundary();
72  delete poly;
73  return false;
74  }
75  }
76  // check whether the polygon was named to be a removed one
77  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), poly->getID()) != myRemoveByNames.end()) {
78  delete poly;
79  return false;
80  }
81  return ShapeContainer::add(poly);
82 }
83 
84 
85 bool
86 PCPolyContainer::add(PointOfInterest* poi, bool ignorePruning) {
87  // check whether the poi lies within the wished area
88  // - if such an area was given
89  if (myDoPrune && !ignorePruning) {
90  if (!myPruningBoundary.around(*poi)) {
91  delete poi;
92  return false;
93  }
94  }
95  // check whether the polygon was named to be a removed one
96  if (find(myRemoveByNames.begin(), myRemoveByNames.end(), poi->getID()) != myRemoveByNames.end()) {
97  delete poi;
98  return false;
99  }
100  return ShapeContainer::add(poi);
101 }
102 
103 
104 void
105 PCPolyContainer::addLanePos(const std::string& poiID, const std::string& laneID, double lanePos) {
106  myLanePosPois[poiID] = std::make_pair(laneID, lanePos);
107 }
108 
109 
110 void
111 PCPolyContainer::save(const std::string& file, bool useGeo) {
112  const GeoConvHelper& gch = GeoConvHelper::getFinal();
113  if (useGeo && !gch.usingGeoProjection()) {
114  WRITE_WARNING("Ignoring option \"proj.plain-geo\" because no geo-conversion has been defined");
115  useGeo = false;
116  }
118  out.writeXMLHeader("additional", "additional_file.xsd");
119  if (useGeo) {
121  } else if (gch.usingGeoProjection()) {
123  }
124  // write polygons
125  for (std::map<std::string, SUMO::Polygon*>::const_iterator i = myPolygons.getMyMap().begin(); i != myPolygons.getMyMap().end(); ++i) {
126  i->second->writeXML(out, useGeo);
127  }
128  // write pois
129  const double zOffset = OptionsCont::getOptions().getFloat("poi-layer-offset");
130  for (std::map<std::string, PointOfInterest*>::const_iterator i = myPOIs.getMyMap().begin(); i != myPOIs.getMyMap().end(); ++i) {
131  std::map<std::string, std::pair<std::string, double> >::const_iterator it = myLanePosPois.find(i->first);
132  if (it == myLanePosPois.end()) {
133  i->second->writeXML(out, useGeo, zOffset);
134  } else {
135  i->second->writeXML(out, useGeo, zOffset, it->second.first, it->second.second);
136  }
137  }
138  out.close();
139 }
140 
141 
143  // XXX duplicate of NWWriter_DlrNavteq::writeHeader()
144  const std::string OUTPUT_VERSION = "6.5"; // XXX duplicate of NWWriter_DlrNavteq OUTPUT_VERSION
145  time_t rawtime;
146  time(&rawtime);
147  char buffer [80];
148  strftime(buffer, 80, "on %c", localtime(&rawtime));
149  device << "# Generated " << buffer << " by " << oc.getFullName() << "\n";
150  device << "# Format matches Extraction version: V" << OUTPUT_VERSION << " \n";
151  std::stringstream tmp;
152  oc.writeConfiguration(tmp, true, false, false);
153  tmp.seekg(std::ios_base::beg);
154  std::string line;
155  while (!tmp.eof()) {
156  std::getline(tmp, line);
157  device << "# " << line << "\n";
158  }
159  device << "#\n";
160 }
161 
162 
163 void
164 PCPolyContainer::saveDlrTDP(const std::string& prefix) {
165  const OptionsCont& oc = OptionsCont::getOptions();
166  const GeoConvHelper& gch = GeoConvHelper::getFinal();
167  const bool haveGeo = gch.usingGeoProjection();
168  const double geoScale = pow(10.0f, haveGeo ? 5 : 2); // see NIImporter_DlrNavteq::GEO_SCALE
169  // write pois
170  OutputDevice& out = OutputDevice::getDevice(prefix + "_points_of_interest.txt");
171  out.setPrecision(0);
172  writeDlrTDPHeader(out, oc);
173  // write format specifier
174  out << "# ID\tCITY\tTYPE\tNAME\tgeo_x\tgeo_y\n";
175  int id = 0;
176  for (std::map<std::string, PointOfInterest*>::const_iterator i = myPOIs.getMyMap().begin(); i != myPOIs.getMyMap().end(); ++i) {
177  Position pos(*(i->second));
178  gch.cartesian2geo(pos);
179  pos.mul(geoScale);
180  out << id << "\t";
181  out << "" << "\t";
182  out << i->second->getType() << "\t";
183  out << i->first << "\t";
184  out << pos.x() << "\t";
185  out << pos.y() << "\t";
186  id++;
187  }
188  out.close();
189  // write polygons
190  OutputDevice& out2 = OutputDevice::getDevice(prefix + "_polygons.txt");
191  out2.setPrecision(0);
192  writeDlrTDPHeader(out2, oc);
193  // write format specifier
194  out2 << "# ID\tCITY\tTYPE\tNAME\tgeo_x1\tgeo_y1\t[geo_x2 geo_y2 ...]\n";
195  id = 0;
196  for (std::map<std::string, SUMO::Polygon*>::const_iterator i = myPolygons.getMyMap().begin(); i != myPolygons.getMyMap().end(); ++i) {
197  out2 << id << "\t";
198  out2 << "" << "\t";
199  out2 << i->second->getType() << "\t";
200  out2 << i->first << "\t";
201 
202  PositionVector shape(i->second->getShape());
203  for (int i = 0; i < (int) shape.size(); i++) {
204  Position pos = shape[i];
205  gch.cartesian2geo(pos);
206  pos.mul(geoScale);
207  out2 << pos.x() << "\t";
208  out2 << pos.y() << "\t";
209  }
210  id++;
211  }
212  out2.close();
213 }
214 
215 
216 int
217 PCPolyContainer::getEnumIDFor(const std::string& key) {
218  return myIDEnums[key]++;
219 }
220 
221 
222 
223 /****************************************************************************/
224 
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.
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:68
#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:63
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 std::string & getID() const
Returns the id.
Definition: Named.h:66
const PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: Polygon.h:87
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:65
A 2D- or 3D-polygon.
Definition: Polygon.h:57
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: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:31
A list of positions.
const IDMap & getMyMap() const
~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 add(SUMO::Polygon *poly, bool ignorePruning=false)
Adds a polygon to the storage.
bool partialWithin(const AbstractPoly &poly, double offset=0) const
Returns whether the boundary is partially within the given polygon.
Definition: Boundary.cpp:224
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:180
void addLanePos(const std::string &poiID, const std::string &laneID, double lanePos)
A storage for options typed value containers)
Definition: OptionsCont.h:99
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:658
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
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:113
std::map< std::string, std::pair< std::string, double > > myLanePosPois
An id to pos map for lane pos specs.
bool myDoPrune
Information whether the pruning boundary shall be used.
static void writeDlrTDPHeader(OutputDevice &device, const OptionsCont &oc)