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

Loading...
Searching...
No Matches
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
6 This file is part of Osmium (https://osmcode.org/libosmium).
7
8 Copyright 2013-2025 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#include <osmium/geom/coordinates.hpp>
37#include <osmium/geom/factory.hpp>
38
39#include <cstddef>
40
41 namespace 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>
189 using RapidGeoJSONFactory = GeometryFactory<detail::RapidGeoJSONFactoryImpl<TWriter>, TProjection>;
190
191 } // namespace geom
192
193} // namespace osmium
194
195#endif // OSMIUM_GEOM_RAPID_GEOJSON_HPP
osmium::geom::GeometryFactory
Definition factory.hpp:149
coordinates.hpp
factory.hpp
detail
Definition attr.hpp:342
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::geom::Coordinates
Definition coordinates.hpp:48
osmium::geom::Coordinates::y
double y
Definition coordinates.hpp:51
osmium::geom::Coordinates::x
double x
Definition coordinates.hpp:50