2014-09-22 22:41:13 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
2014-09-24 23:51:53 +00:00
|
|
|
#include <stack>
|
2014-09-30 22:53:45 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
2014-09-23 00:46:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2014-09-22 22:41:13 +00:00
|
|
|
#include <zlib.h>
|
2014-09-23 00:46:48 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2014-09-23 21:42:17 +00:00
|
|
|
#include <math.h>
|
2014-09-25 23:34:17 +00:00
|
|
|
#include <sqlite3.h>
|
2014-09-22 22:41:13 +00:00
|
|
|
#include "vector_tile.pb.h"
|
|
|
|
|
2014-09-22 23:06:44 +00:00
|
|
|
extern "C" {
|
|
|
|
#include "tile.h"
|
2014-09-29 19:17:35 +00:00
|
|
|
#include "pool.h"
|
2014-09-27 00:07:52 +00:00
|
|
|
#include "clip.h"
|
2014-09-29 19:48:58 +00:00
|
|
|
#include "mbtiles.h"
|
2014-09-22 23:06:44 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 00:12:38 +00:00
|
|
|
#define CMD_BITS 3
|
|
|
|
|
2014-09-22 22:41:13 +00:00
|
|
|
// https://github.com/mapbox/mapnik-vector-tile/blob/master/src/vector_tile_compression.hpp
|
|
|
|
static inline int compress(std::string const& input, std::string& output) {
|
|
|
|
z_stream deflate_s;
|
|
|
|
deflate_s.zalloc = Z_NULL;
|
|
|
|
deflate_s.zfree = Z_NULL;
|
|
|
|
deflate_s.opaque = Z_NULL;
|
|
|
|
deflate_s.avail_in = 0;
|
|
|
|
deflate_s.next_in = Z_NULL;
|
|
|
|
deflateInit(&deflate_s, Z_DEFAULT_COMPRESSION);
|
|
|
|
deflate_s.next_in = (Bytef *)input.data();
|
|
|
|
deflate_s.avail_in = input.size();
|
|
|
|
size_t length = 0;
|
|
|
|
do {
|
|
|
|
size_t increase = input.size() / 2 + 1024;
|
|
|
|
output.resize(length + increase);
|
|
|
|
deflate_s.avail_out = increase;
|
|
|
|
deflate_s.next_out = (Bytef *)(output.data() + length);
|
|
|
|
int ret = deflate(&deflate_s, Z_FINISH);
|
|
|
|
if (ret != Z_STREAM_END && ret != Z_OK && ret != Z_BUF_ERROR) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
length += (increase - deflate_s.avail_out);
|
|
|
|
} while (deflate_s.avail_out == 0);
|
|
|
|
deflateEnd(&deflate_s);
|
|
|
|
output.resize(length);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-24 01:19:06 +00:00
|
|
|
struct draw {
|
|
|
|
int op;
|
|
|
|
long long x;
|
|
|
|
long long y;
|
2014-09-24 23:51:53 +00:00
|
|
|
int necessary;
|
2014-09-30 23:27:00 +00:00
|
|
|
|
|
|
|
draw(int op, long long x, long long y) {
|
|
|
|
this->op = op;
|
|
|
|
this->x = x;
|
|
|
|
this->y = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
draw() { }
|
2014-09-24 01:19:06 +00:00
|
|
|
};
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
typedef std::vector<draw> drawvec;
|
|
|
|
|
|
|
|
drawvec decode_feature(char **meta, int z, unsigned tx, unsigned ty, int detail) {
|
|
|
|
drawvec out;
|
2014-09-23 18:30:32 +00:00
|
|
|
|
|
|
|
while (1) {
|
2014-09-30 22:53:45 +00:00
|
|
|
draw d;
|
2014-09-23 18:30:32 +00:00
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
deserialize_int(meta, &d.op);
|
|
|
|
if (d.op == VT_END) {
|
2014-09-23 18:30:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
if (d.op == VT_MOVETO || d.op == VT_LINETO) {
|
2014-09-23 18:30:32 +00:00
|
|
|
int wx, wy;
|
|
|
|
deserialize_int(meta, &wx);
|
|
|
|
deserialize_int(meta, &wy);
|
|
|
|
|
|
|
|
long long wwx = (unsigned) wx;
|
|
|
|
long long wwy = (unsigned) wy;
|
|
|
|
|
|
|
|
if (z != 0) {
|
|
|
|
wwx -= tx << (32 - z);
|
|
|
|
wwy -= ty << (32 - z);
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
d.x = wwx;
|
|
|
|
d.y = wwy;
|
2014-09-24 01:08:31 +00:00
|
|
|
}
|
2014-09-24 01:19:06 +00:00
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
out.push_back(d);
|
2014-09-24 01:08:31 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
return out;
|
2014-09-24 01:08:31 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 23:27:00 +00:00
|
|
|
int to_feature(drawvec &geom, mapnik::vector::tile_feature *feature) {
|
2014-09-24 01:08:31 +00:00
|
|
|
int px = 0, py = 0;
|
|
|
|
int cmd_idx = -1;
|
|
|
|
int cmd = -1;
|
|
|
|
int length = 0;
|
|
|
|
int drew = 0;
|
2014-09-24 04:40:23 +00:00
|
|
|
int i;
|
2014-09-24 01:08:31 +00:00
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
int n = geom.size();
|
2014-09-24 04:40:23 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
int op = geom[i].op;
|
2014-09-24 01:08:31 +00:00
|
|
|
|
|
|
|
if (op != cmd) {
|
|
|
|
if (cmd_idx >= 0) {
|
|
|
|
if (feature != NULL) {
|
|
|
|
feature->set_geometry(cmd_idx, (length << CMD_BITS) | (cmd & ((1 << CMD_BITS) - 1)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = op;
|
|
|
|
length = 0;
|
|
|
|
|
|
|
|
if (feature != NULL) {
|
|
|
|
cmd_idx = feature->geometry_size();
|
|
|
|
feature->add_geometry(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (op == VT_MOVETO || op == VT_LINETO) {
|
2014-09-24 04:40:23 +00:00
|
|
|
long long wwx = geom[i].x;
|
|
|
|
long long wwy = geom[i].y;
|
2014-09-24 01:08:31 +00:00
|
|
|
|
2014-09-23 18:30:32 +00:00
|
|
|
int dx = wwx - px;
|
|
|
|
int dy = wwy - py;
|
|
|
|
|
2014-09-24 18:42:20 +00:00
|
|
|
if (feature != NULL) {
|
|
|
|
feature->add_geometry((dx << 1) ^ (dx >> 31));
|
|
|
|
feature->add_geometry((dy << 1) ^ (dy >> 31));
|
|
|
|
}
|
2014-09-23 18:30:32 +00:00
|
|
|
|
2014-09-24 18:42:20 +00:00
|
|
|
px = wwx;
|
|
|
|
py = wwy;
|
|
|
|
length++;
|
|
|
|
|
|
|
|
if (op == VT_LINETO && (dx != 0 || dy != 0)) {
|
|
|
|
drew = 1;
|
2014-09-23 18:30:32 +00:00
|
|
|
}
|
|
|
|
} else if (op == VT_CLOSEPATH) {
|
|
|
|
length++;
|
2014-09-30 23:37:09 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "\nInternal error: corrupted geometry\n");
|
|
|
|
exit(EXIT_FAILURE);
|
2014-09-23 18:30:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd_idx >= 0) {
|
|
|
|
if (feature != NULL) {
|
|
|
|
feature->set_geometry(cmd_idx, (length << CMD_BITS) | (cmd & ((1 << CMD_BITS) - 1)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return drew;
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
drawvec remove_noop(drawvec geom, int type) {
|
2014-09-24 18:42:20 +00:00
|
|
|
// first pass: remove empty linetos
|
|
|
|
|
|
|
|
long long x = 0, y = 0;
|
2014-09-30 22:53:45 +00:00
|
|
|
drawvec out;
|
|
|
|
unsigned i;
|
2014-09-24 18:42:20 +00:00
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
for (i = 0; i < geom.size(); i++) {
|
2014-09-24 18:42:20 +00:00
|
|
|
if (geom[i].op == VT_LINETO && geom[i].x == x && geom[i].y == y) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (geom[i].op == VT_CLOSEPATH) {
|
2014-09-30 22:53:45 +00:00
|
|
|
out.push_back(geom[i]);
|
2014-09-24 18:42:20 +00:00
|
|
|
} else { /* moveto or lineto */
|
2014-09-30 22:53:45 +00:00
|
|
|
out.push_back(geom[i]);
|
2014-09-24 18:42:20 +00:00
|
|
|
x = geom[i].x;
|
|
|
|
y = geom[i].y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// second pass: remove unused movetos
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
geom = out;
|
|
|
|
out.resize(0);
|
2014-09-24 18:42:20 +00:00
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
for (i = 0; i < geom.size(); i++) {
|
2014-09-24 18:42:20 +00:00
|
|
|
if (geom[i].op == VT_MOVETO) {
|
2014-09-30 22:53:45 +00:00
|
|
|
if (i + 1 >= geom.size()) {
|
2014-09-24 18:42:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (geom[i + 1].op == VT_MOVETO) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (geom[i + 1].op == VT_CLOSEPATH) {
|
|
|
|
i++; // also remove unused closepath
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
out.push_back(geom[i]);
|
2014-09-24 18:42:20 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 22:43:24 +00:00
|
|
|
// second pass: remove empty movetos
|
|
|
|
|
|
|
|
if (type == VT_LINE) {
|
2014-09-30 22:53:45 +00:00
|
|
|
geom = out;
|
|
|
|
out.resize(0);
|
2014-09-24 22:43:24 +00:00
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
for (i = 0; i < geom.size(); i++) {
|
2014-09-24 22:43:24 +00:00
|
|
|
if (geom[i].op == VT_MOVETO) {
|
2014-09-30 22:53:45 +00:00
|
|
|
if (i > 0 && geom[i - 1].op == VT_LINETO && geom[i - 1].x == geom[i].x && geom[i - 1].y == geom[i].y) {
|
2014-09-24 22:43:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
out.push_back(geom[i]);
|
2014-09-24 22:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 18:42:20 +00:00
|
|
|
return out;
|
|
|
|
}
|
2014-09-22 23:39:25 +00:00
|
|
|
|
2014-09-24 21:58:26 +00:00
|
|
|
int shrink_lines(struct draw *geom, int len, int z, int basezoom) {
|
|
|
|
double scale = 1.0 / exp(log(sqrt(2.5)) * (basezoom - z));
|
|
|
|
struct draw tmp[3 * len];
|
|
|
|
int out = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (i > 0 && (geom[i - 1].op == VT_MOVETO || geom[i - 1].op == VT_LINETO) && geom[i].op == VT_LINETO) {
|
|
|
|
long long cx = (geom[i].x + geom[i - 1].x) / 2;
|
|
|
|
long long cy = (geom[i].y + geom[i - 1].y) / 2;
|
|
|
|
|
|
|
|
tmp[out + 0].op = VT_MOVETO;
|
|
|
|
tmp[out + 0].x = cx + (geom[i - 1].x - cx) * scale;
|
|
|
|
tmp[out + 0].y = cy + (geom[i - 1].y - cy) * scale;
|
|
|
|
|
|
|
|
tmp[out + 1].op = VT_LINETO;
|
|
|
|
tmp[out + 1].x = cx + (geom[i].x - cx) * scale;
|
|
|
|
tmp[out + 1].y = cy + (geom[i].y - cy) * scale;
|
|
|
|
|
|
|
|
tmp[out + 2].op = VT_MOVETO;
|
|
|
|
tmp[out + 2].x = geom[i].x;
|
|
|
|
tmp[out + 2].y = geom[i].y;
|
|
|
|
|
|
|
|
out += 3;
|
|
|
|
} else {
|
|
|
|
tmp[out++] = geom[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(geom, tmp, out * sizeof(struct draw));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
void to_tile_scale(drawvec &geom, int z, int detail) {
|
|
|
|
unsigned i;
|
2014-09-24 21:14:43 +00:00
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
for (i = 0; i < geom.size(); i++) {
|
2014-09-24 21:14:43 +00:00
|
|
|
geom[i].x >>= (32 - detail - z);
|
|
|
|
geom[i].y >>= (32 - detail - z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 23:51:53 +00:00
|
|
|
double square_distance_from_line(long long point_x, long long point_y, long long segA_x, long long segA_y, long long segB_x, long long segB_y) {
|
|
|
|
double p2x = segB_x - segA_x;
|
|
|
|
double p2y = segB_y - segA_y;
|
|
|
|
double something = p2x * p2x + p2y * p2y;
|
|
|
|
double u = 0 == something ? 0 : ((point_x - segA_x) * p2x + (point_y - segA_y) * p2y) / something;
|
|
|
|
|
|
|
|
if (u > 1) {
|
|
|
|
u = 1;
|
|
|
|
} else if (u < 0) {
|
|
|
|
u = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double x = segA_x + u * p2x;
|
|
|
|
double y = segA_y + u * p2y;
|
|
|
|
|
|
|
|
double dx = x - point_x;
|
|
|
|
double dy = y - point_y;
|
|
|
|
|
|
|
|
return dx * dx + dy * dy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/Project-OSRM/osrm-backend/blob/733d1384a40f/Algorithms/DouglasePeucker.cpp
|
2014-09-30 22:53:45 +00:00
|
|
|
void douglas_peucker(drawvec &geom, int start, int n, double e) {
|
2014-09-24 23:51:53 +00:00
|
|
|
e = e * e;
|
|
|
|
std::stack<int> recursion_stack;
|
|
|
|
|
|
|
|
{
|
|
|
|
int left_border = 0;
|
|
|
|
int right_border = 1;
|
|
|
|
// Sweep linerarily over array and identify those ranges that need to be checked
|
|
|
|
do {
|
2014-09-30 22:53:45 +00:00
|
|
|
if (geom[start + right_border].necessary) {
|
2014-09-24 23:51:53 +00:00
|
|
|
recursion_stack.push(left_border);
|
|
|
|
recursion_stack.push(right_border);
|
|
|
|
left_border = right_border;
|
|
|
|
}
|
|
|
|
++right_border;
|
|
|
|
} while (right_border < n);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!recursion_stack.empty()) {
|
|
|
|
// pop next element
|
|
|
|
int second = recursion_stack.top();
|
|
|
|
recursion_stack.pop();
|
|
|
|
int first = recursion_stack.top();
|
|
|
|
recursion_stack.pop();
|
|
|
|
|
2014-09-25 00:23:40 +00:00
|
|
|
double max_distance = -1;
|
2014-09-24 23:51:53 +00:00
|
|
|
int farthest_element_index = second;
|
|
|
|
|
|
|
|
// find index idx of element with max_distance
|
|
|
|
int i;
|
|
|
|
for (i = first + 1; i < second; i++) {
|
2014-09-30 22:53:45 +00:00
|
|
|
double temp_dist = square_distance_from_line(geom[start + i].x, geom[start + i].y,
|
|
|
|
geom[start + first].x, geom[start + first].y,
|
|
|
|
geom[start + second].x, geom[start + second].y);
|
2014-09-24 23:51:53 +00:00
|
|
|
|
|
|
|
double distance = fabs(temp_dist);
|
2014-09-25 00:23:40 +00:00
|
|
|
|
2014-09-24 23:51:53 +00:00
|
|
|
if (distance > e && distance > max_distance) {
|
|
|
|
farthest_element_index = i;
|
|
|
|
max_distance = distance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_distance > e) {
|
|
|
|
// mark idx as necessary
|
2014-09-30 22:53:45 +00:00
|
|
|
geom[start + farthest_element_index].necessary = 1;
|
2014-09-24 23:51:53 +00:00
|
|
|
|
|
|
|
if (1 < farthest_element_index - first) {
|
|
|
|
recursion_stack.push(first);
|
|
|
|
recursion_stack.push(farthest_element_index);
|
|
|
|
}
|
|
|
|
if (1 < second - farthest_element_index) {
|
|
|
|
recursion_stack.push(farthest_element_index);
|
|
|
|
recursion_stack.push(second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
drawvec clip_lines(drawvec &geom, int z, int detail) {
|
|
|
|
drawvec out;
|
|
|
|
unsigned i;
|
2014-09-27 00:07:52 +00:00
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
for (i = 0; i < geom.size(); i++) {
|
|
|
|
if (i > 0 && (geom[i - 1].op == VT_MOVETO || geom[i - 1].op == VT_LINETO) && geom[i].op == VT_LINETO) {
|
2014-09-27 00:07:52 +00:00
|
|
|
double x1 = geom[i - 1].x;
|
|
|
|
double y1 = geom[i - 1].y;
|
|
|
|
|
|
|
|
double x2 = geom[i - 0].x;
|
|
|
|
double y2 = geom[i - 0].y;
|
|
|
|
|
|
|
|
unsigned area = 0xFFFFFFFF;
|
|
|
|
if (z != 0) {
|
|
|
|
area = 1 << (32 - z);
|
|
|
|
}
|
|
|
|
|
|
|
|
int c = clip(&x1, &y1, &x2, &y2, 0, 0, area, area);
|
|
|
|
|
|
|
|
if (c > 1) { // clipped
|
2014-09-30 23:27:00 +00:00
|
|
|
out.push_back(draw(VT_MOVETO, x1, y1));
|
|
|
|
out.push_back(draw(VT_LINETO, x2, y2));
|
|
|
|
out.push_back(draw(VT_MOVETO, geom[i].x, geom[i].y));
|
2014-09-27 00:07:52 +00:00
|
|
|
} else if (c == 1) { // unchanged
|
2014-09-30 22:53:45 +00:00
|
|
|
out.push_back(geom[i]);
|
2014-09-27 00:07:52 +00:00
|
|
|
} else { // clipped away entirely
|
2014-09-30 23:27:00 +00:00
|
|
|
out.push_back(draw(VT_MOVETO, geom[i].x, geom[i].y));
|
2014-09-27 00:07:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-09-30 22:53:45 +00:00
|
|
|
out.push_back(geom[i]);
|
2014-09-27 00:07:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
drawvec simplify_lines(drawvec &geom, int z, int detail) {
|
2014-09-26 17:52:19 +00:00
|
|
|
int res = 1 << (32 - detail - z);
|
2014-09-24 23:51:53 +00:00
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i < geom.size(); i++) {
|
2014-09-24 23:51:53 +00:00
|
|
|
if (geom[i].op == VT_MOVETO) {
|
|
|
|
geom[i].necessary = 1;
|
|
|
|
} else if (geom[i].op == VT_LINETO) {
|
|
|
|
geom[i].necessary = 0;
|
|
|
|
} else {
|
|
|
|
geom[i].necessary = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
for (i = 0; i < geom.size(); i++) {
|
2014-09-24 23:51:53 +00:00
|
|
|
if (geom[i].op == VT_MOVETO) {
|
2014-09-30 22:53:45 +00:00
|
|
|
unsigned j;
|
|
|
|
for (j = i + 1; j < geom.size(); j++) {
|
2014-09-24 23:51:53 +00:00
|
|
|
if (geom[j].op == VT_CLOSEPATH || geom[j].op == VT_MOVETO) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
geom[i].necessary = 1;
|
|
|
|
geom[j - 1].necessary = 1;
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
douglas_peucker(geom, i, j - i, res);
|
2014-09-24 23:51:53 +00:00
|
|
|
i = j - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
drawvec out;
|
|
|
|
for (i = 0; i < geom.size(); i++) {
|
2014-09-24 23:51:53 +00:00
|
|
|
if (geom[i].necessary) {
|
2014-09-30 22:53:45 +00:00
|
|
|
out.push_back(geom[i]);
|
2014-09-24 23:51:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2014-10-01 00:18:23 +00:00
|
|
|
int coalindexcmp(const struct coalesce *c1, const struct coalesce *c2);
|
|
|
|
|
2014-09-29 21:46:15 +00:00
|
|
|
struct coalesce {
|
|
|
|
int type;
|
2014-09-30 22:53:45 +00:00
|
|
|
drawvec geom;
|
2014-10-01 00:24:34 +00:00
|
|
|
std::vector<int> meta;
|
2014-09-29 22:38:33 +00:00
|
|
|
unsigned long long index;
|
2014-10-01 00:18:23 +00:00
|
|
|
|
|
|
|
bool operator< (const coalesce &o) const {
|
|
|
|
int cmp = coalindexcmp(this, &o);
|
|
|
|
if (cmp < 0) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-09-29 21:46:15 +00:00
|
|
|
};
|
|
|
|
|
2014-09-29 22:33:14 +00:00
|
|
|
int coalcmp(const void *v1, const void *v2) {
|
|
|
|
const struct coalesce *c1 = (const struct coalesce *) v1;
|
|
|
|
const struct coalesce *c2 = (const struct coalesce *) v2;
|
|
|
|
|
|
|
|
int cmp = c1->type - c2->type;
|
|
|
|
if (cmp != 0) {
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
2014-10-01 00:24:34 +00:00
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i < c1->meta.size() && i < c2->meta.size(); i++) {
|
2014-09-29 22:33:14 +00:00
|
|
|
cmp = c1->meta[i] - c2->meta[i];
|
|
|
|
|
|
|
|
if (cmp != 0) {
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 00:24:34 +00:00
|
|
|
if (c1->meta.size() < c2->meta.size()) {
|
|
|
|
return -1;
|
|
|
|
} else if (c1->meta.size() > c2->meta.size()) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-09-29 22:33:14 +00:00
|
|
|
}
|
|
|
|
|
2014-10-01 00:18:23 +00:00
|
|
|
int coalindexcmp(const struct coalesce *c1, const struct coalesce *c2) {
|
|
|
|
int cmp = coalcmp((const void *) c1, (const void *) c2);
|
2014-09-29 22:38:33 +00:00
|
|
|
|
|
|
|
if (cmp == 0) {
|
|
|
|
if (c1->index < c2->index) {
|
|
|
|
return -1;
|
|
|
|
} else if (c1->index > c2->index) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
2014-09-25 23:34:17 +00:00
|
|
|
long long write_tile(struct index *start, struct index *end, char *metabase, unsigned *file_bbox, int z, unsigned tx, unsigned ty, int detail, int basezoom, struct pool *file_keys, char *layername, sqlite3 *outdb) {
|
2014-09-22 22:41:13 +00:00
|
|
|
GOOGLE_PROTOBUF_VERIFY_VERSION;
|
|
|
|
|
2014-09-23 17:57:50 +00:00
|
|
|
mapnik::vector::tile tile;
|
2014-09-22 22:41:13 +00:00
|
|
|
mapnik::vector::tile_layer *layer = tile.add_layers();
|
|
|
|
|
2014-09-25 06:22:14 +00:00
|
|
|
layer->set_name(layername);
|
2014-09-23 17:57:50 +00:00
|
|
|
layer->set_version(1);
|
2014-09-23 17:57:01 +00:00
|
|
|
|
2014-09-23 17:57:50 +00:00
|
|
|
layer->set_extent(1 << detail);
|
2014-09-22 22:41:13 +00:00
|
|
|
|
2014-09-29 19:12:54 +00:00
|
|
|
struct pool keys, values, dup;
|
|
|
|
pool_init(&keys, 0);
|
|
|
|
pool_init(&values, 0);
|
|
|
|
pool_init(&dup, 1);
|
2014-09-24 00:18:36 +00:00
|
|
|
|
2014-09-23 21:42:17 +00:00
|
|
|
double interval = 1;
|
|
|
|
double seq = 0;
|
2014-09-24 19:14:35 +00:00
|
|
|
long long count = 0;
|
2014-09-23 21:42:17 +00:00
|
|
|
|
|
|
|
if (z < basezoom) {
|
|
|
|
interval = exp(log(2.5) * (basezoom - z));
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
std::vector<coalesce> features;
|
2014-09-29 21:46:15 +00:00
|
|
|
|
2014-09-22 23:27:10 +00:00
|
|
|
struct index *i;
|
|
|
|
for (i = start; i < end; i++) {
|
2014-09-23 18:30:32 +00:00
|
|
|
int t;
|
2014-09-22 23:27:10 +00:00
|
|
|
|
|
|
|
char *meta = metabase + i->fpos;
|
|
|
|
deserialize_int(&meta, &t);
|
2014-09-22 23:50:24 +00:00
|
|
|
|
2014-09-23 21:42:17 +00:00
|
|
|
if (t == VT_POINT) {
|
|
|
|
seq++;
|
|
|
|
|
|
|
|
if (seq >= 0) {
|
|
|
|
seq -= interval;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
drawvec geom = decode_feature(&meta, z, tx, ty, detail);
|
2014-09-24 01:08:31 +00:00
|
|
|
|
2014-09-27 00:07:52 +00:00
|
|
|
if (t == VT_LINE) {
|
2014-09-30 22:53:45 +00:00
|
|
|
geom = clip_lines(geom, z, detail);
|
2014-09-27 00:07:52 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 23:51:53 +00:00
|
|
|
if (t == VT_LINE || t == VT_POLYGON) {
|
2014-09-30 22:53:45 +00:00
|
|
|
geom = simplify_lines(geom, z, detail);
|
2014-09-24 23:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2014-09-24 21:58:26 +00:00
|
|
|
if (t == VT_LINE && z != basezoom) {
|
|
|
|
len = shrink_lines(geom, len, z, basezoom);
|
|
|
|
}
|
2014-09-24 23:51:53 +00:00
|
|
|
#endif
|
2014-09-24 21:58:26 +00:00
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
to_tile_scale(geom, z, detail);
|
2014-09-24 21:14:43 +00:00
|
|
|
|
2014-09-30 23:27:00 +00:00
|
|
|
if (t == VT_POINT || to_feature(geom, NULL)) {
|
2014-09-24 00:18:36 +00:00
|
|
|
struct pool_val *pv = pool_long_long(&dup, &i->fpos, 0);
|
|
|
|
if (pv->n == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pv->n = 0;
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
struct coalesce c;
|
2014-09-23 00:12:38 +00:00
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
c.type = t;
|
|
|
|
c.index = i->index;
|
|
|
|
c.geom = geom;
|
2014-09-22 23:27:10 +00:00
|
|
|
|
2014-09-23 18:30:32 +00:00
|
|
|
int m;
|
|
|
|
deserialize_int(&meta, &m);
|
2014-09-23 00:12:38 +00:00
|
|
|
|
2014-09-23 18:30:32 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < m; i++) {
|
|
|
|
int t;
|
|
|
|
deserialize_int(&meta, &t);
|
|
|
|
struct pool_val *key = deserialize_string(&meta, &keys, VT_STRING);
|
2014-09-23 18:41:36 +00:00
|
|
|
struct pool_val *value = deserialize_string(&meta, &values, t);
|
2014-09-23 00:12:38 +00:00
|
|
|
|
2014-10-01 00:24:34 +00:00
|
|
|
c.meta.push_back(key->n);
|
|
|
|
c.meta.push_back(value->n);
|
2014-09-23 23:01:19 +00:00
|
|
|
|
2014-10-01 17:20:04 +00:00
|
|
|
if (!is_pooled(file_keys, key->s, t)) {
|
|
|
|
// Dup to retain after munmap
|
|
|
|
pool(file_keys, strdup(key->s), t);
|
|
|
|
}
|
2014-09-22 23:27:10 +00:00
|
|
|
}
|
2014-09-29 21:46:15 +00:00
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
features.push_back(c);
|
2014-09-22 23:27:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 00:18:23 +00:00
|
|
|
std::sort(features.begin(), features.end());
|
2014-09-29 22:33:14 +00:00
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
std::vector<coalesce> out;
|
|
|
|
unsigned x;
|
|
|
|
for (x = 0; x < features.size(); x++) {
|
|
|
|
unsigned y = out.size() - 1;
|
|
|
|
|
2014-10-01 00:18:23 +00:00
|
|
|
if (out.size() > 0 && coalcmp(&features[x], &out[y]) < 0) {
|
|
|
|
fprintf(stderr, "\nfeature out of order\n");
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
if (out.size() > 0 && out[y].geom.size() + features[x].geom.size() < 20000 && coalcmp(&features[x], &out[y]) == 0 && features[x].type != VT_POINT) {
|
|
|
|
unsigned z;
|
|
|
|
for (z = 0; z < features[x].geom.size(); z++) {
|
|
|
|
out[y].geom.push_back(features[x].geom[z]);
|
|
|
|
}
|
2014-09-29 22:33:14 +00:00
|
|
|
} else {
|
2014-09-30 22:53:45 +00:00
|
|
|
out.push_back(features[x]);
|
2014-09-29 22:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
features = out;
|
|
|
|
for (x = 0; x < features.size(); x++) {
|
2014-09-30 00:32:01 +00:00
|
|
|
if (features[x].type == VT_LINE || features[x].type == VT_POLYGON) {
|
2014-09-30 22:53:45 +00:00
|
|
|
features[x].geom = remove_noop(features[x].geom, features[x].type);
|
2014-09-30 00:32:01 +00:00
|
|
|
}
|
|
|
|
|
2014-09-29 21:46:15 +00:00
|
|
|
mapnik::vector::tile_feature *feature = layer->add_features();
|
|
|
|
|
|
|
|
if (features[x].type == VT_POINT) {
|
|
|
|
feature->set_type(mapnik::vector::tile::Point);
|
|
|
|
} else if (features[x].type == VT_LINE) {
|
|
|
|
feature->set_type(mapnik::vector::tile::LineString);
|
|
|
|
} else if (features[x].type == VT_POLYGON) {
|
|
|
|
feature->set_type(mapnik::vector::tile::Polygon);
|
|
|
|
} else {
|
|
|
|
feature->set_type(mapnik::vector::tile::Unknown);
|
|
|
|
}
|
|
|
|
|
2014-09-30 23:27:00 +00:00
|
|
|
to_feature(features[x].geom, feature);
|
2014-10-01 00:24:34 +00:00
|
|
|
count += features[x].geom.size();
|
2014-09-29 21:46:15 +00:00
|
|
|
|
2014-10-01 00:24:34 +00:00
|
|
|
unsigned y;
|
|
|
|
for (y = 0; y < features[x].meta.size(); y++) {
|
2014-09-29 21:46:15 +00:00
|
|
|
feature->add_tags(features[x].meta[y]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:53:45 +00:00
|
|
|
features.resize(0);
|
2014-09-29 22:59:36 +00:00
|
|
|
|
2014-09-22 23:39:25 +00:00
|
|
|
struct pool_val *pv;
|
2014-09-23 05:31:32 +00:00
|
|
|
for (pv = keys.head; pv != NULL; pv = pv->next) {
|
2014-09-22 23:39:25 +00:00
|
|
|
layer->add_keys(pv->s, strlen(pv->s));
|
|
|
|
}
|
2014-09-23 18:41:36 +00:00
|
|
|
for (pv = values.head; pv != NULL; pv = pv->next) {
|
|
|
|
mapnik::vector::tile_value *tv = layer->add_values();
|
|
|
|
|
|
|
|
if (pv->type == VT_NUMBER) {
|
|
|
|
tv->set_double_value(atof(pv->s));
|
|
|
|
} else {
|
|
|
|
tv->set_string_value(pv->s);
|
|
|
|
}
|
|
|
|
}
|
2014-09-22 23:27:10 +00:00
|
|
|
pool_free(&keys);
|
2014-09-24 00:18:36 +00:00
|
|
|
pool_free(&values);
|
|
|
|
pool_free(&dup);
|
2014-09-22 23:39:25 +00:00
|
|
|
|
2014-09-23 17:57:50 +00:00
|
|
|
std::string s;
|
|
|
|
std::string compressed;
|
2014-09-22 23:39:25 +00:00
|
|
|
|
2014-09-23 17:57:50 +00:00
|
|
|
tile.SerializeToString(&s);
|
|
|
|
compress(s, compressed);
|
2014-09-23 19:17:18 +00:00
|
|
|
|
|
|
|
if (compressed.size() > 500000) {
|
|
|
|
fprintf(stderr, "tile %d/%u/%u size is %lld, >500000\n", z, tx, ty, (long long) compressed.size());
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-09-23 00:46:48 +00:00
|
|
|
|
2014-09-29 19:48:58 +00:00
|
|
|
mbtiles_write_tile(outdb, z, tx, ty, compressed.data(), compressed.size());
|
2014-09-24 19:14:35 +00:00
|
|
|
|
|
|
|
return count;
|
2014-09-22 23:27:10 +00:00
|
|
|
}
|
|
|
|
|