From 863c9a59291789244ef697b9ac37b55c52326192 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Thu, 20 Aug 2015 14:27:39 -0700 Subject: [PATCH] Retrieving keys and values --- pool.c | 4 ++-- pool.h | 2 +- tile-join.cc | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/pool.c b/pool.c index 74dd536c..cc55df9f 100644 --- a/pool.c +++ b/pool.c @@ -5,7 +5,7 @@ #define POOL_WIDTH 256 -static int hash(char *s) { +static int hash(const char *s) { int h = 0; for (; *s; s++) { h = h * 37 + *s; @@ -57,7 +57,7 @@ struct pool_val *pool(struct pool *p, char *s, int type) { return *v; } -int is_pooled(struct pool *p, char *s, int type) { +int is_pooled(struct pool *p, const char *s, int type) { int h = hash(s); struct pool_val **v = &(p->vals[h]); diff --git a/pool.h b/pool.h index 147e8c81..683b5511 100644 --- a/pool.h +++ b/pool.h @@ -21,4 +21,4 @@ struct pool_val *pool(struct pool *p, char *s, int type); void pool_free(struct pool *p); void pool_free_strings(struct pool *p); void pool_init(struct pool *p, int n); -int is_pooled(struct pool *p, char *s, int type); +int is_pooled(struct pool *p, const char *s, int type); diff --git a/tile-join.cc b/tile-join.cc index 15b9f005..af02b0d5 100644 --- a/tile-join.cc +++ b/tile-join.cc @@ -1,3 +1,6 @@ +// for vasprintf() on Linux +#define _GNU_SOURCE + #include #include #include @@ -6,6 +9,7 @@ #include #include #include "vector_tile.pb.h" +#include "tile.h" extern "C" { #include "projection.h" @@ -105,6 +109,50 @@ void handle(std::string message, int z, unsigned x, unsigned y, struct pool **fi for (int g = 0; g < feat.geometry_size(); g++) { outfeature->add_geometry(feat.geometry(g)); } + + for (int t = 0; t + 1 < feat.tags_size(); t += 2) { + if (feat.tags(t) >= layer.keys_size() || feat.tags(t + 1) >= layer.values_size()) { + printf("out of range: %d=%d\n", feat.tags(t), feat.tags(t + 1)); + continue; + } + + const char *key = layer.keys(feat.tags(t)).c_str(); + mapnik::vector::tile_value const &val = layer.values(feat.tags(t + 1)); + char *value; + int type; + + if (val.has_string_value()) { + value = strdup(val.string_value().c_str()); + type = VT_STRING; + } else if (val.has_int_value()) { + asprintf(&value, "%lld", val.int_value()); + type = VT_NUMBER; + } else if (val.has_double_value()) { + asprintf(&value, "%g", val.double_value()); + type = VT_NUMBER; + } else if (val.has_float_value()) { + asprintf(&value, "%g", val.float_value()); + type = VT_NUMBER; + } else if (val.has_bool_value()) { + asprintf(&value, "%s", val.bool_value() ? "true" : "false"); + type = VT_BOOLEAN; + } else if (val.has_sint_value()) { + asprintf(&value, "%lld", val.sint_value()); + type = VT_NUMBER; + } else if (val.has_uint_value()) { + asprintf(&value, "%llu", val.uint_value()); + type = VT_NUMBER; + } else { + continue; + } + + if (!is_pooled(file_keys[ll], key, type)) { + pool(file_keys[ll], strdup(key), type); + } + + printf("%d: %s=%s\n", type, key, value); + free(value); + } } } }