Factor out tile reading and writing from tile-join

This commit is contained in:
Eric Fischer
2016-04-22 15:47:06 -07:00
parent f837577b38
commit f9c4fb8374
3 changed files with 48 additions and 62 deletions
-1
View File
@@ -12,7 +12,6 @@
#include <google/protobuf/io/zero_copy_stream_impl_lite.h> #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
#include <google/protobuf/io/coded_stream.h> #include <google/protobuf/io/coded_stream.h>
#include "mvt.hh" #include "mvt.hh"
#include "vector_tile.pb.h"
#include "tile.h" #include "tile.h"
extern "C" { extern "C" {
+48 -58
View File
@@ -9,7 +9,6 @@
#include <zlib.h> #include <zlib.h>
#include <math.h> #include <math.h>
#include "mvt.hh" #include "mvt.hh"
#include "vector_tile.pb.h"
#include "tile.h" #include "tile.h"
extern "C" { extern "C" {
@@ -28,34 +27,24 @@ struct stats {
}; };
void handle(std::string message, int z, unsigned x, unsigned y, struct pool **file_keys, char ***layernames, int *nlayers, sqlite3 *outdb, std::vector<std::string> &header, std::map<std::string, std::vector<std::string> > &mapping, struct pool *exclude, int ifmatched) { void handle(std::string message, int z, unsigned x, unsigned y, struct pool **file_keys, char ***layernames, int *nlayers, sqlite3 *outdb, std::vector<std::string> &header, std::map<std::string, std::vector<std::string> > &mapping, struct pool *exclude, int ifmatched) {
GOOGLE_PROTOBUF_VERIFY_VERSION; mvt_tile tile;
mvt_tile outtile;
// https://github.com/mapbox/mapnik-vector-tile/blob/master/examples/c%2B%2B/tileinfo.cpp
mapnik::vector::tile tile;
mapnik::vector::tile outtile;
int features_added = 0; int features_added = 0;
if (is_compressed(message)) { if (!mvt_decode(message, tile)) {
std::string uncompressed; fprintf(stderr, "Couldn't decompress tile %d/%u/%u\n", z, x, y);
decompress(message, uncompressed);
if (!tile.ParseFromString(uncompressed)) {
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); exit(EXIT_FAILURE);
} }
for (int l = 0; l < tile.layers_size(); l++) { for (size_t l = 0; l < tile.layers.size(); l++) {
mapnik::vector::tile_layer layer = tile.layers(l); mvt_layer &layer = tile.layers[l];
mapnik::vector::tile_layer *outlayer = outtile.add_layers(); mvt_layer outlayer;
outlayer->set_name(layer.name()); outlayer.name = layer.name;
outlayer->set_version(layer.version()); outlayer.version = layer.version;
outlayer->set_extent(layer.extent()); outlayer.extent = layer.extent;
const char *ln = layer.name().c_str(); const char *ln = layer.name.c_str();
int ll; int ll;
for (ll = 0; ll < *nlayers; ll++) { for (ll = 0; ll < *nlayers; ll++) {
@@ -89,46 +78,46 @@ void handle(std::string message, int z, unsigned x, unsigned y, struct pool **fi
pool_init(&keys, 0); pool_init(&keys, 0);
pool_init(&values, 0); pool_init(&values, 0);
for (int f = 0; f < layer.features_size(); f++) { for (size_t f = 0; f < layer.features.size(); f++) {
mapnik::vector::tile_feature feat = layer.features(f); mvt_feature feat = layer.features[f];
std::vector<int> feature_tags; std::vector<int> feature_tags;
int matched = 0; int matched = 0;
for (int t = 0; t + 1 < feat.tags_size(); t += 2) { for (int t = 0; t + 1 < feat.tags.size(); t += 2) {
const char *key = layer.keys(feat.tags(t)).c_str(); const char *key = layer.keys[feat.tags[t]].c_str();
mapnik::vector::tile_value const &val = layer.values(feat.tags(t + 1)); mvt_value &val = layer.values[feat.tags[t + 1]];
char *value; char *value;
int type = -1; int type = -1;
if (val.has_string_value()) { if (val.type == mvt_string) {
value = strdup(val.string_value().c_str()); value = strdup(val.string_value.c_str());
if (value == NULL) { if (value == NULL) {
perror("Out of memory"); perror("Out of memory");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
type = VT_STRING; type = VT_STRING;
} else if (val.has_int_value()) { } else if (val.type == mvt_int) {
if (asprintf(&value, "%lld", (long long) val.int_value()) >= 0) { if (asprintf(&value, "%lld", (long long) val.numeric_value.int_value) >= 0) {
type = VT_NUMBER; type = VT_NUMBER;
} }
} else if (val.has_double_value()) { } else if (val.type == mvt_double) {
if (asprintf(&value, "%g", val.double_value()) >= 0) { if (asprintf(&value, "%g", val.numeric_value.double_value) >= 0) {
type = VT_NUMBER; type = VT_NUMBER;
} }
} else if (val.has_float_value()) { } else if (val.type == mvt_float) {
if (asprintf(&value, "%g", val.float_value()) >= 0) { if (asprintf(&value, "%g", val.numeric_value.float_value) >= 0) {
type = VT_NUMBER; type = VT_NUMBER;
} }
} else if (val.has_bool_value()) { } else if (val.type == mvt_bool) {
if (asprintf(&value, "%s", val.bool_value() ? "true" : "false") >= 0) { if (asprintf(&value, "%s", val.numeric_value.bool_value ? "true" : "false") >= 0) {
type = VT_BOOLEAN; type = VT_BOOLEAN;
} }
} else if (val.has_sint_value()) { } else if (val.type == mvt_sint) {
if (asprintf(&value, "%lld", (long long) val.sint_value()) >= 0) { if (asprintf(&value, "%lld", (long long) val.numeric_value.sint_value) >= 0) {
type = VT_NUMBER; type = VT_NUMBER;
} }
} else if (val.has_uint_value()) { } else if (val.type == mvt_uint) {
if (asprintf(&value, "%llu", (long long) val.uint_value()) >= 0) { if (asprintf(&value, "%llu", (long long) val.numeric_value.uint_value) >= 0) {
type = VT_NUMBER; type = VT_NUMBER;
} }
} else { } else {
@@ -245,50 +234,51 @@ void handle(std::string message, int z, unsigned x, unsigned y, struct pool **fi
} }
if (matched || !ifmatched) { if (matched || !ifmatched) {
mapnik::vector::tile_feature *outfeature = outlayer->add_features(); mvt_feature outfeature;
outfeature->set_type(feat.type()); outfeature.type = feat.type;
outfeature.geometry = feat.geometry;
for (int g = 0; g < feat.geometry_size(); g++) {
outfeature->add_geometry(feat.geometry(g));
}
for (size_t i = 0; i < feature_tags.size(); i++) { for (size_t i = 0; i < feature_tags.size(); i++) {
outfeature->add_tags(feature_tags[i]); outfeature.tags.push_back(feature_tags[i]);
} }
features_added++; features_added++;
outlayer.features.push_back(outfeature);
} }
} }
struct pool_val *pv; struct pool_val *pv;
for (pv = keys.head; pv != NULL; pv = pv->next) { for (pv = keys.head; pv != NULL; pv = pv->next) {
outlayer->add_keys(pv->s, strlen(pv->s)); outlayer.keys.push_back(std::string(pv->s, strlen(pv->s)));
} }
for (pv = values.head; pv != NULL; pv = pv->next) { for (pv = values.head; pv != NULL; pv = pv->next) {
mapnik::vector::tile_value *tv = outlayer->add_values(); mvt_value tv;
if (pv->type == VT_NUMBER) { if (pv->type == VT_NUMBER) {
tv->set_double_value(atof(pv->s)); tv.type = mvt_double;
tv.numeric_value.double_value = atof(pv->s);
} else if (pv->type == VT_BOOLEAN) { } else if (pv->type == VT_BOOLEAN) {
tv->set_bool_value(pv->s[0] == 't'); tv.type = mvt_bool;
tv.numeric_value.bool_value = (pv->s[0] == 't');
} else { } else {
tv->set_string_value(pv->s); tv.type = mvt_string;
tv.string_value = std::string(pv->s);
} }
outlayer.values.push_back(tv);
} }
pool_free_strings(&keys); pool_free_strings(&keys);
pool_free_strings(&values); pool_free_strings(&values);
outtile.layers.push_back(outlayer);
} }
if (features_added == 0) { if (features_added == 0) {
return; return;
} }
std::string s; std::string compressed = mvt_encode(outtile);
std::string compressed;
outtile.SerializeToString(&s);
compress(s, compressed);
if (compressed.size() > 500000) { if (compressed.size() > 500000) {
fprintf(stderr, "Tile %d/%u/%u size is %lld, >500000. Skipping this tile\n.", z, x, y, (long long) compressed.size()); fprintf(stderr, "Tile %d/%u/%u size is %lld, >500000. Skipping this tile\n.", z, x, y, (long long) compressed.size());
-3
View File
@@ -19,7 +19,6 @@
#include <pthread.h> #include <pthread.h>
#include <errno.h> #include <errno.h>
#include "mvt.hh" #include "mvt.hh"
#include "vector_tile.pb.h"
#include "geometry.hh" #include "geometry.hh"
extern "C" { extern "C" {
@@ -557,8 +556,6 @@ long long write_tile(FILE *geoms, long long *geompos_in, char *metabase, char *s
// This only loops if the tile data didn't fit, in which case the detail // 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. // 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) { for (line_detail = detail; line_detail >= min_detail || line_detail == detail; line_detail--, oprogress = 0) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
struct pool keys1[nlayers], values1[nlayers]; struct pool keys1[nlayers], values1[nlayers];
struct pool *keys[nlayers], *values[nlayers]; struct pool *keys[nlayers], *values[nlayers];
int i; int i;