Libosmium  2.8.0
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-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 <cstddef>
38 #include <string>
39 #include <utility>
40 
42 #include <osmium/geom/factory.hpp>
43 
44 namespace osmium {
45 
46  namespace geom {
47 
48  enum class wkt_type : bool {
49  wkt = false,
50  ewkt = true
51  }; // enum class wkt_type
52 
53  namespace detail {
54 
55  class WKTFactoryImpl {
56 
57  std::string m_srid_prefix;
58  std::string m_str;
59  int m_precision;
60  wkt_type m_wkt_type;
61 
62  void init_srid() {
63  }
64 
65  public:
66 
67  using point_type = std::string;
68  using linestring_type = std::string;
69  using polygon_type = std::string;
70  using multipolygon_type = std::string;
71  using ring_type = std::string;
72 
73  WKTFactoryImpl(int srid, int precision = 7, wkt_type wtype = wkt_type::wkt) :
74  m_srid_prefix(),
75  m_precision(precision),
76  m_wkt_type(wtype) {
77  if (m_wkt_type == wkt_type::ewkt) {
78  m_srid_prefix = "SRID=";
79  m_srid_prefix += std::to_string(srid);
80  m_srid_prefix += ';';
81  }
82  }
83 
84  /* Point */
85 
86  point_type make_point(const osmium::geom::Coordinates& xy) const {
87  std::string str {m_srid_prefix};
88  str += "POINT";
89  xy.append_to_string(str, '(', ' ', ')', m_precision);
90  return str;
91  }
92 
93  /* LineString */
94 
95  void linestring_start() {
96  m_str = m_srid_prefix;
97  m_str += "LINESTRING(";
98  }
99 
100  void linestring_add_location(const osmium::geom::Coordinates& xy) {
101  xy.append_to_string(m_str, ' ', m_precision);
102  m_str += ',';
103  }
104 
105  linestring_type linestring_finish(size_t /* num_points */) {
106  assert(!m_str.empty());
107  std::string str;
108 
109  using std::swap;
110  swap(str, m_str);
111 
112  str.back() = ')';
113  return str;
114  }
115 
116  /* MultiPolygon */
117 
118  void multipolygon_start() {
119  m_str = m_srid_prefix;
120  m_str += "MULTIPOLYGON(";
121  }
122 
123  void multipolygon_polygon_start() {
124  m_str += '(';
125  }
126 
127  void multipolygon_polygon_finish() {
128  m_str += "),";
129  }
130 
131  void multipolygon_outer_ring_start() {
132  m_str += '(';
133  }
134 
135  void multipolygon_outer_ring_finish() {
136  assert(!m_str.empty());
137  m_str.back() = ')';
138  }
139 
140  void multipolygon_inner_ring_start() {
141  m_str += ",(";
142  }
143 
144  void multipolygon_inner_ring_finish() {
145  assert(!m_str.empty());
146  m_str.back() = ')';
147  }
148 
149  void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
150  xy.append_to_string(m_str, ' ', m_precision);
151  m_str += ',';
152  }
153 
154  multipolygon_type multipolygon_finish() {
155  assert(!m_str.empty());
156  std::string str;
157 
158  using std::swap;
159  swap(str, m_str);
160 
161  str.back() = ')';
162  return str;
163  }
164 
165  }; // class WKTFactoryImpl
166 
167  } // namespace detail
168 
169  template <typename TProjection = IdentityProjection>
171 
172  } // namespace geom
173 
174 } // namespace osmium
175 
176 #endif // OSMIUM_GEOM_WKT_HPP
Definition: factory.hpp:146
wkt_type
Definition: wkt.hpp:48
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:731
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
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