Libosmium  2.6.1
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-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 
36 #include <cassert>
37 #include <string>
38 #include <utility>
39 
41 #include <osmium/geom/factory.hpp>
42 
43 namespace osmium {
44 
45  namespace geom {
46 
47  namespace detail {
48 
49  class GeoJSONFactoryImpl {
50 
51  std::string m_str;
52  int m_precision;
53 
54  public:
55 
56  typedef std::string point_type;
57  typedef std::string linestring_type;
58  typedef std::string polygon_type;
59  typedef std::string multipolygon_type;
60  typedef std::string ring_type;
61 
62  GeoJSONFactoryImpl(int precision = 7) :
63  m_precision(precision) {
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  std::string str {"{\"type\":\"Point\",\"coordinates\":"};
71  xy.append_to_string(str, '[', ',', ']', m_precision);
72  str += "}";
73  return str;
74  }
75 
76  /* LineString */
77 
78  // { "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }
79  void linestring_start() {
80  m_str = "{\"type\":\"LineString\",\"coordinates\":[";
81  }
82 
83  void linestring_add_location(const osmium::geom::Coordinates& xy) {
84  xy.append_to_string(m_str, '[', ',', ']', m_precision);
85  m_str += ',';
86  }
87 
88  linestring_type linestring_finish(size_t /* num_points */) {
89  assert(!m_str.empty());
90  std::string str;
91 
92  using std::swap;
93  swap(str, m_str);
94 
95  str.back() = ']';
96  str += "}";
97  return str;
98  }
99 
100  /* MultiPolygon */
101 
102  void multipolygon_start() {
103  m_str = "{\"type\":\"MultiPolygon\",\"coordinates\":[";
104  }
105 
106  void multipolygon_polygon_start() {
107  m_str += '[';
108  }
109 
110  void multipolygon_polygon_finish() {
111  m_str += "],";
112  }
113 
114  void multipolygon_outer_ring_start() {
115  m_str += '[';
116  }
117 
118  void multipolygon_outer_ring_finish() {
119  assert(!m_str.empty());
120  m_str.back() = ']';
121  }
122 
123  void multipolygon_inner_ring_start() {
124  m_str += ",[";
125  }
126 
127  void multipolygon_inner_ring_finish() {
128  assert(!m_str.empty());
129  m_str.back() = ']';
130  }
131 
132  void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
133  xy.append_to_string(m_str, '[', ',', ']', m_precision);
134  m_str += ',';
135  }
136 
137  multipolygon_type multipolygon_finish() {
138  assert(!m_str.empty());
139  std::string str;
140 
141  using std::swap;
142  swap(str, m_str);
143 
144  str.back() = ']';
145  str += "}";
146  return str;
147  }
148 
149  }; // class GeoJSONFactoryImpl
150 
151  } // namespace detail
152 
153  template <typename TProjection = IdentityProjection>
155 
156  } // namespace geom
157 
158 } // namespace osmium
159 
160 #endif // OSMIUM_GEOM_GEOJSON_HPP
Definition: factory.hpp:146
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:721
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
Definition: attr.hpp:298
Definition: coordinates.hpp:46
void append_to_string(std::string &s, const char infix, int precision) const
Definition: coordinates.hpp:57