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