Libosmium  2.5.4
Fast and flexible C++ library for working with OpenStreetMap data
wkt.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_GEOM_WKT_HPP
2 #define OSMIUM_GEOM_WKT_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 <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 WKTFactoryImpl {
51 
52  std::string m_str;
53  int m_precision;
54 
55  public:
56 
57  typedef std::string point_type;
58  typedef std::string linestring_type;
59  typedef std::string polygon_type;
60  typedef std::string multipolygon_type;
61  typedef std::string ring_type;
62 
63  WKTFactoryImpl(int precision = 7) :
64  m_precision(precision) {
65  }
66 
67  /* Point */
68 
69  point_type make_point(const osmium::geom::Coordinates& xy) const {
70  std::string str {"POINT"};
71  xy.append_to_string(str, '(', ' ', ')', m_precision);
72  return str;
73  }
74 
75  /* LineString */
76 
77  void linestring_start() {
78  m_str = "LINESTRING(";
79  }
80 
81  void linestring_add_location(const osmium::geom::Coordinates& xy) {
82  xy.append_to_string(m_str, ' ', m_precision);
83  m_str += ',';
84  }
85 
86  linestring_type linestring_finish(size_t /* num_points */) {
87  assert(!m_str.empty());
88  std::string str;
89 
90  using std::swap;
91  swap(str, m_str);
92 
93  str.back() = ')';
94  return str;
95  }
96 
97  /* MultiPolygon */
98 
99  void multipolygon_start() {
100  m_str = "MULTIPOLYGON(";
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 
138  using std::swap;
139  swap(str, m_str);
140 
141  str.back() = ')';
142  return str;
143  }
144 
145  }; // class WKTFactoryImpl
146 
147  } // namespace detail
148 
149  template <typename TProjection = IdentityProjection>
151 
152  } // namespace geom
153 
154 } // namespace osmium
155 
156 #endif // OSMIUM_GEOM_WKT_HPP
Definition: factory.hpp:146
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
Definition: coordinates.hpp:46
void append_to_string(std::string &s, const char infix, int precision) const
Definition: coordinates.hpp:57