Libosmium  2.2.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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  std::swap(str, m_str);
90  str.back() = ')';
91  return str;
92  }
93 
94  /* MultiPolygon */
95 
96  void multipolygon_start() {
97  m_str = "MULTIPOLYGON(";
98  }
99 
100  void multipolygon_polygon_start() {
101  m_str += '(';
102  }
103 
104  void multipolygon_polygon_finish() {
105  m_str += "),";
106  }
107 
108  void multipolygon_outer_ring_start() {
109  m_str += '(';
110  }
111 
112  void multipolygon_outer_ring_finish() {
113  assert(!m_str.empty());
114  m_str.back() = ')';
115  }
116 
117  void multipolygon_inner_ring_start() {
118  m_str += ",(";
119  }
120 
121  void multipolygon_inner_ring_finish() {
122  assert(!m_str.empty());
123  m_str.back() = ')';
124  }
125 
126  void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
127  xy.append_to_string(m_str, ' ', m_precision);
128  m_str += ',';
129  }
130 
131  multipolygon_type multipolygon_finish() {
132  assert(!m_str.empty());
133  std::string str;
134  std::swap(str, m_str);
135  str.back() = ')';
136  return str;
137  }
138 
139  }; // class WKTFactoryImpl
140 
141  } // namespace detail
142 
143  template <class TProjection = IdentityProjection>
145 
146  } // namespace geom
147 
148 } // namespace osmium
149 
150 #endif // OSMIUM_GEOM_WKT_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