Libosmium  2.7.2
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>
62 
63 // MSVC doesn't support throw_with_nested yet
64 #ifdef _MSC_VER
65 # define THROW throw
66 #else
67 # define THROW std::throw_with_nested
68 #endif
69 
70 namespace osmium {
71 
73 
74  geos_geometry_error(const char* message) :
75  geometry_error(std::string("geometry creation failed in GEOS library: ") + message) {
76  }
77 
78  }; // struct geos_geometry_error
79 
80  namespace geom {
81 
82  namespace detail {
83 
84  class GEOSFactoryImpl {
85 
86  std::unique_ptr<const geos::geom::PrecisionModel> m_precision_model;
87  std::unique_ptr<geos::geom::GeometryFactory> m_our_geos_factory;
88  geos::geom::GeometryFactory* m_geos_factory;
89 
90  std::unique_ptr<geos::geom::CoordinateSequence> m_coordinate_sequence;
91  std::vector<std::unique_ptr<geos::geom::LinearRing>> m_rings;
92  std::vector<std::unique_ptr<geos::geom::Polygon>> m_polygons;
93 
94  public:
95 
96  using point_type = std::unique_ptr<geos::geom::Point>;
97  using linestring_type = std::unique_ptr<geos::geom::LineString>;
98  using polygon_type = std::unique_ptr<geos::geom::Polygon>;
99  using multipolygon_type = std::unique_ptr<geos::geom::MultiPolygon>;
100  using ring_type = std::unique_ptr<geos::geom::LinearRing>;
101 
102  explicit GEOSFactoryImpl(geos::geom::GeometryFactory& geos_factory) :
103  m_precision_model(nullptr),
104  m_our_geos_factory(nullptr),
105  m_geos_factory(&geos_factory) {
106  }
107 
108  explicit GEOSFactoryImpl(int srid = -1) :
109  m_precision_model(new geos::geom::PrecisionModel),
110  m_our_geos_factory(new geos::geom::GeometryFactory(m_precision_model.get(), srid)),
111  m_geos_factory(m_our_geos_factory.get()) {
112  }
113 
114  /* Point */
115 
116  point_type make_point(const osmium::geom::Coordinates& xy) const {
117  try {
118  return point_type(m_geos_factory->createPoint(geos::geom::Coordinate(xy.x, xy.y)));
119  } catch (geos::util::GEOSException& e) {
121  }
122  }
123 
124  /* LineString */
125 
126  void linestring_start() {
127  try {
128  m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
129  } catch (geos::util::GEOSException& e) {
131  }
132  }
133 
134  void linestring_add_location(const osmium::geom::Coordinates& xy) {
135  try {
136  m_coordinate_sequence->add(geos::geom::Coordinate(xy.x, xy.y));
137  } catch (geos::util::GEOSException& e) {
139  }
140  }
141 
142  linestring_type linestring_finish(size_t /* num_points */) {
143  try {
144  return linestring_type(m_geos_factory->createLineString(m_coordinate_sequence.release()));
145  } catch (geos::util::GEOSException& e) {
147  }
148  }
149 
150  /* MultiPolygon */
151 
152  void multipolygon_start() {
153  m_polygons.clear();
154  }
155 
156  void multipolygon_polygon_start() {
157  m_rings.clear();
158  }
159 
160  void multipolygon_polygon_finish() {
161  try {
162  assert(!m_rings.empty());
163  auto inner_rings = new std::vector<geos::geom::Geometry*>;
164  std::transform(std::next(m_rings.begin(), 1), m_rings.end(), std::back_inserter(*inner_rings), [](std::unique_ptr<geos::geom::LinearRing>& r) {
165  return r.release();
166  });
167  m_polygons.emplace_back(m_geos_factory->createPolygon(m_rings[0].release(), inner_rings));
168  m_rings.clear();
169  } catch (geos::util::GEOSException& e) {
171  }
172  }
173 
174  void multipolygon_outer_ring_start() {
175  try {
176  m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
177  } catch (geos::util::GEOSException& e) {
179  }
180  }
181 
182  void multipolygon_outer_ring_finish() {
183  try {
184  m_rings.emplace_back(m_geos_factory->createLinearRing(m_coordinate_sequence.release()));
185  } catch (geos::util::GEOSException& e) {
187  }
188  }
189 
190  void multipolygon_inner_ring_start() {
191  try {
192  m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
193  } catch (geos::util::GEOSException& e) {
195  }
196  }
197 
198  void multipolygon_inner_ring_finish() {
199  try {
200  m_rings.emplace_back(m_geos_factory->createLinearRing(m_coordinate_sequence.release()));
201  } catch (geos::util::GEOSException& e) {
203  }
204  }
205 
206  void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
207  try {
208  m_coordinate_sequence->add(geos::geom::Coordinate(xy.x, xy.y));
209  } catch (geos::util::GEOSException& e) {
211  }
212  }
213 
214  multipolygon_type multipolygon_finish() {
215  try {
216  auto polygons = new std::vector<geos::geom::Geometry*>;
217  std::transform(m_polygons.begin(), m_polygons.end(), std::back_inserter(*polygons), [](std::unique_ptr<geos::geom::Polygon>& p) {
218  return p.release();
219  });
220  m_polygons.clear();
221  return multipolygon_type(m_geos_factory->createMultiPolygon(polygons));
222  } catch (geos::util::GEOSException& e) {
224  }
225  }
226 
227  }; // class GEOSFactoryImpl
228 
229  } // namespace detail
230 
231  template <typename TProjection = IdentityProjection>
233 
234  } // namespace geom
235 
236 } // namespace osmium
237 
238 #undef THROW
239 
240 #endif // OSMIUM_GEOM_GEOS_HPP
double y
Definition: coordinates.hpp:49
Definition: factory.hpp:146
#define THROW
Definition: geos.hpp:67
Definition: reader_iterator.hpp:39
Definition: geos.hpp:72
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:74
Definition: attr.hpp:298
Definition: coordinates.hpp:46
double x
Definition: coordinates.hpp:48