Libosmium  2.7.1
Fast and flexible C++ library for working with OpenStreetMap data
ogr.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_GEOM_OGR_HPP
2 #define OSMIUM_GEOM_OGR_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 <cassert>
46 #include <cstddef>
47 #include <memory>
48 #include <utility>
49 
50 #include <ogr_geometry.h>
51 
53 #include <osmium/geom/factory.hpp>
54 
55 namespace osmium {
56 
57  namespace geom {
58 
59  namespace detail {
60 
61  class OGRFactoryImpl {
62 
63  public:
64 
65  using point_type = std::unique_ptr<OGRPoint>;
66  using linestring_type = std::unique_ptr<OGRLineString>;
67  using polygon_type = std::unique_ptr<OGRPolygon>;
68  using multipolygon_type = std::unique_ptr<OGRMultiPolygon>;
69  using ring_type = std::unique_ptr<OGRLinearRing>;
70 
71  private:
72 
73  linestring_type m_linestring{nullptr};
74  multipolygon_type m_multipolygon{nullptr};
75  polygon_type m_polygon{nullptr};
76  ring_type m_ring{nullptr};
77 
78  public:
79 
80  OGRFactoryImpl() = default;
81 
82  /* Point */
83 
84  point_type make_point(const osmium::geom::Coordinates& xy) const {
85  return point_type{new OGRPoint{xy.x, xy.y}};
86  }
87 
88  /* LineString */
89 
90  void linestring_start() {
91  m_linestring.reset(new OGRLineString{});
92  }
93 
94  void linestring_add_location(const osmium::geom::Coordinates& xy) {
95  assert(!!m_linestring);
96  m_linestring->addPoint(xy.x, xy.y);
97  }
98 
99  linestring_type linestring_finish(size_t /* num_points */) {
100  assert(!!m_linestring);
101  return std::move(m_linestring);
102  }
103 
104  /* Polygon */
105 
106  void polygon_start() {
107  m_ring.reset(new OGRLinearRing{});
108  }
109 
110  void polygon_add_location(const osmium::geom::Coordinates& xy) {
111  assert(!!m_ring);
112  m_ring->addPoint(xy.x, xy.y);
113  }
114 
115  polygon_type polygon_finish(size_t /* num_points */) {
116  auto polygon = std::unique_ptr<OGRPolygon>{new OGRPolygon{}};
117  polygon->addRingDirectly(m_ring.release());
118  return polygon;
119  }
120 
121  /* MultiPolygon */
122 
123  void multipolygon_start() {
124  m_multipolygon.reset(new OGRMultiPolygon{});
125  }
126 
127  void multipolygon_polygon_start() {
128  m_polygon.reset(new OGRPolygon{});
129  }
130 
131  void multipolygon_polygon_finish() {
132  assert(!!m_multipolygon);
133  assert(!!m_polygon);
134  m_multipolygon->addGeometryDirectly(m_polygon.release());
135  }
136 
137  void multipolygon_outer_ring_start() {
138  m_ring.reset(new OGRLinearRing{});
139  }
140 
141  void multipolygon_outer_ring_finish() {
142  assert(!!m_polygon);
143  assert(!!m_ring);
144  m_polygon->addRingDirectly(m_ring.release());
145  }
146 
147  void multipolygon_inner_ring_start() {
148  m_ring.reset(new OGRLinearRing{});
149  }
150 
151  void multipolygon_inner_ring_finish() {
152  assert(!!m_polygon);
153  assert(!!m_ring);
154  m_polygon->addRingDirectly(m_ring.release());
155  }
156 
157  void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
158  assert(!!m_polygon);
159  assert(!!m_ring);
160  m_ring->addPoint(xy.x, xy.y);
161  }
162 
163  multipolygon_type multipolygon_finish() {
164  assert(!!m_multipolygon);
165  return std::move(m_multipolygon);
166  }
167 
168  }; // class OGRFactoryImpl
169 
170  } // namespace detail
171 
172  template <typename TProjection = IdentityProjection>
174 
175  } // namespace geom
176 
177 } // namespace osmium
178 
179 #endif // OSMIUM_GEOM_OGR_HPP
double y
Definition: coordinates.hpp:49
Definition: factory.hpp:146
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
Definition: attr.hpp:298
Definition: coordinates.hpp:46
double x
Definition: coordinates.hpp:48