Libosmium  2.20.0
Fast and flexible C++ library for working with OpenStreetMap data
rapid_geojson.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_GEOM_RAPID_GEOJSON_HPP
2#define OSMIUM_GEOM_RAPID_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 <cstddef>
40
41namespace osmium {
42
43 namespace geom {
44
45 namespace detail {
46
54 template <typename TWriter>
55 class RapidGeoJSONFactoryImpl {
56
57 TWriter* m_writer;
58
59 public:
60
61 using point_type = void;
62 using linestring_type = void;
63 using polygon_type = void;
64 using multipolygon_type = void;
65 using ring_type = void;
66
67 RapidGeoJSONFactoryImpl(int /* srid */, TWriter& writer) :
68 m_writer(&writer) {
69 }
70
71 /* Point */
72
73 // { "type": "Point", "coordinates": [100.0, 0.0] }
74 point_type make_point(const osmium::geom::Coordinates& xy) const {
75 m_writer->String("geometry");
76 m_writer->StartObject();
77 m_writer->String("type");
78 m_writer->String("Point");
79 m_writer->String("coordinates");
80 m_writer->StartArray();
81 m_writer->Double(xy.x);
82 m_writer->Double(xy.y);
83 m_writer->EndArray();
84 m_writer->EndObject();
85 }
86
87 /* LineString */
88
89 // { "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }
90 void linestring_start() {
91 m_writer->String("geometry");
92 m_writer->StartObject();
93 m_writer->String("type");
94 m_writer->String("LineString");
95 m_writer->String("coordinates");
96 m_writer->StartArray();
97 }
98
99 void linestring_add_location(const osmium::geom::Coordinates& xy) {
100 m_writer->StartArray();
101 m_writer->Double(xy.x);
102 m_writer->Double(xy.y);
103 m_writer->EndArray();
104 }
105
106 linestring_type linestring_finish(size_t /* num_points */) {
107 m_writer->EndArray();
108 m_writer->EndObject();
109 }
110
111 /* Polygon */
112
113 // { "type": "Polygon", "coordinates": [[[100.0, 0.0], [101.0, 1.0]]] }
114 void polygon_start() {
115 m_writer->String("geometry");
116 m_writer->StartObject();
117 m_writer->String("type");
118 m_writer->String("Polygon");
119 m_writer->String("coordinates");
120 m_writer->StartArray();
121 m_writer->StartArray();
122 }
123
124 void polygon_add_location(const osmium::geom::Coordinates& xy) {
125 m_writer->StartArray();
126 m_writer->Double(xy.x);
127 m_writer->Double(xy.y);
128 m_writer->EndArray();
129 }
130
131 polygon_type polygon_finish(size_t /* num_points */) {
132 m_writer->EndArray();
133 m_writer->EndArray();
134 m_writer->EndObject();
135 }
136
137 /* MultiPolygon */
138
139 void multipolygon_start() {
140 m_writer->String("geometry");
141 m_writer->StartObject();
142 m_writer->String("type");
143 m_writer->String("MultiPolygon");
144 m_writer->String("coordinates");
145 m_writer->StartArray();
146 }
147
148 void multipolygon_polygon_start() {
149 m_writer->StartArray();
150 }
151
152 void multipolygon_polygon_finish() {
153 m_writer->EndArray();
154 }
155
156 void multipolygon_outer_ring_start() {
157 m_writer->StartArray();
158 }
159
160 void multipolygon_outer_ring_finish() {
161 m_writer->EndArray();
162 }
163
164 void multipolygon_inner_ring_start() {
165 m_writer->StartArray();
166 }
167
168 void multipolygon_inner_ring_finish() {
169 m_writer->EndArray();
170 }
171
172 void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
173 m_writer->StartArray();
174 m_writer->Double(xy.x);
175 m_writer->Double(xy.y);
176 m_writer->EndArray();
177 }
178
179 multipolygon_type multipolygon_finish() {
180 m_writer->EndArray();
181 m_writer->EndObject();
182 }
183
184 }; // class RapidGeoJSONFactoryImpl
185
186 } // namespace detail
187
188 template <typename TWriter, typename TProjection = IdentityProjection>
190
191 } // namespace geom
192
193} // namespace osmium
194
195#endif // OSMIUM_GEOM_RAPID_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
double y
Definition: coordinates.hpp:51
double x
Definition: coordinates.hpp:50