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

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

Loading...
Searching...
No Matches
wkt.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_GEOM_WKT_HPP
2#define OSMIUM_GEOM_WKT_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
36#include <osmium/geom/coordinates.hpp>
37#include <osmium/geom/factory.hpp>
38
39#include <cassert>
40#include <cstddef>
41#include <string>
42#include <utility>
43
44namespace osmium {
45
46 namespace geom {
47
48 enum class wkt_type : bool {
49 wkt = false,
50 ewkt = true
51 }; // enum class wkt_type
52
53 namespace detail {
54
55 class WKTFactoryImpl {
56
57 std::string m_srid_prefix;
58 std::string m_str;
59 int m_precision;
60 wkt_type m_wkt_type;
61
62 public:
63
64 using point_type = std::string;
65 using linestring_type = std::string;
66 using polygon_type = std::string;
67 using multipolygon_type = std::string;
68 using ring_type = std::string;
69
70 explicit WKTFactoryImpl(int srid, int precision = 7, wkt_type wtype = wkt_type::wkt) :
71 m_precision(precision),
72 m_wkt_type(wtype) {
73 if (m_wkt_type == wkt_type::ewkt) {
74 m_srid_prefix = "SRID=";
75 m_srid_prefix += std::to_string(srid);
76 m_srid_prefix += ';';
77 }
78 }
79
80 /* Point */
81
82 point_type make_point(const osmium::geom::Coordinates& xy) const {
83 std::string str{m_srid_prefix};
84 str += "POINT";
85 xy.append_to_string(str, '(', ' ', ')', m_precision);
86 return str;
87 }
88
89 /* LineString */
90
91 void linestring_start() {
92 m_str = m_srid_prefix;
93 m_str += "LINESTRING(";
94 }
95
96 void linestring_add_location(const osmium::geom::Coordinates& xy) {
97 xy.append_to_string(m_str, ' ', m_precision);
98 m_str += ',';
99 }
100
101 linestring_type linestring_finish(size_t /* num_points */) {
102 assert(!m_str.empty());
103 std::string str;
104
105 using std::swap;
106 swap(str, m_str);
107
108 str.back() = ')';
109 return str;
110 }
111
112 /* Polygon */
113 void polygon_start() {
114 m_str = m_srid_prefix;
115 m_str += "POLYGON((";
116 }
117
118 void polygon_add_location(const osmium::geom::Coordinates& xy) {
119 xy.append_to_string(m_str, ' ', m_precision);
120 m_str += ',';
121 }
122
123 polygon_type polygon_finish(size_t /* num_points */) {
124 assert(!m_str.empty());
125 std::string str;
126
127 using std::swap;
128 swap(str, m_str);
129
130 str.back() = ')';
131 str += ")";
132 return str;
133 }
134
135 /* MultiPolygon */
136
137 void multipolygon_start() {
138 m_str = m_srid_prefix;
139 m_str += "MULTIPOLYGON(";
140 }
141
142 void multipolygon_polygon_start() {
143 m_str += '(';
144 }
145
146 void multipolygon_polygon_finish() {
147 m_str += "),";
148 }
149
150 void multipolygon_outer_ring_start() {
151 m_str += '(';
152 }
153
154 void multipolygon_outer_ring_finish() {
155 assert(!m_str.empty());
156 m_str.back() = ')';
157 }
158
159 void multipolygon_inner_ring_start() {
160 m_str += ",(";
161 }
162
163 void multipolygon_inner_ring_finish() {
164 assert(!m_str.empty());
165 m_str.back() = ')';
166 }
167
168 void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
169 xy.append_to_string(m_str, ' ', m_precision);
170 m_str += ',';
171 }
172
173 multipolygon_type multipolygon_finish() {
174 assert(!m_str.empty());
175 std::string str;
176
177 using std::swap;
178 swap(str, m_str);
179
180 str.back() = ')';
181 return str;
182 }
183
184 }; // class WKTFactoryImpl
185
186 } // namespace detail
187
188 template <typename TProjection = IdentityProjection>
189 using WKTFactory = GeometryFactory<osmium::geom::detail::WKTFactoryImpl, TProjection>;
190
191 } // namespace geom
192
193} // namespace osmium
194
195#endif // OSMIUM_GEOM_WKT_HPP
osmium::geom::GeometryFactory
Definition factory.hpp:149
coordinates.hpp
factory.hpp
detail
Definition attr.hpp:342
osmium::geom
Everything related to geometry handling.
Definition coordinates.hpp:46
osmium::geom::WKTFactory
GeometryFactory< osmium::geom::detail::WKTFactoryImpl, TProjection > WKTFactory
Definition wkt.hpp:189
osmium::geom::wkt_type
wkt_type
Definition wkt.hpp:48
osmium::geom::wkt_type::wkt
@ wkt
Definition wkt.hpp:49
osmium::geom::wkt_type::ewkt
@ ewkt
Definition wkt.hpp:50
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::geom::Coordinates
Definition coordinates.hpp:48
osmium::geom::Coordinates::append_to_string
void append_to_string(std::string &s, const char infix, int precision) const
Definition coordinates.hpp:100