1 #ifndef OSMIUM_GEOM_WKB_HPP 2 #define OSMIUM_GEOM_WKB_HPP 63 inline void str_push(std::string& str, T data) {
64 str.append(reinterpret_cast<const char*>(&data),
sizeof(T));
67 inline std::string convert_to_hex(
const std::string& str) {
68 static const char* lookup_hex =
"0123456789ABCDEF";
70 out.reserve(str.size() * 2);
73 out += lookup_hex[(
static_cast<unsigned int>(c) >> 4u) & 0xfu];
74 out += lookup_hex[
static_cast<unsigned int>(c) & 0xfu];
80 class WKBFactoryImpl {
89 enum wkbGeometryType : uint32_t {
94 wkbMultiLineString = 5,
96 wkbGeometryCollection = 7,
105 enum class wkb_byte_order_type : uint8_t {
111 uint32_t m_points = 0;
116 std::size_t m_linestring_size_offset = 0;
117 std::size_t m_polygons = 0;
118 std::size_t m_rings = 0;
119 std::size_t m_multipolygon_size_offset = 0;
120 std::size_t m_polygon_size_offset = 0;
121 std::size_t m_ring_size_offset = 0;
123 std::size_t header(std::string& str, wkbGeometryType
type,
bool add_length)
const {
124 #if __BYTE_ORDER == __LITTLE_ENDIAN 125 str_push(str, wkb_byte_order_type::NDR);
127 str_push(str, wkb_byte_order_type::XDR);
130 str_push(str,
type | wkbSRID);
131 str_push(str, m_srid);
135 const std::size_t offset = str.size();
137 str_push(str, static_cast<uint32_t>(0));
142 void set_size(
const std::size_t offset,
const std::size_t size) {
143 if (size > std::numeric_limits<uint32_t>::max()) {
144 throw geometry_error{
"Too many points in geometry"};
146 const auto s =
static_cast<uint32_t
>(size);
147 std::copy_n(reinterpret_cast<const char*>(&s),
sizeof(uint32_t), &m_data[offset]);
152 using point_type = std::string;
153 using linestring_type = std::string;
154 using polygon_type = std::string;
155 using multipolygon_type = std::string;
156 using ring_type = std::string;
168 header(data, wkbPoint,
false);
169 str_push(data, xy.
x);
170 str_push(data, xy.
y);
173 return convert_to_hex(data);
181 void linestring_start() {
183 m_linestring_size_offset = header(m_data, wkbLineString,
true);
187 str_push(m_data, xy.
x);
188 str_push(m_data, xy.
y);
191 linestring_type linestring_finish(std::size_t num_points) {
192 set_size(m_linestring_size_offset, num_points);
199 return convert_to_hex(data);
207 void multipolygon_start() {
210 m_multipolygon_size_offset = header(m_data, wkbMultiPolygon,
true);
213 void multipolygon_polygon_start() {
216 m_polygon_size_offset = header(m_data, wkbPolygon,
true);
219 void multipolygon_polygon_finish() {
220 set_size(m_polygon_size_offset, m_rings);
223 void multipolygon_outer_ring_start() {
226 m_ring_size_offset = m_data.size();
227 str_push(m_data, static_cast<uint32_t>(0));
230 void multipolygon_outer_ring_finish() {
231 set_size(m_ring_size_offset, m_points);
234 void multipolygon_inner_ring_start() {
237 m_ring_size_offset = m_data.size();
238 str_push(m_data, static_cast<uint32_t>(0));
241 void multipolygon_inner_ring_finish() {
242 set_size(m_ring_size_offset, m_points);
246 str_push(m_data, xy.
x);
247 str_push(m_data, xy.
y);
251 multipolygon_type multipolygon_finish() {
252 set_size(m_multipolygon_size_offset, m_polygons);
259 return convert_to_hex(data);
269 template <
typename TProjection = IdentityProjection>
276 #endif // OSMIUM_GEOM_WKB_HPP double y
Definition: coordinates.hpp:51
Definition: factory.hpp:148
type
Definition: entity_bits.hpp:63
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:755
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: coordinates.hpp:48
wkb_type
Definition: wkb.hpp:50
out_type
Definition: wkb.hpp:55
double x
Definition: coordinates.hpp:50