Change the JSON writing functions to object methods

This commit is contained in:
Eric Fischer 2018-03-16 13:43:21 -07:00
parent 623e0c90fe
commit 1c4aa57ffd
5 changed files with 323 additions and 317 deletions

View File

@ -27,29 +27,29 @@ int minzoom = 0;
int maxzoom = 32;
bool force = false;
void do_stats(mvt_tile &tile, size_t size, bool compressed, int z, unsigned x, unsigned y, json_write_state &state) {
json_write_hash(stdout, state);
void do_stats(mvt_tile &tile, size_t size, bool compressed, int z, unsigned x, unsigned y, json_writer &state) {
state.json_write_hash();
json_write_string(stdout, "zoom", state);
json_write_signed(stdout, z, state);
state.json_write_string("zoom");
state.json_write_signed(z);
json_write_string(stdout, "x", state);
json_write_unsigned(stdout, x, state);
state.json_write_string("x");
state.json_write_unsigned(x);
json_write_string(stdout, "y", state);
json_write_unsigned(stdout, y, state);
state.json_write_string("y");
state.json_write_unsigned(y);
json_write_string(stdout, "bytes", state);
json_write_unsigned(stdout, size, state);
state.json_write_string("bytes");
state.json_write_unsigned(size);
json_write_string(stdout, "compressed", state);
json_write_bool(stdout, compressed, state);
state.json_write_string("compressed");
state.json_write_bool(compressed);
json_write_string(stdout, "layers", state);
json_write_hash(stdout, state);
state.json_write_string("layers");
state.json_write_hash();
for (size_t i = 0; i < tile.layers.size(); i++) {
json_write_string(stdout, tile.layers[i].name, state);
state.json_write_string(tile.layers[i].name);
size_t points = 0, lines = 0, polygons = 0;
for (size_t j = 0; j < tile.layers[i].features.size(); j++) {
@ -62,30 +62,30 @@ void do_stats(mvt_tile &tile, size_t size, bool compressed, int z, unsigned x, u
}
}
json_write_hash(stdout, state);
state.json_write_hash();
json_write_string(stdout, "points", state);
json_write_unsigned(stdout, points, state);
state.json_write_string("points");
state.json_write_unsigned(points);
json_write_string(stdout, "lines", state);
json_write_unsigned(stdout, lines, state);
state.json_write_string("lines");
state.json_write_unsigned(lines);
json_write_string(stdout, "polygons", state);
json_write_unsigned(stdout, polygons, state);
state.json_write_string("polygons");
state.json_write_unsigned(polygons);
json_write_string(stdout, "extent", state);
json_write_signed(stdout, tile.layers[i].extent, state);
state.json_write_string("extent");
state.json_write_signed(tile.layers[i].extent);
json_end_hash(stdout, state);
state.json_end_hash();
}
json_end_hash(stdout, state);
json_end_hash(stdout, state);
state.json_end_hash();
state.json_end_hash();
json_write_newline(stdout, state);
state.json_write_newline();
}
void handle(std::string message, int z, unsigned x, unsigned y, std::set<std::string> const &to_decode, bool pipeline, bool stats, json_write_state &state) {
void handle(std::string message, int z, unsigned x, unsigned y, std::set<std::string> const &to_decode, bool pipeline, bool stats, json_writer &state) {
mvt_tile tile;
bool was_compressed;
@ -105,52 +105,52 @@ void handle(std::string message, int z, unsigned x, unsigned y, std::set<std::st
}
if (!pipeline) {
json_write_hash(stdout, state);
state.json_write_hash();
json_write_string(stdout, "type", state);
json_write_string(stdout, "FeatureCollection", state);
state.json_write_string("type");
state.json_write_string("FeatureCollection");
if (true) {
json_write_string(stdout, "properties", state);
json_write_hash(stdout, state);
state.json_write_string("properties");
state.json_write_hash();
json_write_string(stdout, "zoom", state);
json_write_signed(stdout, z, state);
state.json_write_string("zoom");
state.json_write_signed(z);
json_write_string(stdout, "x", state);
json_write_signed(stdout, x, state);
state.json_write_string("x");
state.json_write_signed(x);
json_write_string(stdout, "y", state);
json_write_signed(stdout, y, state);
state.json_write_string("y");
state.json_write_signed(y);
if (!was_compressed) {
json_write_string(stdout, "compressed", state);
json_write_bool(stdout, false, state);
state.json_write_string("compressed");
state.json_write_bool(false);
}
json_end_hash(stdout, state);
state.json_end_hash();
if (projection != projections) {
json_write_string(stdout, "crs", state);
json_write_hash(stdout, state);
state.json_write_string("crs");
state.json_write_hash();
json_write_string(stdout, "type", state);
json_write_string(stdout, "name", state);
state.json_write_string("type");
state.json_write_string("name");
json_write_string(stdout, "properties", state);
json_write_hash(stdout, state);
state.json_write_string("properties");
state.json_write_hash();
json_write_string(stdout, "name", state);
json_write_string(stdout, projection->alias, state);
state.json_write_string("name");
state.json_write_string(projection->alias);
json_end_hash(stdout, state);
json_end_hash(stdout, state);
state.json_end_hash();
state.json_end_hash();
}
}
json_write_string(stdout, "features", state);
json_write_array(stdout, state);
json_write_newline(stdout, state);
state.json_write_string("features");
state.json_write_array();
state.json_write_newline();
}
bool first_layer = true;
@ -169,32 +169,32 @@ void handle(std::string message, int z, unsigned x, unsigned y, std::set<std::st
if (!pipeline) {
if (true) {
if (!first_layer) {
json_comma_newline(stdout, state);
state.json_comma_newline();
}
json_write_hash(stdout, state);
state.json_write_hash();
json_write_string(stdout, "type", state);
json_write_string(stdout, "FeatureCollection", state);
state.json_write_string("type");
state.json_write_string("FeatureCollection");
json_write_string(stdout, "properties", state);
json_write_hash(stdout, state);
state.json_write_string("properties");
state.json_write_hash();
json_write_string(stdout, "layer", state);
json_write_string(stdout, layer.name, state);
state.json_write_string("layer");
state.json_write_string(layer.name);
json_write_string(stdout, "version", state);
json_write_signed(stdout, layer.version, state);
state.json_write_string("version");
state.json_write_signed(layer.version);
json_write_string(stdout, "extent", state);
json_write_signed(stdout, layer.extent, state);
state.json_write_string("extent");
state.json_write_signed(layer.extent);
json_end_hash(stdout, state);
state.json_end_hash();
json_write_string(stdout, "features", state);
json_write_array(stdout, state);
state.json_write_string("features");
state.json_write_array();
json_write_newline(stdout, state);
state.json_write_newline();
first_layer = false;
}
}
@ -205,21 +205,21 @@ void handle(std::string message, int z, unsigned x, unsigned y, std::set<std::st
exit(EXIT_FAILURE);
}
layer_to_geojson(stdout, layer, z, x, y, !pipeline, pipeline, pipeline, false, 0, 0, 0, !force, state);
layer_to_geojson(layer, z, x, y, !pipeline, pipeline, pipeline, false, 0, 0, 0, !force, state);
if (!pipeline) {
if (true) {
json_end_array(stdout, state);
json_end_hash(stdout, state);
json_write_newline(stdout, state);
state.json_end_array();
state.json_end_hash();
state.json_write_newline();
}
}
}
if (!pipeline) {
json_end_array(stdout, state);
json_end_hash(stdout, state);
json_write_newline(stdout, state);
state.json_end_array();
state.json_end_hash();
state.json_write_newline();
}
}
@ -228,7 +228,7 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
bool isdir = false;
int oz = z;
unsigned ox = x, oy = y;
json_write_state state;
json_writer state(stdout);
int fd = open(fname, O_RDONLY | O_CLOEXEC);
if (fd >= 0) {
@ -280,14 +280,14 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
int within = 0;
if (!pipeline && !stats) {
json_write_hash(stdout, state);
state.json_write_hash();
json_write_string(stdout, "type", state);
json_write_string(stdout, "FeatureCollection", state);
state.json_write_string("type");
state.json_write_string("FeatureCollection");
json_write_string(stdout, "properties", state);
json_write_hash(stdout, state);
json_write_newline(stdout, state);
state.json_write_string("properties");
state.json_write_hash();
state.json_write_newline();
const char *sql2 = "SELECT name, value from metadata order by name;";
sqlite3_stmt *stmt2;
@ -298,7 +298,7 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
while (sqlite3_step(stmt2) == SQLITE_ROW) {
if (within) {
json_comma_newline(stdout, state);
state.json_comma_newline();
}
within = 1;
@ -310,27 +310,27 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
exit(EXIT_FAILURE);
}
json_write_string(stdout, (char *) name, state);
json_write_string(stdout, (char *) value, state);
state.json_write_string((char *) name);
state.json_write_string((char *) value);
}
json_write_newline(stdout, state);
state.json_write_newline();
state.wantnl = false; // XXX
sqlite3_finalize(stmt2);
}
if (stats) {
json_write_array(stdout, state);
json_write_newline(stdout, state);
state.json_write_array();
state.json_write_newline();
}
if (!pipeline && !stats) {
json_end_hash(stdout, state);
state.json_end_hash();
json_write_string(stdout, "features", state);
json_write_array(stdout, state);
json_write_newline(stdout, state);
state.json_write_string("features");
state.json_write_array();
state.json_write_newline();
}
if (isdir) {
@ -338,13 +338,13 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
for (size_t i = 0; i < tiles.size(); i++) {
if (!pipeline && !stats) {
if (within) {
json_comma_newline(stdout, state);
state.json_comma_newline();
}
within = 1;
}
if (stats) {
if (within) {
json_comma_newline(stdout, state);
state.json_comma_newline();
}
within = 1;
}
@ -381,13 +381,13 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
while (sqlite3_step(stmt) == SQLITE_ROW) {
if (!pipeline && !stats) {
if (within) {
json_comma_newline(stdout, state);
state.json_comma_newline();
}
within = 1;
}
if (stats) {
if (within) {
json_comma_newline(stdout, state);
state.json_comma_newline();
}
within = 1;
}
@ -412,16 +412,16 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
}
if (!pipeline && !stats) {
json_end_array(stdout, state);
json_end_hash(stdout, state);
json_write_newline(stdout, state);
state.json_end_array();
state.json_end_hash();
state.json_write_newline();
}
if (stats) {
json_end_array(stdout, state);
json_write_newline(stdout, state);
state.json_end_array();
state.json_write_newline();
}
if (pipeline) {
json_write_newline(stdout, state);
state.json_write_newline();
}
} else {
int handled = 0;

View File

@ -51,9 +51,9 @@ void *run_writer(void *a) {
exit(EXIT_FAILURE);
}
json_write_state state;
json_writer state(fp);
for (size_t i = 0; i < wa->layers->size(); i++) {
layer_to_geojson(fp, (*(wa->layers))[i], wa->z, wa->x, wa->y, false, true, false, true, 0, 0, 0, true, state);
layer_to_geojson((*(wa->layers))[i], wa->z, wa->x, wa->y, false, true, false, true, 0, 0, 0, true, state);
}
if (fclose(fp) != 0) {

View File

@ -1376,7 +1376,7 @@ struct run_prefilter_args {
void *run_prefilter(void *v) {
run_prefilter_args *rpa = (run_prefilter_args *) v;
json_write_state state;
json_writer state(rpa->prefilter_fp);
while (1) {
serial_feature sf = next_feature(rpa->geoms, rpa->geompos_in, rpa->metabase, rpa->meta_off, rpa->z, rpa->tx, rpa->ty, rpa->initial_x, rpa->initial_y, rpa->original_features, rpa->unclipped_features, rpa->nextzoom, rpa->maxzoom, rpa->minzoom, rpa->max_zoom_increment, rpa->pass, rpa->passes, rpa->along, rpa->alongminus, rpa->buffer, rpa->within, rpa->first_time, rpa->geomfile, rpa->geompos, rpa->oprogress, rpa->todo, rpa->fname, rpa->child_shards);
@ -1413,7 +1413,7 @@ void *run_prefilter(void *v) {
decode_meta(sf.m, sf.keys, sf.values, rpa->stringpool + rpa->pool_off[sf.segment], tmp_layer, tmp_feature);
tmp_layer.features.push_back(tmp_feature);
layer_to_geojson(rpa->prefilter_fp, tmp_layer, 0, 0, 0, false, true, false, true, sf.index, sf.seq, sf.extent, true, state);
layer_to_geojson(tmp_layer, 0, 0, 0, false, true, false, true, sf.index, sf.seq, sf.extent, true, state);
}
if (fclose(rpa->prefilter_fp) != 0) {

View File

@ -9,72 +9,72 @@
#include "write_json.hpp"
#include "milo/dtoa_milo.h"
static void json_adjust(FILE *f, json_write_state &state) {
if (state.state.size() == 0) {
state.state.push_back(JSON_WRITE_TOP);
} else if (state.state[state.state.size() - 1] == JSON_WRITE_TOP) {
void json_writer::json_adjust() {
if (state.size() == 0) {
state.push_back(JSON_WRITE_TOP);
} else if (state[state.size() - 1] == JSON_WRITE_TOP) {
fprintf(f, "\n");
state.state[state.state.size() - 1] = JSON_WRITE_TOP;
} else if (state.state[state.state.size() - 1] == JSON_WRITE_HASH) {
if (!state.nospace) {
state[state.size() - 1] = JSON_WRITE_TOP;
} else if (state[state.size() - 1] == JSON_WRITE_HASH) {
if (!nospace) {
fprintf(f, " ");
}
state.nospace = false;
state.state[state.state.size() - 1] = JSON_WRITE_HASH_KEY;
} else if (state.state[state.state.size() - 1] == JSON_WRITE_HASH_KEY) {
nospace = false;
state[state.size() - 1] = JSON_WRITE_HASH_KEY;
} else if (state[state.size() - 1] == JSON_WRITE_HASH_KEY) {
fprintf(f, ": ");
state.state[state.state.size() - 1] = JSON_WRITE_HASH_VALUE;
} else if (state.state[state.state.size() - 1] == JSON_WRITE_HASH_VALUE) {
if (state.wantnl) {
state[state.size() - 1] = JSON_WRITE_HASH_VALUE;
} else if (state[state.size() - 1] == JSON_WRITE_HASH_VALUE) {
if (wantnl) {
fprintf(f, ",\n");
state.nospace = false;
nospace = false;
} else {
fprintf(f, ", ");
}
state.wantnl = false;
state.state[state.state.size() - 1] = JSON_WRITE_HASH_KEY;
} else if (state.state[state.state.size() - 1] == JSON_WRITE_ARRAY) {
if (!state.nospace) {
wantnl = false;
state[state.size() - 1] = JSON_WRITE_HASH_KEY;
} else if (state[state.size() - 1] == JSON_WRITE_ARRAY) {
if (!nospace) {
fprintf(f, " ");
}
state.nospace = false;
state.state[state.state.size() - 1] = JSON_WRITE_ARRAY_ELEMENT;
} else if (state.state[state.state.size() - 1] == JSON_WRITE_ARRAY_ELEMENT) {
if (state.wantnl) {
nospace = false;
state[state.size() - 1] = JSON_WRITE_ARRAY_ELEMENT;
} else if (state[state.size() - 1] == JSON_WRITE_ARRAY_ELEMENT) {
if (wantnl) {
fprintf(f, ",\n");
state.nospace = false;
nospace = false;
} else {
fprintf(f, ", ");
}
state.wantnl = false;
state.state[state.state.size() - 1] = JSON_WRITE_ARRAY_ELEMENT;
wantnl = false;
state[state.size() - 1] = JSON_WRITE_ARRAY_ELEMENT;
} else {
fprintf(stderr, "Impossible JSON state\n");
exit(EXIT_FAILURE);
}
}
void json_write_array(FILE *f, json_write_state &state) {
json_adjust(f, state);
void json_writer::json_write_array() {
json_adjust();
fprintf(f, "[");
state.state.push_back(JSON_WRITE_ARRAY);
state.push_back(JSON_WRITE_ARRAY);
}
void json_end_array(FILE *f, json_write_state &state) {
if (state.state.size() == 0) {
void json_writer::json_end_array() {
if (state.size() == 0) {
fprintf(stderr, "End JSON array at top level\n");
exit(EXIT_FAILURE);
}
json_write_tok tok = state.state[state.state.size() - 1];
state.state.pop_back();
json_write_tok tok = state[state.size() - 1];
state.pop_back();
if (tok == JSON_WRITE_ARRAY || tok == JSON_WRITE_ARRAY_ELEMENT) {
if (!state.nospace) {
if (!nospace) {
fprintf(f, " ");
}
state.nospace = false;
nospace = false;
fprintf(f, "]");
} else {
fprintf(stderr, "End JSON array with unexpected state\n");
@ -82,33 +82,33 @@ void json_end_array(FILE *f, json_write_state &state) {
}
}
void json_write_hash(FILE *f, json_write_state &state) {
json_adjust(f, state);
void json_writer::json_write_hash() {
json_adjust();
fprintf(f, "{");
state.state.push_back(JSON_WRITE_HASH);
state.push_back(JSON_WRITE_HASH);
}
void json_end_hash(FILE *f, json_write_state &state) {
if (state.state.size() == 0) {
void json_writer::json_end_hash() {
if (state.size() == 0) {
fprintf(stderr, "End JSON hash at top level\n");
exit(EXIT_FAILURE);
}
json_write_tok tok = state.state[state.state.size() - 1];
state.state.pop_back();
json_write_tok tok = state[state.size() - 1];
state.pop_back();
if (tok == JSON_WRITE_HASH) {
if (!state.nospace) {
if (!nospace) {
fprintf(f, " "); // Preserve accidental extra space from before
}
state.nospace = false;
nospace = false;
fprintf(f, "}");
} else if (tok == JSON_WRITE_HASH_VALUE) {
if (!state.nospace) {
if (!nospace) {
fprintf(f, " ");
}
state.nospace = false;
nospace = false;
fprintf(f, "}");
} else {
fprintf(stderr, "End JSON hash with unexpected state\n");
@ -116,8 +116,8 @@ void json_end_hash(FILE *f, json_write_state &state) {
}
}
void json_write_string(FILE *f, std::string const &s, json_write_state &state) {
json_adjust(f, state);
void json_writer::json_write_string(std::string const &s) {
json_adjust();
putc('"', f);
for (size_t i = 0; i < s.size(); i++) {
@ -132,39 +132,39 @@ void json_write_string(FILE *f, std::string const &s, json_write_state &state) {
putc('"', f);
}
void json_write_number(FILE *f, double d, json_write_state &state) {
json_adjust(f, state);
void json_writer::json_write_number(double d) {
json_adjust();
fputs(milo::dtoa_milo(d).c_str(), f);
}
// Just to avoid changing expected output format
void json_write_float(FILE *f, double d, json_write_state &state) {
json_adjust(f, state);
// Just to avoid json_writer:: changing expected output format
void json_writer::json_write_float(double d) {
json_adjust();
fprintf(f, "%f", d);
}
void json_write_unsigned(FILE *f, unsigned long long v, json_write_state &state) {
json_adjust(f, state);
void json_writer::json_write_unsigned(unsigned long long v) {
json_adjust();
fprintf(f, "%llu", v);
}
void json_write_signed(FILE *f, long long v, json_write_state &state) {
json_adjust(f, state);
void json_writer::json_write_signed(long long v) {
json_adjust();
fprintf(f, "%lld", v);
}
void json_write_stringified(FILE *f, std::string const &s, json_write_state &state) {
json_adjust(f, state);
void json_writer::json_write_stringified(std::string const &s) {
json_adjust();
fputs(s.c_str(), f);
}
void json_write_bool(FILE *f, bool b, json_write_state &state) {
json_adjust(f, state);
void json_writer::json_write_bool(bool b) {
json_adjust();
if (b) {
fputs("true", f);
@ -173,19 +173,19 @@ void json_write_bool(FILE *f, bool b, json_write_state &state) {
}
}
void json_write_null(FILE *f, json_write_state &state) {
json_adjust(f, state);
void json_writer::json_write_null() {
json_adjust();
fputs("null", f);
}
void json_write_newline(FILE *f, json_write_state &state) {
void json_writer::json_write_newline() {
putc('\n', f);
state.nospace = true;
nospace = true;
}
void json_comma_newline(FILE *, json_write_state &state) {
state.wantnl = true;
void json_writer::json_comma_newline() {
wantnl = true;
}
struct lonlat {
@ -204,61 +204,61 @@ struct lonlat {
}
};
void layer_to_geojson(FILE *fp, 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_write_state &state) {
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) {
for (size_t f = 0; f < layer.features.size(); f++) {
mvt_feature const &feat = layer.features[f];
json_write_hash(fp, state);
json_write_string(fp, "type", state);
json_write_string(fp, "Feature", state);
state.json_write_hash();
state.json_write_string("type");
state.json_write_string("Feature");
if (feat.has_id) {
json_write_string(fp, "id", state);
json_write_unsigned(fp, feat.id, state);
state.json_write_string("id");
state.json_write_unsigned(feat.id);
}
if (name || zoom || index != 0 || sequence != 0 || extent != 0) {
json_write_string(fp, "tippecanoe", state);
json_write_hash(fp, state);
state.json_write_string("tippecanoe");
state.json_write_hash();
if (name) {
json_write_string(fp, "layer", state);
json_write_string(fp, layer.name, state);
state.json_write_string("layer");
state.json_write_string(layer.name);
}
if (zoom) {
json_write_string(fp, "minzoom", state);
json_write_unsigned(fp, z, state);
state.json_write_string("minzoom");
state.json_write_unsigned(z);
json_write_string(fp, "maxzoom", state);
json_write_unsigned(fp, z, state);
state.json_write_string("maxzoom");
state.json_write_unsigned(z);
}
if (dropped) {
json_write_string(fp, "dropped", state);
json_write_bool(fp, feat.dropped, state);
state.json_write_string("dropped");
state.json_write_bool(feat.dropped);
}
if (index != 0) {
json_write_string(fp, "index", state);
json_write_unsigned(fp, index, state);
state.json_write_string("index");
state.json_write_unsigned(index);
}
if (sequence != 0) {
json_write_string(fp, "sequence", state);
json_write_signed(fp, sequence, state);
state.json_write_string("sequence");
state.json_write_signed(sequence);
}
if (extent != 0) {
json_write_string(fp, "extent", state);
json_write_signed(fp, extent, state);
state.json_write_string("extent");
state.json_write_signed(extent);
}
json_end_hash(fp, state);
state.json_end_hash();
}
json_write_string(fp, "properties", state);
json_write_hash(fp, state);
state.json_write_string("properties");
state.json_write_hash();
for (size_t t = 0; t + 1 < feat.tags.size(); t += 2) {
if (feat.tags[t] >= layer.keys.size()) {
@ -274,33 +274,33 @@ void layer_to_geojson(FILE *fp, mvt_layer const &layer, unsigned z, unsigned x,
mvt_value const &val = layer.values[feat.tags[t + 1]];
if (val.type == mvt_string) {
json_write_string(fp, key, state);
json_write_string(fp, val.string_value, state);
state.json_write_string(key);
state.json_write_string(val.string_value);
} else if (val.type == mvt_int) {
json_write_string(fp, key, state);
json_write_signed(fp, val.numeric_value.int_value, state);
state.json_write_string(key);
state.json_write_signed(val.numeric_value.int_value);
} else if (val.type == mvt_double) {
json_write_string(fp, key, state);
json_write_number(fp, val.numeric_value.double_value, state);
state.json_write_string(key);
state.json_write_number(val.numeric_value.double_value);
} else if (val.type == mvt_float) {
json_write_string(fp, key, state);
json_write_number(fp, val.numeric_value.float_value, state);
state.json_write_string(key);
state.json_write_number(val.numeric_value.float_value);
} else if (val.type == mvt_sint) {
json_write_string(fp, key, state);
json_write_signed(fp, val.numeric_value.sint_value, state);
state.json_write_string(key);
state.json_write_signed(val.numeric_value.sint_value);
} else if (val.type == mvt_uint) {
json_write_string(fp, key, state);
json_write_unsigned(fp, val.numeric_value.uint_value, state);
state.json_write_string(key);
state.json_write_unsigned(val.numeric_value.uint_value);
} else if (val.type == mvt_bool) {
json_write_string(fp, key, state);
json_write_bool(fp, val.numeric_value.bool_value, state);
state.json_write_string(key);
state.json_write_bool(val.numeric_value.bool_value);
}
}
json_end_hash(fp, state);
state.json_end_hash();
json_write_string(fp, "geometry", state);
json_write_hash(fp, state);
state.json_write_string("geometry");
state.json_write_hash();
std::vector<lonlat> ops;
@ -325,30 +325,30 @@ void layer_to_geojson(FILE *fp, mvt_layer const &layer, unsigned z, unsigned x,
if (feat.type == VT_POINT) {
if (ops.size() == 1) {
json_write_string(fp, "type", state);
json_write_string(fp, "Point", state);
state.json_write_string("type");
state.json_write_string("Point");
json_write_string(fp, "coordinates", state);
state.json_write_string("coordinates");
json_write_array(fp, state);
json_write_float(fp, ops[0].lon, state);
json_write_float(fp, ops[0].lat, state);
json_end_array(fp, state);
state.json_write_array();
state.json_write_float(ops[0].lon);
state.json_write_float(ops[0].lat);
state.json_end_array();
} else {
json_write_string(fp, "type", state);
json_write_string(fp, "MultiPoint", state);
state.json_write_string("type");
state.json_write_string("MultiPoint");
json_write_string(fp, "coordinates", state);
json_write_array(fp, state);
state.json_write_string("coordinates");
state.json_write_array();
for (size_t i = 0; i < ops.size(); i++) {
json_write_array(fp, state);
json_write_float(fp, ops[i].lon, state);
json_write_float(fp, ops[i].lat, state);
json_end_array(fp, state);
state.json_write_array();
state.json_write_float(ops[i].lon);
state.json_write_float(ops[i].lat);
state.json_end_array();
}
json_end_array(fp, state);
state.json_end_array();
}
} else if (feat.type == VT_LINE) {
int movetos = 0;
@ -359,59 +359,59 @@ void layer_to_geojson(FILE *fp, mvt_layer const &layer, unsigned z, unsigned x,
}
if (movetos < 2) {
json_write_string(fp, "type", state);
json_write_string(fp, "LineString", state);
state.json_write_string("type");
state.json_write_string("LineString");
json_write_string(fp, "coordinates", state);
json_write_array(fp, state);
state.json_write_string("coordinates");
state.json_write_array();
for (size_t i = 0; i < ops.size(); i++) {
json_write_array(fp, state);
json_write_float(fp, ops[i].lon, state);
json_write_float(fp, ops[i].lat, state);
json_end_array(fp, state);
state.json_write_array();
state.json_write_float(ops[i].lon);
state.json_write_float(ops[i].lat);
state.json_end_array();
}
json_end_array(fp, state);
state.json_end_array();
} else {
json_write_string(fp, "type", state);
json_write_string(fp, "MultiLineString", state);
state.json_write_string("type");
state.json_write_string("MultiLineString");
json_write_string(fp, "coordinates", state);
json_write_array(fp, state);
json_write_array(fp, state);
state.json_write_string("coordinates");
state.json_write_array();
state.json_write_array();
int sstate = 0;
for (size_t i = 0; i < ops.size(); i++) {
if (ops[i].op == VT_MOVETO) {
if (sstate == 0) {
json_write_array(fp, state);
json_write_float(fp, ops[i].lon, state);
json_write_float(fp, ops[i].lat, state);
json_end_array(fp, state);
state.json_write_array();
state.json_write_float(ops[i].lon);
state.json_write_float(ops[i].lat);
state.json_end_array();
sstate = 1;
} else {
json_end_array(fp, state);
json_write_array(fp, state);
state.json_end_array();
state.json_write_array();
json_write_array(fp, state);
json_write_float(fp, ops[i].lon, state);
json_write_float(fp, ops[i].lat, state);
json_end_array(fp, state);
state.json_write_array();
state.json_write_float(ops[i].lon);
state.json_write_float(ops[i].lat);
state.json_end_array();
sstate = 1;
}
} else {
json_write_array(fp, state);
json_write_float(fp, ops[i].lon, state);
json_write_float(fp, ops[i].lat, state);
json_end_array(fp, state);
state.json_write_array();
state.json_write_float(ops[i].lon);
state.json_write_float(ops[i].lat);
state.json_end_array();
}
}
json_end_array(fp, state);
json_end_array(fp, state);
state.json_end_array();
state.json_end_array();
}
} else if (feat.type == VT_POLYGON) {
std::vector<std::vector<lonlat> > rings;
@ -465,24 +465,24 @@ void layer_to_geojson(FILE *fp, mvt_layer const &layer, unsigned z, unsigned x,
outer++;
}
// fprintf(fp, "\"area\": %Lf,", area);
// fprintf("\"area\": %Lf,", area);
}
if (outer > 1) {
json_write_string(fp, "type", state);
json_write_string(fp, "MultiPolygon", state);
state.json_write_string("type");
state.json_write_string("MultiPolygon");
json_write_string(fp, "coordinates", state);
json_write_array(fp, state);
json_write_array(fp, state);
json_write_array(fp, state);
state.json_write_string("coordinates");
state.json_write_array();
state.json_write_array();
state.json_write_array();
} else {
json_write_string(fp, "type", state);
json_write_string(fp, "Polygon", state);
state.json_write_string("type");
state.json_write_string("Polygon");
json_write_string(fp, "coordinates", state);
json_write_array(fp, state);
json_write_array(fp, state);
state.json_write_string("coordinates");
state.json_write_array();
state.json_write_array();
}
int sstate = 0;
@ -503,32 +503,32 @@ void layer_to_geojson(FILE *fp, mvt_layer const &layer, unsigned z, unsigned x,
if (areas[i] >= 0) {
if (sstate != 0) {
// new multipolygon
json_end_array(fp, state);
json_end_array(fp, state);
state.json_end_array();
state.json_end_array();
json_write_array(fp, state);
json_write_array(fp, state);
state.json_write_array();
state.json_write_array();
}
sstate = 1;
}
if (sstate == 2) {
// new ring in the same polygon
json_end_array(fp, state);
json_write_array(fp, state);
state.json_end_array();
state.json_write_array();
}
for (size_t j = 0; j < rings[i].size(); j++) {
if (rings[i][j].op != VT_CLOSEPATH) {
json_write_array(fp, state);
json_write_float(fp, rings[i][j].lon, state);
json_write_float(fp, rings[i][j].lat, state);
json_end_array(fp, state);
state.json_write_array();
state.json_write_float(rings[i][j].lon);
state.json_write_float(rings[i][j].lat);
state.json_end_array();
} else {
json_write_array(fp, state);
json_write_float(fp, rings[i][0].lon, state);
json_write_float(fp, rings[i][0].lat, state);
json_end_array(fp, state);
state.json_write_array();
state.json_write_float(rings[i][0].lon);
state.json_write_float(rings[i][0].lat);
state.json_end_array();
}
}
@ -536,21 +536,21 @@ void layer_to_geojson(FILE *fp, mvt_layer const &layer, unsigned z, unsigned x,
}
if (outer > 1) {
json_end_array(fp, state);
json_end_array(fp, state);
json_end_array(fp, state);
state.json_end_array();
state.json_end_array();
state.json_end_array();
} else {
json_end_array(fp, state);
json_end_array(fp, state);
state.json_end_array();
state.json_end_array();
}
}
json_end_hash(fp, state);
json_end_hash(fp, state);
state.json_end_hash();
state.json_end_hash();
if (comma) {
json_write_newline(fp, state);
json_comma_newline(fp, state);
state.json_write_newline();
state.json_comma_newline();
}
}
}

View File

@ -14,12 +14,13 @@ enum json_write_tok {
JSON_WRITE_TOP,
};
struct json_write_state {
struct json_writer {
std::vector<json_write_tok> state;
bool nospace = false;
bool wantnl = false;
FILE *f;
~json_write_state() {
~json_writer() {
if (state.size() > 0) {
if (state.size() != 1 || state[0] != JSON_WRITE_TOP) {
fprintf(stderr, "JSON not closed at end\n");
@ -27,24 +28,29 @@ struct json_write_state {
}
}
}
json_writer(FILE *fp) {
f = fp;
}
void json_write_array();
void json_end_array();
void json_write_hash();
void json_end_hash();
void json_write_string(std::string const &s);
void json_write_number(double d);
void json_write_float(double d);
void json_write_unsigned(unsigned long long v);
void json_write_signed(long long v);
void json_write_stringified(std::string const &s);
void json_write_bool(bool b);
void json_write_null();
void json_write_newline();
void json_comma_newline();
void json_adjust();
};
void layer_to_geojson(FILE *fp, 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_write_state &state);
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);
void fprintq(FILE *f, const char *s);
void json_write_array(FILE *f, json_write_state &state);
void json_end_array(FILE *f, json_write_state &state);
void json_write_hash(FILE *f, json_write_state &state);
void json_end_hash(FILE *f, json_write_state &state);
void json_write_string(FILE *f, std::string const &s, json_write_state &state);
void json_write_number(FILE *f, double d, json_write_state &state);
void json_write_float(FILE *f, double d, json_write_state &state);
void json_write_unsigned(FILE *f, unsigned long long v, json_write_state &state);
void json_write_signed(FILE *f, long long v, json_write_state &state);
void json_write_stringified(FILE *f, std::string const &s, json_write_state &state);
void json_write_bool(FILE *f, bool b, json_write_state &state);
void json_write_null(FILE *f, json_write_state &state);
void json_write_newline(FILE *f, json_write_state &state);
void json_comma_newline(FILE *f, json_write_state &state);
#endif