diff --git a/Makefile b/Makefile index 8811642..7d3a4f4 100644 --- a/Makefile +++ b/Makefile @@ -35,16 +35,16 @@ C = $(shell find . '(' -name '*.c' -o -name '*.cc' ')') INCLUDES = -I/usr/local/include LIBS = -L/usr/local/lib -tippecanoe: geojson.o jsonpull.o vector_tile.pb.o tile.o clip.o pool.o mbtiles.o geometry.o projection.o memfile.o clipper/clipper.o protobuf.o +tippecanoe: geojson.o jsonpull.o vector_tile.pb.o tile.o clip.o pool.o mbtiles.o geometry.o projection.o memfile.o clipper/clipper.o mvt.o $(CXX) $(PG) $(LIBS) -O3 -g -Wall $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lprotobuf-lite -lsqlite3 -lpthread tippecanoe-enumerate: enumerate.o $(CC) $(PG) $(LIBS) -O3 -g -Wall $(CFLAGS) -o $@ $^ $(LDFLAGS) -lsqlite3 -tippecanoe-decode: decode.o vector_tile.pb.o projection.o protobuf.o +tippecanoe-decode: decode.o vector_tile.pb.o projection.o mvt.o $(CXX) $(PG) $(LIBS) -O3 -g -Wall $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lprotobuf-lite -lsqlite3 -tile-join: tile-join.o vector_tile.pb.o projection.o pool.o mbtiles.o protobuf.o +tile-join: tile-join.o vector_tile.pb.o projection.o pool.o mbtiles.o mvt.o $(CXX) $(PG) $(LIBS) -O3 -g -Wall $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lprotobuf-lite -lsqlite3 libjsonpull.a: jsonpull.o diff --git a/decode.cc b/decode.cc index d1f76ab..92f9069 100644 --- a/decode.cc +++ b/decode.cc @@ -11,7 +11,7 @@ #include #include #include -#include "protobuf.hh" +#include "mvt.hh" #include "vector_tile.pb.h" #include "tile.h" @@ -47,9 +47,9 @@ struct draw { void handle(std::string message, int z, unsigned x, unsigned y, int describe) { int within = 0; - pb_tile tile; + mvt_tile tile; - if (!pb_decode(message, tile)) { + if (!mvt_decode(message, tile)) { fprintf(stderr, "Couldn't parse tile %d/%u/%u\n", z, x, y); exit(EXIT_FAILURE); } @@ -63,7 +63,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) { printf(", \"features\": [\n"); for (int l = 0; l < tile.layers.size(); l++) { - pb_layer &layer = tile.layers[l]; + mvt_layer &layer = tile.layers[l]; int extent = layer.extent; if (describe) { @@ -81,7 +81,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) { } for (int f = 0; f < layer.features.size(); f++) { - pb_feature &feat = layer.features[f]; + mvt_feature &feat = layer.features[f]; if (within) { printf(",\n"); @@ -106,16 +106,16 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) { } const char *key = layer.keys[feat.tags[t]].c_str(); - pb_value const &val = layer.values[feat.tags[t + 1]]; + mvt_value const &val = layer.values[feat.tags[t + 1]]; - if (val.type == pb_string) { + if (val.type == mvt_string) { printq(key); printf(": "); printq(val.string_value.c_str()); - } else if (val.type == pb_int) { + } else if (val.type == mvt_int) { printq(key); printf(": %lld", (long long) val.numeric_value.int_value); - } else if (val.type == pb_double) { + } else if (val.type == mvt_double) { printq(key); double v = val.numeric_value.double_value; if (v == (long long) v) { @@ -123,7 +123,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) { } else { printf(": %g", v); } - } else if (val.type == pb_float) { + } else if (val.type == mvt_float) { printq(key); double v = val.numeric_value.float_value; if (v == (long long) v) { @@ -131,13 +131,13 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) { } else { printf(": %g", v); } - } else if (val.type == pb_sint) { + } else if (val.type == mvt_sint) { printq(key); printf(": %lld", (long long) val.numeric_value.sint_value); - } else if (val.type == pb_uint) { + } else if (val.type == mvt_uint) { printq(key); printf(": %lld", (long long) val.numeric_value.uint_value); - } else if (val.type == pb_bool) { + } else if (val.type == mvt_bool) { printq(key); printf(": %s", val.numeric_value.bool_value ? "true" : "false"); } diff --git a/protobuf.cc b/mvt.cc similarity index 87% rename from protobuf.cc rename to mvt.cc index 5a89452..426eebb 100644 --- a/protobuf.cc +++ b/mvt.cc @@ -4,7 +4,7 @@ #include #include #include -#include "protobuf.hh" +#include "mvt.hh" #include "vector_tile.pb.h" // https://github.com/mapbox/mapnik-vector-tile/blob/master/src/vector_tile_compression.hpp @@ -75,7 +75,7 @@ int dezig(unsigned n) { return (n >> 1) ^ (-(n & 1)); } -bool pb_decode(std::string &message, pb_tile &out) { +bool mvt_decode(std::string &message, mvt_tile &out) { GOOGLE_PROTOBUF_VERIFY_VERSION; // https://github.com/mapbox/mapnik-vector-tile/blob/master/examples/c%2B%2B/tileinfo.cpp @@ -97,7 +97,7 @@ bool pb_decode(std::string &message, pb_tile &out) { for (size_t l = 0; l < tile.layers_size(); l++) { mapnik::vector::tile_layer layer = tile.layers(l); - pb_layer pbl; + mvt_layer pbl; pbl.extent = layer.extent(); pbl.name = layer.name(); @@ -107,28 +107,28 @@ bool pb_decode(std::string &message, pb_tile &out) { for (size_t i = 0; i < layer.values_size(); i++) { mapnik::vector::tile_value const &val = layer.values(i); - pb_value pbv; + mvt_value pbv; if (val.has_string_value()) { - pbv.type = pb_string; + pbv.type = mvt_string; pbv.string_value = val.string_value(); } else if (val.has_float_value()) { - pbv.type = pb_float; + pbv.type = mvt_float; pbv.numeric_value.float_value = val.float_value(); } else if (val.has_double_value()) { - pbv.type = pb_double; + pbv.type = mvt_double; pbv.numeric_value.double_value = val.double_value(); } else if (val.has_int_value()) { - pbv.type = pb_int; + pbv.type = mvt_int; pbv.numeric_value.int_value = val.int_value(); } else if (val.has_uint_value()) { - pbv.type = pb_uint; + pbv.type = mvt_uint; pbv.numeric_value.uint_value = val.uint_value(); } else if (val.has_sint_value()) { - pbv.type = pb_sint; + pbv.type = mvt_sint; pbv.numeric_value.sint_value = val.sint_value(); } else if (val.has_bool_value()) { - pbv.type = pb_bool; + pbv.type = mvt_bool; pbv.numeric_value.bool_value = val.bool_value(); } @@ -137,7 +137,7 @@ bool pb_decode(std::string &message, pb_tile &out) { for (size_t f = 0; f < layer.features_size(); f++) { mapnik::vector::tile_feature feat = layer.features(f); - pb_feature pbf; + mvt_feature pbf; pbf.type = feat.type(); for (size_t i = 0; i < feat.tags_size(); i++) { @@ -150,16 +150,16 @@ bool pb_decode(std::string &message, pb_tile &out) { uint32_t op = geom & 7; uint32_t count = geom >> 3; - if (op == pb_moveto || op == pb_lineto) { + if (op == mvt_moveto || op == mvt_lineto) { for (size_t k = 0; k < count; k++) { px += dezig(feat.geometry(g + 1)); py += dezig(feat.geometry(g + 2)); g += 2; - pbf.geometry.push_back(pb_geometry(op, px, py)); + pbf.geometry.push_back(mvt_geometry(op, px, py)); } } else { - pbf.geometry.push_back(pb_geometry(op, 0, 0)); + pbf.geometry.push_back(mvt_geometry(op, 0, 0)); } } @@ -172,7 +172,7 @@ bool pb_decode(std::string &message, pb_tile &out) { return true; } -std::string pb_encode(pb_tile &in) { +std::string mvt_encode(mvt_tile &in) { GOOGLE_PROTOBUF_VERIFY_VERSION; mapnik::vector::tile tile; @@ -189,21 +189,21 @@ std::string pb_encode(pb_tile &in) { } for (size_t v = 0; v < in.layers[i].values.size(); v++) { mapnik::vector::tile_value *tv = layer->add_values(); - pb_value &pbv = in.layers[i].values[v]; + mvt_value &pbv = in.layers[i].values[v]; - if (pbv.type == pb_string) { + if (pbv.type == mvt_string) { tv->set_string_value(pbv.string_value); - } else if (pbv.type == pb_float) { + } else if (pbv.type == mvt_float) { tv->set_float_value(pbv.numeric_value.float_value); - } else if (pbv.type == pb_double) { + } else if (pbv.type == mvt_double) { tv->set_double_value(pbv.numeric_value.double_value); - } else if (pbv.type == pb_int) { + } else if (pbv.type == mvt_int) { tv->set_int_value(pbv.numeric_value.int_value); - } else if (pbv.type == pb_uint) { + } else if (pbv.type == mvt_uint) { tv->set_uint_value(pbv.numeric_value.uint_value); - } else if (pbv.type == pb_sint) { + } else if (pbv.type == mvt_sint) { tv->set_sint_value(pbv.numeric_value.sint_value); - } else if (pbv.type == pb_bool) { + } else if (pbv.type == mvt_bool) { tv->set_bool_value(pbv.numeric_value.bool_value); } } @@ -216,11 +216,11 @@ std::string pb_encode(pb_tile &in) { } int type = in.layers[i].features[f].type; - if (type == pb_point) { + if (type == mvt_point) { feature->set_type(mapnik::vector::tile::Point); - } else if (type == pb_linestring) { + } else if (type == mvt_linestring) { feature->set_type(mapnik::vector::tile::LineString); - } else if (type == pb_polygon) { + } else if (type == mvt_polygon) { feature->set_type(mapnik::vector::tile::Polygon); } else { fprintf(stderr, "Corrupt geometry type\n"); @@ -236,7 +236,7 @@ std::string pb_encode(pb_tile &in) { int cmd = -1; int length = 0; - std::vector &geom = in.layers[i].features[f].geometry; + std::vector &geom = in.layers[i].features[f].geometry; for (size_t g = 0; g < geom.size(); g++) { int op = geom[g].op; @@ -252,7 +252,7 @@ std::string pb_encode(pb_tile &in) { feature->add_geometry(0); } - if (op == pb_moveto || op == pb_lineto) { + if (op == mvt_moveto || op == mvt_lineto) { long long wwx = geom[g].x; long long wwy = geom[g].y; @@ -267,7 +267,7 @@ std::string pb_encode(pb_tile &in) { px = wwx; py = wwy; length++; - } else if (op == pb_closepath) { + } else if (op == mvt_closepath) { length++; } else { fprintf(stderr, "\nInternal error: corrupted geometry\n"); diff --git a/mvt.hh b/mvt.hh new file mode 100644 index 0000000..a92fa5c --- /dev/null +++ b/mvt.hh @@ -0,0 +1,63 @@ +bool is_compressed(std::string const &data); +int decompress(std::string const &input, std::string &output); +int compress(std::string const &input, std::string &output); +int dezig(unsigned n); + +enum mvt_geometry_type { + mvt_point = 1, mvt_linestring = 2, mvt_polygon = 3 +}; + +enum mvt_operation { + mvt_moveto = 1, mvt_lineto = 2, mvt_closepath = 7 +}; + +enum mvt_value_type { + mvt_string, mvt_float, mvt_double, mvt_int, mvt_uint, mvt_sint, mvt_bool +}; + +struct mvt_value { + mvt_value_type type; + std::string string_value; + union { + float float_value; + double double_value; + long long int_value; + unsigned long long uint_value; + long long sint_value; + bool bool_value; + } numeric_value; +}; + +struct mvt_geometry { + int /* mvt_operation */ op; + long long x; + long long y; + + mvt_geometry(int op, long long x, long long y) { + this->op = op; + this->x = x; + this->y = y; + } +}; + +struct mvt_feature { + std::vector tags; + int /* mvt_geometry_type */ type; + std::vector geometry; +}; + +struct mvt_layer { + int version; + std::string name; + std::vector features; + std::vector keys; + std::vector values; + int extent; +}; + +struct mvt_tile { + std::vector layers; +}; + +bool mvt_decode(std::string &message, mvt_tile &out); +std::string mvt_encode(mvt_tile &in); diff --git a/protobuf.hh b/protobuf.hh deleted file mode 100644 index b378af7..0000000 --- a/protobuf.hh +++ /dev/null @@ -1,63 +0,0 @@ -bool is_compressed(std::string const &data); -int decompress(std::string const &input, std::string &output); -int compress(std::string const &input, std::string &output); -int dezig(unsigned n); - -enum pb_geometry_type { - pb_point = 1, pb_linestring = 2, pb_polygon = 3 -}; - -enum pb_operation { - pb_moveto = 1, pb_lineto = 2, pb_closepath = 7 -}; - -enum pb_value_type { - pb_string, pb_float, pb_double, pb_int, pb_uint, pb_sint, pb_bool -}; - -struct pb_value { - pb_value_type type; - std::string string_value; - union { - float float_value; - double double_value; - long long int_value; - unsigned long long uint_value; - long long sint_value; - bool bool_value; - } numeric_value; -}; - -struct pb_geometry { - int /* pb_operation */ op; - long long x; - long long y; - - pb_geometry(int op, long long x, long long y) { - this->op = op; - this->x = x; - this->y = y; - } -}; - -struct pb_feature { - std::vector tags; - int /* pb_geometry_type */ type; - std::vector geometry; -}; - -struct pb_layer { - int version; - std::string name; - std::vector features; - std::vector keys; - std::vector values; - int extent; -}; - -struct pb_tile { - std::vector layers; -}; - -bool pb_decode(std::string &message, pb_tile &out); -std::string pb_encode(pb_tile &in); diff --git a/tile-join.cc b/tile-join.cc index 32719d6..1afe629 100644 --- a/tile-join.cc +++ b/tile-join.cc @@ -8,7 +8,7 @@ #include #include #include -#include "protobuf.hh" +#include "mvt.hh" #include "vector_tile.pb.h" #include "tile.h" diff --git a/tile.cc b/tile.cc index b581c5a..7202e65 100644 --- a/tile.cc +++ b/tile.cc @@ -18,7 +18,7 @@ #include #include #include -#include "protobuf.hh" +#include "mvt.hh" #include "vector_tile.pb.h" #include "geometry.hh" @@ -38,11 +38,11 @@ extern "C" { pthread_mutex_t db_lock = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t var_lock = PTHREAD_MUTEX_INITIALIZER; -std::vector to_feature(drawvec &geom) { - std::vector out; +std::vector to_feature(drawvec &geom) { + std::vector out; for (size_t i = 0; i < geom.size(); i++) { - out.push_back(pb_geometry(geom[i].op, geom[i].x, geom[i].y)); + out.push_back(mvt_geometry(geom[i].op, geom[i].x, geom[i].y)); } return out; @@ -203,8 +203,8 @@ static int is_integer(const char *s, long long *v) { return 1; } -pb_tile create_tile(char **layernames, int line_detail, std::vector > &features, long long *count, struct pool **keys, struct pool **values, int nlayers) { - pb_tile tile; +mvt_tile create_tile(char **layernames, int line_detail, std::vector > &features, long long *count, struct pool **keys, struct pool **values, int nlayers) { + mvt_tile tile; int i; for (i = 0; i < nlayers; i++) { @@ -212,7 +212,7 @@ pb_tile create_tile(char **layernames, int line_detail, std::vectors, strlen(pv->s))); } for (pv = values[i]->head; pv != NULL; pv = pv->next) { - pb_value tv; + mvt_value tv; if (pv->type == VT_NUMBER) { long long v; if (is_integer(pv->s, &v)) { if (v >= 0) { - tv.type = pb_int; + tv.type = mvt_int; tv.numeric_value.int_value = v; } else { - tv.type = pb_sint; + tv.type = mvt_sint; tv.numeric_value.sint_value = v; } } else { - tv.type = pb_double; + tv.type = mvt_double; tv.numeric_value.double_value = atof(pv->s); } } else if (pv->type == VT_BOOLEAN) { - tv.type = pb_bool; + tv.type = mvt_bool; tv.numeric_value.bool_value = (pv->s[0] == 't'); } else { - tv.type = pb_string; + tv.type = mvt_string; tv.string_value = pv->s; } @@ -944,7 +944,7 @@ long long write_tile(FILE *geoms, long long *geompos_in, char *metabase, char *s return -1; } - pb_tile tile = create_tile(layernames, line_detail, features, &count, keys, values, nlayers); + mvt_tile tile = create_tile(layernames, line_detail, features, &count, keys, values, nlayers); int i; for (i = 0; i < nlayers; i++) { @@ -952,7 +952,7 @@ long long write_tile(FILE *geoms, long long *geompos_in, char *metabase, char *s pool_free(&values1[i]); } - std::string compressed = pb_encode(tile); + std::string compressed = mvt_encode(tile); if (compressed.size() > 500000 && !prevent[P_KILOBYTE_LIMIT]) { if (!quiet) {