/usr/include/mapnik/util/geometry_to_wkb.hpp is in libmapnik-dev 2.2.0+ds1-6build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | /*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2011 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#ifndef MAPNIK_GEOMETRY_TO_WKB_HPP
#define MAPNIK_GEOMETRY_TO_WKB_HPP
// mapnik
#include <mapnik/global.hpp>
#include <mapnik/geometry.hpp>
// boost
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/interprocess/streams/bufferstream.hpp>
#include <boost/foreach.hpp>
// stl
#include <vector>
#include <cstdio>
namespace mapnik { namespace util {
std::string to_hex(const char* blob, unsigned size)
{
std::string buf;
buf.reserve(size*2);
std::ostringstream s(buf);
s.seekp(0);
char hex[3];
std::memset(hex,0,3);
for ( unsigned pos=0; pos < size; ++pos)
{
std::sprintf (hex, "%02x", int(blob[pos]) & 0xff);
s << hex;
}
return s.str();
}
enum wkbByteOrder {
wkbXDR=0,
wkbNDR=1
};
inline void reverse_bytes(char size, char *address)
{
char * first = address;
char * last = first + size - 1;
for(;first < last;++first, --last)
{
char x = *last;
*last = *first;
*first = x;
}
}
template <typename S, typename T>
inline void write (S & stream, T val, std::size_t size, wkbByteOrder byte_order)
{
#ifdef MAPNIK_BIG_ENDIAN
bool need_swap = byte_order ? wkbNDR : wkbXDR;
#else
bool need_swap = byte_order ? wkbXDR : wkbNDR;
#endif
char* buf = reinterpret_cast<char*>(&val);
if (need_swap)
{
reverse_bytes(size,buf);
}
stream.write(buf,size);
}
struct wkb_buffer
{
wkb_buffer(std::size_t size)
: size_(size),
data_( (size_!=0) ? static_cast<char*>(::operator new (size_)):0)
{}
~wkb_buffer()
{
::operator delete(data_);
}
inline std::size_t size() const
{
return size_;
}
inline char* buffer()
{
return data_;
}
std::size_t size_;
char * data_;
};
typedef boost::shared_ptr<wkb_buffer> wkb_buffer_ptr;
template<typename GeometryType>
wkb_buffer_ptr to_point_wkb( GeometryType const& g, wkbByteOrder byte_order)
{
assert(g.size() == 1);
std::size_t size = 1 + 4 + 8*2 ; // byteOrder + wkbType + Point
wkb_buffer_ptr wkb = boost::make_shared<wkb_buffer>(size);
boost::interprocess::bufferstream ss(wkb->buffer(), wkb->size(), std::ios::out | std::ios::binary);
ss.write(reinterpret_cast<char*>(&byte_order),1);
int type = static_cast<int>(mapnik::Point);
write(ss,type,4,byte_order);
double x = 0;
double y = 0;
g.vertex(0,&x,&y);
write(ss,x,8,byte_order);
write(ss,y,8,byte_order);
assert(ss.good());
return wkb;
}
template<typename GeometryType>
wkb_buffer_ptr to_line_string_wkb( GeometryType const& g, wkbByteOrder byte_order)
{
unsigned num_points = g.size();
assert(num_points > 1);
std::size_t size = 1 + 4 + 4 + 8*2*num_points ; // byteOrder + wkbType + numPoints + Point*numPoints
wkb_buffer_ptr wkb = boost::make_shared<wkb_buffer>(size);
boost::interprocess::bufferstream ss(wkb->buffer(), wkb->size(), std::ios::out | std::ios::binary);
ss.write(reinterpret_cast<char*>(&byte_order),1);
int type = static_cast<int>(mapnik::LineString);
write(ss,type,4,byte_order);
write(ss,num_points,4,byte_order);
double x = 0;
double y = 0;
for (unsigned i=0; i< num_points; ++i)
{
g.vertex(i,&x,&y);
write(ss,x,8,byte_order);
write(ss,y,8,byte_order);
}
assert(ss.good());
return wkb;
}
template<typename GeometryType>
wkb_buffer_ptr to_polygon_wkb( GeometryType const& g, wkbByteOrder byte_order)
{
unsigned num_points = g.size();
assert(num_points > 1);
typedef std::pair<double,double> point_type;
typedef std::vector<point_type> linear_ring;
boost::ptr_vector<linear_ring> rings;
double x = 0;
double y = 0;
std::size_t size = 1 + 4 + 4 ; // byteOrder + wkbType + numRings
for (unsigned i=0; i< num_points; ++i)
{
unsigned command = g.vertex(i,&x,&y);
if (command == SEG_MOVETO)
{
rings.push_back(new linear_ring); // start new loop
rings.back().push_back(std::make_pair(x,y));
size += 4; // num_points
size += 2 * 8; // point
}
else if (command == SEG_LINETO)
{
rings.back().push_back(std::make_pair(x,y));
size += 2 * 8; // point
}
}
unsigned num_rings = rings.size();
wkb_buffer_ptr wkb = boost::make_shared<wkb_buffer>(size);
boost::interprocess::bufferstream ss(wkb->buffer(), wkb->size(), std::ios::out | std::ios::binary);
ss.write(reinterpret_cast<char*>(&byte_order),1);
int type = static_cast<int>(mapnik::Polygon);
write(ss,type,4,byte_order);
write(ss,num_rings,4,byte_order);
BOOST_FOREACH ( linear_ring const& ring, rings)
{
unsigned num_ring_points = ring.size();
write(ss,num_ring_points,4,byte_order);
BOOST_FOREACH ( point_type const& pt, ring)
{
write(ss,pt.first,8,byte_order);
write(ss,pt.second,8,byte_order);
}
}
assert(ss.good());
return wkb;
}
template<typename GeometryType>
wkb_buffer_ptr to_wkb(GeometryType const& g, wkbByteOrder byte_order )
{
wkb_buffer_ptr wkb;
switch (g.type())
{
case mapnik::Point:
wkb = to_point_wkb(g, byte_order);
break;
case mapnik::LineString:
wkb = to_line_string_wkb(g, byte_order);
break;
case mapnik::Polygon:
wkb = to_polygon_wkb(g, byte_order);
break;
default:
break;
}
return wkb;
}
wkb_buffer_ptr to_wkb(geometry_container const& paths, wkbByteOrder byte_order )
{
if (paths.size() == 1)
{
// single geometry
return to_wkb(paths.front(), byte_order);
}
if (paths.size() > 1)
{
// multi geometry or geometry collection
std::vector<wkb_buffer_ptr> wkb_cont;
bool collection = false;
int multi_type = 0;
size_t multi_size = 1 + 4 + 4;
geometry_container::const_iterator itr = paths.begin();
geometry_container::const_iterator end = paths.end();
for ( ; itr!=end; ++itr)
{
wkb_buffer_ptr wkb = to_wkb(*itr,byte_order);
multi_size += wkb->size();
int type = static_cast<int>(itr->type());
if (multi_type > 0 && multi_type != itr->type())
collection = true;
multi_type = type;
wkb_cont.push_back(wkb);
}
wkb_buffer_ptr multi_wkb = boost::make_shared<wkb_buffer>(multi_size);
boost::interprocess::bufferstream ss(multi_wkb->buffer(), multi_wkb->size(), std::ios::out | std::ios::binary);
ss.write(reinterpret_cast<char*>(&byte_order),1);
multi_type = collection ? 7 : multi_type + 3;
write(ss,multi_type, 4, byte_order);
write(ss,paths.size(),4,byte_order);
BOOST_FOREACH ( wkb_buffer_ptr const& wkb, wkb_cont)
{
ss.write(wkb->buffer(),wkb->size());
}
return multi_wkb;
}
return wkb_buffer_ptr();
}
}}
#endif // MAPNIK_GEOMETRY_TO_WKB_HPP
|