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