Libosmium  2.8.0
Fast and flexible C++ library for working with OpenStreetMap data
geos.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_GEOM_GEOS_HPP
2 #define OSMIUM_GEOM_GEOS_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2016 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
45 #include <memory>
46 #include <string>
47 #include <utility>
48 
49 #include <geos/geom/Coordinate.h>
50 #include <geos/geom/CoordinateSequence.h>
51 #include <geos/geom/CoordinateSequenceFactory.h>
52 #include <geos/geom/GeometryFactory.h>
53 #include <geos/geom/LinearRing.h>
54 #include <geos/geom/MultiPolygon.h>
55 #include <geos/geom/Point.h>
56 #include <geos/geom/Polygon.h>
57 #include <geos/geom/PrecisionModel.h>
58 #include <geos/util/GEOSException.h>
59 
60 #include <osmium/geom/factory.hpp>
63 
64 // MSVC doesn't support throw_with_nested yet
65 #ifdef _MSC_VER
66 # define THROW throw
67 #else
68 # define THROW std::throw_with_nested
69 #endif
70 
71 namespace osmium {
72 
74 
75  geos_geometry_error(const char* message) :
76  geometry_error(std::string("geometry creation failed in GEOS library: ") + message) {
77  }
78 
79  }; // struct geos_geometry_error
80 
81  namespace geom {
82 
83  namespace detail {
84 
85  class GEOSFactoryImpl {
86 
87  std::unique_ptr<const geos::geom::PrecisionModel> m_precision_model;
88  std::unique_ptr<geos::geom::GeometryFactory> m_our_geos_factory;
89  geos::geom::GeometryFactory* m_geos_factory;
90 
91  std::unique_ptr<geos::geom::CoordinateSequence> m_coordinate_sequence;
92  std::vector<std::unique_ptr<geos::geom::LinearRing>> m_rings;
93  std::vector<std::unique_ptr<geos::geom::Polygon>> m_polygons;
94 
95  public:
96 
97  using point_type = std::unique_ptr<geos::geom::Point>;
98  using linestring_type = std::unique_ptr<geos::geom::LineString>;
99  using polygon_type = std::unique_ptr<geos::geom::Polygon>;
100  using multipolygon_type = std::unique_ptr<geos::geom::MultiPolygon>;
101  using ring_type = std::unique_ptr<geos::geom::LinearRing>;
102 
103  explicit GEOSFactoryImpl(int /* srid */, geos::geom::GeometryFactory& geos_factory) :
104  m_precision_model(nullptr),
105  m_our_geos_factory(nullptr),
106  m_geos_factory(&geos_factory) {
107  }
108 
113  OSMIUM_DEPRECATED explicit GEOSFactoryImpl(int /* srid */, int srid) :
114  m_precision_model(new geos::geom::PrecisionModel),
115  m_our_geos_factory(new geos::geom::GeometryFactory(m_precision_model.get(), srid)),
116  m_geos_factory(m_our_geos_factory.get()) {
117  }
118 
119  explicit GEOSFactoryImpl(int srid) :
120  m_precision_model(new geos::geom::PrecisionModel),
121  m_our_geos_factory(new geos::geom::GeometryFactory(m_precision_model.get(), srid)),
122  m_geos_factory(m_our_geos_factory.get()) {
123  }
124 
125  /* Point */
126 
127  point_type make_point(const osmium::geom::Coordinates& xy) const {
128  try {
129  return point_type(m_geos_factory->createPoint(geos::geom::Coordinate(xy.x, xy.y)));
130  } catch (geos::util::GEOSException& e) {
132  }
133  }
134 
135  /* LineString */
136 
137  void linestring_start() {
138  try {
139  m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
140  } catch (geos::util::GEOSException& e) {
142  }
143  }
144 
145  void linestring_add_location(const osmium::geom::Coordinates& xy) {
146  try {
147  m_coordinate_sequence->add(geos::geom::Coordinate(xy.x, xy.y));
148  } catch (geos::util::GEOSException& e) {
150  }
151  }
152 
153  linestring_type linestring_finish(size_t /* num_points */) {
154  try {
155  return linestring_type(m_geos_factory->createLineString(m_coordinate_sequence.release()));
156  } catch (geos::util::GEOSException& e) {
158  }
159  }
160 
161  /* MultiPolygon */
162 
163  void multipolygon_start() {
164  m_polygons.clear();
165  }
166 
167  void multipolygon_polygon_start() {
168  m_rings.clear();
169  }
170 
171  void multipolygon_polygon_finish() {
172  try {
173  assert(!m_rings.empty());
174  auto inner_rings = new std::vector<geos::geom::Geometry*>;
175  std::transform(std::next(m_rings.begin(), 1), m_rings.end(), std::back_inserter(*inner_rings), [](std::unique_ptr<geos::geom::LinearRing>& r) {
176  return r.release();
177  });
178  m_polygons.emplace_back(m_geos_factory->createPolygon(m_rings[0].release(), inner_rings));
179  m_rings.clear();
180  } catch (geos::util::GEOSException& e) {
182  }
183  }
184 
185  void multipolygon_outer_ring_start() {
186  try {
187  m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
188  } catch (geos::util::GEOSException& e) {
190  }
191  }
192 
193  void multipolygon_outer_ring_finish() {
194  try {
195  m_rings.emplace_back(m_geos_factory->createLinearRing(m_coordinate_sequence.release()));
196  } catch (geos::util::GEOSException& e) {
198  }
199  }
200 
201  void multipolygon_inner_ring_start() {
202  try {
203  m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
204  } catch (geos::util::GEOSException& e) {
206  }
207  }
208 
209  void multipolygon_inner_ring_finish() {
210  try {
211  m_rings.emplace_back(m_geos_factory->createLinearRing(m_coordinate_sequence.release()));
212  } catch (geos::util::GEOSException& e) {
214  }
215  }
216 
217  void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
218  try {
219  m_coordinate_sequence->add(geos::geom::Coordinate(xy.x, xy.y));
220  } catch (geos::util::GEOSException& e) {
222  }
223  }
224 
225  multipolygon_type multipolygon_finish() {
226  try {
227  auto polygons = new std::vector<geos::geom::Geometry*>;
228  std::transform(m_polygons.begin(), m_polygons.end(), std::back_inserter(*polygons), [](std::unique_ptr<geos::geom::Polygon>& p) {
229  return p.release();
230  });
231  m_polygons.clear();
232  return multipolygon_type(m_geos_factory->createMultiPolygon(polygons));
233  } catch (geos::util::GEOSException& e) {
235  }
236  }
237 
238  }; // class GEOSFactoryImpl
239 
240  } // namespace detail
241 
242  template <typename TProjection = IdentityProjection>
244 
245  } // namespace geom
246 
247 } // namespace osmium
248 
249 #undef THROW
250 
251 #endif // OSMIUM_GEOM_GEOS_HPP
double y
Definition: coordinates.hpp:49
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
Definition: factory.hpp:146
#define THROW
Definition: geos.hpp:68
Definition: reader_iterator.hpp:39
Definition: geos.hpp:73
Coordinates transform(const CRS &src, const CRS &dest, Coordinates c)
Definition: projection.hpp:109
Definition: factory.hpp:57
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
geos_geometry_error(const char *message)
Definition: geos.hpp:75
Definition: attr.hpp:298
Definition: coordinates.hpp:46
double x
Definition: coordinates.hpp:48