2015-08-20 20:02:34 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-04-23 06:32:02 +00:00
|
|
|
#include <string.h>
|
2015-08-20 20:02:34 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sqlite3.h>
|
2015-11-02 22:34:01 +00:00
|
|
|
#include <limits.h>
|
2015-08-20 23:45:45 +00:00
|
|
|
#include <vector>
|
2015-08-20 20:02:34 +00:00
|
|
|
#include <string>
|
2015-08-20 23:45:45 +00:00
|
|
|
#include <map>
|
2016-04-28 21:43:04 +00:00
|
|
|
#include <set>
|
2015-08-20 20:02:34 +00:00
|
|
|
#include <zlib.h>
|
|
|
|
#include <math.h>
|
2016-09-20 19:53:17 +00:00
|
|
|
#include <pthread.h>
|
2016-04-27 19:22:47 +00:00
|
|
|
#include "mvt.hpp"
|
2016-04-27 21:00:14 +00:00
|
|
|
#include "projection.hpp"
|
|
|
|
#include "pool.hpp"
|
|
|
|
#include "mbtiles.hpp"
|
2016-04-27 22:09:06 +00:00
|
|
|
#include "geometry.hpp"
|
2015-08-20 20:02:34 +00:00
|
|
|
|
2015-08-21 00:32:58 +00:00
|
|
|
std::string dequote(std::string s);
|
|
|
|
|
2016-09-20 00:20:44 +00:00
|
|
|
bool pk = false;
|
2016-09-20 23:00:03 +00:00
|
|
|
size_t CPUS;
|
2016-09-20 00:20:44 +00:00
|
|
|
|
2015-08-20 23:12:34 +00:00
|
|
|
struct stats {
|
|
|
|
int minzoom;
|
|
|
|
int maxzoom;
|
|
|
|
double midlat, midlon;
|
|
|
|
double minlat, minlon, maxlat, maxlon;
|
|
|
|
};
|
|
|
|
|
2016-09-20 19:53:17 +00:00
|
|
|
void handle(std::string message, int z, unsigned x, unsigned y, std::map<std::string, layermap_entry> &layermap, std::vector<std::string> &header, std::map<std::string, std::vector<std::string>> &mapping, std::set<std::string> &exclude, int ifmatched, mvt_tile &outtile) {
|
2016-04-22 22:47:06 +00:00
|
|
|
mvt_tile tile;
|
2015-09-30 18:56:22 +00:00
|
|
|
int features_added = 0;
|
2015-08-20 20:02:34 +00:00
|
|
|
|
2016-04-25 19:13:52 +00:00
|
|
|
if (!tile.decode(message)) {
|
2016-04-22 22:47:06 +00:00
|
|
|
fprintf(stderr, "Couldn't decompress tile %d/%u/%u\n", z, x, y);
|
2015-08-20 20:02:34 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2016-04-22 22:47:06 +00:00
|
|
|
for (size_t l = 0; l < tile.layers.size(); l++) {
|
|
|
|
mvt_layer &layer = tile.layers[l];
|
2015-08-20 20:02:34 +00:00
|
|
|
|
2016-09-19 23:02:14 +00:00
|
|
|
size_t ol;
|
|
|
|
for (ol = 0; ol < outtile.layers.size(); ol++) {
|
|
|
|
if (tile.layers[l].name == outtile.layers[ol].name) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ol == outtile.layers.size()) {
|
|
|
|
outtile.layers.push_back(mvt_layer());
|
|
|
|
|
|
|
|
outtile.layers[ol].name = layer.name;
|
|
|
|
outtile.layers[ol].version = layer.version;
|
|
|
|
outtile.layers[ol].extent = layer.extent;
|
|
|
|
}
|
|
|
|
|
|
|
|
mvt_layer &outlayer = outtile.layers[ol];
|
2015-08-20 20:02:34 +00:00
|
|
|
|
2016-09-19 23:25:30 +00:00
|
|
|
if (layer.extent != outlayer.extent) {
|
|
|
|
if (layer.extent > outlayer.extent) {
|
|
|
|
for (size_t i = 0; i < outlayer.features.size(); i++) {
|
|
|
|
for (size_t j = 0; j < outlayer.features[i].geometry.size(); j++) {
|
|
|
|
outlayer.features[i].geometry[j].x = outlayer.features[i].geometry[j].x * layer.extent / outlayer.extent;
|
|
|
|
outlayer.features[i].geometry[j].y = outlayer.features[i].geometry[j].y * layer.extent / outlayer.extent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
outlayer.extent = layer.extent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:53:41 +00:00
|
|
|
if (layermap.count(layer.name) == 0) {
|
|
|
|
layermap.insert(std::pair<std::string, layermap_entry>(layer.name, layermap_entry(layermap.size())));
|
2016-09-20 00:53:31 +00:00
|
|
|
auto file_keys = layermap.find(layer.name);
|
|
|
|
file_keys->second.minzoom = z;
|
|
|
|
file_keys->second.maxzoom = z;
|
2015-08-20 20:42:24 +00:00
|
|
|
}
|
2016-09-19 23:53:41 +00:00
|
|
|
auto file_keys = layermap.find(layer.name);
|
2015-08-20 20:42:24 +00:00
|
|
|
|
2016-04-22 22:47:06 +00:00
|
|
|
for (size_t f = 0; f < layer.features.size(); f++) {
|
|
|
|
mvt_feature feat = layer.features[f];
|
2016-04-25 21:19:38 +00:00
|
|
|
mvt_feature outfeature;
|
2015-08-24 21:12:46 +00:00
|
|
|
int matched = 0;
|
2015-08-20 21:27:39 +00:00
|
|
|
|
2016-08-16 20:21:15 +00:00
|
|
|
if (feat.has_id) {
|
|
|
|
outfeature.has_id = true;
|
|
|
|
outfeature.id = feat.id;
|
|
|
|
}
|
|
|
|
|
2016-05-03 23:39:26 +00:00
|
|
|
for (size_t t = 0; t + 1 < feat.tags.size(); t += 2) {
|
2016-04-22 22:47:06 +00:00
|
|
|
const char *key = layer.keys[feat.tags[t]].c_str();
|
|
|
|
mvt_value &val = layer.values[feat.tags[t + 1]];
|
2016-05-03 18:14:09 +00:00
|
|
|
std::string value;
|
2015-08-21 17:33:57 +00:00
|
|
|
int type = -1;
|
2015-08-20 21:27:39 +00:00
|
|
|
|
2016-04-22 22:47:06 +00:00
|
|
|
if (val.type == mvt_string) {
|
2016-05-03 18:14:09 +00:00
|
|
|
value = val.string_value;
|
2015-08-20 21:27:39 +00:00
|
|
|
type = VT_STRING;
|
2016-04-22 22:47:06 +00:00
|
|
|
} else if (val.type == mvt_int) {
|
2016-05-03 18:14:09 +00:00
|
|
|
aprintf(&value, "%lld", (long long) val.numeric_value.int_value);
|
|
|
|
type = VT_NUMBER;
|
2016-04-22 22:47:06 +00:00
|
|
|
} else if (val.type == mvt_double) {
|
2016-05-03 18:14:09 +00:00
|
|
|
aprintf(&value, "%g", val.numeric_value.double_value);
|
|
|
|
type = VT_NUMBER;
|
2016-04-22 22:47:06 +00:00
|
|
|
} else if (val.type == mvt_float) {
|
2016-05-03 18:14:09 +00:00
|
|
|
aprintf(&value, "%g", val.numeric_value.float_value);
|
|
|
|
type = VT_NUMBER;
|
2016-04-22 22:47:06 +00:00
|
|
|
} else if (val.type == mvt_bool) {
|
2016-05-03 18:14:09 +00:00
|
|
|
aprintf(&value, "%s", val.numeric_value.bool_value ? "true" : "false");
|
|
|
|
type = VT_BOOLEAN;
|
2016-04-22 22:47:06 +00:00
|
|
|
} else if (val.type == mvt_sint) {
|
2016-05-03 18:14:09 +00:00
|
|
|
aprintf(&value, "%lld", (long long) val.numeric_value.sint_value);
|
|
|
|
type = VT_NUMBER;
|
2016-04-22 22:47:06 +00:00
|
|
|
} else if (val.type == mvt_uint) {
|
2016-05-03 18:14:09 +00:00
|
|
|
aprintf(&value, "%llu", (long long) val.numeric_value.uint_value);
|
|
|
|
type = VT_NUMBER;
|
2015-08-20 21:27:39 +00:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-21 17:33:57 +00:00
|
|
|
if (type < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-04-28 21:43:04 +00:00
|
|
|
if (exclude.count(std::string(key)) == 0) {
|
|
|
|
type_and_string tas;
|
|
|
|
tas.string = std::string(key);
|
|
|
|
tas.type = type;
|
2016-09-19 23:53:41 +00:00
|
|
|
file_keys->second.file_keys.insert(tas);
|
2016-04-25 21:19:38 +00:00
|
|
|
outlayer.tag(outfeature, layer.keys[feat.tags[t]], val);
|
2015-08-22 00:32:44 +00:00
|
|
|
}
|
2015-08-21 00:32:58 +00:00
|
|
|
|
2015-10-31 00:59:15 +00:00
|
|
|
if (header.size() > 0 && strcmp(key, header[0].c_str()) == 0) {
|
2016-09-20 19:53:17 +00:00
|
|
|
std::map<std::string, std::vector<std::string>>::iterator ii = mapping.find(value);
|
2015-08-21 00:32:58 +00:00
|
|
|
|
|
|
|
if (ii != mapping.end()) {
|
|
|
|
std::vector<std::string> fields = ii->second;
|
2015-08-24 21:12:46 +00:00
|
|
|
matched = 1;
|
2015-08-21 00:32:58 +00:00
|
|
|
|
2016-03-25 19:20:32 +00:00
|
|
|
for (size_t i = 1; i < fields.size(); i++) {
|
2015-08-21 00:32:58 +00:00
|
|
|
std::string joinkey = header[i];
|
|
|
|
std::string joinval = fields[i];
|
2016-05-03 22:48:42 +00:00
|
|
|
int attr_type = VT_STRING;
|
2015-08-21 00:32:58 +00:00
|
|
|
|
|
|
|
if (joinval.size() > 0) {
|
|
|
|
if (joinval[0] == '"') {
|
|
|
|
joinval = dequote(joinval);
|
|
|
|
} else if ((joinval[0] >= '0' && joinval[0] <= '9') || joinval[0] == '-') {
|
2016-05-03 22:48:42 +00:00
|
|
|
attr_type = VT_NUMBER;
|
2015-08-21 00:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *sjoinkey = joinkey.c_str();
|
|
|
|
|
2016-04-28 21:43:04 +00:00
|
|
|
if (exclude.count(joinkey) == 0) {
|
|
|
|
type_and_string tas;
|
|
|
|
tas.string = std::string(sjoinkey);
|
2016-05-03 22:48:42 +00:00
|
|
|
tas.type = attr_type;
|
2016-09-19 23:53:41 +00:00
|
|
|
file_keys->second.file_keys.insert(tas);
|
2015-08-21 00:32:58 +00:00
|
|
|
|
2016-04-25 21:19:38 +00:00
|
|
|
mvt_value outval;
|
2016-05-03 22:48:42 +00:00
|
|
|
if (attr_type == VT_STRING) {
|
2016-04-25 21:19:38 +00:00
|
|
|
outval.type = mvt_string;
|
|
|
|
outval.string_value = joinval;
|
2015-08-22 00:32:44 +00:00
|
|
|
} else {
|
2016-04-25 21:19:38 +00:00
|
|
|
outval.type = mvt_double;
|
|
|
|
outval.numeric_value.double_value = atof(joinval.c_str());
|
2015-08-22 00:32:44 +00:00
|
|
|
}
|
2015-08-21 00:32:58 +00:00
|
|
|
|
2016-04-25 21:19:38 +00:00
|
|
|
outlayer.tag(outfeature, joinkey, outval);
|
2015-08-22 00:32:44 +00:00
|
|
|
}
|
2015-08-21 00:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-20 21:27:39 +00:00
|
|
|
}
|
2015-08-24 21:12:46 +00:00
|
|
|
|
|
|
|
if (matched || !ifmatched) {
|
2016-04-22 22:47:06 +00:00
|
|
|
outfeature.type = feat.type;
|
|
|
|
outfeature.geometry = feat.geometry;
|
2015-08-24 21:12:46 +00:00
|
|
|
|
2016-09-19 23:25:30 +00:00
|
|
|
if (layer.extent != outlayer.extent) {
|
|
|
|
for (size_t i = 0; i < outfeature.geometry.size(); i++) {
|
|
|
|
outfeature.geometry[i].x = outfeature.geometry[i].x * outlayer.extent / layer.extent;
|
|
|
|
outfeature.geometry[i].y = outfeature.geometry[i].y * outlayer.extent / layer.extent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 18:56:22 +00:00
|
|
|
features_added++;
|
2016-04-22 22:47:06 +00:00
|
|
|
outlayer.features.push_back(outfeature);
|
2016-09-20 00:53:31 +00:00
|
|
|
|
|
|
|
if (z < file_keys->second.minzoom) {
|
|
|
|
file_keys->second.minzoom = z;
|
|
|
|
}
|
|
|
|
if (z > file_keys->second.maxzoom) {
|
|
|
|
file_keys->second.maxzoom = z;
|
|
|
|
}
|
2015-08-24 21:12:46 +00:00
|
|
|
}
|
2015-08-20 20:02:34 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-20 22:15:47 +00:00
|
|
|
|
2015-09-30 18:56:22 +00:00
|
|
|
if (features_added == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-20 20:02:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 22:34:01 +00:00
|
|
|
double min(double a, double b) {
|
|
|
|
if (a < b) {
|
|
|
|
return a;
|
|
|
|
} else {
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double max(double a, double b) {
|
|
|
|
if (a > b) {
|
|
|
|
return a;
|
|
|
|
} else {
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 22:29:13 +00:00
|
|
|
struct reader {
|
|
|
|
long long zoom;
|
|
|
|
long long x;
|
|
|
|
long long sorty;
|
|
|
|
long long y;
|
|
|
|
|
2016-09-20 17:17:02 +00:00
|
|
|
std::string data;
|
2016-09-19 22:29:13 +00:00
|
|
|
|
|
|
|
sqlite3 *db;
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
struct reader *next;
|
|
|
|
|
|
|
|
bool operator<(const struct reader &r) const {
|
|
|
|
if (zoom < r.zoom) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (zoom > r.zoom) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x < r.x) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (x > r.x) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sorty < r.sorty) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-20 17:17:02 +00:00
|
|
|
if (sorty > r.sorty) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data < r.data) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-19 22:29:13 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct reader *begin_reading(char *fname) {
|
2015-08-20 20:02:34 +00:00
|
|
|
sqlite3 *db;
|
|
|
|
|
|
|
|
if (sqlite3_open(fname, &db) != SQLITE_OK) {
|
|
|
|
fprintf(stderr, "%s: %s\n", fname, sqlite3_errmsg(db));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2016-09-19 22:29:13 +00:00
|
|
|
const char *sql = "SELECT zoom_level, tile_column, tile_row, tile_data from tiles order by zoom_level, tile_column, tile_row;";
|
2015-08-20 20:02:34 +00:00
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) {
|
|
|
|
fprintf(stderr, "%s: select failed: %s\n", fname, sqlite3_errmsg(db));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2016-09-19 22:29:13 +00:00
|
|
|
struct reader *r = new reader;
|
|
|
|
r->db = db;
|
|
|
|
r->stmt = stmt;
|
|
|
|
r->next = NULL;
|
2015-08-20 20:02:34 +00:00
|
|
|
|
2016-09-19 22:29:13 +00:00
|
|
|
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
|
|
|
r->zoom = sqlite3_column_int(stmt, 0);
|
|
|
|
r->x = sqlite3_column_int(stmt, 1);
|
|
|
|
r->sorty = sqlite3_column_int(stmt, 2);
|
|
|
|
r->y = (1LL << r->zoom) - 1 - r->sorty;
|
2015-08-20 20:02:34 +00:00
|
|
|
|
2016-09-20 17:17:02 +00:00
|
|
|
const char *data = (const char *) sqlite3_column_blob(stmt, 3);
|
|
|
|
size_t len = sqlite3_column_bytes(stmt, 3);
|
|
|
|
|
|
|
|
r->data = std::string(data, len);
|
2016-09-19 22:29:13 +00:00
|
|
|
} else {
|
|
|
|
r->zoom = 32;
|
2015-08-20 20:02:34 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 22:29:13 +00:00
|
|
|
return r;
|
|
|
|
}
|
2015-08-20 20:02:34 +00:00
|
|
|
|
2016-09-20 19:53:17 +00:00
|
|
|
struct zxy {
|
|
|
|
long long z;
|
|
|
|
long long x;
|
|
|
|
long long y;
|
|
|
|
|
|
|
|
zxy(long long _z, long long _x, long long _y) {
|
|
|
|
z = _z;
|
|
|
|
x = _x;
|
|
|
|
y = _y;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(zxy const &other) const {
|
|
|
|
if (z < other.z) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (z > other.z) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x < other.x) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (x > other.x) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y < other.y) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct arg {
|
|
|
|
std::map<zxy, std::vector<std::string>> inputs;
|
|
|
|
std::map<zxy, std::string> outputs;
|
|
|
|
|
|
|
|
std::map<std::string, layermap_entry> *layermap;
|
|
|
|
|
|
|
|
std::vector<std::string> *header;
|
|
|
|
std::map<std::string, std::vector<std::string>> *mapping;
|
|
|
|
std::set<std::string> *exclude;
|
|
|
|
int ifmatched;
|
|
|
|
};
|
|
|
|
|
|
|
|
void *join_worker(void *v) {
|
|
|
|
arg *a = (arg *) v;
|
|
|
|
|
|
|
|
for (auto ai = a->inputs.begin(); ai != a->inputs.end(); ++ai) {
|
|
|
|
mvt_tile tile;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ai->second.size(); i++) {
|
|
|
|
handle(ai->second[i], ai->first.z, ai->first.x, ai->first.y, *(a->layermap), *(a->header), *(a->mapping), *(a->exclude), a->ifmatched, tile);
|
|
|
|
}
|
|
|
|
|
2016-09-20 21:19:40 +00:00
|
|
|
ai->second.clear();
|
|
|
|
|
2016-09-20 19:53:17 +00:00
|
|
|
bool anything = false;
|
|
|
|
for (size_t i = 0; i < tile.layers.size(); i++) {
|
|
|
|
if (tile.layers[i].features.size() > 0) {
|
|
|
|
anything = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (anything) {
|
|
|
|
std::string compressed = tile.encode();
|
|
|
|
|
|
|
|
if (!pk && compressed.size() > 500000) {
|
|
|
|
fprintf(stderr, "Tile %lld/%lld/%lld size is %lld, >500000. Skipping this tile\n.", ai->first.z, ai->first.x, ai->first.y, (long long) compressed.size());
|
|
|
|
} else {
|
|
|
|
a->outputs.insert(std::pair<zxy, std::string>(ai->first, compressed));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void handle_tasks(std::map<zxy, std::vector<std::string>> &tasks, std::vector<std::map<std::string, layermap_entry>> &layermaps, sqlite3 *outdb, std::vector<std::string> &header, std::map<std::string, std::vector<std::string>> &mapping, std::set<std::string> &exclude, int ifmatched) {
|
|
|
|
pthread_t pthreads[CPUS];
|
|
|
|
std::vector<arg> args;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < CPUS; i++) {
|
|
|
|
args.push_back(arg());
|
|
|
|
|
|
|
|
args[i].layermap = &layermaps[i];
|
|
|
|
args[i].header = &header;
|
|
|
|
args[i].mapping = &mapping;
|
|
|
|
args[i].exclude = &exclude;
|
|
|
|
args[i].ifmatched = ifmatched;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t count = 0;
|
2016-09-20 21:19:40 +00:00
|
|
|
// This isn't careful about distributing tasks evenly across CPUs,
|
|
|
|
// but, from testing, it actually takes a little longer to do
|
|
|
|
// the proper allocation than is saved by perfectly balanced threads.
|
2016-09-20 19:53:17 +00:00
|
|
|
for (auto ai = tasks.begin(); ai != tasks.end(); ++ai) {
|
|
|
|
args[count].inputs.insert(*ai);
|
|
|
|
count = (count + 1) % CPUS;
|
|
|
|
|
|
|
|
if (ai == tasks.begin()) {
|
|
|
|
fprintf(stderr, "%lld/%lld/%lld \r", ai->first.z, ai->first.x, ai->first.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < CPUS; i++) {
|
|
|
|
if (pthread_create(&pthreads[i], NULL, join_worker, &args[i]) != 0) {
|
|
|
|
perror("pthread_create");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 23:00:03 +00:00
|
|
|
for (size_t i = 0; i < CPUS; i++) {
|
2016-09-20 19:53:17 +00:00
|
|
|
void *retval;
|
|
|
|
|
|
|
|
if (pthread_join(pthreads[i], &retval) != 0) {
|
|
|
|
perror("pthread_join");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto ai = args[i].outputs.begin(); ai != args[i].outputs.end(); ++ai) {
|
|
|
|
mbtiles_write_tile(outdb, ai->first.z, ai->first.x, ai->first.y, ai->second.data(), ai->second.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void decode(struct reader *readers, char *map, std::map<std::string, layermap_entry> &layermap, sqlite3 *outdb, struct stats *st, std::vector<std::string> &header, std::map<std::string, std::vector<std::string>> &mapping, std::set<std::string> &exclude, int ifmatched, std::string &attribution) {
|
|
|
|
std::vector<std::map<std::string, layermap_entry>> layermaps;
|
2016-09-20 18:04:24 +00:00
|
|
|
for (size_t i = 0; i < CPUS; i++) {
|
|
|
|
layermaps.push_back(std::map<std::string, layermap_entry>());
|
|
|
|
}
|
|
|
|
|
2016-09-20 19:53:17 +00:00
|
|
|
std::map<zxy, std::vector<std::string>> tasks;
|
2016-09-19 23:02:14 +00:00
|
|
|
|
2016-09-19 22:29:13 +00:00
|
|
|
while (readers != NULL && readers->zoom < 32) {
|
|
|
|
reader *r = readers;
|
|
|
|
readers = readers->next;
|
|
|
|
r->next = NULL;
|
|
|
|
|
2016-09-20 19:53:17 +00:00
|
|
|
zxy tile = zxy(r->zoom, r->x, r->y);
|
|
|
|
if (tasks.count(tile) == 0) {
|
|
|
|
tasks.insert(std::pair<zxy, std::vector<std::string>>(tile, std::vector<std::string>()));
|
|
|
|
}
|
|
|
|
auto f = tasks.find(tile);
|
|
|
|
f->second.push_back(r->data);
|
2016-09-19 23:02:14 +00:00
|
|
|
|
|
|
|
if (readers == NULL || readers->zoom != r->zoom || readers->x != r->x || readers->y != r->y) {
|
2016-09-20 19:53:17 +00:00
|
|
|
if (tasks.size() > 100 * CPUS) {
|
|
|
|
handle_tasks(tasks, layermaps, outdb, header, mapping, exclude, ifmatched);
|
|
|
|
tasks.clear();
|
2016-09-19 23:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 22:29:13 +00:00
|
|
|
if (sqlite3_step(r->stmt) == SQLITE_ROW) {
|
|
|
|
r->zoom = sqlite3_column_int(r->stmt, 0);
|
|
|
|
r->x = sqlite3_column_int(r->stmt, 1);
|
|
|
|
r->sorty = sqlite3_column_int(r->stmt, 2);
|
|
|
|
r->y = (1LL << r->zoom) - 1 - r->sorty;
|
|
|
|
|
2016-09-20 17:17:02 +00:00
|
|
|
const char *data = (const char *) sqlite3_column_blob(r->stmt, 3);
|
|
|
|
size_t len = sqlite3_column_bytes(r->stmt, 3);
|
|
|
|
|
|
|
|
r->data = std::string(data, len);
|
2016-09-19 22:29:13 +00:00
|
|
|
} else {
|
|
|
|
r->zoom = 32;
|
2015-08-20 23:12:34 +00:00
|
|
|
}
|
2016-09-19 22:29:13 +00:00
|
|
|
|
|
|
|
struct reader **rr;
|
|
|
|
|
|
|
|
for (rr = &readers; *rr != NULL; rr = &((*rr)->next)) {
|
|
|
|
if (*r < **rr) {
|
|
|
|
break;
|
|
|
|
}
|
2015-08-20 23:12:34 +00:00
|
|
|
}
|
2016-09-19 22:29:13 +00:00
|
|
|
|
|
|
|
r->next = *rr;
|
|
|
|
*rr = r;
|
2015-08-20 23:12:34 +00:00
|
|
|
}
|
2016-09-19 22:29:13 +00:00
|
|
|
|
2016-09-20 19:53:17 +00:00
|
|
|
handle_tasks(tasks, layermaps, outdb, header, mapping, exclude, ifmatched);
|
2016-09-20 18:04:24 +00:00
|
|
|
layermap = merge_layermaps(layermaps);
|
|
|
|
|
2016-09-19 23:34:06 +00:00
|
|
|
struct reader *next;
|
|
|
|
for (struct reader *r = readers; r != NULL; r = next) {
|
|
|
|
next = r->next;
|
2016-09-19 22:29:13 +00:00
|
|
|
sqlite3_finalize(r->stmt);
|
|
|
|
|
|
|
|
if (sqlite3_prepare_v2(r->db, "SELECT value from metadata where name = 'minzoom'", -1, &r->stmt, NULL) == SQLITE_OK) {
|
|
|
|
if (sqlite3_step(r->stmt) == SQLITE_ROW) {
|
|
|
|
int minzoom = sqlite3_column_int(r->stmt, 0);
|
|
|
|
st->minzoom = min(st->minzoom, minzoom);
|
|
|
|
}
|
|
|
|
sqlite3_finalize(r->stmt);
|
2015-08-20 23:12:34 +00:00
|
|
|
}
|
2016-09-19 22:29:13 +00:00
|
|
|
if (sqlite3_prepare_v2(r->db, "SELECT value from metadata where name = 'maxzoom'", -1, &r->stmt, NULL) == SQLITE_OK) {
|
|
|
|
if (sqlite3_step(r->stmt) == SQLITE_ROW) {
|
|
|
|
int maxzoom = sqlite3_column_int(r->stmt, 0);
|
|
|
|
st->maxzoom = max(st->maxzoom, maxzoom);
|
|
|
|
}
|
|
|
|
sqlite3_finalize(r->stmt);
|
2016-04-13 19:49:41 +00:00
|
|
|
}
|
2016-09-19 22:29:13 +00:00
|
|
|
if (sqlite3_prepare_v2(r->db, "SELECT value from metadata where name = 'center'", -1, &r->stmt, NULL) == SQLITE_OK) {
|
|
|
|
if (sqlite3_step(r->stmt) == SQLITE_ROW) {
|
|
|
|
const unsigned char *s = sqlite3_column_text(r->stmt, 0);
|
|
|
|
sscanf((char *) s, "%lf,%lf", &st->midlon, &st->midlat);
|
|
|
|
}
|
|
|
|
sqlite3_finalize(r->stmt);
|
|
|
|
}
|
|
|
|
if (sqlite3_prepare_v2(r->db, "SELECT value from metadata where name = 'attribution'", -1, &r->stmt, NULL) == SQLITE_OK) {
|
|
|
|
if (sqlite3_step(r->stmt) == SQLITE_ROW) {
|
|
|
|
attribution = std::string((char *) sqlite3_column_text(r->stmt, 0));
|
|
|
|
}
|
|
|
|
sqlite3_finalize(r->stmt);
|
|
|
|
}
|
|
|
|
if (sqlite3_prepare_v2(r->db, "SELECT value from metadata where name = 'bounds'", -1, &r->stmt, NULL) == SQLITE_OK) {
|
|
|
|
if (sqlite3_step(r->stmt) == SQLITE_ROW) {
|
|
|
|
const unsigned char *s = sqlite3_column_text(r->stmt, 0);
|
|
|
|
double minlon, minlat, maxlon, maxlat;
|
|
|
|
sscanf((char *) s, "%lf,%lf,%lf,%lf", &minlon, &minlat, &maxlon, &maxlat);
|
|
|
|
st->minlon = min(minlon, st->minlon);
|
|
|
|
st->maxlon = max(maxlon, st->maxlon);
|
|
|
|
st->minlat = min(minlat, st->minlat);
|
|
|
|
st->maxlat = max(maxlat, st->maxlat);
|
|
|
|
}
|
|
|
|
sqlite3_finalize(r->stmt);
|
2015-08-20 23:12:34 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 22:29:13 +00:00
|
|
|
if (sqlite3_close(r->db) != SQLITE_OK) {
|
|
|
|
fprintf(stderr, "Could not close database: %s\n", sqlite3_errmsg(r->db));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2016-09-19 23:34:06 +00:00
|
|
|
|
|
|
|
delete r;
|
2015-08-20 20:02:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void usage(char **argv) {
|
2016-09-20 00:20:44 +00:00
|
|
|
fprintf(stderr, "Usage: %s [-f] [-i] [-pk] [-c joins.csv] [-x exclude ...] -o new.mbtiles source.mbtiles ...\n", argv[0]);
|
2015-08-20 20:02:34 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-08-20 23:45:45 +00:00
|
|
|
#define MAXLINE 10000 /* XXX */
|
|
|
|
|
|
|
|
std::vector<std::string> split(char *s) {
|
|
|
|
std::vector<std::string> ret;
|
|
|
|
|
|
|
|
while (*s && *s != '\n') {
|
|
|
|
char *start = s;
|
|
|
|
int within = 0;
|
|
|
|
|
|
|
|
for (; *s && *s != '\n'; s++) {
|
|
|
|
if (*s == '"') {
|
|
|
|
within = !within;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*s == ',' && !within) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string v = std::string(start, s - start);
|
|
|
|
ret.push_back(v);
|
|
|
|
|
|
|
|
if (*s == ',') {
|
|
|
|
s++;
|
2016-09-21 19:32:03 +00:00
|
|
|
|
|
|
|
while (*s && isspace(*s)) {
|
|
|
|
s++;
|
|
|
|
}
|
2015-08-20 23:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-21 00:32:58 +00:00
|
|
|
std::string dequote(std::string s) {
|
|
|
|
std::string out;
|
2016-03-25 19:20:32 +00:00
|
|
|
for (size_t i = 0; i < s.size(); i++) {
|
2015-08-21 00:32:58 +00:00
|
|
|
if (s[i] == '"') {
|
|
|
|
if (i + 1 < s.size() && s[i + 1] == '"') {
|
|
|
|
out.push_back('"');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
out.push_back(s[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2016-09-20 19:53:17 +00:00
|
|
|
void readcsv(char *fn, std::vector<std::string> &header, std::map<std::string, std::vector<std::string>> &mapping) {
|
2015-08-20 23:45:45 +00:00
|
|
|
FILE *f = fopen(fn, "r");
|
|
|
|
if (f == NULL) {
|
|
|
|
perror(fn);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
char s[MAXLINE];
|
|
|
|
if (fgets(s, MAXLINE, f)) {
|
|
|
|
header = split(s);
|
2015-08-21 00:32:58 +00:00
|
|
|
|
2016-03-25 19:20:32 +00:00
|
|
|
for (size_t i = 0; i < header.size(); i++) {
|
2015-08-21 00:32:58 +00:00
|
|
|
header[i] = dequote(header[i]);
|
|
|
|
}
|
2015-08-20 23:45:45 +00:00
|
|
|
}
|
|
|
|
while (fgets(s, MAXLINE, f)) {
|
|
|
|
std::vector<std::string> line = split(s);
|
2015-08-21 00:32:58 +00:00
|
|
|
if (line.size() > 0) {
|
|
|
|
line[0] = dequote(line[0]);
|
|
|
|
}
|
2015-08-20 23:45:45 +00:00
|
|
|
|
2016-03-25 19:20:32 +00:00
|
|
|
for (size_t i = 0; i < line.size() && i < header.size(); i++) {
|
2015-08-21 00:32:58 +00:00
|
|
|
// printf("putting %s\n", line[0].c_str());
|
2016-09-20 19:53:17 +00:00
|
|
|
mapping.insert(std::pair<std::string, std::vector<std::string>>(line[0], line));
|
2015-08-20 23:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2015-08-20 20:02:34 +00:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
char *outfile = NULL;
|
2015-08-20 22:47:44 +00:00
|
|
|
char *csv = NULL;
|
2015-08-20 20:02:34 +00:00
|
|
|
int force = 0;
|
2015-08-24 21:12:46 +00:00
|
|
|
int ifmatched = 0;
|
2015-08-20 20:02:34 +00:00
|
|
|
|
2016-09-20 18:04:24 +00:00
|
|
|
CPUS = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
if (CPUS < 1) {
|
|
|
|
CPUS = 1;
|
|
|
|
}
|
|
|
|
|
2015-08-20 23:45:45 +00:00
|
|
|
std::vector<std::string> header;
|
2016-09-20 19:53:17 +00:00
|
|
|
std::map<std::string, std::vector<std::string>> mapping;
|
2015-08-20 23:45:45 +00:00
|
|
|
|
2016-04-28 21:43:04 +00:00
|
|
|
std::set<std::string> exclude;
|
2015-08-22 00:32:44 +00:00
|
|
|
|
2015-08-20 20:02:34 +00:00
|
|
|
extern int optind;
|
|
|
|
extern char *optarg;
|
|
|
|
int i;
|
|
|
|
|
2016-09-20 00:20:44 +00:00
|
|
|
while ((i = getopt(argc, argv, "fo:c:x:ip:")) != -1) {
|
2015-08-20 20:02:34 +00:00
|
|
|
switch (i) {
|
2015-08-21 21:27:11 +00:00
|
|
|
case 'o':
|
|
|
|
outfile = optarg;
|
|
|
|
break;
|
2015-08-20 20:02:34 +00:00
|
|
|
|
2015-08-21 21:27:11 +00:00
|
|
|
case 'f':
|
|
|
|
force = 1;
|
|
|
|
break;
|
2015-08-20 20:02:34 +00:00
|
|
|
|
2015-08-24 21:12:46 +00:00
|
|
|
case 'i':
|
|
|
|
ifmatched = 1;
|
|
|
|
break;
|
|
|
|
|
2016-09-20 00:20:44 +00:00
|
|
|
case 'p':
|
|
|
|
if (strcmp(optarg, "k") == 0) {
|
|
|
|
pk = true;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s: Unknown option for -p%s\n", argv[0], optarg);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-08-21 21:27:11 +00:00
|
|
|
case 'c':
|
|
|
|
if (csv != NULL) {
|
|
|
|
fprintf(stderr, "Only one -c for now\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2015-08-20 23:45:45 +00:00
|
|
|
|
2015-08-21 21:27:11 +00:00
|
|
|
csv = optarg;
|
|
|
|
readcsv(csv, header, mapping);
|
|
|
|
break;
|
2015-08-20 22:47:44 +00:00
|
|
|
|
2015-08-22 00:32:44 +00:00
|
|
|
case 'x':
|
2016-04-28 21:43:04 +00:00
|
|
|
exclude.insert(std::string(optarg));
|
2015-08-22 00:32:44 +00:00
|
|
|
break;
|
|
|
|
|
2015-08-21 21:27:11 +00:00
|
|
|
default:
|
|
|
|
usage(argv);
|
2015-08-20 20:02:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-31 00:59:15 +00:00
|
|
|
if (argc - optind < 1 || outfile == NULL) {
|
2015-08-20 20:02:34 +00:00
|
|
|
usage(argv);
|
|
|
|
}
|
|
|
|
|
2015-08-20 22:01:34 +00:00
|
|
|
if (force) {
|
|
|
|
unlink(outfile);
|
|
|
|
}
|
|
|
|
|
2016-01-28 22:18:31 +00:00
|
|
|
sqlite3 *outdb = mbtiles_open(outfile, argv, 0);
|
2015-08-20 23:12:34 +00:00
|
|
|
struct stats st;
|
|
|
|
memset(&st, 0, sizeof(st));
|
2015-11-02 22:34:01 +00:00
|
|
|
st.minzoom = st.minlat = st.minlon = INT_MAX;
|
|
|
|
st.maxzoom = st.maxlat = st.maxlon = INT_MIN;
|
2015-08-20 22:01:34 +00:00
|
|
|
|
2016-09-19 23:53:41 +00:00
|
|
|
std::map<std::string, layermap_entry> layermap;
|
2016-05-03 18:40:36 +00:00
|
|
|
std::string attribution;
|
2015-08-20 20:02:34 +00:00
|
|
|
|
2016-09-19 22:29:13 +00:00
|
|
|
struct reader *readers = NULL;
|
|
|
|
|
2015-08-20 22:47:44 +00:00
|
|
|
for (i = optind; i < argc; i++) {
|
2016-09-19 22:29:13 +00:00
|
|
|
reader *r = begin_reading(argv[i]);
|
|
|
|
struct reader **rr;
|
|
|
|
|
|
|
|
for (rr = &readers; *rr != NULL; rr = &((*rr)->next)) {
|
|
|
|
if (*r < **rr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r->next = *rr;
|
|
|
|
*rr = r;
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:53:41 +00:00
|
|
|
decode(readers, csv, layermap, outdb, &st, header, mapping, exclude, ifmatched, attribution);
|
2016-08-29 23:38:57 +00:00
|
|
|
|
2016-08-30 21:17:28 +00:00
|
|
|
mbtiles_write_metadata(outdb, outfile, st.minzoom, st.maxzoom, st.minlat, st.minlon, st.maxlat, st.maxlon, st.midlat, st.midlon, 0, attribution.size() != 0 ? attribution.c_str() : NULL, layermap);
|
2015-08-20 22:01:34 +00:00
|
|
|
mbtiles_close(outdb, argv);
|
|
|
|
|
2015-08-20 20:02:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|