2014-10-23 22:40:27 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sqlite3.h>
|
|
|
|
#include <string>
|
|
|
|
#include <zlib.h>
|
|
|
|
#include <math.h>
|
2016-03-22 23:50:01 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
2016-02-20 00:33:48 +00:00
|
|
|
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
|
|
|
|
#include <google/protobuf/io/coded_stream.h>
|
2014-10-23 22:40:27 +00:00
|
|
|
#include "vector_tile.pb.h"
|
2015-10-09 19:41:28 +00:00
|
|
|
#include "tile.h"
|
2014-10-23 22:40:27 +00:00
|
|
|
|
2014-10-25 00:22:14 +00:00
|
|
|
extern "C" {
|
2015-06-03 18:21:40 +00:00
|
|
|
#include "projection.h"
|
2014-10-25 00:22:14 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 22:40:27 +00:00
|
|
|
// https://github.com/mapbox/mapnik-vector-tile/blob/master/src/vector_tile_compression.hpp
|
2015-06-03 18:21:40 +00:00
|
|
|
inline bool is_compressed(std::string const &data) {
|
|
|
|
return data.size() > 2 && (((uint8_t) data[0] == 0x78 && (uint8_t) data[1] == 0x9C) || ((uint8_t) data[0] == 0x1F && (uint8_t) data[1] == 0x8B));
|
2014-10-23 22:40:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/mapbox/mapnik-vector-tile/blob/master/src/vector_tile_compression.hpp
|
2015-06-03 18:21:40 +00:00
|
|
|
inline int decompress(std::string const &input, std::string &output) {
|
2014-10-23 22:40:27 +00:00
|
|
|
z_stream inflate_s;
|
|
|
|
inflate_s.zalloc = Z_NULL;
|
|
|
|
inflate_s.zfree = Z_NULL;
|
|
|
|
inflate_s.opaque = Z_NULL;
|
|
|
|
inflate_s.avail_in = 0;
|
|
|
|
inflate_s.next_in = Z_NULL;
|
2014-11-14 06:47:54 +00:00
|
|
|
if (inflateInit2(&inflate_s, 32 + 15) != Z_OK) {
|
|
|
|
fprintf(stderr, "error: %s\n", inflate_s.msg);
|
|
|
|
}
|
2015-06-03 18:21:40 +00:00
|
|
|
inflate_s.next_in = (Bytef *) input.data();
|
2014-10-23 22:40:27 +00:00
|
|
|
inflate_s.avail_in = input.size();
|
|
|
|
size_t length = 0;
|
|
|
|
do {
|
|
|
|
output.resize(length + 2 * input.size());
|
|
|
|
inflate_s.avail_out = 2 * input.size();
|
2015-06-03 18:21:40 +00:00
|
|
|
inflate_s.next_out = (Bytef *) (output.data() + length);
|
2014-10-23 22:40:27 +00:00
|
|
|
int ret = inflate(&inflate_s, Z_FINISH);
|
|
|
|
if (ret != Z_STREAM_END && ret != Z_OK && ret != Z_BUF_ERROR) {
|
2014-11-14 06:47:54 +00:00
|
|
|
fprintf(stderr, "error: %s\n", inflate_s.msg);
|
2014-10-23 22:40:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
length += (2 * input.size() - inflate_s.avail_out);
|
|
|
|
} while (inflate_s.avail_out == 0);
|
|
|
|
inflateEnd(&inflate_s);
|
|
|
|
output.resize(length);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dezig(unsigned n) {
|
|
|
|
return (n >> 1) ^ (-(n & 1));
|
|
|
|
}
|
|
|
|
|
2015-10-09 19:41:28 +00:00
|
|
|
void printq(const char *s) {
|
|
|
|
putchar('"');
|
|
|
|
for (; *s; s++) {
|
|
|
|
if (*s == '\\' || *s == '"') {
|
|
|
|
printf("\\%c", *s);
|
|
|
|
} else if (*s >= 0 && *s < ' ') {
|
|
|
|
printf("\\u%04x", *s);
|
|
|
|
} else {
|
|
|
|
putchar(*s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
putchar('"');
|
|
|
|
}
|
|
|
|
|
|
|
|
struct draw {
|
|
|
|
int op;
|
|
|
|
double lon;
|
|
|
|
double lat;
|
|
|
|
|
|
|
|
draw(int op, double lon, double lat) {
|
|
|
|
this->op = op;
|
|
|
|
this->lon = lon;
|
|
|
|
this->lat = lat;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-19 18:12:11 +00:00
|
|
|
void handle(std::string message, int z, unsigned x, unsigned y, int describe) {
|
2014-10-23 22:40:27 +00:00
|
|
|
GOOGLE_PROTOBUF_VERIFY_VERSION;
|
2015-10-09 19:41:28 +00:00
|
|
|
int within = 0;
|
2014-10-23 22:40:27 +00:00
|
|
|
|
|
|
|
// https://github.com/mapbox/mapnik-vector-tile/blob/master/examples/c%2B%2B/tileinfo.cpp
|
|
|
|
mapnik::vector::tile tile;
|
|
|
|
|
|
|
|
if (is_compressed(message)) {
|
|
|
|
std::string uncompressed;
|
2015-06-03 18:21:40 +00:00
|
|
|
decompress(message, uncompressed);
|
2016-02-20 00:33:48 +00:00
|
|
|
google::protobuf::io::ArrayInputStream stream(uncompressed.c_str(), uncompressed.length());
|
|
|
|
google::protobuf::io::CodedInputStream codedstream(&stream);
|
|
|
|
codedstream.SetTotalBytesLimit(10 * 67108864, 5 * 67108864);
|
|
|
|
if (!tile.ParseFromCodedStream(&codedstream)) {
|
2014-10-23 22:40:27 +00:00
|
|
|
fprintf(stderr, "Couldn't decompress tile %d/%u/%u\n", z, x, y);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
} else if (!tile.ParseFromString(message)) {
|
|
|
|
fprintf(stderr, "Couldn't parse tile %d/%u/%u\n", z, x, y);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-10-19 18:12:11 +00:00
|
|
|
printf("{ \"type\": \"FeatureCollection\"");
|
|
|
|
|
|
|
|
if (describe) {
|
|
|
|
printf(", \"properties\": { \"zoom\": %d, \"x\": %d, \"y\": %d }", z, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(", \"features\": [\n");
|
2015-10-09 19:41:28 +00:00
|
|
|
|
2014-10-23 22:40:27 +00:00
|
|
|
for (int l = 0; l < tile.layers_size(); l++) {
|
|
|
|
mapnik::vector::tile_layer layer = tile.layers(l);
|
|
|
|
int extent = layer.extent();
|
|
|
|
|
2015-10-31 00:30:18 +00:00
|
|
|
if (describe) {
|
|
|
|
if (l != 0) {
|
|
|
|
printf(",\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("{ \"type\": \"FeatureCollection\"");
|
2015-11-02 22:03:45 +00:00
|
|
|
printf(", \"properties\": { \"layer\": ");
|
2015-10-31 00:30:18 +00:00
|
|
|
printq(layer.name().c_str());
|
|
|
|
printf(" }");
|
|
|
|
printf(", \"features\": [\n");
|
|
|
|
|
|
|
|
within = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-23 22:40:27 +00:00
|
|
|
for (int f = 0; f < layer.features_size(); f++) {
|
|
|
|
mapnik::vector::tile_feature feat = layer.features(f);
|
|
|
|
int px = 0, py = 0;
|
|
|
|
|
2015-10-09 19:41:28 +00:00
|
|
|
if (within) {
|
|
|
|
printf(",\n");
|
|
|
|
}
|
|
|
|
within = 1;
|
|
|
|
|
|
|
|
printf("{ \"type\": \"Feature\"");
|
|
|
|
printf(", \"properties\": { ");
|
|
|
|
|
2015-10-14 19:24:18 +00:00
|
|
|
for (int t = 0; t + 1 < feat.tags_size(); t += 2) {
|
2015-10-09 19:41:28 +00:00
|
|
|
if (t != 0) {
|
|
|
|
printf(", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *key = layer.keys(feat.tags(t)).c_str();
|
|
|
|
mapnik::vector::tile_value const &val = layer.values(feat.tags(t + 1));
|
|
|
|
|
|
|
|
if (val.has_string_value()) {
|
|
|
|
printq(key);
|
|
|
|
printf(": ");
|
|
|
|
printq(val.string_value().c_str());
|
|
|
|
} else if (val.has_int_value()) {
|
|
|
|
printq(key);
|
|
|
|
printf(": %lld", (long long) val.int_value());
|
|
|
|
} else if (val.has_double_value()) {
|
|
|
|
printq(key);
|
2015-12-15 00:10:34 +00:00
|
|
|
double v = val.double_value();
|
|
|
|
if (v == (long long) v) {
|
|
|
|
printf(": %lld", (long long) v);
|
|
|
|
} else {
|
|
|
|
printf(": %g", v);
|
|
|
|
}
|
2015-10-09 19:41:28 +00:00
|
|
|
} else if (val.has_float_value()) {
|
|
|
|
printq(key);
|
2015-12-15 00:10:34 +00:00
|
|
|
double v = val.float_value();
|
|
|
|
if (v == (long long) v) {
|
|
|
|
printf(": %lld", (long long) v);
|
|
|
|
} else {
|
|
|
|
printf(": %g", v);
|
|
|
|
}
|
2015-10-09 19:41:28 +00:00
|
|
|
} else if (val.has_sint_value()) {
|
|
|
|
printq(key);
|
|
|
|
printf(": %lld", (long long) val.sint_value());
|
|
|
|
} else if (val.has_uint_value()) {
|
|
|
|
printq(key);
|
|
|
|
printf(": %lld", (long long) val.uint_value());
|
|
|
|
} else if (val.has_bool_value()) {
|
|
|
|
printq(key);
|
|
|
|
printf(": %s", val.bool_value() ? "true" : "false");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(" }, \"geometry\": { ");
|
|
|
|
|
|
|
|
std::vector<draw> ops;
|
|
|
|
|
2014-10-23 22:40:27 +00:00
|
|
|
for (int g = 0; g < feat.geometry_size(); g++) {
|
|
|
|
uint32_t geom = feat.geometry(g);
|
|
|
|
uint32_t op = geom & 7;
|
|
|
|
uint32_t count = geom >> 3;
|
|
|
|
|
2015-10-09 19:41:28 +00:00
|
|
|
if (op == VT_MOVETO || op == VT_LINETO) {
|
2014-10-23 22:40:27 +00:00
|
|
|
for (unsigned k = 0; k < count; k++) {
|
|
|
|
px += dezig(feat.geometry(g + 1));
|
|
|
|
py += dezig(feat.geometry(g + 2));
|
|
|
|
g += 2;
|
|
|
|
|
|
|
|
long long scale = 1LL << (32 - z);
|
2016-01-20 21:58:17 +00:00
|
|
|
long long wx = scale * x + (scale / extent) * px;
|
|
|
|
long long wy = scale * y + (scale / extent) * py;
|
2014-10-23 22:40:27 +00:00
|
|
|
|
|
|
|
double lat, lon;
|
|
|
|
tile2latlon(wx, wy, 32, &lat, &lon);
|
2015-10-09 19:41:28 +00:00
|
|
|
|
|
|
|
ops.push_back(draw(op, lon, lat));
|
2014-10-23 22:40:27 +00:00
|
|
|
}
|
2015-10-09 19:41:28 +00:00
|
|
|
} else {
|
2015-10-09 23:45:34 +00:00
|
|
|
ops.push_back(draw(op, 0, 0));
|
2014-10-23 22:40:27 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-09 19:41:28 +00:00
|
|
|
|
|
|
|
if (feat.type() == VT_POINT) {
|
|
|
|
if (ops.size() == 1) {
|
|
|
|
printf("\"type\": \"Point\", \"coordinates\": [ %f, %f ]", ops[0].lon, ops[0].lat);
|
|
|
|
} else {
|
|
|
|
printf("\"type\": \"MultiPoint\", \"coordinates\": [ ");
|
|
|
|
for (unsigned i = 0; i < ops.size(); i++) {
|
|
|
|
if (i != 0) {
|
|
|
|
printf(", ");
|
|
|
|
}
|
|
|
|
printf("[ %f, %f ]", ops[i].lon, ops[i].lat);
|
|
|
|
}
|
|
|
|
printf(" ]");
|
|
|
|
}
|
|
|
|
} else if (feat.type() == VT_LINE) {
|
|
|
|
int movetos = 0;
|
|
|
|
for (unsigned i = 0; i < ops.size(); i++) {
|
|
|
|
if (ops[i].op == VT_MOVETO) {
|
|
|
|
movetos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (movetos < 2) {
|
|
|
|
printf("\"type\": \"LineString\", \"coordinates\": [ ");
|
|
|
|
for (unsigned i = 0; i < ops.size(); i++) {
|
|
|
|
if (i != 0) {
|
|
|
|
printf(", ");
|
|
|
|
}
|
|
|
|
printf("[ %f, %f ]", ops[i].lon, ops[i].lat);
|
|
|
|
}
|
|
|
|
printf(" ]");
|
|
|
|
} else {
|
|
|
|
printf("\"type\": \"MultiLineString\", \"coordinates\": [ [ ");
|
|
|
|
int state = 0;
|
|
|
|
for (unsigned i = 0; i < ops.size(); i++) {
|
|
|
|
if (ops[i].op == VT_MOVETO) {
|
|
|
|
if (state == 0) {
|
|
|
|
printf("[ %f, %f ]", ops[i].lon, ops[i].lat);
|
|
|
|
state = 1;
|
|
|
|
} else {
|
|
|
|
printf(" ], [ ");
|
|
|
|
printf("[ %f, %f ]", ops[i].lon, ops[i].lat);
|
|
|
|
state = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf(", [ %f, %f ]", ops[i].lon, ops[i].lat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf(" ] ]");
|
|
|
|
}
|
|
|
|
} else if (feat.type() == VT_POLYGON) {
|
2015-10-09 23:45:34 +00:00
|
|
|
std::vector<std::vector<draw> > rings;
|
|
|
|
std::vector<double> areas;
|
2015-10-09 19:41:28 +00:00
|
|
|
|
2015-10-09 23:45:34 +00:00
|
|
|
for (unsigned i = 0; i < ops.size(); i++) {
|
|
|
|
if (ops[i].op == VT_MOVETO) {
|
|
|
|
rings.push_back(std::vector<draw>());
|
|
|
|
areas.push_back(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int n = rings.size() - 1;
|
|
|
|
if (n >= 0) {
|
|
|
|
rings[n].push_back(ops[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int outer = 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < rings.size(); i++) {
|
|
|
|
double area = 0;
|
|
|
|
for (unsigned k = 0; k < rings[i].size(); k++) {
|
|
|
|
if (rings[i][k].op != VT_CLOSEPATH) {
|
|
|
|
area += rings[i][k].lon * rings[i][(k + 1) % rings[i].size()].lat;
|
|
|
|
area -= rings[i][k].lat * rings[i][(k + 1) % rings[i].size()].lon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
areas[i] = area;
|
2015-11-02 21:52:52 +00:00
|
|
|
if (areas[i] <= 0 || i == 0) {
|
2015-10-09 23:45:34 +00:00
|
|
|
outer++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// printf("area %f\n", area / .00000274 / .00000274);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outer > 1) {
|
|
|
|
printf("\"type\": \"MultiPolygon\", \"coordinates\": [ [ [ ");
|
|
|
|
} else {
|
|
|
|
printf("\"type\": \"Polygon\", \"coordinates\": [ [ ");
|
|
|
|
}
|
|
|
|
|
|
|
|
int state = 0;
|
|
|
|
for (unsigned i = 0; i < rings.size(); i++) {
|
|
|
|
if (areas[i] <= 0) {
|
|
|
|
if (state != 0) {
|
|
|
|
// new multipolygon
|
|
|
|
printf(" ] ], [ [ ");
|
|
|
|
}
|
|
|
|
state = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == 2) {
|
|
|
|
// new ring in the same polygon
|
|
|
|
printf(" ], [ ");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned j = 0; j < rings[i].size(); j++) {
|
|
|
|
if (rings[i][j].op != VT_CLOSEPATH) {
|
|
|
|
if (j != 0) {
|
|
|
|
printf(", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("[ %f, %f ]", rings[i][j].lon, rings[i][j].lat);
|
2015-10-28 21:34:57 +00:00
|
|
|
} else {
|
|
|
|
if (j != 0) {
|
|
|
|
printf(", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("[ %f, %f ]", rings[i][0].lon, rings[i][0].lat);
|
2015-10-09 23:45:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outer > 1) {
|
|
|
|
printf(" ] ] ]");
|
|
|
|
} else {
|
|
|
|
printf(" ] ]");
|
|
|
|
}
|
2015-10-09 19:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf(" } }\n");
|
2014-10-23 22:40:27 +00:00
|
|
|
}
|
2015-10-31 00:30:18 +00:00
|
|
|
|
|
|
|
if (describe) {
|
|
|
|
printf("] }\n");
|
|
|
|
}
|
2014-10-23 22:40:27 +00:00
|
|
|
}
|
2015-10-09 19:41:28 +00:00
|
|
|
|
|
|
|
printf("] }\n");
|
2014-10-23 22:40:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void decode(char *fname, int z, unsigned x, unsigned y) {
|
|
|
|
sqlite3 *db;
|
2014-10-27 23:00:16 +00:00
|
|
|
int oz = z;
|
|
|
|
unsigned ox = x, oy = y;
|
2014-10-23 22:40:27 +00:00
|
|
|
|
2016-03-22 23:50:01 +00:00
|
|
|
int fd = open(fname, O_RDONLY);
|
|
|
|
if (fd >= 0) {
|
|
|
|
struct stat st;
|
|
|
|
if (fstat(fd, &st) == 0) {
|
|
|
|
if (st.st_size < 50 * 1024 * 1024) {
|
|
|
|
char *map = (char *) mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
if (map != NULL && map != MAP_FAILED) {
|
|
|
|
if (strcmp(map, "SQLite format 3") != 0) {
|
2016-03-23 00:12:09 +00:00
|
|
|
if (z >= 0) {
|
|
|
|
std::string s = std::string(map, st.st_size);
|
|
|
|
handle(s, z, x, y, 1);
|
|
|
|
munmap(map, st.st_size);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Must specify zoom/x/y to decode a single pbf file\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2016-03-22 23:50:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
munmap(map, st.st_size);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
perror("fstat");
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
} else {
|
|
|
|
perror(fname);
|
|
|
|
}
|
|
|
|
|
2014-10-23 22:40:27 +00:00
|
|
|
if (sqlite3_open(fname, &db) != SQLITE_OK) {
|
2015-06-03 18:21:40 +00:00
|
|
|
fprintf(stderr, "%s: %s\n", fname, sqlite3_errmsg(db));
|
2014-10-23 22:40:27 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-10-19 18:12:11 +00:00
|
|
|
if (z < 0) {
|
2016-01-28 22:06:51 +00:00
|
|
|
printf("{ \"type\": \"FeatureCollection\", \"properties\": {\n");
|
|
|
|
|
|
|
|
const char *sql2 = "SELECT name, value from metadata order by name;";
|
|
|
|
sqlite3_stmt *stmt2;
|
|
|
|
if (sqlite3_prepare_v2(db, sql2, -1, &stmt2, NULL) != SQLITE_OK) {
|
|
|
|
fprintf(stderr, "%s: select failed: %s\n", fname, sqlite3_errmsg(db));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int within = 0;
|
|
|
|
while (sqlite3_step(stmt2) == SQLITE_ROW) {
|
|
|
|
if (within) {
|
|
|
|
printf(",\n");
|
|
|
|
}
|
|
|
|
within = 1;
|
|
|
|
|
|
|
|
const unsigned char *name = sqlite3_column_text(stmt2, 0);
|
|
|
|
const unsigned char *value = sqlite3_column_text(stmt2, 1);
|
|
|
|
|
|
|
|
printq((char *) name);
|
|
|
|
printf(": ");
|
|
|
|
printq((char *) value);
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3_finalize(stmt2);
|
|
|
|
|
2015-10-19 18:12:11 +00:00
|
|
|
const char *sql = "SELECT tile_data, zoom_level, tile_column, tile_row from tiles order by zoom_level, tile_column, tile_row;";
|
2014-10-27 23:00:16 +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);
|
|
|
|
}
|
2014-10-23 22:40:27 +00:00
|
|
|
|
2016-01-28 22:06:51 +00:00
|
|
|
printf("\n}, \"features\": [\n");
|
2014-10-23 22:40:27 +00:00
|
|
|
|
2016-01-28 22:06:51 +00:00
|
|
|
within = 0;
|
2014-10-27 23:00:16 +00:00
|
|
|
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
2015-10-19 18:12:11 +00:00
|
|
|
if (within) {
|
|
|
|
printf(",\n");
|
|
|
|
}
|
|
|
|
within = 1;
|
|
|
|
|
2014-10-27 23:00:16 +00:00
|
|
|
int len = sqlite3_column_bytes(stmt, 0);
|
2015-10-19 18:12:11 +00:00
|
|
|
int z = sqlite3_column_int(stmt, 1);
|
|
|
|
int x = sqlite3_column_int(stmt, 2);
|
|
|
|
int y = sqlite3_column_int(stmt, 3);
|
|
|
|
y = (1LL << z) - 1 - y;
|
2014-10-27 23:00:16 +00:00
|
|
|
const char *s = (const char *) sqlite3_column_blob(stmt, 0);
|
2014-10-23 22:40:27 +00:00
|
|
|
|
2015-10-19 18:12:11 +00:00
|
|
|
handle(std::string(s, len), z, x, y, 1);
|
2014-10-27 23:00:16 +00:00
|
|
|
}
|
2014-10-23 22:40:27 +00:00
|
|
|
|
2015-10-19 18:12:11 +00:00
|
|
|
printf("] }\n");
|
|
|
|
|
2014-10-27 23:00:16 +00:00
|
|
|
sqlite3_finalize(stmt);
|
2015-10-19 18:12:11 +00:00
|
|
|
} else {
|
|
|
|
int handled = 0;
|
|
|
|
while (z >= 0 && !handled) {
|
|
|
|
const char *sql = "SELECT tile_data from tiles where zoom_level = ? and tile_column = ? and tile_row = ?;";
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3_bind_int(stmt, 1, z);
|
|
|
|
sqlite3_bind_int(stmt, 2, x);
|
|
|
|
sqlite3_bind_int(stmt, 3, (1LL << z) - 1 - y);
|
|
|
|
|
|
|
|
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
|
|
|
int len = sqlite3_column_bytes(stmt, 0);
|
|
|
|
const char *s = (const char *) sqlite3_column_blob(stmt, 0);
|
|
|
|
|
|
|
|
if (z != oz) {
|
|
|
|
fprintf(stderr, "%s: Warning: using tile %d/%u/%u instead of %d/%u/%u\n", fname, z, x, y, oz, ox, oy);
|
|
|
|
}
|
|
|
|
|
|
|
|
handle(std::string(s, len), z, x, y, 0);
|
|
|
|
handled = 1;
|
|
|
|
}
|
2014-10-27 23:00:16 +00:00
|
|
|
|
2015-10-19 18:12:11 +00:00
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
|
|
|
|
z--;
|
|
|
|
x /= 2;
|
|
|
|
y /= 2;
|
|
|
|
}
|
2014-10-27 23:00:16 +00:00
|
|
|
}
|
2014-10-23 22:40:27 +00:00
|
|
|
|
2015-06-03 18:21:40 +00:00
|
|
|
if (sqlite3_close(db) != SQLITE_OK) {
|
|
|
|
fprintf(stderr, "%s: could not close database: %s\n", fname, sqlite3_errmsg(db));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-10-23 22:40:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void usage(char **argv) {
|
|
|
|
fprintf(stderr, "Usage: %s file.mbtiles zoom x y\n", argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
extern int optind;
|
|
|
|
// extern char *optarg;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
while ((i = getopt(argc, argv, "")) != -1) {
|
|
|
|
usage(argv);
|
|
|
|
}
|
|
|
|
|
2015-10-19 18:12:11 +00:00
|
|
|
if (argc == optind + 4) {
|
|
|
|
decode(argv[optind], atoi(argv[optind + 1]), atoi(argv[optind + 2]), atoi(argv[optind + 3]));
|
|
|
|
} else if (argc == optind + 1) {
|
|
|
|
decode(argv[optind], -1, -1, -1);
|
|
|
|
} else {
|
2014-10-23 22:40:27 +00:00
|
|
|
usage(argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|