Libosmium  2.3.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-2015 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  std::swap(str, m_str);
92  str.back() = ']';
93  str += "}";
94  return str;
95  }
96 
97  /* MultiPolygon */
98 
99  void multipolygon_start() {
100  m_str = "{\"type\":\"MultiPolygon\",\"coordinates\":[";
101  }
102 
103  void multipolygon_polygon_start() {
104  m_str += '[';
105  }
106 
107  void multipolygon_polygon_finish() {
108  m_str += "],";
109  }
110 
111  void multipolygon_outer_ring_start() {
112  m_str += '[';
113  }
114 
115  void multipolygon_outer_ring_finish() {
116  assert(!m_str.empty());
117  m_str.back() = ']';
118  }
119 
120  void multipolygon_inner_ring_start() {
121  m_str += ",[";
122  }
123 
124  void multipolygon_inner_ring_finish() {
125  assert(!m_str.empty());
126  m_str.back() = ']';
127  }
128 
129  void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
130  xy.append_to_string(m_str, '[', ',', ']', m_precision);
131  m_str += ',';
132  }
133 
134  multipolygon_type multipolygon_finish() {
135  assert(!m_str.empty());
136  std::string str;
137  std::swap(str, m_str);
138  str.back() = ']';
139  str += "}";
140  return str;
141  }
142 
143  }; // class GeoJSONFactoryImpl
144 
145  } // namespace detail
146 
147  template <class TProjection = IdentityProjection>
149 
150  } // namespace geom
151 
152 } // namespace osmium
153 
154 #endif // OSMIUM_GEOM_GEOJSON_HPP
Definition: factory.hpp:146
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
Definition: coordinates.hpp:47
void append_to_string(std::string &s, const char infix, int precision) const
Definition: coordinates.hpp:58