Libosmium  2.13.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 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2017 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 <cassert>
37 #include <cstddef>
38 #include <string>
39 #include <utility>
40 
42 #include <osmium/geom/factory.hpp>
43 
44 namespace 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  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  /* MultiPolygon */
102 
103  void multipolygon_start() {
104  m_str = "{\"type\":\"MultiPolygon\",\"coordinates\":[";
105  }
106 
107  void multipolygon_polygon_start() {
108  m_str += '[';
109  }
110 
111  void multipolygon_polygon_finish() {
112  m_str += "],";
113  }
114 
115  void multipolygon_outer_ring_start() {
116  m_str += '[';
117  }
118 
119  void multipolygon_outer_ring_finish() {
120  assert(!m_str.empty());
121  m_str.back() = ']';
122  }
123 
124  void multipolygon_inner_ring_start() {
125  m_str += ",[";
126  }
127 
128  void multipolygon_inner_ring_finish() {
129  assert(!m_str.empty());
130  m_str.back() = ']';
131  }
132 
133  void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
134  xy.append_to_string(m_str, '[', ',', ']', m_precision);
135  m_str += ',';
136  }
137 
138  multipolygon_type multipolygon_finish() {
139  assert(!m_str.empty());
140  std::string str;
141 
142  using std::swap;
143  swap(str, m_str);
144 
145  str.back() = ']';
146  str += "}";
147  return str;
148  }
149 
150  }; // class GeoJSONFactoryImpl
151 
152  } // namespace detail
153 
154  template <typename TProjection = IdentityProjection>
156 
157  } // namespace geom
158 
159 } // namespace osmium
160 
161 #endif // OSMIUM_GEOM_GEOJSON_HPP
Definition: factory.hpp:148
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:762
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: attr.hpp:333
Definition: coordinates.hpp:48
void append_to_string(std::string &s, const char infix, int precision) const
Definition: coordinates.hpp:96