forked from systemed/tilemaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.cpp
128 lines (103 loc) · 4.02 KB
/
helpers.cpp
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
#include "helpers.h"
#include <string>
#include <stdexcept>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#define MOD_GZIP_ZLIB_WINDOWSIZE 15
#define MOD_GZIP_ZLIB_CFACTOR 9
#define MOD_GZIP_ZLIB_BSIZE 8096
namespace geom = boost::geometry;
using namespace std;
// zlib routines from http://panthema.net/2007/0328-ZLibString.html
// Compress a STL string using zlib with given compression level, and return the binary data
std::string compress_string(const std::string& str,
int compressionlevel,
bool asGzip) {
z_stream zs; // z_stream is zlib's control structure
memset(&zs, 0, sizeof(zs));
if (asGzip) {
if (deflateInit2(&zs, compressionlevel, Z_DEFLATED,
MOD_GZIP_ZLIB_WINDOWSIZE + 16, MOD_GZIP_ZLIB_CFACTOR, Z_DEFAULT_STRATEGY) != Z_OK)
throw(std::runtime_error("deflateInit2 failed while compressing."));
} else {
if (deflateInit(&zs, compressionlevel) != Z_OK)
throw(std::runtime_error("deflateInit failed while compressing."));
}
zs.next_in = (Bytef*)str.data();
zs.avail_in = str.size(); // set the z_stream's input
int ret;
char outbuffer[32768];
std::string outstring;
// retrieve the compressed bytes blockwise
do {
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = deflate(&zs, Z_FINISH);
if (outstring.size() < zs.total_out) {
// append the block to the output string
outstring.append(outbuffer,
zs.total_out - outstring.size());
}
} while (ret == Z_OK);
deflateEnd(&zs);
if (ret != Z_STREAM_END) { // an error occurred that was not EOF
std::ostringstream oss;
oss << "Exception during zlib compression: (" << ret << ") " << zs.msg;
throw(std::runtime_error(oss.str()));
}
return outstring;
}
// Decompress an STL string using zlib and return the original data.
std::string decompress_string(const std::string& str, bool asGzip) {
z_stream zs; // z_stream is zlib's control structure
memset(&zs, 0, sizeof(zs));
if (asGzip) {
if (inflateInit2(&zs, 16+MAX_WBITS) != Z_OK)
throw(std::runtime_error("inflateInit2 failed while decompressing."));
} else {
if (inflateInit(&zs) != Z_OK)
throw(std::runtime_error("inflateInit failed while decompressing."));
}
zs.next_in = (Bytef*)str.data();
zs.avail_in = str.size();
int ret;
char outbuffer[32768];
std::string outstring;
// get the decompressed bytes blockwise using repeated calls to inflate
do {
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = inflate(&zs, 0);
if (outstring.size() < zs.total_out) {
outstring.append(outbuffer,
zs.total_out - outstring.size());
}
} while (ret == Z_OK);
inflateEnd(&zs);
if (ret != Z_STREAM_END) { // an error occurred that was not EOF
std::ostringstream oss;
oss << "Exception during zlib decompression: (" << ret << ") "
<< zs.msg;
throw(std::runtime_error(oss.str()));
}
return outstring;
}
// Parse a Boost error
std::string boost_validity_error(unsigned failure) {
switch (failure) {
case 10: return "too few points";
case 11: return "wrong topological dimension";
case 12: return "spikes (nodes go back on themselves)";
case 13: return "consecutive duplicate points";
case 20: return "not been closed";
case 21: return "self-intersections";
case 22: return "the wrong orientation";
case 30: return "interior rings outside";
case 31: return "nested interior rings";
case 32: return "disconnected interior (contains polygons whose interiors are not disjoint)";
case 40: return "intersecting interiors";
default: return "something mysterious wrong with it, Boost validity_failure_type " + to_string(failure);
}
}