Libosmium: include/osmium/geom/ogr.hpp Source File

Libosmium Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
ogr.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_GEOM_OGR_HPP
2#define OSMIUM_GEOM_OGR_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2026 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
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
55namespace 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 explicit OGRFactoryImpl(int /* srid */) {
81 }
82
83 /* Point */
84
85 static point_type make_point(const osmium::geom::Coordinates& xy) {
86 return std::make_unique<OGRPoint>(xy.x, xy.y);
87 }
88
89 /* LineString */
90
91 void linestring_start() {
92 m_linestring = std::make_unique<OGRLineString>();
93 }
94
95 void linestring_add_location(const osmium::geom::Coordinates& xy) {
96 assert(!!m_linestring);
97 m_linestring->addPoint(xy.x, xy.y);
98 }
99
100 linestring_type linestring_finish(size_t /* num_points */) {
101 assert(!!m_linestring);
102 return std::move(m_linestring);
103 }
104
105 /* Polygon */
106
107 void polygon_start() {
108 m_ring = std::make_unique<OGRLinearRing>();
109 }
110
111 void polygon_add_location(const osmium::geom::Coordinates& xy) {
112 assert(!!m_ring);
113 m_ring->addPoint(xy.x, xy.y);
114 }
115
116 polygon_type polygon_finish(size_t /* num_points */) {
117 auto polygon = std::make_unique<OGRPolygon>();
118 polygon->addRingDirectly(m_ring.release());
119 return polygon;
120 }
121
122 /* MultiPolygon */
123
124 void multipolygon_start() {
125 m_multipolygon = std::make_unique<OGRMultiPolygon>();
126 }
127
128 void multipolygon_polygon_start() {
129 m_polygon = std::make_unique<OGRPolygon>();
130 }
131
132 void multipolygon_polygon_finish() {
133 assert(!!m_multipolygon);
134 assert(!!m_polygon);
135 m_multipolygon->addGeometryDirectly(m_polygon.release());
136 }
137
138 void multipolygon_outer_ring_start() {
139 m_ring = std::make_unique<OGRLinearRing>();
140 }
141
142 void multipolygon_outer_ring_finish() {
143 assert(!!m_polygon);
144 assert(!!m_ring);
145 m_polygon->addRingDirectly(m_ring.release());
146 }
147
148 void multipolygon_inner_ring_start() {
149 m_ring = std::make_unique<OGRLinearRing>();
150 }
151
152 void multipolygon_inner_ring_finish() {
153 assert(!!m_polygon);
154 assert(!!m_ring);
155 m_polygon->addRingDirectly(m_ring.release());
156 }
157
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
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
osmium::geom::GeometryFactory
Definition factory.hpp:149
coordinates.hpp
factory.hpp
osmium::geom
Everything related to geometry handling.
Definition coordinates.hpp:46
osmium::geom::OGRFactory
GeometryFactory< osmium::geom::detail::OGRFactoryImpl, TProjection > OGRFactory
Definition ogr.hpp:174
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::geom::Coordinates::y
double y
Definition coordinates.hpp:51
osmium::geom::Coordinates::x
double x
Definition coordinates.hpp:50