1 #ifndef OSMIUM_GEOM_WKB_HPP
2 #define OSMIUM_GEOM_WKB_HPP
42 #if !defined(_WIN32) && !defined(__APPLE__)
45 # define __LITTLE_ENDIAN 1234
46 # define __BYTE_ORDER __LITTLE_ENDIAN
70 inline void str_push(std::string& str, T data) {
71 size_t size = str.size();
72 str.resize(size +
sizeof(T));
73 std::copy_n(reinterpret_cast<char*>(&data),
sizeof(T), &str[size]);
76 inline std::string convert_to_hex(
const std::string& str) {
77 static const char* lookup_hex =
"0123456789ABCDEF";
81 out += lookup_hex[(c >> 4) & 0xf];
82 out += lookup_hex[c & 0xf];
88 class WKBFactoryImpl {
91 static constexpr uint32_t srid = 4326;
100 enum wkbGeometryType : uint32_t {
105 wkbMultiLineString = 5,
107 wkbGeometryCollection = 7,
116 enum class wkb_byte_order_type : uint8_t {
122 uint32_t m_points {0};
126 size_t m_linestring_size_offset = 0;
127 size_t m_polygons = 0;
129 size_t m_multipolygon_size_offset = 0;
130 size_t m_polygon_size_offset = 0;
131 size_t m_ring_size_offset = 0;
133 size_t header(std::string& str, wkbGeometryType
type,
bool add_length)
const {
134 #if __BYTE_ORDER == __LITTLE_ENDIAN
135 str_push(str, wkb_byte_order_type::NDR);
137 str_push(str, wkb_byte_order_type::XDR);
140 str_push(str, type | wkbSRID);
145 size_t offset = str.size();
147 str_push(str, static_cast<uint32_t>(0));
152 void set_size(
const size_t offset,
const size_t size) {
153 *
reinterpret_cast<uint32_t*
>(&m_data[offset]) = static_cast_with_assert<uint32_t>(size);
158 typedef std::string point_type;
159 typedef std::string linestring_type;
160 typedef std::string polygon_type;
161 typedef std::string multipolygon_type;
162 typedef std::string ring_type;
173 header(data, wkbPoint,
false);
174 str_push(data, xy.
x);
175 str_push(data, xy.
y);
178 return convert_to_hex(data);
186 void linestring_start() {
188 m_linestring_size_offset = header(m_data, wkbLineString,
true);
192 str_push(m_data, xy.
x);
193 str_push(m_data, xy.
y);
196 linestring_type linestring_finish(
size_t num_points) {
197 set_size(m_linestring_size_offset, num_points);
199 std::swap(data, m_data);
202 return convert_to_hex(data);
210 void multipolygon_start() {
213 m_multipolygon_size_offset = header(m_data, wkbMultiPolygon,
true);
216 void multipolygon_polygon_start() {
219 m_polygon_size_offset = header(m_data, wkbPolygon,
true);
222 void multipolygon_polygon_finish() {
223 set_size(m_polygon_size_offset, m_rings);
226 void multipolygon_outer_ring_start() {
229 m_ring_size_offset = m_data.size();
230 str_push(m_data, static_cast<uint32_t>(0));
233 void multipolygon_outer_ring_finish() {
234 set_size(m_ring_size_offset, m_points);
237 void multipolygon_inner_ring_start() {
240 m_ring_size_offset = m_data.size();
241 str_push(m_data, static_cast<uint32_t>(0));
244 void multipolygon_inner_ring_finish() {
245 set_size(m_ring_size_offset, m_points);
249 str_push(m_data, xy.
x);
250 str_push(m_data, xy.
y);
254 multipolygon_type multipolygon_finish() {
255 set_size(m_multipolygon_size_offset, m_polygons);
257 std::swap(data, m_data);
260 return convert_to_hex(data);
270 template <
class TProjection = IdentityProjection>
277 #endif // OSMIUM_GEOM_WKB_HPP
double y
Definition: coordinates.hpp:50
Definition: factory.hpp:117
type
Definition: entity_bits.hpp:60
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
Definition: coordinates.hpp:47
wkb_type
Definition: wkb.hpp:57
out_type
Definition: wkb.hpp:62
double x
Definition: coordinates.hpp:49