Libosmium  2.20.0
Fast and flexible C++ library for working with OpenStreetMap data
geojson.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_GEOM_GEOJSON_HPP
2#define OSMIUM_GEOM_GEOJSON_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2023 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
38
39#include <cassert>
40#include <cstddef>
41#include <string>
42#include <utility>
43
44namespace osmium {
45
46 namespace geom {
47
48 namespace detail {
49
50 class GeoJSONFactoryImpl {
51
52 std::string m_str;
53 int m_precision;
54
55 public:
56
57 using point_type = std::string;
58 using linestring_type = std::string;
59 using polygon_type = std::string;
60 using multipolygon_type = std::string;
61 using ring_type = std::string;
62
63 explicit GeoJSONFactoryImpl(int /*srid*/, int precision = 7) :
64 m_precision(precision) {
65 }
66
67 /* Point */
68
69 // { "type": "Point", "coordinates": [100.0, 0.0] }
70 point_type make_point(const osmium::geom::Coordinates& xy) const {
71 std::string str{"{\"type\":\"Point\",\"coordinates\":"};
72 xy.append_to_string(str, '[', ',', ']', m_precision);
73 str += "}";
74 return str;
75 }
76
77 /* LineString */
78
79 // { "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }
80 void linestring_start() {
81 m_str = "{\"type\":\"LineString\",\"coordinates\":[";
82 }
83
84 void linestring_add_location(const osmium::geom::Coordinates& xy) {
85 xy.append_to_string(m_str, '[', ',', ']', m_precision);
86 m_str += ',';
87 }
88
89 linestring_type linestring_finish(size_t /*num_points*/) {
90 assert(!m_str.empty());
91 std::string str;
92
93 using std::swap;
94 swap(str, m_str);
95
96 str.back() = ']';
97 str += "}";
98 return str;
99 }
100
101 /* Polygon */
102 void polygon_start() {
103 m_str = "{\"type\":\"Polygon\",\"coordinates\":[[";
104 }
105
106 void polygon_add_location(const osmium::geom::Coordinates& xy) {
107 xy.append_to_string(m_str, '[', ',', ']', m_precision);
108 m_str += ',';
109 }
110
111 polygon_type polygon_finish(size_t /*num_points*/) {
112 assert(!m_str.empty());
113 std::string str;
114
115 using std::swap;
116 swap(str, m_str);
117
118 str.back() = ']';
119 str += "]}";
120 return str;
121 }
122
123 /* MultiPolygon */
124
125 void multipolygon_start() {
126 m_str = "{\"type\":\"MultiPolygon\",\"coordinates\":[";
127 }
128
129 void multipolygon_polygon_start() {
130 m_str += '[';
131 }
132
133 void multipolygon_polygon_finish() {
134 m_str += "],";
135 }
136
137 void multipolygon_outer_ring_start() {
138 m_str += '[';
139 }
140
141 void multipolygon_outer_ring_finish() {
142 assert(!m_str.empty());
143 m_str.back() = ']';
144 }
145
146 void multipolygon_inner_ring_start() {
147 m_str += ",[";
148 }
149
150 void multipolygon_inner_ring_finish() {
151 assert(!m_str.empty());
152 m_str.back() = ']';
153 }
154
155 void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
156 xy.append_to_string(m_str, '[', ',', ']', m_precision);
157 m_str += ',';
158 }
159
160 multipolygon_type multipolygon_finish() {
161 assert(!m_str.empty());
162 std::string str;
163
164 using std::swap;
165 swap(str, m_str);
166
167 str.back() = ']';
168 str += "}";
169 return str;
170 }
171
172 }; // class GeoJSONFactoryImpl
173
174 } // namespace detail
175
176 template <typename TProjection = IdentityProjection>
178
179 } // namespace geom
180
181} // namespace osmium
182
183#endif // OSMIUM_GEOM_GEOJSON_HPP
Definition: factory.hpp:149
Definition: attr.hpp:342
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: coordinates.hpp:48
void append_to_string(std::string &s, const char infix, int precision) const
Definition: coordinates.hpp:100