1 #ifndef OSMIUM_GEOM_OGR_HPP
2 #define OSMIUM_GEOM_OGR_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2021 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 
36 /**
37  * @file
38  *
39  * This file contains code for conversion of OSM geometries into OGR
40  * geometries.
41  *
42  * @attention If you include this file, you'll need to link with `libgdal`.
43  */
44 
45 #include <osmium/geom/coordinates.hpp>
46 #include <osmium/geom/factory.hpp>
47 
48 #include <ogr_geometry.h>
49 
50 #include <cassert>
51 #include <cstddef>
52 #include <memory>
53 #include <utility>
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 
OGRFactoryImpl(int)80                 explicit OGRFactoryImpl(int /* srid */) {
81                 }
82 
83                 /* Point */
84 
make_point(const osmium::geom::Coordinates & xy)85                 static point_type make_point(const osmium::geom::Coordinates& xy) {
86                     return point_type{new OGRPoint{xy.x, xy.y}};
87                 }
88 
89                 /* LineString */
90 
linestring_start()91                 void linestring_start() {
92                     m_linestring.reset(new OGRLineString{});
93                 }
94 
linestring_add_location(const osmium::geom::Coordinates & xy)95                 void linestring_add_location(const osmium::geom::Coordinates& xy) {
96                     assert(!!m_linestring);
97                     m_linestring->addPoint(xy.x, xy.y);
98                 }
99 
linestring_finish(size_t)100                 linestring_type linestring_finish(size_t /* num_points */) {
101                     assert(!!m_linestring);
102                     return std::move(m_linestring);
103                 }
104 
105                 /* Polygon */
106 
polygon_start()107                 void polygon_start() {
108                     m_ring.reset(new OGRLinearRing{});
109                 }
110 
polygon_add_location(const osmium::geom::Coordinates & xy)111                 void polygon_add_location(const osmium::geom::Coordinates& xy) {
112                     assert(!!m_ring);
113                     m_ring->addPoint(xy.x, xy.y);
114                 }
115 
polygon_finish(size_t)116                 polygon_type polygon_finish(size_t /* num_points */) {
117                     auto polygon = std::unique_ptr<OGRPolygon>{new OGRPolygon{}};
118                     polygon->addRingDirectly(m_ring.release());
119                     return polygon;
120                 }
121 
122                 /* MultiPolygon */
123 
multipolygon_start()124                 void multipolygon_start() {
125                     m_multipolygon.reset(new OGRMultiPolygon{});
126                 }
127 
multipolygon_polygon_start()128                 void multipolygon_polygon_start() {
129                     m_polygon.reset(new OGRPolygon{});
130                 }
131 
multipolygon_polygon_finish()132                 void multipolygon_polygon_finish() {
133                     assert(!!m_multipolygon);
134                     assert(!!m_polygon);
135                     m_multipolygon->addGeometryDirectly(m_polygon.release());
136                 }
137 
multipolygon_outer_ring_start()138                 void multipolygon_outer_ring_start() {
139                     m_ring.reset(new OGRLinearRing{});
140                 }
141 
multipolygon_outer_ring_finish()142                 void multipolygon_outer_ring_finish() {
143                     assert(!!m_polygon);
144                     assert(!!m_ring);
145                     m_polygon->addRingDirectly(m_ring.release());
146                 }
147 
multipolygon_inner_ring_start()148                 void multipolygon_inner_ring_start() {
149                     m_ring.reset(new OGRLinearRing{});
150                 }
151 
multipolygon_inner_ring_finish()152                 void multipolygon_inner_ring_finish() {
153                     assert(!!m_polygon);
154                     assert(!!m_ring);
155                     m_polygon->addRingDirectly(m_ring.release());
156                 }
157 
multipolygon_add_location(const osmium::geom::Coordinates & xy)158                 void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
159                     assert(!!m_polygon);
160                     assert(!!m_ring);
161                     m_ring->addPoint(xy.x, xy.y);
162                 }
163 
multipolygon_finish()164                 multipolygon_type multipolygon_finish() {
165                     assert(!!m_multipolygon);
166                     return std::move(m_multipolygon);
167                 }
168 
169             }; // class OGRFactoryImpl
170 
171         } // namespace detail
172 
173         template <typename TProjection = IdentityProjection>
174         using OGRFactory = GeometryFactory<osmium::geom::detail::OGRFactoryImpl, TProjection>;
175 
176     } // namespace geom
177 
178 } // namespace osmium
179 
180 #endif // OSMIUM_GEOM_OGR_HPP
181