Skip to content

Commit

Permalink
Add a tippecanoe-decode option to restrict which attributes to decode
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Nov 7, 2024
1 parent 23667bb commit 6a6d47e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
8 changes: 7 additions & 1 deletion decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
int minzoom = 0;
int maxzoom = 32;
bool force = false;
std::set<std::string> include_attr;

bool progress_time() {
return false;
Expand Down Expand Up @@ -217,7 +218,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, std::set<std::st
} else if (coordinate_mode == 2) { // integer
scale = 1;
}
layer_to_geojson(layer, z, x, y, !pipeline, pipeline, pipeline, false, 0, 0, 0, !force, state, scale);
layer_to_geojson(layer, z, x, y, !pipeline, pipeline, pipeline, false, 0, 0, 0, !force, state, scale, include_attr);

if (!pipeline) {
if (true) {
Expand Down Expand Up @@ -571,6 +572,7 @@ int main(int argc, char **argv) {
{"stats", no_argument, 0, 'S'},
{"force", no_argument, 0, 'f'},
{"exclude-metadata-row", required_argument, 0, 'x'},
{"include", required_argument, 0, 'y'},
{0, 0, 0, 0},
};

Expand Down Expand Up @@ -630,6 +632,10 @@ int main(int argc, char **argv) {
exclude_meta.insert(optarg);
break;

case 'y':
include_attr.insert(optarg);
break;

default:
usage(argv);
}
Expand Down
2 changes: 1 addition & 1 deletion plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void *run_writer(void *a) {

json_writer state(fp);
for (size_t i = 0; i < wa->layers->size(); i++) {
layer_to_geojson((*(wa->layers))[i], wa->z, wa->x, wa->y, false, true, false, true, 0, 0, 0, true, state, 0);
layer_to_geojson((*(wa->layers))[i], wa->z, wa->x, wa->y, false, true, false, true, 0, 0, 0, true, state, 0, std::set<std::string>());
}

if (fclose(fp) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ void *run_prefilter(void *v) {
decode_meta(sf, tmp_layer, tmp_feature);
tmp_layer.features.push_back(tmp_feature);

layer_to_geojson(tmp_layer, 0, 0, 0, false, true, false, true, sf.index, sf.seq, sf.extent, true, state, 0);
layer_to_geojson(tmp_layer, 0, 0, 0, false, true, false, true, sf.index, sf.seq, sf.extent, true, state, 0, std::set<std::string>());
}

if (fclose(rpa->prefilter_fp) != 0) {
Expand Down
6 changes: 5 additions & 1 deletion write_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ void write_coords(json_writer &state, lonlat const &ll, double scale) {
}
}

void layer_to_geojson(mvt_layer const &layer, unsigned z, unsigned x, unsigned y, bool comma, bool name, bool zoom, bool write_dropped, unsigned long long index, long long sequence, long long extent, bool complain, json_writer &state, double scale) {
void layer_to_geojson(mvt_layer const &layer, unsigned z, unsigned x, unsigned y, bool comma, bool name, bool zoom, bool write_dropped, unsigned long long index, long long sequence, long long extent, bool complain, json_writer &state, double scale, std::set<std::string> const &include_attr) {
for (size_t f = 0; f < layer.features.size(); f++) {
mvt_feature const &feat = layer.features[f];

Expand Down Expand Up @@ -334,6 +334,10 @@ void layer_to_geojson(mvt_layer const &layer, unsigned z, unsigned x, unsigned y
exit(EXIT_IMPOSSIBLE);
}

if (include_attr.size() > 0 && include_attr.find(layer.keys[feat.tags[t]]) == include_attr.end()) {
continue;
}

const char *key = layer.keys[feat.tags[t]].c_str();
mvt_value const &val = layer.values[feat.tags[t + 1]];

Expand Down
3 changes: 2 additions & 1 deletion write_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string>
#include <vector>
#include <stdio.h>
#include <set>

enum json_write_tok {
JSON_WRITE_HASH,
Expand Down Expand Up @@ -61,7 +62,7 @@ struct json_writer {
void adds(std::string const &s);
};

void layer_to_geojson(mvt_layer const &layer, unsigned z, unsigned x, unsigned y, bool comma, bool name, bool zoom, bool dropped, unsigned long long index, long long sequence, long long extent, bool complain, json_writer &state, double scale);
void layer_to_geojson(mvt_layer const &layer, unsigned z, unsigned x, unsigned y, bool comma, bool name, bool zoom, bool dropped, unsigned long long index, long long sequence, long long extent, bool complain, json_writer &state, double scale, std::set<std::string> const &include_attr);
void fprintq(FILE *f, const char *s);

#endif

0 comments on commit 6a6d47e

Please sign in to comment.