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>
|
2014-10-31 23:50:28 +00:00
|
|
|
#include <map>
|
2014-11-06 19:29:11 +00:00
|
|
|
#include <set>
|
2014-09-30 22:53:45 +00:00
|
|
|
#include <algorithm>
|
2014-09-23 00:46:48 +00:00
|
|
|
#include <stdio.h>
|
2015-12-14 23:45:00 +00:00
|
|
|
#include <stdlib.h>
|
2016-04-23 00:51:35 +00:00
|
|
|
#include <string.h>
|
2014-09-23 00:46:48 +00:00
|
|
|
#include <unistd.h>
|
2015-07-10 17:26:23 +00:00
|
|
|
#include <limits.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>
|
2015-06-20 00:29:56 +00:00
|
|
|
#include <sys/mman.h>
|
2016-04-26 23:56:24 +00:00
|
|
|
#include <cmath>
|
2014-09-25 23:34:17 +00:00
|
|
|
#include <sqlite3.h>
|
2015-10-19 19:32:40 +00:00
|
|
|
#include <pthread.h>
|
2015-12-20 23:13:33 +00:00
|
|
|
#include <errno.h>
|
2016-04-27 19:22:47 +00:00
|
|
|
#include "mvt.hpp"
|
|
|
|
#include "geometry.hpp"
|
2016-04-27 21:00:14 +00:00
|
|
|
#include "tile.hpp"
|
|
|
|
#include "pool.hpp"
|
|
|
|
#include "projection.hpp"
|
2016-04-27 21:19:10 +00:00
|
|
|
#include "serial.hpp"
|
2016-04-27 22:09:06 +00:00
|
|
|
#include "options.hpp"
|
|
|
|
#include "main.hpp"
|
2016-04-28 21:43:04 +00:00
|
|
|
#include "mbtiles.hpp"
|
2014-09-22 23:06:44 +00:00
|
|
|
|
2014-09-23 00:12:38 +00:00
|
|
|
#define CMD_BITS 3
|
|
|
|
|
2015-07-10 17:26:23 +00:00
|
|
|
#define XSTRINGIFY(s) STRINGIFY(s)
|
|
|
|
#define STRINGIFY(s) #s
|
|
|
|
|
2015-10-19 19:32:40 +00:00
|
|
|
pthread_mutex_t db_lock = PTHREAD_MUTEX_INITIALIZER;
|
2015-10-19 20:26:47 +00:00
|
|
|
pthread_mutex_t var_lock = PTHREAD_MUTEX_INITIALIZER;
|
2015-10-19 19:32:40 +00:00
|
|
|
|
2016-04-22 22:10:16 +00:00
|
|
|
std::vector<mvt_geometry> to_feature(drawvec &geom) {
|
|
|
|
std::vector<mvt_geometry> out;
|
2014-09-23 18:30:32 +00:00
|
|
|
|
2016-04-22 22:06:26 +00:00
|
|
|
for (size_t i = 0; i < geom.size(); i++) {
|
2016-04-22 22:10:16 +00:00
|
|
|
out.push_back(mvt_geometry(geom[i].op, geom[i].x, geom[i].y));
|
2014-09-23 18:30:32 +00:00
|
|
|
}
|
|
|
|
|
2016-04-22 22:06:26 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool draws_something(drawvec &geom) {
|
|
|
|
for (size_t i = 1; i < geom.size(); i++) {
|
|
|
|
if (geom[i].op == VT_LINETO && (geom[i].x != geom[i - 1].x || geom[i].y != geom[i - 1].y)) {
|
|
|
|
return true;
|
2014-09-23 18:30:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-22 22:06:26 +00:00
|
|
|
return false;
|
2014-09-23 18:30:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-10 22:30:49 +00:00
|
|
|
int metacmp(int m1, const std::vector<long long> &keys1, const std::vector<long long> &values1, char *stringpool1, int m2, const std::vector<long long> &keys2, const std::vector<long long> &values2, char *stringpool2);
|
2014-10-01 00:18:23 +00:00
|
|
|
int coalindexcmp(const struct coalesce *c1, const struct coalesce *c2);
|
2016-04-25 23:19:52 +00:00
|
|
|
static int is_integer(const char *s, long long *v);
|
2014-10-01 00:18:23 +00:00
|
|
|
|
2014-09-29 21:46:15 +00:00
|
|
|
struct coalesce {
|
2016-04-25 23:19:52 +00:00
|
|
|
char *meta;
|
2016-04-25 23:47:30 +00:00
|
|
|
char *stringpool;
|
2016-05-23 22:57:28 +00:00
|
|
|
std::vector<long long> keys;
|
|
|
|
std::vector<long long> values;
|
2016-05-11 21:47:23 +00:00
|
|
|
drawvec geom;
|
2014-09-29 22:38:33 +00:00
|
|
|
unsigned long long index;
|
2014-10-22 00:52:52 +00:00
|
|
|
unsigned long long index2;
|
2015-09-14 22:42:06 +00:00
|
|
|
long long original_seq;
|
2016-05-11 21:47:23 +00:00
|
|
|
int type;
|
|
|
|
int m;
|
|
|
|
bool coalesced;
|
2016-05-27 23:25:40 +00:00
|
|
|
double spacing;
|
2014-10-01 00:18:23 +00:00
|
|
|
|
2015-06-03 18:21:40 +00:00
|
|
|
bool operator<(const coalesce &o) const {
|
2014-10-01 00:18:23 +00:00
|
|
|
int cmp = coalindexcmp(this, &o);
|
|
|
|
if (cmp < 0) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-09-29 21:46:15 +00:00
|
|
|
};
|
|
|
|
|
2015-09-14 22:42:06 +00:00
|
|
|
struct preservecmp {
|
2015-09-15 20:23:34 +00:00
|
|
|
bool operator()(const struct coalesce &a, const struct coalesce &b) {
|
2015-09-14 22:42:06 +00:00
|
|
|
return a.original_seq < b.original_seq;
|
|
|
|
}
|
|
|
|
} preservecmp;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-10 22:30:49 +00:00
|
|
|
return metacmp(c1->m, c1->keys, c1->values, c1->stringpool, c2->m, c2->keys, c2->values, c2->stringpool);
|
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;
|
|
|
|
}
|
2014-10-22 00:52:52 +00:00
|
|
|
|
|
|
|
if (c1->index2 > c2->index2) {
|
|
|
|
return -1;
|
|
|
|
} else if (c1->index2 < c2->index2) {
|
|
|
|
return 1;
|
|
|
|
}
|
2014-09-29 22:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
2016-05-10 22:30:49 +00:00
|
|
|
mvt_value retrieve_string(long long off, char *stringpool, int *otype) {
|
2016-04-25 23:19:52 +00:00
|
|
|
int type = stringpool[off];
|
|
|
|
char *s = stringpool + off + 1;
|
|
|
|
|
|
|
|
if (otype != NULL) {
|
|
|
|
*otype = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
mvt_value tv;
|
|
|
|
if (type == VT_NUMBER) {
|
|
|
|
long long v;
|
|
|
|
if (is_integer(s, &v)) {
|
|
|
|
if (v >= 0) {
|
|
|
|
tv.type = mvt_int;
|
|
|
|
tv.numeric_value.int_value = v;
|
|
|
|
} else {
|
|
|
|
tv.type = mvt_sint;
|
|
|
|
tv.numeric_value.sint_value = v;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tv.type = mvt_double;
|
|
|
|
tv.numeric_value.double_value = atof(s);
|
|
|
|
}
|
|
|
|
} else if (type == VT_BOOLEAN) {
|
|
|
|
tv.type = mvt_bool;
|
|
|
|
tv.numeric_value.bool_value = (s[0] == 't');
|
|
|
|
} else {
|
|
|
|
tv.type = mvt_string;
|
|
|
|
tv.string_value = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tv;
|
2015-06-18 00:18:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-10 22:30:49 +00:00
|
|
|
void decode_meta(int m, std::vector<long long> &metakeys, std::vector<long long> &metavals, char *stringpool, mvt_layer &layer, mvt_feature &feature) {
|
2014-10-08 21:01:47 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < m; i++) {
|
2016-04-25 23:19:52 +00:00
|
|
|
int otype;
|
2016-05-10 22:30:49 +00:00
|
|
|
mvt_value key = retrieve_string(metakeys[i], stringpool, NULL);
|
|
|
|
mvt_value value = retrieve_string(metavals[i], stringpool, &otype);
|
2014-10-08 21:01:47 +00:00
|
|
|
|
2016-04-25 23:19:52 +00:00
|
|
|
layer.tag(feature, key.string_value, value);
|
2014-10-08 21:01:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 22:30:49 +00:00
|
|
|
int metacmp(int m1, const std::vector<long long> &keys1, const std::vector<long long> &values1, char *stringpool1, int m2, const std::vector<long long> &keys2, const std::vector<long long> &values2, char *stringpool2) {
|
2016-04-25 23:47:30 +00:00
|
|
|
// XXX
|
|
|
|
// Ideally this would make identical features compare the same lexically
|
|
|
|
// even if their attributes were declared in different orders in different instances.
|
|
|
|
// In practice, this is probably good enough to put "identical" features together.
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < m1 && i < m2; i++) {
|
2016-05-10 22:30:49 +00:00
|
|
|
mvt_value key1 = retrieve_string(keys1[i], stringpool1, NULL);
|
|
|
|
mvt_value key2 = retrieve_string(keys2[i], stringpool2, NULL);
|
2016-04-25 23:47:30 +00:00
|
|
|
|
|
|
|
if (key1.string_value < key2.string_value) {
|
|
|
|
return -1;
|
|
|
|
} else if (key1.string_value > key2.string_value) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-05-10 22:30:49 +00:00
|
|
|
long long off1 = values1[i];
|
2016-04-25 23:47:30 +00:00
|
|
|
int type1 = stringpool1[off1];
|
|
|
|
char *s1 = stringpool1 + off1 + 1;
|
|
|
|
|
2016-05-10 22:30:49 +00:00
|
|
|
long long off2 = values2[i];
|
2016-04-25 23:47:30 +00:00
|
|
|
int type2 = stringpool2[off2];
|
|
|
|
char *s2 = stringpool2 + off2 + 1;
|
|
|
|
|
|
|
|
if (type1 != type2) {
|
|
|
|
return type1 - type2;
|
|
|
|
}
|
|
|
|
int cmp = strcmp(s1, s2);
|
|
|
|
if (s1 != s2) {
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m1 < m2) {
|
|
|
|
return -1;
|
|
|
|
} else if (m1 > m2) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 23:45:00 +00:00
|
|
|
static int is_integer(const char *s, long long *v) {
|
|
|
|
errno = 0;
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
*v = strtoll(s, &endptr, 0);
|
|
|
|
if (*v == 0 && errno != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((*v == LLONG_MIN || *v == LLONG_MAX) && (errno == ERANGE)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (*endptr != '\0') {
|
|
|
|
// Special case: If it is an integer followed by .0000 or similar,
|
|
|
|
// it is still an integer
|
|
|
|
|
|
|
|
if (*endptr != '.') {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
endptr++;
|
|
|
|
for (; *endptr != '\0'; endptr++) {
|
|
|
|
if (*endptr != '0') {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-08 23:59:00 +00:00
|
|
|
struct sll {
|
2015-06-03 18:21:40 +00:00
|
|
|
char *name;
|
2014-10-08 23:59:00 +00:00
|
|
|
long long val;
|
|
|
|
|
2015-06-03 18:21:40 +00:00
|
|
|
bool operator<(const sll &o) const {
|
2014-10-08 23:59:00 +00:00
|
|
|
if (this->val < o.val) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-10-08 23:39:44 +00:00
|
|
|
|
2016-05-03 22:48:42 +00:00
|
|
|
sll(char *nname, long long nval) {
|
|
|
|
this->name = nname;
|
|
|
|
this->val = nval;
|
2014-10-08 23:59:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-27 23:25:40 +00:00
|
|
|
void rewrite(drawvec &geom, int z, int nextzoom, int maxzoom, long long *bbox, unsigned tx, unsigned ty, int buffer, int line_detail, int *within, long long *geompos, FILE **geomfile, const char *fname, signed char t, int layer, long long metastart, signed char feature_minzoom, int child_shards, int max_zoom_increment, long long seq, int tippecanoe_minzoom, int tippecanoe_maxzoom, int segment, unsigned *initial_x, unsigned *initial_y, int m, std::vector<long long> &metakeys, std::vector<long long> &metavals) {
|
2015-12-15 20:00:05 +00:00
|
|
|
if (geom.size() > 0 && nextzoom <= maxzoom) {
|
2015-06-30 23:36:26 +00:00
|
|
|
int xo, yo;
|
|
|
|
int span = 1 << (nextzoom - z);
|
|
|
|
|
2015-07-01 22:06:12 +00:00
|
|
|
// Get the feature bounding box in pixel (256) coordinates at the child zoom
|
|
|
|
// in order to calculate which sub-tiles it can touch including the buffer.
|
|
|
|
long long bbox2[4];
|
|
|
|
int k;
|
|
|
|
for (k = 0; k < 4; k++) {
|
|
|
|
// Division instead of right-shift because coordinates can be negative
|
|
|
|
bbox2[k] = bbox[k] / (1 << (32 - nextzoom - 8));
|
|
|
|
}
|
2016-01-20 21:58:17 +00:00
|
|
|
// Decrement the top and left edges so that any features that are
|
|
|
|
// touching the edge can potentially be included in the adjacent tiles too.
|
|
|
|
bbox2[0] -= buffer + 1;
|
|
|
|
bbox2[1] -= buffer + 1;
|
2015-07-01 22:06:12 +00:00
|
|
|
bbox2[2] += buffer;
|
|
|
|
bbox2[3] += buffer;
|
|
|
|
|
|
|
|
for (k = 0; k < 4; k++) {
|
|
|
|
if (bbox2[k] < 0) {
|
|
|
|
bbox2[k] = 0;
|
|
|
|
}
|
|
|
|
if (bbox2[k] >= 256 * span) {
|
|
|
|
bbox2[k] = 256 * (span - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bbox2[k] /= 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (xo = bbox2[0]; xo <= bbox2[2]; xo++) {
|
|
|
|
for (yo = bbox2[1]; yo <= bbox2[3]; yo++) {
|
2015-07-01 17:12:23 +00:00
|
|
|
unsigned jx = tx * span + xo;
|
|
|
|
unsigned jy = ty * span + yo;
|
|
|
|
|
|
|
|
// j is the shard that the child tile's data is being written to.
|
|
|
|
//
|
2015-07-08 23:33:22 +00:00
|
|
|
// Be careful: We can't jump more zoom levels than max_zoom_increment
|
2015-07-01 17:12:23 +00:00
|
|
|
// because that could break the constraint that each of the children
|
|
|
|
// of the current tile must have its own shard, because the data for
|
|
|
|
// the child tile must be contiguous within the shard.
|
|
|
|
//
|
|
|
|
// But it's OK to spread children across all the shards, not just
|
|
|
|
// the four that would normally result from splitting one tile,
|
|
|
|
// because it will go through all the shards when it does the
|
|
|
|
// next zoom.
|
2015-07-08 23:33:22 +00:00
|
|
|
//
|
|
|
|
// If child_shards is a power of 2 but not a power of 4, this will
|
|
|
|
// shard X more widely than Y. XXX Is there a better way to do this
|
|
|
|
// without causing collisions?
|
2015-07-01 17:12:23 +00:00
|
|
|
|
2015-07-08 23:35:02 +00:00
|
|
|
int j = ((jx << max_zoom_increment) |
|
|
|
|
((jy & ((1 << max_zoom_increment) - 1)))) &
|
|
|
|
(child_shards - 1);
|
2015-06-30 23:36:26 +00:00
|
|
|
|
2015-07-01 22:06:12 +00:00
|
|
|
{
|
2015-06-30 23:36:26 +00:00
|
|
|
if (!within[j]) {
|
|
|
|
serialize_int(geomfile[j], nextzoom, &geompos[j], fname);
|
|
|
|
serialize_uint(geomfile[j], tx * span + xo, &geompos[j], fname);
|
|
|
|
serialize_uint(geomfile[j], ty * span + yo, &geompos[j], fname);
|
|
|
|
within[j] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Offset from tile coordinates back to world coordinates
|
|
|
|
unsigned sx = 0, sy = 0;
|
|
|
|
if (z != 0) {
|
|
|
|
sx = tx << (32 - z);
|
|
|
|
sy = ty << (32 - z);
|
|
|
|
}
|
|
|
|
|
|
|
|
// printf("type %d, meta %lld\n", t, metastart);
|
|
|
|
serialize_byte(geomfile[j], t, &geompos[j], fname);
|
2015-09-14 22:42:06 +00:00
|
|
|
serialize_long_long(geomfile[j], seq, &geompos[j], fname);
|
2015-10-06 23:51:23 +00:00
|
|
|
serialize_long_long(geomfile[j], (layer << 2) | ((tippecanoe_minzoom != -1) << 1) | (tippecanoe_maxzoom != -1), &geompos[j], fname);
|
|
|
|
if (tippecanoe_minzoom != -1) {
|
|
|
|
serialize_int(geomfile[j], tippecanoe_minzoom, geompos, fname);
|
|
|
|
}
|
|
|
|
if (tippecanoe_maxzoom != -1) {
|
|
|
|
serialize_int(geomfile[j], tippecanoe_maxzoom, geompos, fname);
|
|
|
|
}
|
2015-12-22 01:21:18 +00:00
|
|
|
serialize_int(geomfile[j], segment, &geompos[j], fname);
|
2015-12-23 00:58:27 +00:00
|
|
|
long long wx = initial_x[segment], wy = initial_y[segment];
|
2015-06-30 23:36:26 +00:00
|
|
|
|
2016-03-25 19:20:32 +00:00
|
|
|
for (size_t u = 0; u < geom.size(); u++) {
|
2015-06-30 23:36:26 +00:00
|
|
|
serialize_byte(geomfile[j], geom[u].op, &geompos[j], fname);
|
|
|
|
|
|
|
|
if (geom[u].op != VT_CLOSEPATH) {
|
|
|
|
serialize_long_long(geomfile[j], ((geom[u].x + sx) >> geometry_scale) - (wx >> geometry_scale), &geompos[j], fname);
|
|
|
|
serialize_long_long(geomfile[j], ((geom[u].y + sy) >> geometry_scale) - (wy >> geometry_scale), &geompos[j], fname);
|
|
|
|
wx = geom[u].x + sx;
|
|
|
|
wy = geom[u].y + sy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
serialize_byte(geomfile[j], VT_END, &geompos[j], fname);
|
2016-05-10 22:30:49 +00:00
|
|
|
|
|
|
|
serialize_int(geomfile[j], m, &geompos[j], fname);
|
|
|
|
serialize_long_long(geomfile[j], metastart, &geompos[j], fname);
|
|
|
|
if (metastart < 0) {
|
|
|
|
for (int i = 0; i < m; i++) {
|
|
|
|
serialize_long_long(geomfile[j], metakeys[i], &geompos[j], fname);
|
|
|
|
serialize_long_long(geomfile[j], metavals[i], &geompos[j], fname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 23:36:26 +00:00
|
|
|
serialize_byte(geomfile[j], feature_minzoom, &geompos[j], fname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 20:29:40 +00:00
|
|
|
struct partial {
|
2016-02-11 19:09:05 +00:00
|
|
|
std::vector<drawvec> geoms;
|
2016-05-23 22:57:28 +00:00
|
|
|
std::vector<long long> keys;
|
|
|
|
std::vector<long long> values;
|
2016-01-05 20:29:40 +00:00
|
|
|
char *meta;
|
2016-05-11 21:47:23 +00:00
|
|
|
long long layer;
|
2016-01-05 20:29:40 +00:00
|
|
|
long long original_seq;
|
|
|
|
unsigned long long index;
|
|
|
|
unsigned long long index2;
|
2016-05-11 21:47:23 +00:00
|
|
|
int m;
|
|
|
|
int segment;
|
|
|
|
bool reduced;
|
2016-01-05 20:29:40 +00:00
|
|
|
int z;
|
|
|
|
int line_detail;
|
2016-01-08 19:31:10 +00:00
|
|
|
int maxzoom;
|
2016-05-27 23:25:40 +00:00
|
|
|
double spacing;
|
2016-07-12 23:51:56 +00:00
|
|
|
double simplification;
|
2016-05-11 21:47:23 +00:00
|
|
|
signed char t;
|
2016-01-05 20:29:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct partial_arg {
|
|
|
|
std::vector<struct partial> *partials;
|
|
|
|
int task;
|
|
|
|
int tasks;
|
|
|
|
};
|
|
|
|
|
2016-05-05 20:39:21 +00:00
|
|
|
drawvec revive_polygon(drawvec &geom, double area, int z, int detail) {
|
|
|
|
// From area in world coordinates to area in tile coordinates
|
|
|
|
long long divisor = 1LL << (32 - detail - z);
|
|
|
|
area /= divisor * divisor;
|
|
|
|
|
|
|
|
if (area == 0) {
|
|
|
|
return drawvec();
|
|
|
|
}
|
|
|
|
|
|
|
|
int height = ceil(sqrt(area));
|
|
|
|
int width = round(area / height);
|
|
|
|
if (width == 0) {
|
|
|
|
width = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long sx = 0, sy = 0, n = 0;
|
|
|
|
for (size_t i = 0; i < geom.size(); i++) {
|
|
|
|
if (geom[i].op == VT_MOVETO || geom[i].op == VT_LINETO) {
|
|
|
|
sx += geom[i].x;
|
|
|
|
sy += geom[i].y;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n > 0) {
|
|
|
|
sx /= n;
|
|
|
|
sy /= n;
|
|
|
|
|
|
|
|
drawvec out;
|
|
|
|
out.push_back(draw(VT_MOVETO, sx - (width / 2), sy - (height / 2)));
|
|
|
|
out.push_back(draw(VT_LINETO, sx - (width / 2) + width, sy - (height / 2)));
|
|
|
|
out.push_back(draw(VT_LINETO, sx - (width / 2) + width, sy - (height / 2) + height));
|
|
|
|
out.push_back(draw(VT_LINETO, sx - (width / 2), sy - (height / 2) + height));
|
|
|
|
out.push_back(draw(VT_LINETO, sx - (width / 2), sy - (height / 2)));
|
|
|
|
|
|
|
|
return out;
|
|
|
|
} else {
|
|
|
|
return drawvec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 20:29:40 +00:00
|
|
|
void *partial_feature_worker(void *v) {
|
|
|
|
struct partial_arg *a = (struct partial_arg *) v;
|
|
|
|
std::vector<struct partial> *partials = a->partials;
|
|
|
|
|
2016-03-25 19:20:32 +00:00
|
|
|
for (size_t i = a->task; i < (*partials).size(); i += a->tasks) {
|
2016-02-11 20:22:22 +00:00
|
|
|
drawvec geom = (*partials)[i].geoms[0]; // XXX assumption of a single geometry at the beginning
|
|
|
|
(*partials)[i].geoms.clear(); // avoid keeping two copies in memory
|
2016-01-05 20:29:40 +00:00
|
|
|
signed char t = (*partials)[i].t;
|
|
|
|
int z = (*partials)[i].z;
|
|
|
|
int line_detail = (*partials)[i].line_detail;
|
2016-01-08 19:31:10 +00:00
|
|
|
int maxzoom = (*partials)[i].maxzoom;
|
2016-01-05 20:29:40 +00:00
|
|
|
|
2016-05-05 20:39:21 +00:00
|
|
|
double area = 0;
|
|
|
|
if (t == VT_POLYGON) {
|
|
|
|
area = get_area(geom, 0, geom.size());
|
|
|
|
}
|
|
|
|
|
2016-01-11 18:46:25 +00:00
|
|
|
if ((t == VT_LINE || t == VT_POLYGON) && !(prevent[P_SIMPLIFY] || (z == maxzoom && prevent[P_SIMPLIFY_LOW]))) {
|
2016-01-07 19:35:11 +00:00
|
|
|
if (1 /* !reduced */) { // XXX why did this not simplify if reduced?
|
2016-01-05 20:29:40 +00:00
|
|
|
if (t == VT_LINE) {
|
|
|
|
geom = remove_noop(geom, t, 32 - z - line_detail);
|
|
|
|
}
|
|
|
|
|
2016-07-12 23:51:56 +00:00
|
|
|
drawvec ngeom = simplify_lines(geom, z, line_detail, !(prevent[P_CLIPPING] || prevent[P_DUPLICATION]), (*partials)[i].simplification);
|
2016-05-05 20:39:21 +00:00
|
|
|
|
|
|
|
if (t != VT_POLYGON || ngeom.size() >= 3) {
|
|
|
|
geom = ngeom;
|
|
|
|
}
|
2016-01-05 20:29:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (t == VT_LINE && z != basezoom) {
|
|
|
|
geom = shrink_lines(geom, z, line_detail, basezoom, &along);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-01-11 18:46:25 +00:00
|
|
|
if (t == VT_LINE && additional[A_REVERSE]) {
|
2016-01-05 20:29:40 +00:00
|
|
|
geom = reorder_lines(geom);
|
|
|
|
}
|
|
|
|
|
|
|
|
to_tile_scale(geom, z, line_detail);
|
|
|
|
|
2016-02-11 19:09:05 +00:00
|
|
|
std::vector<drawvec> geoms;
|
|
|
|
geoms.push_back(geom);
|
|
|
|
|
2016-02-11 20:14:32 +00:00
|
|
|
if (t == VT_POLYGON && !prevent[P_POLYGON_SPLIT]) {
|
|
|
|
geoms = chop_polygon(geoms);
|
2016-02-11 19:09:05 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 20:29:40 +00:00
|
|
|
if (t == VT_POLYGON) {
|
|
|
|
// Scaling may have made the polygon degenerate.
|
|
|
|
// Give Clipper a chance to try to fix it.
|
2016-05-03 22:48:42 +00:00
|
|
|
for (size_t g = 0; g < geoms.size(); g++) {
|
2016-05-05 20:39:21 +00:00
|
|
|
drawvec before = geoms[g];
|
2016-05-03 22:48:42 +00:00
|
|
|
geoms[g] = clean_or_clip_poly(geoms[g], 0, 0, 0, false);
|
2016-03-18 23:31:56 +00:00
|
|
|
if (additional[A_DEBUG_POLYGON]) {
|
2016-05-03 22:48:42 +00:00
|
|
|
check_polygon(geoms[g], before);
|
2016-03-18 23:31:56 +00:00
|
|
|
}
|
2016-05-05 20:39:21 +00:00
|
|
|
|
|
|
|
if (geoms[g].size() < 3) {
|
|
|
|
geoms[g] = revive_polygon(before, area / geoms.size(), z, line_detail);
|
|
|
|
}
|
2016-02-11 19:09:05 +00:00
|
|
|
}
|
2016-01-05 20:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Worth skipping this if not coalescing anyway?
|
2016-02-11 19:09:05 +00:00
|
|
|
if (geoms.size() > 0 && geoms[0].size() > 0) {
|
|
|
|
(*partials)[i].index = encode(geoms[0][0].x, geoms[0][0].y);
|
|
|
|
(*partials)[i].index2 = encode(geoms[0][geoms[0].size() - 1].x, geoms[0][geoms[0].size() - 1].y);
|
2016-01-05 20:29:40 +00:00
|
|
|
|
|
|
|
// Anything numbered below the start of the line
|
|
|
|
// can't possibly be the next feature.
|
|
|
|
// We want lowest-but-not-under.
|
|
|
|
if ((*partials)[i].index2 < (*partials)[i].index) {
|
|
|
|
(*partials)[i].index2 = ~0LL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(*partials)[i].index = 0;
|
|
|
|
(*partials)[i].index2 = 0;
|
|
|
|
}
|
|
|
|
|
2016-02-11 19:09:05 +00:00
|
|
|
(*partials)[i].geoms = geoms;
|
2016-01-05 20:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-02-03 23:20:45 +00:00
|
|
|
int manage_gap(unsigned long long index, unsigned long long *previndex, double scale, double gamma, double *gap) {
|
|
|
|
if (gamma > 0) {
|
|
|
|
if (*gap > 0) {
|
|
|
|
if (index == *previndex) {
|
|
|
|
return 1; // Exact duplicate: can't fulfil the gap requirement
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:41:41 +00:00
|
|
|
if (index < *previndex || std::exp(std::log((index - *previndex) / scale) * gamma) >= *gap) {
|
2016-02-03 23:20:45 +00:00
|
|
|
// Dot is further from the previous than the nth root of the gap,
|
|
|
|
// so produce it, and choose a new gap at the next point.
|
|
|
|
*gap = 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
2016-04-27 23:41:41 +00:00
|
|
|
} else if (index >= *previndex) {
|
2016-02-03 23:20:45 +00:00
|
|
|
*gap = (index - *previndex) / scale;
|
|
|
|
|
|
|
|
if (*gap == 0) {
|
|
|
|
return 1; // Exact duplicate: skip
|
|
|
|
} else if (*gap < 1) {
|
|
|
|
return 1; // Narrow dot spacing: need to stretch out
|
|
|
|
} else {
|
|
|
|
*gap = 0; // Wider spacing than minimum: so pass through unchanged
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*previndex = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-12 23:51:56 +00:00
|
|
|
long long write_tile(FILE *geoms, long long *geompos_in, char *metabase, char *stringpool, int z, unsigned tx, unsigned ty, int detail, int min_detail, int basezoom, std::vector<std::string> *layernames, sqlite3 *outdb, double droprate, int buffer, const char *fname, FILE **geomfile, int minzoom, int maxzoom, double todo, volatile long long *along, long long alongminus, double gamma, int nlayers, int child_shards, long long *meta_off, long long *pool_off, unsigned *initial_x, unsigned *initial_y, volatile int *running, double simplification) {
|
2014-10-07 17:27:17 +00:00
|
|
|
int line_detail;
|
2015-05-20 21:57:00 +00:00
|
|
|
double fraction = 1;
|
2014-09-22 22:41:13 +00:00
|
|
|
|
2016-04-11 22:59:02 +00:00
|
|
|
long long og = *geompos_in;
|
2014-12-04 00:18:43 +00:00
|
|
|
|
2015-07-08 23:33:22 +00:00
|
|
|
// XXX is there a way to do this without floating point?
|
2016-04-26 23:56:24 +00:00
|
|
|
int max_zoom_increment = std::log(child_shards) / std::log(4);
|
2015-07-08 23:33:22 +00:00
|
|
|
if (child_shards < 4 || max_zoom_increment < 1) {
|
|
|
|
fprintf(stderr, "Internal error: %d shards, max zoom increment %d\n", child_shards, max_zoom_increment);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if ((((child_shards - 1) << 1) & child_shards) != child_shards) {
|
|
|
|
fprintf(stderr, "Internal error: %d shards not a power of 2\n", child_shards);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-06-29 23:42:26 +00:00
|
|
|
int nextzoom = z + 1;
|
2015-12-15 20:00:05 +00:00
|
|
|
if (nextzoom < minzoom) {
|
|
|
|
if (z + max_zoom_increment > minzoom) {
|
|
|
|
nextzoom = minzoom;
|
2015-06-29 23:42:26 +00:00
|
|
|
} else {
|
2015-07-08 23:33:22 +00:00
|
|
|
nextzoom = z + max_zoom_increment;
|
2015-06-29 23:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 21:09:10 +00:00
|
|
|
static volatile double oprogress = 0;
|
|
|
|
|
|
|
|
// This only loops if the tile data didn't fit, in which case the detail
|
|
|
|
// goes down and the progress indicator goes backward for the next try.
|
|
|
|
for (line_detail = detail; line_detail >= min_detail || line_detail == detail; line_detail--, oprogress = 0) {
|
2014-10-07 17:27:17 +00:00
|
|
|
long long count = 0;
|
2014-10-07 22:37:55 +00:00
|
|
|
double accum_area = 0;
|
2014-09-23 21:42:17 +00:00
|
|
|
|
2015-03-06 18:56:02 +00:00
|
|
|
double interval = 0;
|
|
|
|
double seq = 0;
|
|
|
|
if (z < basezoom) {
|
2016-04-26 23:56:24 +00:00
|
|
|
interval = std::exp(std::log(droprate) * (basezoom - z));
|
2015-03-06 18:56:02 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 21:57:00 +00:00
|
|
|
double fraction_accum = 0;
|
|
|
|
|
2016-05-27 23:25:40 +00:00
|
|
|
unsigned long long previndex = 0, density_previndex = 0;
|
2015-03-06 21:12:32 +00:00
|
|
|
double scale = (double) (1LL << (64 - 2 * (z + 8)));
|
2016-05-27 23:25:40 +00:00
|
|
|
double gap = 0, density_gap = 0;
|
|
|
|
double spacing = 0;
|
2015-03-06 21:12:32 +00:00
|
|
|
|
2015-06-05 17:23:25 +00:00
|
|
|
long long original_features = 0;
|
|
|
|
long long unclipped_features = 0;
|
|
|
|
|
2016-01-05 20:29:40 +00:00
|
|
|
std::vector<struct partial> partials;
|
2015-03-23 18:36:35 +00:00
|
|
|
std::vector<std::vector<coalesce> > features;
|
2016-05-03 23:39:26 +00:00
|
|
|
for (int i = 0; i < nlayers; i++) {
|
2015-03-23 18:36:35 +00:00
|
|
|
features.push_back(std::vector<coalesce>());
|
|
|
|
}
|
2014-09-22 23:27:10 +00:00
|
|
|
|
2015-07-08 23:33:22 +00:00
|
|
|
int within[child_shards];
|
|
|
|
long long geompos[child_shards];
|
|
|
|
memset(within, '\0', sizeof(within));
|
|
|
|
memset(geompos, '\0', sizeof(geompos));
|
2014-11-14 20:42:50 +00:00
|
|
|
|
2016-04-11 22:59:02 +00:00
|
|
|
if (*geompos_in != og) {
|
|
|
|
if (fseek(geoms, og, SEEK_SET) != 0) {
|
|
|
|
perror("fseek geom");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
*geompos_in = og;
|
|
|
|
}
|
2014-12-04 00:18:43 +00:00
|
|
|
|
|
|
|
while (1) {
|
2015-03-23 22:37:49 +00:00
|
|
|
signed char t;
|
2016-04-11 22:59:02 +00:00
|
|
|
deserialize_byte_io(geoms, &t, geompos_in);
|
2014-12-04 00:18:43 +00:00
|
|
|
if (t < 0) {
|
|
|
|
break;
|
2014-11-12 23:57:45 +00:00
|
|
|
}
|
2014-10-27 19:56:51 +00:00
|
|
|
|
2015-09-14 22:42:06 +00:00
|
|
|
long long original_seq;
|
2016-04-11 22:59:02 +00:00
|
|
|
deserialize_long_long_io(geoms, &original_seq, geompos_in);
|
2015-09-14 22:42:06 +00:00
|
|
|
|
2015-07-27 23:20:20 +00:00
|
|
|
long long layer;
|
2016-04-11 22:59:02 +00:00
|
|
|
deserialize_long_long_io(geoms, &layer, geompos_in);
|
2015-10-06 23:51:23 +00:00
|
|
|
int tippecanoe_minzoom = -1, tippecanoe_maxzoom = -1;
|
|
|
|
if (layer & 2) {
|
2016-04-11 22:59:02 +00:00
|
|
|
deserialize_int_io(geoms, &tippecanoe_minzoom, geompos_in);
|
2015-10-06 23:51:23 +00:00
|
|
|
}
|
|
|
|
if (layer & 1) {
|
2016-04-11 22:59:02 +00:00
|
|
|
deserialize_int_io(geoms, &tippecanoe_maxzoom, geompos_in);
|
2015-10-06 23:51:23 +00:00
|
|
|
}
|
|
|
|
layer >>= 2;
|
2015-03-23 18:36:35 +00:00
|
|
|
|
2015-12-22 01:21:18 +00:00
|
|
|
int segment;
|
2016-04-11 22:59:02 +00:00
|
|
|
deserialize_int_io(geoms, &segment, geompos_in);
|
2015-12-22 01:21:18 +00:00
|
|
|
|
2014-12-11 21:34:50 +00:00
|
|
|
long long bbox[4];
|
2014-09-24 01:08:31 +00:00
|
|
|
|
2016-04-11 22:59:02 +00:00
|
|
|
drawvec geom = decode_geometry(geoms, geompos_in, z, tx, ty, line_detail, bbox, initial_x[segment], initial_y[segment]);
|
2014-12-04 00:18:43 +00:00
|
|
|
|
2016-05-10 22:30:49 +00:00
|
|
|
long long metastart;
|
|
|
|
int m;
|
|
|
|
deserialize_int_io(geoms, &m, geompos_in);
|
|
|
|
deserialize_long_long_io(geoms, &metastart, geompos_in);
|
|
|
|
char *meta = NULL;
|
|
|
|
std::vector<long long> metakeys, metavals;
|
|
|
|
|
|
|
|
if (metastart >= 0) {
|
|
|
|
meta = metabase + metastart + meta_off[segment];
|
|
|
|
|
|
|
|
for (int i = 0; i < m; i++) {
|
|
|
|
long long k, v;
|
|
|
|
deserialize_long_long(&meta, &k);
|
|
|
|
deserialize_long_long(&meta, &v);
|
|
|
|
metakeys.push_back(k);
|
|
|
|
metavals.push_back(v);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < m; i++) {
|
|
|
|
long long k, v;
|
|
|
|
deserialize_long_long_io(geoms, &k, geompos_in);
|
|
|
|
deserialize_long_long_io(geoms, &v, geompos_in);
|
|
|
|
metakeys.push_back(k);
|
|
|
|
metavals.push_back(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 19:05:14 +00:00
|
|
|
signed char feature_minzoom;
|
2016-04-11 22:59:02 +00:00
|
|
|
deserialize_byte_io(geoms, &feature_minzoom, geompos_in);
|
2014-10-07 22:37:55 +00:00
|
|
|
|
2016-07-08 22:49:59 +00:00
|
|
|
double progress = floor((((*geompos_in + *along - alongminus) / (double) todo) + z) / (maxzoom + 1) * 1000) / 10;
|
2015-10-19 20:39:44 +00:00
|
|
|
if (progress >= oprogress + 0.1) {
|
2015-07-08 22:06:21 +00:00
|
|
|
if (!quiet) {
|
|
|
|
fprintf(stderr, " %3.1f%% %d/%u/%u \r", progress, z, tx, ty);
|
|
|
|
}
|
2014-12-18 01:08:04 +00:00
|
|
|
oprogress = progress;
|
2014-12-18 00:01:33 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 17:23:25 +00:00
|
|
|
original_features++;
|
|
|
|
|
2015-10-07 23:52:52 +00:00
|
|
|
if (z == 0 && t == VT_POLYGON) {
|
|
|
|
geom = fix_polygon(geom);
|
|
|
|
}
|
|
|
|
|
2014-12-11 21:34:50 +00:00
|
|
|
int quick = quick_check(bbox, z, line_detail, buffer);
|
|
|
|
if (quick == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-12-03 21:12:34 +00:00
|
|
|
if (z == 0) {
|
|
|
|
if (bbox[0] < 0 || bbox[2] > 1LL << 32) {
|
|
|
|
// If the geometry extends off the edge of the world, concatenate on another copy
|
|
|
|
// shifted by 360 degrees, and then make sure both copies get clipped down to size.
|
|
|
|
|
2016-03-25 19:20:32 +00:00
|
|
|
size_t n = geom.size();
|
2015-12-09 00:57:04 +00:00
|
|
|
|
|
|
|
if (bbox[0] < 0) {
|
2016-03-25 19:20:32 +00:00
|
|
|
for (size_t i = 0; i < n; i++) {
|
2015-12-09 00:57:04 +00:00
|
|
|
geom.push_back(draw(geom[i].op, geom[i].x + (1LL << 32), geom[i].y));
|
|
|
|
}
|
2015-12-03 21:12:34 +00:00
|
|
|
}
|
2015-12-09 00:57:04 +00:00
|
|
|
|
|
|
|
if (bbox[2] > 1LL << 32) {
|
2016-03-25 19:20:32 +00:00
|
|
|
for (size_t i = 0; i < n; i++) {
|
2015-12-09 00:57:04 +00:00
|
|
|
geom.push_back(draw(geom[i].op, geom[i].x - (1LL << 32), geom[i].y));
|
|
|
|
}
|
2015-12-03 21:12:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bbox[0] = 0;
|
|
|
|
bbox[2] = 1LL << 32;
|
|
|
|
|
|
|
|
quick = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 22:00:32 +00:00
|
|
|
// Can't accept the quick check if guaranteeing no duplication, since the
|
|
|
|
// overlap might have been in the buffer.
|
|
|
|
if (quick != 1 || prevent[P_DUPLICATION]) {
|
2016-04-19 22:32:58 +00:00
|
|
|
drawvec clipped;
|
|
|
|
|
|
|
|
// Do the clipping, even if we are going to include the whole feature,
|
|
|
|
// so that we can know whether the feature itself, or only the feature's
|
|
|
|
// bounding box, touches the tile.
|
|
|
|
|
2014-12-11 21:34:50 +00:00
|
|
|
if (t == VT_LINE) {
|
2016-04-19 22:32:58 +00:00
|
|
|
clipped = clip_lines(geom, z, line_detail, buffer);
|
2014-12-11 21:34:50 +00:00
|
|
|
}
|
|
|
|
if (t == VT_POLYGON) {
|
2016-04-19 22:32:58 +00:00
|
|
|
clipped = simple_clip_poly(geom, z, line_detail, buffer);
|
2014-12-11 21:34:50 +00:00
|
|
|
}
|
|
|
|
if (t == VT_POINT) {
|
2016-04-19 22:32:58 +00:00
|
|
|
clipped = clip_point(geom, z, line_detail, buffer);
|
2014-12-11 21:34:50 +00:00
|
|
|
}
|
2014-12-04 00:18:43 +00:00
|
|
|
|
2016-04-19 22:32:58 +00:00
|
|
|
clipped = remove_noop(clipped, t, 0);
|
|
|
|
|
|
|
|
// Must clip at z0 even if we don't want clipping, to handle features
|
|
|
|
// that are duplicated across the date line
|
|
|
|
|
2016-04-20 22:00:32 +00:00
|
|
|
if (prevent[P_DUPLICATION] && z != 0) {
|
|
|
|
if (point_within_tile((bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2, z, line_detail, buffer)) {
|
|
|
|
// geom is unchanged
|
|
|
|
} else {
|
|
|
|
geom.clear();
|
|
|
|
}
|
|
|
|
} else if (prevent[P_CLIPPING] && z != 0) {
|
2016-04-19 22:32:58 +00:00
|
|
|
if (clipped.size() == 0) {
|
|
|
|
geom.clear();
|
|
|
|
} else {
|
|
|
|
// geom is unchanged
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
geom = clipped;
|
|
|
|
}
|
2014-12-12 00:08:53 +00:00
|
|
|
}
|
2014-12-04 00:18:43 +00:00
|
|
|
|
2015-06-05 17:23:25 +00:00
|
|
|
if (geom.size() > 0) {
|
|
|
|
unclipped_features++;
|
|
|
|
}
|
|
|
|
|
2015-05-20 22:15:45 +00:00
|
|
|
if (line_detail == detail && fraction == 1) { /* only write out the next zoom once, even if we retry */
|
2016-05-10 22:30:49 +00:00
|
|
|
rewrite(geom, z, nextzoom, maxzoom, bbox, tx, ty, buffer, line_detail, within, geompos, geomfile, fname, t, layer, metastart, feature_minzoom, child_shards, max_zoom_increment, original_seq, tippecanoe_minzoom, tippecanoe_maxzoom, segment, initial_x, initial_y, m, metakeys, metavals);
|
2014-12-04 00:18:43 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 20:00:05 +00:00
|
|
|
if (z < minzoom) {
|
2014-12-17 19:05:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-06 23:51:23 +00:00
|
|
|
if (tippecanoe_minzoom != -1 && z < tippecanoe_minzoom) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (tippecanoe_maxzoom != -1 && z > tippecanoe_maxzoom) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-03-06 18:56:02 +00:00
|
|
|
if (t == VT_LINE && z + line_detail <= feature_minzoom) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-03-23 21:44:21 +00:00
|
|
|
if (t == VT_POINT && z < feature_minzoom && gamma < 0) {
|
2014-12-04 00:18:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-05-27 23:25:40 +00:00
|
|
|
unsigned long long index = 0;
|
|
|
|
if (additional[A_CALCULATE_FEATURE_DENSITY] || gamma > 0) {
|
|
|
|
index = encode(bbox[0] / 2 + bbox[2] / 2, bbox[1] / 2 + bbox[3] / 2);
|
|
|
|
}
|
|
|
|
|
2016-04-06 21:17:44 +00:00
|
|
|
if (gamma >= 0 && (t == VT_POINT ||
|
|
|
|
(additional[A_LINE_DROP] && t == VT_LINE) ||
|
|
|
|
(additional[A_POLYGON_DROP] && t == VT_POLYGON))) {
|
2015-03-06 18:56:02 +00:00
|
|
|
seq++;
|
|
|
|
if (seq >= 0) {
|
|
|
|
seq -= interval;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-06 21:12:32 +00:00
|
|
|
|
2016-05-27 23:35:46 +00:00
|
|
|
if (gamma > 0) {
|
2016-02-03 23:20:45 +00:00
|
|
|
if (manage_gap(index, &previndex, scale, gamma, &gap)) {
|
|
|
|
continue;
|
2015-03-06 21:12:32 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-06 18:56:02 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 23:25:40 +00:00
|
|
|
if (additional[A_CALCULATE_FEATURE_DENSITY]) {
|
|
|
|
// Gamma is always 1 for this calculation so there is a reasonable
|
|
|
|
// interpretation when no features are being dropped.
|
|
|
|
// The spacing is only calculated if a feature would be retained by
|
|
|
|
// that standard, so that duplicates aren't reported as infinitely dense.
|
|
|
|
|
|
|
|
double o_density_previndex = density_previndex;
|
|
|
|
if (!manage_gap(index, &density_previndex, scale, 1, &density_gap)) {
|
|
|
|
spacing = (index - o_density_previndex) / scale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 21:57:00 +00:00
|
|
|
fraction_accum += fraction;
|
|
|
|
if (fraction_accum < 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fraction_accum -= 1;
|
|
|
|
|
2014-12-04 00:18:43 +00:00
|
|
|
bool reduced = false;
|
|
|
|
if (t == VT_POLYGON) {
|
|
|
|
geom = reduce_tiny_poly(geom, z, line_detail, &reduced, &accum_area);
|
|
|
|
}
|
2014-10-03 23:33:22 +00:00
|
|
|
|
2016-01-05 20:29:40 +00:00
|
|
|
if (geom.size() > 0) {
|
|
|
|
partial p;
|
2016-02-11 19:09:05 +00:00
|
|
|
p.geoms.push_back(geom);
|
2016-01-05 20:29:40 +00:00
|
|
|
p.layer = layer;
|
2016-04-04 18:18:37 +00:00
|
|
|
p.m = m;
|
2016-01-05 20:29:40 +00:00
|
|
|
p.meta = meta;
|
|
|
|
p.t = t;
|
|
|
|
p.segment = segment;
|
|
|
|
p.original_seq = original_seq;
|
|
|
|
p.reduced = reduced;
|
|
|
|
p.z = z;
|
|
|
|
p.line_detail = line_detail;
|
2016-01-08 19:31:10 +00:00
|
|
|
p.maxzoom = maxzoom;
|
2016-05-10 22:30:49 +00:00
|
|
|
p.keys = metakeys;
|
|
|
|
p.values = metavals;
|
2016-05-27 23:25:40 +00:00
|
|
|
p.spacing = spacing;
|
2016-07-12 23:51:56 +00:00
|
|
|
p.simplification = simplification;
|
2016-01-05 20:29:40 +00:00
|
|
|
partials.push_back(p);
|
2014-10-07 17:27:17 +00:00
|
|
|
}
|
2016-01-05 20:29:40 +00:00
|
|
|
}
|
2014-09-24 23:51:53 +00:00
|
|
|
|
2016-01-05 21:56:36 +00:00
|
|
|
int tasks = ceil((double) CPUS / *running);
|
2016-01-05 20:29:40 +00:00
|
|
|
if (tasks < 1) {
|
|
|
|
tasks = 1;
|
|
|
|
}
|
2014-09-24 21:58:26 +00:00
|
|
|
|
2016-01-05 20:29:40 +00:00
|
|
|
pthread_t pthreads[tasks];
|
|
|
|
partial_arg args[tasks];
|
|
|
|
for (int i = 0; i < tasks; i++) {
|
|
|
|
args[i].task = i;
|
|
|
|
args[i].tasks = tasks;
|
|
|
|
args[i].partials = &partials;
|
|
|
|
|
|
|
|
if (tasks > 1) {
|
|
|
|
if (pthread_create(&pthreads[i], NULL, partial_feature_worker, &args[i]) != 0) {
|
|
|
|
perror("pthread_create");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
partial_feature_worker(&args[i]);
|
2014-10-21 23:09:51 +00:00
|
|
|
}
|
2016-01-05 20:29:40 +00:00
|
|
|
}
|
2014-10-21 23:09:51 +00:00
|
|
|
|
2016-01-05 20:29:40 +00:00
|
|
|
if (tasks > 1) {
|
|
|
|
for (int i = 0; i < tasks; i++) {
|
|
|
|
void *retval;
|
2014-09-24 21:14:43 +00:00
|
|
|
|
2016-01-05 20:29:40 +00:00
|
|
|
if (pthread_join(pthreads[i], &retval) != 0) {
|
|
|
|
perror("pthread_join");
|
|
|
|
}
|
2015-10-16 00:11:29 +00:00
|
|
|
}
|
2016-01-05 20:29:40 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 19:20:32 +00:00
|
|
|
for (size_t i = 0; i < partials.size(); i++) {
|
2016-05-11 21:23:39 +00:00
|
|
|
std::vector<drawvec> &pgeoms = partials[i].geoms;
|
2016-01-05 20:29:40 +00:00
|
|
|
long long layer = partials[i].layer;
|
|
|
|
signed char t = partials[i].t;
|
|
|
|
long long original_seq = partials[i].original_seq;
|
2015-10-16 00:11:29 +00:00
|
|
|
|
2016-02-11 19:09:05 +00:00
|
|
|
// A complex polygon may have been split up into multiple geometries.
|
|
|
|
// Break them out into multiple features if necessary.
|
2016-05-03 22:48:42 +00:00
|
|
|
for (size_t j = 0; j < pgeoms.size(); j++) {
|
|
|
|
if (t == VT_POINT || draws_something(pgeoms[j])) {
|
2016-02-11 19:09:05 +00:00
|
|
|
struct coalesce c;
|
|
|
|
|
|
|
|
c.type = t;
|
|
|
|
c.index = partials[i].index;
|
|
|
|
c.index2 = partials[i].index2;
|
2016-05-03 22:48:42 +00:00
|
|
|
c.geom = pgeoms[j];
|
2016-05-11 21:23:39 +00:00
|
|
|
pgeoms[j].clear();
|
2016-02-11 19:09:05 +00:00
|
|
|
c.coalesced = false;
|
|
|
|
c.original_seq = original_seq;
|
2016-04-25 23:19:52 +00:00
|
|
|
c.m = partials[i].m;
|
|
|
|
c.meta = partials[i].meta;
|
2016-04-25 23:52:20 +00:00
|
|
|
c.stringpool = stringpool + pool_off[partials[i].segment];
|
2016-05-10 22:30:49 +00:00
|
|
|
c.keys = partials[i].keys;
|
|
|
|
c.values = partials[i].values;
|
2016-05-27 23:25:40 +00:00
|
|
|
c.spacing = partials[i].spacing;
|
2016-02-11 19:09:05 +00:00
|
|
|
|
|
|
|
features[layer].push_back(c);
|
|
|
|
}
|
2014-10-07 17:27:17 +00:00
|
|
|
}
|
2014-09-22 23:27:10 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 21:23:39 +00:00
|
|
|
partials.clear();
|
|
|
|
|
2014-12-11 23:46:54 +00:00
|
|
|
int j;
|
2015-07-08 23:33:22 +00:00
|
|
|
for (j = 0; j < child_shards; j++) {
|
2014-12-11 23:46:54 +00:00
|
|
|
if (within[j]) {
|
2015-03-24 00:44:23 +00:00
|
|
|
serialize_byte(geomfile[j], -2, &geompos[j], fname);
|
2014-12-11 23:46:54 +00:00
|
|
|
within[j] = 0;
|
|
|
|
}
|
2014-12-04 00:18:43 +00:00
|
|
|
}
|
|
|
|
|
2015-03-23 18:36:35 +00:00
|
|
|
for (j = 0; j < nlayers; j++) {
|
2016-01-11 18:46:25 +00:00
|
|
|
if (additional[A_REORDER]) {
|
2015-04-21 15:19:51 +00:00
|
|
|
std::sort(features[j].begin(), features[j].end());
|
|
|
|
}
|
2015-03-23 18:36:35 +00:00
|
|
|
|
|
|
|
std::vector<coalesce> out;
|
2016-04-27 18:13:15 +00:00
|
|
|
if (features[j].size() > 0) {
|
|
|
|
out.push_back(features[j][0]);
|
|
|
|
}
|
|
|
|
for (size_t x = 1; x < features[j].size(); x++) {
|
2016-03-25 19:20:32 +00:00
|
|
|
size_t y = out.size() - 1;
|
2014-09-29 22:33:14 +00:00
|
|
|
|
2015-04-21 15:19:51 +00:00
|
|
|
#if 0
|
2015-03-23 18:36:35 +00:00
|
|
|
if (out.size() > 0 && coalcmp(&features[j][x], &out[y]) < 0) {
|
|
|
|
fprintf(stderr, "\nfeature out of order\n");
|
|
|
|
}
|
2015-04-21 15:19:51 +00:00
|
|
|
#endif
|
2014-09-30 22:53:45 +00:00
|
|
|
|
2016-02-11 19:09:05 +00:00
|
|
|
if (additional[A_COALESCE] && out.size() > 0 && out[y].geom.size() + features[j][x].geom.size() < 700 && coalcmp(&features[j][x], &out[y]) == 0 && features[j][x].type != VT_POINT) {
|
2016-05-03 22:48:42 +00:00
|
|
|
for (size_t g = 0; g < features[j][x].geom.size(); g++) {
|
|
|
|
out[y].geom.push_back(features[j][x].geom[g]);
|
2015-03-23 18:36:35 +00:00
|
|
|
}
|
|
|
|
out[y].coalesced = true;
|
|
|
|
} else {
|
|
|
|
out.push_back(features[j][x]);
|
|
|
|
}
|
2014-10-07 17:27:17 +00:00
|
|
|
}
|
2016-02-11 19:09:05 +00:00
|
|
|
|
2015-03-23 18:36:35 +00:00
|
|
|
features[j] = out;
|
2014-10-01 00:18:23 +00:00
|
|
|
|
2015-12-11 20:31:10 +00:00
|
|
|
out.clear();
|
2016-03-25 19:20:32 +00:00
|
|
|
for (size_t x = 0; x < features[j].size(); x++) {
|
2015-03-23 18:36:35 +00:00
|
|
|
if (features[j][x].coalesced && features[j][x].type == VT_LINE) {
|
2015-07-01 19:16:50 +00:00
|
|
|
features[j][x].geom = remove_noop(features[j][x].geom, features[j][x].type, 0);
|
2016-04-20 22:00:32 +00:00
|
|
|
features[j][x].geom = simplify_lines(features[j][x].geom, 32, 0,
|
2016-07-12 23:51:56 +00:00
|
|
|
!(prevent[P_CLIPPING] || prevent[P_DUPLICATION]), simplification);
|
2014-10-07 17:27:17 +00:00
|
|
|
}
|
2015-12-11 20:31:10 +00:00
|
|
|
|
2016-04-18 22:46:07 +00:00
|
|
|
if (features[j][x].type == VT_POLYGON) {
|
|
|
|
if (features[j][x].coalesced) {
|
|
|
|
features[j][x].geom = clean_or_clip_poly(features[j][x].geom, 0, 0, 0, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
features[j][x].geom = close_poly(features[j][x].geom);
|
2014-10-07 17:27:17 +00:00
|
|
|
}
|
2015-12-11 20:31:10 +00:00
|
|
|
|
|
|
|
if (features[j][x].geom.size() > 0) {
|
|
|
|
out.push_back(features[j][x]);
|
|
|
|
}
|
2014-09-30 22:53:45 +00:00
|
|
|
}
|
2015-12-11 20:31:10 +00:00
|
|
|
features[j] = out;
|
2015-09-14 22:42:06 +00:00
|
|
|
|
2016-01-11 18:46:25 +00:00
|
|
|
if (prevent[P_INPUT_ORDER]) {
|
2015-09-14 22:42:06 +00:00
|
|
|
std::sort(features[j].begin(), features[j].end(), preservecmp);
|
|
|
|
}
|
2014-09-29 22:33:14 +00:00
|
|
|
}
|
2014-09-30 00:32:01 +00:00
|
|
|
|
2016-04-25 23:19:52 +00:00
|
|
|
mvt_tile tile;
|
|
|
|
|
2016-05-03 22:48:42 +00:00
|
|
|
for (size_t k = 0; k < features.size(); k++) {
|
2016-04-25 23:52:20 +00:00
|
|
|
mvt_layer layer;
|
2016-04-25 23:19:52 +00:00
|
|
|
|
2016-05-03 22:48:42 +00:00
|
|
|
layer.name = (*layernames)[k];
|
2016-04-25 23:52:20 +00:00
|
|
|
layer.version = 2;
|
|
|
|
layer.extent = 1 << line_detail;
|
2016-04-25 23:19:52 +00:00
|
|
|
|
2016-05-03 22:48:42 +00:00
|
|
|
for (size_t x = 0; x < features[k].size(); x++) {
|
2016-04-25 23:52:20 +00:00
|
|
|
mvt_feature feature;
|
2016-04-25 23:19:52 +00:00
|
|
|
|
2016-05-03 22:48:42 +00:00
|
|
|
if (features[k][x].type == VT_LINE || features[k][x].type == VT_POLYGON) {
|
|
|
|
features[k][x].geom = remove_noop(features[k][x].geom, features[k][x].type, 0);
|
2016-04-25 23:19:52 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 00:45:12 +00:00
|
|
|
if (features[k][x].geom.size() == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:48:42 +00:00
|
|
|
feature.type = features[k][x].type;
|
|
|
|
feature.geometry = to_feature(features[k][x].geom);
|
|
|
|
count += features[k][x].geom.size();
|
2016-05-11 21:23:39 +00:00
|
|
|
features[k][x].geom.clear();
|
2016-04-25 23:19:52 +00:00
|
|
|
|
2016-05-10 22:30:49 +00:00
|
|
|
decode_meta(features[k][x].m, features[k][x].keys, features[k][x].values, features[k][x].stringpool, layer, feature);
|
2016-05-27 23:25:40 +00:00
|
|
|
|
|
|
|
if (additional[A_CALCULATE_FEATURE_DENSITY]) {
|
|
|
|
int glow = 255;
|
|
|
|
if (features[k][x].spacing > 0) {
|
|
|
|
glow = (1 / features[k][x].spacing);
|
|
|
|
if (glow > 255) {
|
|
|
|
glow = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mvt_value v;
|
|
|
|
v.type = mvt_sint;
|
|
|
|
v.numeric_value.sint_value = glow;
|
|
|
|
layer.tag(feature, "tippecanoe_feature_density", v);
|
|
|
|
}
|
|
|
|
|
2016-04-25 23:52:20 +00:00
|
|
|
layer.features.push_back(feature);
|
2016-04-25 23:19:52 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 23:52:20 +00:00
|
|
|
if (layer.features.size() > 0) {
|
|
|
|
tile.layers.push_back(layer);
|
2016-04-25 23:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 17:23:25 +00:00
|
|
|
if (z == 0 && unclipped_features < original_features / 2) {
|
|
|
|
fprintf(stderr, "\n\nMore than half the features were clipped away at zoom level 0.\n");
|
2015-06-05 17:34:19 +00:00
|
|
|
fprintf(stderr, "Is your data in the wrong projection? It should be in WGS84/EPSG:4326.\n");
|
2015-06-05 17:23:25 +00:00
|
|
|
}
|
|
|
|
|
2015-03-23 18:36:35 +00:00
|
|
|
long long totalsize = 0;
|
|
|
|
for (j = 0; j < nlayers; j++) {
|
|
|
|
totalsize += features[j].size();
|
2014-10-18 18:40:09 +00:00
|
|
|
}
|
|
|
|
|
2016-07-08 22:49:59 +00:00
|
|
|
double progress = floor((((*geompos_in + *along - alongminus) / (double) todo) + z) / (maxzoom + 1) * 1000) / 10;
|
|
|
|
if (progress >= oprogress + 0.1) {
|
|
|
|
if (!quiet) {
|
|
|
|
fprintf(stderr, " %3.1f%% %d/%u/%u \r", progress, z, tx, ty);
|
|
|
|
}
|
|
|
|
oprogress = progress;
|
|
|
|
}
|
|
|
|
|
2016-07-12 22:56:57 +00:00
|
|
|
if (totalsize > 0 && tile.layers.size() > 0) {
|
2016-01-11 18:46:25 +00:00
|
|
|
if (totalsize > 200000 && !prevent[P_FEATURE_LIMIT]) {
|
2015-03-23 18:36:35 +00:00
|
|
|
fprintf(stderr, "tile %d/%u/%u has %lld features, >200000 \n", z, tx, ty, totalsize);
|
2016-03-07 21:21:53 +00:00
|
|
|
fprintf(stderr, "Try using -B to set a higher base zoom level.\n");
|
2015-03-07 00:33:32 +00:00
|
|
|
return -1;
|
2014-12-19 22:33:39 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 19:13:52 +00:00
|
|
|
std::string compressed = tile.encode();
|
2014-09-23 19:17:18 +00:00
|
|
|
|
2016-01-11 18:46:25 +00:00
|
|
|
if (compressed.size() > 500000 && !prevent[P_KILOBYTE_LIMIT]) {
|
2015-07-08 22:06:21 +00:00
|
|
|
if (!quiet) {
|
|
|
|
fprintf(stderr, "tile %d/%u/%u size is %lld with detail %d, >500000 \n", z, tx, ty, (long long) compressed.size(), line_detail);
|
|
|
|
}
|
2014-10-08 23:39:44 +00:00
|
|
|
|
2016-01-11 18:46:25 +00:00
|
|
|
if (prevent[P_DYNAMIC_DROP]) {
|
2015-05-20 22:15:45 +00:00
|
|
|
// The 95% is a guess to avoid too many retries
|
|
|
|
// and probably actually varies based on how much duplicated metadata there is
|
|
|
|
|
|
|
|
fraction = fraction * 500000 / compressed.size() * 0.95;
|
2015-07-08 22:06:21 +00:00
|
|
|
if (!quiet) {
|
|
|
|
fprintf(stderr, "Going to try keeping %0.2f%% of the features to make it fit\n", fraction * 100);
|
|
|
|
}
|
2015-06-03 18:21:40 +00:00
|
|
|
line_detail++; // to keep it the same when the loop decrements it
|
2015-05-20 21:57:00 +00:00
|
|
|
}
|
2014-11-20 22:31:50 +00:00
|
|
|
} else {
|
2015-10-19 19:32:40 +00:00
|
|
|
if (pthread_mutex_lock(&db_lock) != 0) {
|
|
|
|
perror("pthread_mutex_lock");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2014-11-20 22:31:50 +00:00
|
|
|
mbtiles_write_tile(outdb, z, tx, ty, compressed.data(), compressed.size());
|
2015-10-19 19:32:40 +00:00
|
|
|
|
|
|
|
if (pthread_mutex_unlock(&db_lock) != 0) {
|
|
|
|
perror("pthread_mutex_unlock");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2014-11-20 22:31:50 +00:00
|
|
|
return count;
|
2014-10-08 23:59:00 +00:00
|
|
|
}
|
2014-10-07 17:27:17 +00:00
|
|
|
} else {
|
|
|
|
return count;
|
|
|
|
}
|
2014-09-23 19:17:18 +00:00
|
|
|
}
|
2014-09-23 00:46:48 +00:00
|
|
|
|
2014-10-07 17:27:17 +00:00
|
|
|
fprintf(stderr, "could not make tile %d/%u/%u small enough\n", z, tx, ty);
|
2015-03-07 00:33:32 +00:00
|
|
|
return -1;
|
2014-09-22 23:27:10 +00:00
|
|
|
}
|
2015-06-20 00:29:56 +00:00
|
|
|
|
2015-07-23 23:17:23 +00:00
|
|
|
struct task {
|
|
|
|
int fileno;
|
|
|
|
struct task *next;
|
2015-11-12 00:10:39 +00:00
|
|
|
};
|
2015-07-23 23:17:23 +00:00
|
|
|
|
|
|
|
struct write_tile_args {
|
|
|
|
struct task *tasks;
|
|
|
|
char *metabase;
|
|
|
|
char *stringpool;
|
|
|
|
int min_detail;
|
|
|
|
int basezoom;
|
2016-05-03 17:52:49 +00:00
|
|
|
std::vector<std::string> *layernames;
|
2015-07-23 23:17:23 +00:00
|
|
|
sqlite3 *outdb;
|
|
|
|
double droprate;
|
|
|
|
int buffer;
|
|
|
|
const char *fname;
|
|
|
|
FILE **geomfile;
|
|
|
|
double todo;
|
2015-10-20 17:15:02 +00:00
|
|
|
volatile long long *along;
|
2015-07-23 23:17:23 +00:00
|
|
|
double gamma;
|
|
|
|
int nlayers;
|
|
|
|
int child_shards;
|
|
|
|
int *geomfd;
|
|
|
|
off_t *geom_size;
|
2015-10-20 17:15:02 +00:00
|
|
|
volatile unsigned *midx;
|
|
|
|
volatile unsigned *midy;
|
2015-07-23 23:17:23 +00:00
|
|
|
int maxzoom;
|
|
|
|
int minzoom;
|
|
|
|
int full_detail;
|
|
|
|
int low_detail;
|
2016-07-12 23:51:56 +00:00
|
|
|
double simplification;
|
2015-10-20 17:15:02 +00:00
|
|
|
volatile long long *most;
|
2015-12-22 02:00:07 +00:00
|
|
|
long long *meta_off;
|
|
|
|
long long *pool_off;
|
2015-12-23 00:58:27 +00:00
|
|
|
unsigned *initial_x;
|
|
|
|
unsigned *initial_y;
|
2016-01-05 21:56:36 +00:00
|
|
|
volatile int *running;
|
2016-05-03 17:52:49 +00:00
|
|
|
int err;
|
2015-07-23 23:17:23 +00:00
|
|
|
};
|
|
|
|
|
2015-10-19 19:32:40 +00:00
|
|
|
void *run_thread(void *vargs) {
|
|
|
|
write_tile_args *arg = (write_tile_args *) vargs;
|
2015-07-23 23:17:23 +00:00
|
|
|
struct task *task;
|
|
|
|
|
|
|
|
for (task = arg->tasks; task != NULL; task = task->next) {
|
|
|
|
int j = task->fileno;
|
|
|
|
|
|
|
|
if (arg->geomfd[j] < 0) {
|
|
|
|
// only one source file for zoom level 0
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arg->geom_size[j] == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// printf("%lld of geom_size\n", (long long) geom_size[j]);
|
|
|
|
|
2016-04-11 22:59:02 +00:00
|
|
|
FILE *geom = fdopen(arg->geomfd[j], "rb");
|
|
|
|
if (geom == NULL) {
|
2015-07-23 23:17:23 +00:00
|
|
|
perror("mmap geom");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2016-04-11 22:59:02 +00:00
|
|
|
long long geompos = 0;
|
|
|
|
long long prevgeom = 0;
|
2015-07-23 23:17:23 +00:00
|
|
|
|
2016-04-11 22:59:02 +00:00
|
|
|
while (1) {
|
2015-07-23 23:17:23 +00:00
|
|
|
int z;
|
|
|
|
unsigned x, y;
|
|
|
|
|
2016-04-11 22:59:02 +00:00
|
|
|
if (!deserialize_int_io(geom, &z, &geompos)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
deserialize_uint_io(geom, &x, &geompos);
|
|
|
|
deserialize_uint_io(geom, &y, &geompos);
|
2015-07-23 23:17:23 +00:00
|
|
|
|
|
|
|
// fprintf(stderr, "%d/%u/%u\n", z, x, y);
|
|
|
|
|
2016-07-12 23:51:56 +00:00
|
|
|
long long len = write_tile(geom, &geompos, arg->metabase, arg->stringpool, z, x, y, z == arg->maxzoom ? arg->full_detail : arg->low_detail, arg->min_detail, arg->basezoom, arg->layernames, arg->outdb, arg->droprate, arg->buffer, arg->fname, arg->geomfile, arg->minzoom, arg->maxzoom, arg->todo, arg->along, geompos, arg->gamma, arg->nlayers, arg->child_shards, arg->meta_off, arg->pool_off, arg->initial_x, arg->initial_y, arg->running, arg->simplification);
|
2015-07-23 23:17:23 +00:00
|
|
|
|
|
|
|
if (len < 0) {
|
2016-05-03 17:52:49 +00:00
|
|
|
int *err = &arg->err;
|
2015-10-19 20:39:44 +00:00
|
|
|
*err = z - 1;
|
|
|
|
return err;
|
2015-07-23 23:17:23 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 21:17:04 +00:00
|
|
|
if (pthread_mutex_lock(&var_lock) != 0) {
|
2015-10-19 20:32:02 +00:00
|
|
|
perror("pthread_mutex_lock");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2016-01-28 22:38:10 +00:00
|
|
|
if (z == arg->maxzoom) {
|
|
|
|
if (len > *arg->most) {
|
|
|
|
*arg->midx = x;
|
|
|
|
*arg->midy = y;
|
|
|
|
*arg->most = len;
|
|
|
|
} else if (len == *arg->most) {
|
2016-01-28 23:35:22 +00:00
|
|
|
unsigned long long a = (((unsigned long long) x) << 32) | y;
|
2016-01-28 22:38:10 +00:00
|
|
|
unsigned long long b = (((unsigned long long) *arg->midx) << 32) | *arg->midy;
|
|
|
|
|
|
|
|
if (a < b) {
|
|
|
|
*arg->midx = x;
|
|
|
|
*arg->midy = y;
|
|
|
|
*arg->most = len;
|
|
|
|
}
|
|
|
|
}
|
2015-07-23 23:17:23 +00:00
|
|
|
}
|
2015-10-19 20:32:02 +00:00
|
|
|
|
2016-04-11 22:59:02 +00:00
|
|
|
*arg->along += geompos - prevgeom;
|
|
|
|
prevgeom = geompos;
|
2015-10-19 21:17:04 +00:00
|
|
|
|
|
|
|
if (pthread_mutex_unlock(&var_lock) != 0) {
|
2015-10-19 20:32:02 +00:00
|
|
|
perror("pthread_mutex_unlock");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2015-07-23 23:17:23 +00:00
|
|
|
}
|
|
|
|
|
2016-04-11 22:59:02 +00:00
|
|
|
if (fclose(geom) != 0) {
|
|
|
|
perror("close geom");
|
|
|
|
exit(EXIT_FAILURE);
|
2015-07-23 23:17:23 +00:00
|
|
|
}
|
2016-04-11 22:59:02 +00:00
|
|
|
// Since the fclose() has closed the underlying file descriptor
|
|
|
|
arg->geomfd[j] = -1;
|
2015-07-23 23:17:23 +00:00
|
|
|
}
|
2015-10-19 19:32:40 +00:00
|
|
|
|
2016-01-05 21:56:36 +00:00
|
|
|
arg->running--;
|
2015-10-19 19:32:40 +00:00
|
|
|
return NULL;
|
2015-07-23 23:17:23 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 23:51:56 +00:00
|
|
|
int traverse_zooms(int *geomfd, off_t *geom_size, char *metabase, char *stringpool, unsigned *midx, unsigned *midy, std::vector<std::string> &layernames, int maxzoom, int minzoom, int basezoom, sqlite3 *outdb, double droprate, int buffer, const char *fname, const char *tmpdir, double gamma, int nlayers, int full_detail, int low_detail, int min_detail, long long *meta_off, long long *pool_off, unsigned *initial_x, unsigned *initial_y, double simplification) {
|
2015-06-20 00:29:56 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i <= maxzoom; i++) {
|
|
|
|
long long most = 0;
|
|
|
|
|
2015-07-08 23:33:22 +00:00
|
|
|
FILE *sub[TEMP_FILES];
|
|
|
|
int subfd[TEMP_FILES];
|
2015-06-20 00:29:56 +00:00
|
|
|
int j;
|
2015-07-08 23:33:22 +00:00
|
|
|
for (j = 0; j < TEMP_FILES; j++) {
|
2015-07-10 17:26:23 +00:00
|
|
|
char geomname[strlen(tmpdir) + strlen("/geom.XXXXXXXX" XSTRINGIFY(INT_MAX)) + 1];
|
2015-06-20 00:29:56 +00:00
|
|
|
sprintf(geomname, "%s/geom%d.XXXXXXXX", tmpdir, j);
|
|
|
|
subfd[j] = mkstemp(geomname);
|
|
|
|
// printf("%s\n", geomname);
|
|
|
|
if (subfd[j] < 0) {
|
|
|
|
perror(geomname);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
sub[j] = fopen(geomname, "wb");
|
|
|
|
if (sub[j] == NULL) {
|
|
|
|
perror(geomname);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
unlink(geomname);
|
|
|
|
}
|
|
|
|
|
2015-07-09 22:24:47 +00:00
|
|
|
int useful_threads = 0;
|
2015-06-20 00:29:56 +00:00
|
|
|
long long todo = 0;
|
|
|
|
long long along = 0;
|
2015-07-08 23:33:22 +00:00
|
|
|
for (j = 0; j < TEMP_FILES; j++) {
|
2015-06-20 00:29:56 +00:00
|
|
|
todo += geom_size[j];
|
2015-07-09 22:24:47 +00:00
|
|
|
if (geom_size[j] > 0) {
|
|
|
|
useful_threads++;
|
|
|
|
}
|
2015-06-20 00:29:56 +00:00
|
|
|
}
|
2015-07-08 23:33:22 +00:00
|
|
|
|
2015-11-12 00:10:39 +00:00
|
|
|
int threads = CPUS;
|
2015-07-09 22:24:47 +00:00
|
|
|
if (threads > TEMP_FILES / 4) {
|
|
|
|
threads = TEMP_FILES / 4;
|
|
|
|
}
|
|
|
|
// XXX is it useful to divide further if we know we are skipping
|
|
|
|
// some zoom levels? Is it faster to have fewer CPUs working on
|
|
|
|
// sharding, but more deeply, or fewer CPUs, less deeply?
|
|
|
|
if (threads > useful_threads) {
|
|
|
|
threads = useful_threads;
|
|
|
|
}
|
2016-04-27 23:41:41 +00:00
|
|
|
|
2015-07-09 22:24:47 +00:00
|
|
|
// Round down to a power of 2
|
2016-04-27 23:41:41 +00:00
|
|
|
for (int e = 0; e < 30; e++) {
|
|
|
|
if (threads >= (1 << e) && threads < (1 << (e + 1))) {
|
|
|
|
threads = 1 << e;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (threads >= (1 << 30)) {
|
|
|
|
threads = 1 << 30;
|
|
|
|
}
|
2015-07-08 23:33:22 +00:00
|
|
|
|
2015-07-09 23:09:40 +00:00
|
|
|
// Assign temporary files to threads
|
|
|
|
|
2015-11-12 00:10:39 +00:00
|
|
|
struct task tasks[TEMP_FILES];
|
2015-07-09 23:09:40 +00:00
|
|
|
struct dispatch {
|
|
|
|
struct task *tasks;
|
|
|
|
long long todo;
|
|
|
|
struct dispatch *next;
|
|
|
|
} dispatches[threads];
|
|
|
|
struct dispatch *dispatch_head = &dispatches[0];
|
|
|
|
for (j = 0; j < threads; j++) {
|
|
|
|
dispatches[j].tasks = NULL;
|
|
|
|
dispatches[j].todo = 0;
|
|
|
|
if (j + 1 < threads) {
|
|
|
|
dispatches[j].next = &dispatches[j + 1];
|
|
|
|
} else {
|
|
|
|
dispatches[j].next = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < TEMP_FILES; j++) {
|
|
|
|
if (geom_size[j] == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks[j].fileno = j;
|
|
|
|
tasks[j].next = dispatch_head->tasks;
|
|
|
|
dispatch_head->tasks = &tasks[j];
|
|
|
|
dispatch_head->todo += geom_size[j];
|
|
|
|
|
|
|
|
struct dispatch *here = dispatch_head;
|
|
|
|
dispatch_head = dispatch_head->next;
|
|
|
|
|
|
|
|
dispatch **d;
|
|
|
|
for (d = &dispatch_head; *d != NULL; d = &((*d)->next)) {
|
|
|
|
if (here->todo < (*d)->todo) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
here->next = *d;
|
|
|
|
*d = here;
|
2015-07-09 23:14:24 +00:00
|
|
|
}
|
2015-07-09 23:09:40 +00:00
|
|
|
|
2015-10-19 19:32:40 +00:00
|
|
|
pthread_t pthreads[threads];
|
|
|
|
write_tile_args args[threads];
|
2016-01-05 21:56:36 +00:00
|
|
|
int running = threads;
|
2015-10-19 19:32:40 +00:00
|
|
|
|
2015-07-09 23:14:24 +00:00
|
|
|
int thread;
|
|
|
|
for (thread = 0; thread < threads; thread++) {
|
2015-10-19 19:32:40 +00:00
|
|
|
args[thread].metabase = metabase;
|
|
|
|
args[thread].stringpool = stringpool;
|
|
|
|
args[thread].min_detail = min_detail;
|
2015-12-15 19:56:49 +00:00
|
|
|
args[thread].basezoom = basezoom;
|
2016-05-03 17:52:49 +00:00
|
|
|
args[thread].layernames = &layernames;
|
2015-10-20 19:03:49 +00:00
|
|
|
args[thread].outdb = outdb; // locked with db_lock
|
2015-10-19 19:32:40 +00:00
|
|
|
args[thread].droprate = droprate;
|
|
|
|
args[thread].buffer = buffer;
|
|
|
|
args[thread].fname = fname;
|
|
|
|
args[thread].geomfile = sub + thread * (TEMP_FILES / threads);
|
|
|
|
args[thread].todo = todo;
|
2015-10-20 19:03:49 +00:00
|
|
|
args[thread].along = &along; // locked with var_lock
|
2015-10-19 19:32:40 +00:00
|
|
|
args[thread].gamma = gamma;
|
|
|
|
args[thread].nlayers = nlayers;
|
|
|
|
args[thread].child_shards = TEMP_FILES / threads;
|
2016-07-12 23:51:56 +00:00
|
|
|
args[thread].simplification = simplification;
|
2015-10-19 19:32:40 +00:00
|
|
|
|
|
|
|
args[thread].geomfd = geomfd;
|
|
|
|
args[thread].geom_size = geom_size;
|
2015-10-20 19:03:49 +00:00
|
|
|
args[thread].midx = midx; // locked with var_lock
|
|
|
|
args[thread].midy = midy; // locked with var_lock
|
2015-10-19 19:32:40 +00:00
|
|
|
args[thread].maxzoom = maxzoom;
|
|
|
|
args[thread].minzoom = minzoom;
|
|
|
|
args[thread].full_detail = full_detail;
|
|
|
|
args[thread].low_detail = low_detail;
|
2015-10-20 19:03:49 +00:00
|
|
|
args[thread].most = &most; // locked with var_lock
|
2015-12-22 02:00:07 +00:00
|
|
|
args[thread].meta_off = meta_off;
|
|
|
|
args[thread].pool_off = pool_off;
|
2015-12-23 00:58:27 +00:00
|
|
|
args[thread].initial_x = initial_x;
|
|
|
|
args[thread].initial_y = initial_y;
|
2015-10-19 19:32:40 +00:00
|
|
|
|
|
|
|
args[thread].tasks = dispatches[thread].tasks;
|
2016-01-05 21:56:36 +00:00
|
|
|
args[thread].running = &running;
|
2015-10-19 19:32:40 +00:00
|
|
|
|
|
|
|
if (pthread_create(&pthreads[thread], NULL, run_thread, &args[thread]) != 0) {
|
|
|
|
perror("pthread_create");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 20:39:44 +00:00
|
|
|
int err = INT_MAX;
|
|
|
|
|
2015-10-19 19:32:40 +00:00
|
|
|
for (thread = 0; thread < threads; thread++) {
|
|
|
|
void *retval;
|
|
|
|
|
|
|
|
if (pthread_join(pthreads[thread], &retval) != 0) {
|
|
|
|
perror("pthread_join");
|
|
|
|
}
|
2015-10-19 20:39:44 +00:00
|
|
|
|
|
|
|
if (retval != NULL) {
|
|
|
|
err = *((int *) retval);
|
|
|
|
}
|
2015-06-20 00:29:56 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 23:33:22 +00:00
|
|
|
for (j = 0; j < TEMP_FILES; j++) {
|
2016-04-05 18:13:31 +00:00
|
|
|
// Can be < 0 if there is only one source file, at z0
|
|
|
|
if (geomfd[j] >= 0) {
|
|
|
|
if (close(geomfd[j]) != 0) {
|
|
|
|
perror("close geom");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fclose(sub[j]) != 0) {
|
|
|
|
perror("close subfile");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2015-06-20 00:29:56 +00:00
|
|
|
|
|
|
|
struct stat geomst;
|
|
|
|
if (fstat(subfd[j], &geomst) != 0) {
|
|
|
|
perror("stat geom\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
geomfd[j] = subfd[j];
|
|
|
|
geom_size[j] = geomst.st_size;
|
|
|
|
}
|
2015-10-19 20:39:44 +00:00
|
|
|
|
|
|
|
if (err != INT_MAX) {
|
|
|
|
return err;
|
|
|
|
}
|
2015-06-20 00:29:56 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 21:07:24 +00:00
|
|
|
int j;
|
|
|
|
for (j = 0; j < TEMP_FILES; j++) {
|
|
|
|
// Can be < 0 if there is only one source file, at z0
|
|
|
|
if (geomfd[j] >= 0) {
|
|
|
|
if (close(geomfd[j]) != 0) {
|
|
|
|
perror("close geom");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 22:06:21 +00:00
|
|
|
if (!quiet) {
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2015-06-20 00:29:56 +00:00
|
|
|
return maxzoom;
|
|
|
|
}
|