Skip to content

Commit

Permalink
Most of the rest of joining JSON attributes onto features in overzoom
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Apr 8, 2024
1 parent 8876015 commit 1d48570
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 17 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ tippecanoe-enumerate: enumerate.o
tippecanoe-decode: decode.o projection.o mvt.o write_json.o text.o jsonpull/jsonpull.o dirtiles.o pmtiles_file.o
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3

tile-join: tile-join.o projection.o mbtiles.o mvt.o memfile.o dirtiles.o jsonpull/jsonpull.o text.o evaluator.o csv.o write_json.o pmtiles_file.o clip.o attribute.o thread.o
tile-join: tile-join.o projection.o mbtiles.o mvt.o memfile.o dirtiles.o jsonpull/jsonpull.o text.o evaluator.o csv.o write_json.o pmtiles_file.o clip.o attribute.o thread.o read_json.o projection.o
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread

tippecanoe-json-tool: jsontool.o jsonpull/jsonpull.o csv.o text.o geojson-loop.o
Expand All @@ -76,8 +76,8 @@ tippecanoe-json-tool: jsontool.o jsonpull/jsonpull.o csv.o text.o geojson-loop.o
unit: unit.o text.o sort.o mvt.o
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread

tippecanoe-overzoom: overzoom.o mvt.o clip.o evaluator.o jsonpull/jsonpull.o text.o attribute.o
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread
tippecanoe-overzoom: overzoom.o mvt.o clip.o evaluator.o jsonpull/jsonpull.o text.o attribute.o read_json.o
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread projection.o

-include $(wildcard *.d)

Expand Down
43 changes: 39 additions & 4 deletions clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "evaluator.hpp"
#include "serial.hpp"
#include "attribute.hpp"
#include "read_json.hpp"

static std::vector<std::pair<double, double>> clip_poly1(std::vector<std::pair<double, double>> &geom,
long long minx, long long miny, long long maxx, long long maxy,
Expand Down Expand Up @@ -886,25 +887,59 @@ Joined values win over original values, at least for the moment.
*/

void join_by_id(mvt_feature &feature, const json_object *j) {
void join_by_id(mvt_layer &layer, mvt_feature &feature, const json_object *j, std::shared_ptr<std::string> tile_stringpool) {
if (!feature.has_id) {
return;
}

if (j->type != JSON_HASH) {
fprintf(stderr, "list of attributes to be joined to features is not a json object\n");
exit(EXIT_JSON);
}

for (size_t i = 0; i < j->value.object.length; i++) {
const json_object *key = j->value.object.keys[i];
if (key->type == JSON_STRING) {
unsigned long long feature_id = atoll(j->value.object.keys[i]->value.string.string);
if (feature_id == feature.id) {
const json_object *value = j->value.object.values[i];

if (value->type == JSON_HASH) {
for (size_t a = 0; a < value->value.object.length; a++) {
json_object *k = value->value.object.keys[a];
json_object *v = value->value.object.values[a];

if (k->type != JSON_STRING) {
fprintf(stderr, "Expected string for key in json join, not %s\n", json_stringify(k));
exit(EXIT_JSON);
}

serial_val sv = stringify_value(v, "joined json", 1, value);
mvt_value mv = stringified_to_mvt_value(sv.type, sv.s.c_str(), tile_stringpool);
layer.tag(feature, k->value.string.string, mv);
}
} else {
fprintf(stderr, "Expected attribute object for value in json join, not %s\n", json_stringify(value));
exit(EXIT_JSON);
}

break;
}
} else {
fprintf(stderr, "Expected string for key in json join, not %s\n", json_stringify(key));
exit(EXIT_JSON);
}
}
}

std::string overzoom(const mvt_tile &tile, int oz, int ox, int oy, int nz, int nx, int ny,
std::string overzoom(mvt_tile tile, int oz, int ox, int oy, int nz, int nx, int ny,
int detail, int buffer, std::set<std::string> const &keep, bool do_compress,
std::vector<std::pair<unsigned, unsigned>> *next_overzoomed_tiles,
bool demultiply, json_object *filter, bool preserve_input_order, std::unordered_map<std::string, attribute_op> const &attribute_accum, std::vector<std::string> const &unidecode_data, json_object *join_attributes_json) {
mvt_tile outtile;
std::shared_ptr<std::string> tile_stringpool = std::make_shared<std::string>();

for (auto const &layer : tile.layers) {
for (auto &layer : tile.layers) {
mvt_layer outlayer = mvt_layer();

int det = detail;
Expand All @@ -923,7 +958,7 @@ std::string overzoom(const mvt_tile &tile, int oz, int ox, int oy, int nz, int n

for (auto feature : layer.features) {
if (join_attributes_json != NULL) {
join_by_id(feature, join_attributes_json);
join_by_id(layer, feature, join_attributes_json, tile_stringpool);
}

bool flush_multiplier_cluster = false;
Expand Down
2 changes: 1 addition & 1 deletion geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void visvalingam(drawvec &ls, size_t start, size_t end, double threshold, size_t
int pnpoly(const drawvec &vert, size_t start, size_t nvert, long long testx, long long testy);
double distance_from_line(long long point_x, long long point_y, long long segA_x, long long segA_y, long long segB_x, long long segB_y);

std::string overzoom(const mvt_tile &tile, int oz, int ox, int oy, int nz, int nx, int ny,
std::string overzoom(mvt_tile tile, int oz, int ox, int oy, int nz, int nx, int ny,
int detail, int buffer, std::set<std::string> const &keep, bool do_compress,
std::vector<std::pair<unsigned, unsigned>> *next_overzoomed_tiles,
bool demultiply, json_object *filter, bool preserve_input_order,
Expand Down
6 changes: 3 additions & 3 deletions jsonpull/jsonpull.c
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ void json_disconnect(json_object *o) {
o->parent = NULL;
}

static void json_print_one(struct string *val, json_object *o) {
static void json_print_one(struct string *val, const json_object *o) {
if (o == NULL) {
string_append_string(val, "...");
} else if (o->type == JSON_STRING) {
Expand Down Expand Up @@ -955,7 +955,7 @@ static void json_print_one(struct string *val, json_object *o) {
}
}

static void json_print(struct string *val, json_object *o) {
static void json_print(struct string *val, const json_object *o) {
if (o == NULL) {
// Hash value in incompletely read hash
string_append_string(val, "...");
Expand Down Expand Up @@ -987,7 +987,7 @@ static void json_print(struct string *val, json_object *o) {
}
}

char *json_stringify(json_object *o) {
char *json_stringify(const json_object *o) {
struct string val;
string_init(&val);
json_print(&val, o);
Expand Down
2 changes: 1 addition & 1 deletion jsonpull/jsonpull.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void json_disconnect(json_object *j);

json_object *json_hash_get(json_object *o, const char *s);

char *json_stringify(json_object *o);
char *json_stringify(const json_object *o);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions read_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int mb_geometry[GEOM_TYPES] = {
VT_POLYGON,
};

void json_context(json_object *j) {
void json_context(const json_object *j) {
char *s = json_stringify(j);

if (strlen(s) >= 500) {
Expand Down Expand Up @@ -121,7 +121,7 @@ void parse_geometry(int t, json_object *j, drawvec &out, int op, const char *fna
// type and stringified value. All numeric values, even if they are integers,
// even integers that are too large to fit in a double but will still be
// stringified with their original precision, are recorded here as mvt_double.
serial_val stringify_value(json_object *value, const char *reading, int line, json_object *feature) {
serial_val stringify_value(json_object const *value, char const *reading, int line, json_object const *feature) {
serial_val sv;

if (value != NULL) {
Expand Down
5 changes: 2 additions & 3 deletions read_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ extern const char *geometry_names[GEOM_TYPES];
extern int geometry_within[GEOM_TYPES];
extern int mb_geometry[GEOM_TYPES];

void json_context(json_object *j);
void json_context(const json_object *j);
void parse_geometry(int t, json_object *j, drawvec &out, int op, const char *fname, int line, json_object *feature);

serial_val stringify_value(json_object *value, const char *reading, int line, json_object *feature);
serial_val stringify_value(json_object const *, char const *, int, json_object const *);

0 comments on commit 1d48570

Please sign in to comment.