Libosmium  2.9.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <algorithm>
46 #include <cassert>
47 #include <cstddef>
48 #include <iterator>
49 #include <memory>
50 #include <string>
51 #include <utility>
52 #include <vector>
53 
54 #include <geos/geom/Coordinate.h>
55 #include <geos/geom/CoordinateSequence.h>
56 #include <geos/geom/CoordinateSequenceFactory.h>
57 #include <geos/geom/GeometryFactory.h>
58 #include <geos/geom/LinearRing.h>
59 #include <geos/geom/MultiPolygon.h>
60 #include <geos/geom/Point.h>
61 #include <geos/geom/Polygon.h>
62 #include <geos/geom/PrecisionModel.h>
63 #include <geos/util/GEOSException.h>
64 
65 #include <osmium/geom/factory.hpp>
68 
69 // MSVC doesn't support throw_with_nested yet
70 #ifdef _MSC_VER
71 # define THROW throw
72 #else
73 # include <exception>
74 # define THROW std::throw_with_nested
75 #endif
76 
77 namespace osmium {
78 
80 
81  explicit geos_geometry_error(const char* message) :
82  geometry_error(std::string{"geometry creation failed in GEOS library: "} + message) {
83  }
84 
85  }; // struct geos_geometry_error
86 
87  namespace geom {
88 
89  namespace detail {
90 
91  class GEOSFactoryImpl {
92 
93  std::unique_ptr<const geos::geom::PrecisionModel> m_precision_model;
94  std::unique_ptr<geos::geom::GeometryFactory> m_our_geos_factory;
95  geos::geom::GeometryFactory* m_geos_factory;
96 
97  std::unique_ptr<geos::geom::CoordinateSequence> m_coordinate_sequence;
98  std::vector<std::unique_ptr<geos::geom::LinearRing>> m_rings;
99  std::vector<std::unique_ptr<geos::geom::Polygon>> m_polygons;
100 
101  public:
102 
103  using point_type = std::unique_ptr<geos::geom::Point>;
104  using linestring_type = std::unique_ptr<geos::geom::LineString>;
105  using polygon_type = std::unique_ptr<geos::geom::Polygon>;
106  using multipolygon_type = std::unique_ptr<geos::geom::MultiPolygon>;
107  using ring_type = std::unique_ptr<geos::geom::LinearRing>;
108 
109  explicit GEOSFactoryImpl(int /* srid */, geos::geom::GeometryFactory& geos_factory) :
110  m_precision_model(nullptr),
111  m_our_geos_factory(nullptr),
112  m_geos_factory(&geos_factory) {
113  }
114 
119  OSMIUM_DEPRECATED explicit GEOSFactoryImpl(int /* srid */, 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  explicit GEOSFactoryImpl(int srid) :
126  m_precision_model(new geos::geom::PrecisionModel),
127  m_our_geos_factory(new geos::geom::GeometryFactory(m_precision_model.get(), srid)),
128  m_geos_factory(m_our_geos_factory.get()) {
129  }
130 
131  /* Point */
132 
133  point_type make_point(const osmium::geom::Coordinates& xy) const {
134  try {
135  return point_type(m_geos_factory->createPoint(geos::geom::Coordinate(xy.x, xy.y)));
136  } catch (const geos::util::GEOSException& e) {
138  }
139  }
140 
141  /* LineString */
142 
143  void linestring_start() {
144  try {
145  m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
146  } catch (const geos::util::GEOSException& e) {
148  }
149  }
150 
151  void linestring_add_location(const osmium::geom::Coordinates& xy) {
152  try {
153  m_coordinate_sequence->add(geos::geom::Coordinate(xy.x, xy.y));
154  } catch (const geos::util::GEOSException& e) {
156  }
157  }
158 
159  linestring_type linestring_finish(size_t /* num_points */) {
160  try {
161  return linestring_type(m_geos_factory->createLineString(m_coordinate_sequence.release()));
162  } catch (const geos::util::GEOSException& e) {
164  }
165  }
166 
167  /* MultiPolygon */
168 
169  void multipolygon_start() {
170  m_polygons.clear();
171  }
172 
173  void multipolygon_polygon_start() {
174  m_rings.clear();
175  }
176 
177  void multipolygon_polygon_finish() {
178  try {
179  assert(!m_rings.empty());
180  auto inner_rings = new std::vector<geos::geom::Geometry*>;
181  std::transform(std::next(m_rings.begin(), 1), m_rings.end(), std::back_inserter(*inner_rings), [](std::unique_ptr<geos::geom::LinearRing>& r) {
182  return r.release();
183  });
184  m_polygons.emplace_back(m_geos_factory->createPolygon(m_rings[0].release(), inner_rings));
185  m_rings.clear();
186  } catch (const geos::util::GEOSException& e) {
188  }
189  }
190 
191  void multipolygon_outer_ring_start() {
192  try {
193  m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
194  } catch (const geos::util::GEOSException& e) {
196  }
197  }
198 
199  void multipolygon_outer_ring_finish() {
200  try {
201  m_rings.emplace_back(m_geos_factory->createLinearRing(m_coordinate_sequence.release()));
202  } catch (const geos::util::GEOSException& e) {
204  }
205  }
206 
207  void multipolygon_inner_ring_start() {
208  try {
209  m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
210  } catch (const geos::util::GEOSException& e) {
212  }
213  }
214 
215  void multipolygon_inner_ring_finish() {
216  try {
217  m_rings.emplace_back(m_geos_factory->createLinearRing(m_coordinate_sequence.release()));
218  } catch (const geos::util::GEOSException& e) {
220  }
221  }
222 
223  void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
224  try {
225  m_coordinate_sequence->add(geos::geom::Coordinate(xy.x, xy.y));
226  } catch (const geos::util::GEOSException& e) {
228  }
229  }
230 
231  multipolygon_type multipolygon_finish() {
232  try {
233  auto polygons = new std::vector<geos::geom::Geometry*>;
234  std::transform(m_polygons.begin(), m_polygons.end(), std::back_inserter(*polygons), [](std::unique_ptr<geos::geom::Polygon>& p) {
235  return p.release();
236  });
237  m_polygons.clear();
238  return multipolygon_type(m_geos_factory->createMultiPolygon(polygons));
239  } catch (const geos::util::GEOSException& e) {
241  }
242  }
243 
244  }; // class GEOSFactoryImpl
245 
246  } // namespace detail
247 
248  template <typename TProjection = IdentityProjection>
250 
251  } // namespace geom
252 
253 } // namespace osmium
254 
255 #undef THROW
256 
257 #endif // OSMIUM_GEOM_GEOS_HPP
double y
Definition: coordinates.hpp:49
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
Definition: factory.hpp:148
#define THROW
Definition: geos.hpp:74
Definition: reader_iterator.hpp:39
Definition: geos.hpp:79
Coordinates transform(const CRS &src, const CRS &dest, Coordinates c)
Definition: projection.hpp:113
Definition: factory.hpp:59
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
geos_geometry_error(const char *message)
Definition: geos.hpp:81
Definition: attr.hpp:333
Definition: coordinates.hpp:46
double x
Definition: coordinates.hpp:48