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