mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-05 18:05:42 +02:00
Methods instead of functions
This commit is contained in:
@@ -48,7 +48,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) {
|
|||||||
int within = 0;
|
int within = 0;
|
||||||
mvt_tile tile;
|
mvt_tile tile;
|
||||||
|
|
||||||
if (!mvt_decode(message, tile)) {
|
if (!tile.decode(message)) {
|
||||||
fprintf(stderr, "Couldn't parse tile %d/%u/%u\n", z, x, y);
|
fprintf(stderr, "Couldn't parse tile %d/%u/%u\n", z, x, y);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,12 @@
|
|||||||
#include "protozero/pbf_reader.hpp"
|
#include "protozero/pbf_reader.hpp"
|
||||||
#include "protozero/pbf_writer.hpp"
|
#include "protozero/pbf_writer.hpp"
|
||||||
|
|
||||||
|
mvt_geometry::mvt_geometry(int op, long long x, long long y) {
|
||||||
|
this->op = op;
|
||||||
|
this->x = x;
|
||||||
|
this->y = y;
|
||||||
|
}
|
||||||
|
|
||||||
// https://github.com/mapbox/mapnik-vector-tile/blob/master/src/vector_tile_compression.hpp
|
// https://github.com/mapbox/mapnik-vector-tile/blob/master/src/vector_tile_compression.hpp
|
||||||
bool is_compressed(std::string const &data) {
|
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));
|
return data.size() > 2 && (((uint8_t) data[0] == 0x78 && (uint8_t) data[1] == 0x9C) || ((uint8_t) data[0] == 0x1F && (uint8_t) data[1] == 0x8B));
|
||||||
@@ -74,8 +80,8 @@ int dezig(unsigned n) {
|
|||||||
return (n >> 1) ^ (-(n & 1));
|
return (n >> 1) ^ (-(n & 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mvt_decode(std::string &message, mvt_tile &out) {
|
bool mvt_tile::decode(std::string &message) {
|
||||||
out.layers.clear();
|
layers.clear();
|
||||||
std::string src;
|
std::string src;
|
||||||
|
|
||||||
if (is_compressed(message)) {
|
if (is_compressed(message)) {
|
||||||
@@ -226,7 +232,7 @@ bool mvt_decode(std::string &message, mvt_tile &out) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
out.layers.push_back(layer);
|
layers.push_back(layer);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,27 +245,27 @@ bool mvt_decode(std::string &message, mvt_tile &out) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string mvt_encode(mvt_tile &in) {
|
std::string mvt_tile::encode() {
|
||||||
std::string data;
|
std::string data;
|
||||||
|
|
||||||
protozero::pbf_writer writer(data);
|
protozero::pbf_writer writer(data);
|
||||||
|
|
||||||
for (size_t i = 0; i < in.layers.size(); i++) {
|
for (size_t i = 0; i < layers.size(); i++) {
|
||||||
std::string layer_string;
|
std::string layer_string;
|
||||||
protozero::pbf_writer layer_writer(layer_string);
|
protozero::pbf_writer layer_writer(layer_string);
|
||||||
|
|
||||||
layer_writer.add_uint32(15, 2); /* version */
|
layer_writer.add_uint32(15, 2); /* version */
|
||||||
layer_writer.add_string(1, in.layers[i].name); /* name */
|
layer_writer.add_string(1, layers[i].name); /* name */
|
||||||
layer_writer.add_uint32(5, in.layers[i].extent); /* extent */
|
layer_writer.add_uint32(5, layers[i].extent); /* extent */
|
||||||
|
|
||||||
for (size_t j = 0; j < in.layers[i].keys.size(); j++) {
|
for (size_t j = 0; j < layers[i].keys.size(); j++) {
|
||||||
layer_writer.add_string(3, in.layers[i].keys[j]); /* key */
|
layer_writer.add_string(3, layers[i].keys[j]); /* key */
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t v = 0; v < in.layers[i].values.size(); v++) {
|
for (size_t v = 0; v < layers[i].values.size(); v++) {
|
||||||
std::string value_string;
|
std::string value_string;
|
||||||
protozero::pbf_writer value_writer(value_string);
|
protozero::pbf_writer value_writer(value_string);
|
||||||
mvt_value &pbv = in.layers[i].values[v];
|
mvt_value &pbv = layers[i].values[v];
|
||||||
|
|
||||||
if (pbv.type == mvt_string) {
|
if (pbv.type == mvt_string) {
|
||||||
value_writer.add_string(1, pbv.string_value);
|
value_writer.add_string(1, pbv.string_value);
|
||||||
@@ -280,12 +286,12 @@ std::string mvt_encode(mvt_tile &in) {
|
|||||||
layer_writer.add_message(4, value_string);
|
layer_writer.add_message(4, value_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t f = 0; f < in.layers[i].features.size(); f++) {
|
for (size_t f = 0; f < layers[i].features.size(); f++) {
|
||||||
std::string feature_string;
|
std::string feature_string;
|
||||||
protozero::pbf_writer feature_writer(feature_string);
|
protozero::pbf_writer feature_writer(feature_string);
|
||||||
|
|
||||||
feature_writer.add_enum(3, in.layers[i].features[f].type);
|
feature_writer.add_enum(3, layers[i].features[f].type);
|
||||||
feature_writer.add_packed_uint32(2, std::begin(in.layers[i].features[f].tags), std::end(in.layers[i].features[f].tags));
|
feature_writer.add_packed_uint32(2, std::begin(layers[i].features[f].tags), std::end(layers[i].features[f].tags));
|
||||||
|
|
||||||
std::vector<uint32_t> geometry;
|
std::vector<uint32_t> geometry;
|
||||||
|
|
||||||
@@ -294,7 +300,7 @@ std::string mvt_encode(mvt_tile &in) {
|
|||||||
int cmd = -1;
|
int cmd = -1;
|
||||||
int length = 0;
|
int length = 0;
|
||||||
|
|
||||||
std::vector<mvt_geometry> &geom = in.layers[i].features[f].geometry;
|
std::vector<mvt_geometry> &geom = layers[i].features[f].geometry;
|
||||||
|
|
||||||
for (size_t g = 0; g < geom.size(); g++) {
|
for (size_t g = 0; g < geom.size(); g++) {
|
||||||
int op = geom[g].op;
|
int op = geom[g].op;
|
||||||
|
|||||||
@@ -1,7 +1,16 @@
|
|||||||
bool is_compressed(std::string const &data);
|
enum mvt_operation {
|
||||||
int decompress(std::string const &input, std::string &output);
|
mvt_moveto = 1,
|
||||||
int compress(std::string const &input, std::string &output);
|
mvt_lineto = 2,
|
||||||
int dezig(unsigned n);
|
mvt_closepath = 7
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mvt_geometry {
|
||||||
|
int /* mvt_operation */ op;
|
||||||
|
long long x;
|
||||||
|
long long y;
|
||||||
|
|
||||||
|
mvt_geometry(int op, long long x, long long y);
|
||||||
|
};
|
||||||
|
|
||||||
enum mvt_geometry_type {
|
enum mvt_geometry_type {
|
||||||
mvt_point = 1,
|
mvt_point = 1,
|
||||||
@@ -9,10 +18,10 @@ enum mvt_geometry_type {
|
|||||||
mvt_polygon = 3
|
mvt_polygon = 3
|
||||||
};
|
};
|
||||||
|
|
||||||
enum mvt_operation {
|
struct mvt_feature {
|
||||||
mvt_moveto = 1,
|
std::vector<unsigned> tags;
|
||||||
mvt_lineto = 2,
|
int /* mvt_geometry_type */ type;
|
||||||
mvt_closepath = 7
|
std::vector<mvt_geometry> geometry;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum mvt_value_type {
|
enum mvt_value_type {
|
||||||
@@ -38,24 +47,6 @@ struct mvt_value {
|
|||||||
} numeric_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<unsigned> tags;
|
|
||||||
int /* mvt_geometry_type */ type;
|
|
||||||
std::vector<mvt_geometry> geometry;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct mvt_layer {
|
struct mvt_layer {
|
||||||
int version;
|
int version;
|
||||||
std::string name;
|
std::string name;
|
||||||
@@ -67,7 +58,12 @@ struct mvt_layer {
|
|||||||
|
|
||||||
struct mvt_tile {
|
struct mvt_tile {
|
||||||
std::vector<mvt_layer> layers;
|
std::vector<mvt_layer> layers;
|
||||||
|
|
||||||
|
std::string encode();
|
||||||
|
bool decode(std::string &message);
|
||||||
};
|
};
|
||||||
|
|
||||||
bool mvt_decode(std::string &message, mvt_tile &out);
|
bool is_compressed(std::string const &data);
|
||||||
std::string mvt_encode(mvt_tile &in);
|
int decompress(std::string const &input, std::string &output);
|
||||||
|
int compress(std::string const &input, std::string &output);
|
||||||
|
int dezig(unsigned n);
|
||||||
|
|||||||
+2
-2
@@ -32,7 +32,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, struct pool **fi
|
|||||||
mvt_tile outtile;
|
mvt_tile outtile;
|
||||||
int features_added = 0;
|
int features_added = 0;
|
||||||
|
|
||||||
if (!mvt_decode(message, tile)) {
|
if (!tile.decode(message)) {
|
||||||
fprintf(stderr, "Couldn't decompress tile %d/%u/%u\n", z, x, y);
|
fprintf(stderr, "Couldn't decompress tile %d/%u/%u\n", z, x, y);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@@ -279,7 +279,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, struct pool **fi
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string compressed = mvt_encode(outtile);
|
std::string compressed = outtile.encode();
|
||||||
|
|
||||||
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());
|
||||||
|
|||||||
@@ -950,7 +950,7 @@ long long write_tile(FILE *geoms, long long *geompos_in, char *metabase, char *s
|
|||||||
pool_free(&values1[i]);
|
pool_free(&values1[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string compressed = mvt_encode(tile);
|
std::string compressed = tile.encode();
|
||||||
|
|
||||||
if (compressed.size() > 500000 && !prevent[P_KILOBYTE_LIMIT]) {
|
if (compressed.size() > 500000 && !prevent[P_KILOBYTE_LIMIT]) {
|
||||||
if (!quiet) {
|
if (!quiet) {
|
||||||
|
|||||||
Reference in New Issue
Block a user