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
+106 -106
View File
@@ -27,29 +27,29 @@ int minzoom = 0;
int maxzoom = 32; int maxzoom = 32;
bool force = false; bool force = false;
void do_stats(mvt_tile &tile, size_t size, bool compressed, int z, unsigned x, unsigned y, json_write_state &state) { void do_stats(mvt_tile &tile, size_t size, bool compressed, int z, unsigned x, unsigned y, json_writer &state) {
json_write_hash(stdout, state); state.json_write_hash();
json_write_string(stdout, "zoom", state); state.json_write_string("zoom");
json_write_signed(stdout, z, state); state.json_write_signed(z);
json_write_string(stdout, "x", state); state.json_write_string("x");
json_write_unsigned(stdout, x, state); state.json_write_unsigned(x);
json_write_string(stdout, "y", state); state.json_write_string("y");
json_write_unsigned(stdout, y, state); state.json_write_unsigned(y);
json_write_string(stdout, "bytes", state); state.json_write_string("bytes");
json_write_unsigned(stdout, size, state); state.json_write_unsigned(size);
json_write_string(stdout, "compressed", state); state.json_write_string("compressed");
json_write_bool(stdout, compressed, state); state.json_write_bool(compressed);
json_write_string(stdout, "layers", state); state.json_write_string("layers");
json_write_hash(stdout, state); state.json_write_hash();
for (size_t i = 0; i < tile.layers.size(); i++) { 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; size_t points = 0, lines = 0, polygons = 0;
for (size_t j = 0; j < tile.layers[i].features.size(); j++) { 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); state.json_write_string("points");
json_write_unsigned(stdout, points, state); state.json_write_unsigned(points);
json_write_string(stdout, "lines", state); state.json_write_string("lines");
json_write_unsigned(stdout, lines, state); state.json_write_unsigned(lines);
json_write_string(stdout, "polygons", state); state.json_write_string("polygons");
json_write_unsigned(stdout, polygons, state); state.json_write_unsigned(polygons);
json_write_string(stdout, "extent", state); state.json_write_string("extent");
json_write_signed(stdout, tile.layers[i].extent, state); state.json_write_signed(tile.layers[i].extent);
json_end_hash(stdout, state); state.json_end_hash();
} }
json_end_hash(stdout, state); state.json_end_hash();
json_end_hash(stdout, state); 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; mvt_tile tile;
bool was_compressed; bool was_compressed;
@@ -105,52 +105,52 @@ void handle(std::string message, int z, unsigned x, unsigned y, std::set<std::st
} }
if (!pipeline) { if (!pipeline) {
json_write_hash(stdout, state); state.json_write_hash();
json_write_string(stdout, "type", state); state.json_write_string("type");
json_write_string(stdout, "FeatureCollection", state); state.json_write_string("FeatureCollection");
if (true) { if (true) {
json_write_string(stdout, "properties", state); state.json_write_string("properties");
json_write_hash(stdout, state); state.json_write_hash();
json_write_string(stdout, "zoom", state); state.json_write_string("zoom");
json_write_signed(stdout, z, state); state.json_write_signed(z);
json_write_string(stdout, "x", state); state.json_write_string("x");
json_write_signed(stdout, x, state); state.json_write_signed(x);
json_write_string(stdout, "y", state); state.json_write_string("y");
json_write_signed(stdout, y, state); state.json_write_signed(y);
if (!was_compressed) { if (!was_compressed) {
json_write_string(stdout, "compressed", state); state.json_write_string("compressed");
json_write_bool(stdout, false, state); state.json_write_bool(false);
} }
json_end_hash(stdout, state); state.json_end_hash();
if (projection != projections) { if (projection != projections) {
json_write_string(stdout, "crs", state); state.json_write_string("crs");
json_write_hash(stdout, state); state.json_write_hash();
json_write_string(stdout, "type", state); state.json_write_string("type");
json_write_string(stdout, "name", state); state.json_write_string("name");
json_write_string(stdout, "properties", state); state.json_write_string("properties");
json_write_hash(stdout, state); state.json_write_hash();
json_write_string(stdout, "name", state); state.json_write_string("name");
json_write_string(stdout, projection->alias, state); state.json_write_string(projection->alias);
json_end_hash(stdout, state); state.json_end_hash();
json_end_hash(stdout, state); state.json_end_hash();
} }
} }
json_write_string(stdout, "features", state); state.json_write_string("features");
json_write_array(stdout, state); state.json_write_array();
json_write_newline(stdout, state); state.json_write_newline();
} }
bool first_layer = true; 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 (!pipeline) {
if (true) { if (true) {
if (!first_layer) { 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); state.json_write_string("type");
json_write_string(stdout, "FeatureCollection", state); state.json_write_string("FeatureCollection");
json_write_string(stdout, "properties", state); state.json_write_string("properties");
json_write_hash(stdout, state); state.json_write_hash();
json_write_string(stdout, "layer", state); state.json_write_string("layer");
json_write_string(stdout, layer.name, state); state.json_write_string(layer.name);
json_write_string(stdout, "version", state); state.json_write_string("version");
json_write_signed(stdout, layer.version, state); state.json_write_signed(layer.version);
json_write_string(stdout, "extent", state); state.json_write_string("extent");
json_write_signed(stdout, layer.extent, state); state.json_write_signed(layer.extent);
json_end_hash(stdout, state); state.json_end_hash();
json_write_string(stdout, "features", state); state.json_write_string("features");
json_write_array(stdout, state); state.json_write_array();
json_write_newline(stdout, state); state.json_write_newline();
first_layer = false; 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); 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 (!pipeline) {
if (true) { if (true) {
json_end_array(stdout, state); state.json_end_array();
json_end_hash(stdout, state); state.json_end_hash();
json_write_newline(stdout, state); state.json_write_newline();
} }
} }
} }
if (!pipeline) { if (!pipeline) {
json_end_array(stdout, state); state.json_end_array();
json_end_hash(stdout, state); state.json_end_hash();
json_write_newline(stdout, state); 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; bool isdir = false;
int oz = z; int oz = z;
unsigned ox = x, oy = y; unsigned ox = x, oy = y;
json_write_state state; json_writer state(stdout);
int fd = open(fname, O_RDONLY | O_CLOEXEC); int fd = open(fname, O_RDONLY | O_CLOEXEC);
if (fd >= 0) { 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; int within = 0;
if (!pipeline && !stats) { if (!pipeline && !stats) {
json_write_hash(stdout, state); state.json_write_hash();
json_write_string(stdout, "type", state); state.json_write_string("type");
json_write_string(stdout, "FeatureCollection", state); state.json_write_string("FeatureCollection");
json_write_string(stdout, "properties", state); state.json_write_string("properties");
json_write_hash(stdout, state); state.json_write_hash();
json_write_newline(stdout, state); state.json_write_newline();
const char *sql2 = "SELECT name, value from metadata order by name;"; const char *sql2 = "SELECT name, value from metadata order by name;";
sqlite3_stmt *stmt2; 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) { while (sqlite3_step(stmt2) == SQLITE_ROW) {
if (within) { if (within) {
json_comma_newline(stdout, state); state.json_comma_newline();
} }
within = 1; within = 1;
@@ -310,27 +310,27 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
json_write_string(stdout, (char *) name, state); state.json_write_string((char *) name);
json_write_string(stdout, (char *) value, state); state.json_write_string((char *) value);
} }
json_write_newline(stdout, state); state.json_write_newline();
state.wantnl = false; // XXX state.wantnl = false; // XXX
sqlite3_finalize(stmt2); sqlite3_finalize(stmt2);
} }
if (stats) { if (stats) {
json_write_array(stdout, state); state.json_write_array();
json_write_newline(stdout, state); state.json_write_newline();
} }
if (!pipeline && !stats) { if (!pipeline && !stats) {
json_end_hash(stdout, state); state.json_end_hash();
json_write_string(stdout, "features", state); state.json_write_string("features");
json_write_array(stdout, state); state.json_write_array();
json_write_newline(stdout, state); state.json_write_newline();
} }
if (isdir) { 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++) { for (size_t i = 0; i < tiles.size(); i++) {
if (!pipeline && !stats) { if (!pipeline && !stats) {
if (within) { if (within) {
json_comma_newline(stdout, state); state.json_comma_newline();
} }
within = 1; within = 1;
} }
if (stats) { if (stats) {
if (within) { if (within) {
json_comma_newline(stdout, state); state.json_comma_newline();
} }
within = 1; 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) { while (sqlite3_step(stmt) == SQLITE_ROW) {
if (!pipeline && !stats) { if (!pipeline && !stats) {
if (within) { if (within) {
json_comma_newline(stdout, state); state.json_comma_newline();
} }
within = 1; within = 1;
} }
if (stats) { if (stats) {
if (within) { if (within) {
json_comma_newline(stdout, state); state.json_comma_newline();
} }
within = 1; within = 1;
} }
@@ -412,16 +412,16 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
} }
if (!pipeline && !stats) { if (!pipeline && !stats) {
json_end_array(stdout, state); state.json_end_array();
json_end_hash(stdout, state); state.json_end_hash();
json_write_newline(stdout, state); state.json_write_newline();
} }
if (stats) { if (stats) {
json_end_array(stdout, state); state.json_end_array();
json_write_newline(stdout, state); state.json_write_newline();
} }
if (pipeline) { if (pipeline) {
json_write_newline(stdout, state); state.json_write_newline();
} }
} else { } else {
int handled = 0; int handled = 0;
+2 -2
View File
@@ -51,9 +51,9 @@ void *run_writer(void *a) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
json_write_state state; json_writer state(fp);
for (size_t i = 0; i < wa->layers->size(); i++) { 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) { if (fclose(fp) != 0) {
+2 -2
View File
@@ -1376,7 +1376,7 @@ struct run_prefilter_args {
void *run_prefilter(void *v) { void *run_prefilter(void *v) {
run_prefilter_args *rpa = (run_prefilter_args *) v; run_prefilter_args *rpa = (run_prefilter_args *) v;
json_write_state state; json_writer state(rpa->prefilter_fp);
while (1) { 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); 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); 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); 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) { if (fclose(rpa->prefilter_fp) != 0) {
+189 -189
View File
@@ -9,72 +9,72 @@
#include "write_json.hpp" #include "write_json.hpp"
#include "milo/dtoa_milo.h" #include "milo/dtoa_milo.h"
static void json_adjust(FILE *f, json_write_state &state) { void json_writer::json_adjust() {
if (state.state.size() == 0) { if (state.size() == 0) {
state.state.push_back(JSON_WRITE_TOP); state.push_back(JSON_WRITE_TOP);
} else if (state.state[state.state.size() - 1] == JSON_WRITE_TOP) { } else if (state[state.size() - 1] == JSON_WRITE_TOP) {
fprintf(f, "\n"); fprintf(f, "\n");
state.state[state.state.size() - 1] = JSON_WRITE_TOP; state[state.size() - 1] = JSON_WRITE_TOP;
} else if (state.state[state.state.size() - 1] == JSON_WRITE_HASH) { } else if (state[state.size() - 1] == JSON_WRITE_HASH) {
if (!state.nospace) { if (!nospace) {
fprintf(f, " "); fprintf(f, " ");
} }
state.nospace = false; nospace = false;
state.state[state.state.size() - 1] = JSON_WRITE_HASH_KEY; state[state.size() - 1] = JSON_WRITE_HASH_KEY;
} else if (state.state[state.state.size() - 1] == JSON_WRITE_HASH_KEY) { } else if (state[state.size() - 1] == JSON_WRITE_HASH_KEY) {
fprintf(f, ": "); fprintf(f, ": ");
state.state[state.state.size() - 1] = JSON_WRITE_HASH_VALUE; state[state.size() - 1] = JSON_WRITE_HASH_VALUE;
} else if (state.state[state.state.size() - 1] == JSON_WRITE_HASH_VALUE) { } else if (state[state.size() - 1] == JSON_WRITE_HASH_VALUE) {
if (state.wantnl) { if (wantnl) {
fprintf(f, ",\n"); fprintf(f, ",\n");
state.nospace = false; nospace = false;
} else { } else {
fprintf(f, ", "); fprintf(f, ", ");
} }
state.wantnl = false; wantnl = false;
state.state[state.state.size() - 1] = JSON_WRITE_HASH_KEY; state[state.size() - 1] = JSON_WRITE_HASH_KEY;
} else if (state.state[state.state.size() - 1] == JSON_WRITE_ARRAY) { } else if (state[state.size() - 1] == JSON_WRITE_ARRAY) {
if (!state.nospace) { if (!nospace) {
fprintf(f, " "); fprintf(f, " ");
} }
state.nospace = false; nospace = false;
state.state[state.state.size() - 1] = JSON_WRITE_ARRAY_ELEMENT; state[state.size() - 1] = JSON_WRITE_ARRAY_ELEMENT;
} else if (state.state[state.state.size() - 1] == JSON_WRITE_ARRAY_ELEMENT) { } else if (state[state.size() - 1] == JSON_WRITE_ARRAY_ELEMENT) {
if (state.wantnl) { if (wantnl) {
fprintf(f, ",\n"); fprintf(f, ",\n");
state.nospace = false; nospace = false;
} else { } else {
fprintf(f, ", "); fprintf(f, ", ");
} }
state.wantnl = false; wantnl = false;
state.state[state.state.size() - 1] = JSON_WRITE_ARRAY_ELEMENT; state[state.size() - 1] = JSON_WRITE_ARRAY_ELEMENT;
} else { } else {
fprintf(stderr, "Impossible JSON state\n"); fprintf(stderr, "Impossible JSON state\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
void json_write_array(FILE *f, json_write_state &state) { void json_writer::json_write_array() {
json_adjust(f, state); json_adjust();
fprintf(f, "["); 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) { void json_writer::json_end_array() {
if (state.state.size() == 0) { if (state.size() == 0) {
fprintf(stderr, "End JSON array at top level\n"); fprintf(stderr, "End JSON array at top level\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
json_write_tok tok = state.state[state.state.size() - 1]; json_write_tok tok = state[state.size() - 1];
state.state.pop_back(); state.pop_back();
if (tok == JSON_WRITE_ARRAY || tok == JSON_WRITE_ARRAY_ELEMENT) { if (tok == JSON_WRITE_ARRAY || tok == JSON_WRITE_ARRAY_ELEMENT) {
if (!state.nospace) { if (!nospace) {
fprintf(f, " "); fprintf(f, " ");
} }
state.nospace = false; nospace = false;
fprintf(f, "]"); fprintf(f, "]");
} else { } else {
fprintf(stderr, "End JSON array with unexpected state\n"); 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) { void json_writer::json_write_hash() {
json_adjust(f, state); json_adjust();
fprintf(f, "{"); 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) { void json_writer::json_end_hash() {
if (state.state.size() == 0) { if (state.size() == 0) {
fprintf(stderr, "End JSON hash at top level\n"); fprintf(stderr, "End JSON hash at top level\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
json_write_tok tok = state.state[state.state.size() - 1]; json_write_tok tok = state[state.size() - 1];
state.state.pop_back(); state.pop_back();
if (tok == JSON_WRITE_HASH) { if (tok == JSON_WRITE_HASH) {
if (!state.nospace) { if (!nospace) {
fprintf(f, " "); // Preserve accidental extra space from before fprintf(f, " "); // Preserve accidental extra space from before
} }
state.nospace = false; nospace = false;
fprintf(f, "}"); fprintf(f, "}");
} else if (tok == JSON_WRITE_HASH_VALUE) { } else if (tok == JSON_WRITE_HASH_VALUE) {
if (!state.nospace) { if (!nospace) {
fprintf(f, " "); fprintf(f, " ");
} }
state.nospace = false; nospace = false;
fprintf(f, "}"); fprintf(f, "}");
} else { } else {
fprintf(stderr, "End JSON hash with unexpected state\n"); 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) { void json_writer::json_write_string(std::string const &s) {
json_adjust(f, state); json_adjust();
putc('"', f); putc('"', f);
for (size_t i = 0; i < s.size(); i++) { 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); putc('"', f);
} }
void json_write_number(FILE *f, double d, json_write_state &state) { void json_writer::json_write_number(double d) {
json_adjust(f, state); json_adjust();
fputs(milo::dtoa_milo(d).c_str(), f); fputs(milo::dtoa_milo(d).c_str(), f);
} }
// Just to avoid changing expected output format // Just to avoid json_writer:: changing expected output format
void json_write_float(FILE *f, double d, json_write_state &state) { void json_writer::json_write_float(double d) {
json_adjust(f, state); json_adjust();
fprintf(f, "%f", d); fprintf(f, "%f", d);
} }
void json_write_unsigned(FILE *f, unsigned long long v, json_write_state &state) { void json_writer::json_write_unsigned(unsigned long long v) {
json_adjust(f, state); json_adjust();
fprintf(f, "%llu", v); fprintf(f, "%llu", v);
} }
void json_write_signed(FILE *f, long long v, json_write_state &state) { void json_writer::json_write_signed(long long v) {
json_adjust(f, state); json_adjust();
fprintf(f, "%lld", v); fprintf(f, "%lld", v);
} }
void json_write_stringified(FILE *f, std::string const &s, json_write_state &state) { void json_writer::json_write_stringified(std::string const &s) {
json_adjust(f, state); json_adjust();
fputs(s.c_str(), f); fputs(s.c_str(), f);
} }
void json_write_bool(FILE *f, bool b, json_write_state &state) { void json_writer::json_write_bool(bool b) {
json_adjust(f, state); json_adjust();
if (b) { if (b) {
fputs("true", f); 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) { void json_writer::json_write_null() {
json_adjust(f, state); json_adjust();
fputs("null", f); fputs("null", f);
} }
void json_write_newline(FILE *f, json_write_state &state) { void json_writer::json_write_newline() {
putc('\n', f); putc('\n', f);
state.nospace = true; nospace = true;
} }
void json_comma_newline(FILE *, json_write_state &state) { void json_writer::json_comma_newline() {
state.wantnl = true; wantnl = true;
} }
struct lonlat { 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++) { for (size_t f = 0; f < layer.features.size(); f++) {
mvt_feature const &feat = layer.features[f]; mvt_feature const &feat = layer.features[f];
json_write_hash(fp, state); state.json_write_hash();
json_write_string(fp, "type", state); state.json_write_string("type");
json_write_string(fp, "Feature", state); state.json_write_string("Feature");
if (feat.has_id) { if (feat.has_id) {
json_write_string(fp, "id", state); state.json_write_string("id");
json_write_unsigned(fp, feat.id, state); state.json_write_unsigned(feat.id);
} }
if (name || zoom || index != 0 || sequence != 0 || extent != 0) { if (name || zoom || index != 0 || sequence != 0 || extent != 0) {
json_write_string(fp, "tippecanoe", state); state.json_write_string("tippecanoe");
json_write_hash(fp, state); state.json_write_hash();
if (name) { if (name) {
json_write_string(fp, "layer", state); state.json_write_string("layer");
json_write_string(fp, layer.name, state); state.json_write_string(layer.name);
} }
if (zoom) { if (zoom) {
json_write_string(fp, "minzoom", state); state.json_write_string("minzoom");
json_write_unsigned(fp, z, state); state.json_write_unsigned(z);
json_write_string(fp, "maxzoom", state); state.json_write_string("maxzoom");
json_write_unsigned(fp, z, state); state.json_write_unsigned(z);
} }
if (dropped) { if (dropped) {
json_write_string(fp, "dropped", state); state.json_write_string("dropped");
json_write_bool(fp, feat.dropped, state); state.json_write_bool(feat.dropped);
} }
if (index != 0) { if (index != 0) {
json_write_string(fp, "index", state); state.json_write_string("index");
json_write_unsigned(fp, index, state); state.json_write_unsigned(index);
} }
if (sequence != 0) { if (sequence != 0) {
json_write_string(fp, "sequence", state); state.json_write_string("sequence");
json_write_signed(fp, sequence, state); state.json_write_signed(sequence);
} }
if (extent != 0) { if (extent != 0) {
json_write_string(fp, "extent", state); state.json_write_string("extent");
json_write_signed(fp, extent, state); state.json_write_signed(extent);
} }
json_end_hash(fp, state); state.json_end_hash();
} }
json_write_string(fp, "properties", state); state.json_write_string("properties");
json_write_hash(fp, state); state.json_write_hash();
for (size_t t = 0; t + 1 < feat.tags.size(); t += 2) { for (size_t t = 0; t + 1 < feat.tags.size(); t += 2) {
if (feat.tags[t] >= layer.keys.size()) { 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]]; mvt_value const &val = layer.values[feat.tags[t + 1]];
if (val.type == mvt_string) { if (val.type == mvt_string) {
json_write_string(fp, key, state); state.json_write_string(key);
json_write_string(fp, val.string_value, state); state.json_write_string(val.string_value);
} else if (val.type == mvt_int) { } else if (val.type == mvt_int) {
json_write_string(fp, key, state); state.json_write_string(key);
json_write_signed(fp, val.numeric_value.int_value, state); state.json_write_signed(val.numeric_value.int_value);
} else if (val.type == mvt_double) { } else if (val.type == mvt_double) {
json_write_string(fp, key, state); state.json_write_string(key);
json_write_number(fp, val.numeric_value.double_value, state); state.json_write_number(val.numeric_value.double_value);
} else if (val.type == mvt_float) { } else if (val.type == mvt_float) {
json_write_string(fp, key, state); state.json_write_string(key);
json_write_number(fp, val.numeric_value.float_value, state); state.json_write_number(val.numeric_value.float_value);
} else if (val.type == mvt_sint) { } else if (val.type == mvt_sint) {
json_write_string(fp, key, state); state.json_write_string(key);
json_write_signed(fp, val.numeric_value.sint_value, state); state.json_write_signed(val.numeric_value.sint_value);
} else if (val.type == mvt_uint) { } else if (val.type == mvt_uint) {
json_write_string(fp, key, state); state.json_write_string(key);
json_write_unsigned(fp, val.numeric_value.uint_value, state); state.json_write_unsigned(val.numeric_value.uint_value);
} else if (val.type == mvt_bool) { } else if (val.type == mvt_bool) {
json_write_string(fp, key, state); state.json_write_string(key);
json_write_bool(fp, val.numeric_value.bool_value, state); state.json_write_bool(val.numeric_value.bool_value);
} }
} }
json_end_hash(fp, state); state.json_end_hash();
json_write_string(fp, "geometry", state); state.json_write_string("geometry");
json_write_hash(fp, state); state.json_write_hash();
std::vector<lonlat> ops; 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 (feat.type == VT_POINT) {
if (ops.size() == 1) { if (ops.size() == 1) {
json_write_string(fp, "type", state); state.json_write_string("type");
json_write_string(fp, "Point", state); state.json_write_string("Point");
json_write_string(fp, "coordinates", state); state.json_write_string("coordinates");
json_write_array(fp, state); state.json_write_array();
json_write_float(fp, ops[0].lon, state); state.json_write_float(ops[0].lon);
json_write_float(fp, ops[0].lat, state); state.json_write_float(ops[0].lat);
json_end_array(fp, state); state.json_end_array();
} else { } else {
json_write_string(fp, "type", state); state.json_write_string("type");
json_write_string(fp, "MultiPoint", state); state.json_write_string("MultiPoint");
json_write_string(fp, "coordinates", state); state.json_write_string("coordinates");
json_write_array(fp, state); state.json_write_array();
for (size_t i = 0; i < ops.size(); i++) { for (size_t i = 0; i < ops.size(); i++) {
json_write_array(fp, state); state.json_write_array();
json_write_float(fp, ops[i].lon, state); state.json_write_float(ops[i].lon);
json_write_float(fp, ops[i].lat, state); state.json_write_float(ops[i].lat);
json_end_array(fp, state); state.json_end_array();
} }
json_end_array(fp, state); state.json_end_array();
} }
} else if (feat.type == VT_LINE) { } else if (feat.type == VT_LINE) {
int movetos = 0; int movetos = 0;
@@ -359,59 +359,59 @@ void layer_to_geojson(FILE *fp, mvt_layer const &layer, unsigned z, unsigned x,
} }
if (movetos < 2) { if (movetos < 2) {
json_write_string(fp, "type", state); state.json_write_string("type");
json_write_string(fp, "LineString", state); state.json_write_string("LineString");
json_write_string(fp, "coordinates", state); state.json_write_string("coordinates");
json_write_array(fp, state); state.json_write_array();
for (size_t i = 0; i < ops.size(); i++) { for (size_t i = 0; i < ops.size(); i++) {
json_write_array(fp, state); state.json_write_array();
json_write_float(fp, ops[i].lon, state); state.json_write_float(ops[i].lon);
json_write_float(fp, ops[i].lat, state); state.json_write_float(ops[i].lat);
json_end_array(fp, state); state.json_end_array();
} }
json_end_array(fp, state); state.json_end_array();
} else { } else {
json_write_string(fp, "type", state); state.json_write_string("type");
json_write_string(fp, "MultiLineString", state); state.json_write_string("MultiLineString");
json_write_string(fp, "coordinates", state); state.json_write_string("coordinates");
json_write_array(fp, state); state.json_write_array();
json_write_array(fp, state); state.json_write_array();
int sstate = 0; int sstate = 0;
for (size_t i = 0; i < ops.size(); i++) { for (size_t i = 0; i < ops.size(); i++) {
if (ops[i].op == VT_MOVETO) { if (ops[i].op == VT_MOVETO) {
if (sstate == 0) { if (sstate == 0) {
json_write_array(fp, state); state.json_write_array();
json_write_float(fp, ops[i].lon, state); state.json_write_float(ops[i].lon);
json_write_float(fp, ops[i].lat, state); state.json_write_float(ops[i].lat);
json_end_array(fp, state); state.json_end_array();
sstate = 1; sstate = 1;
} else { } else {
json_end_array(fp, state); state.json_end_array();
json_write_array(fp, state); state.json_write_array();
json_write_array(fp, state); state.json_write_array();
json_write_float(fp, ops[i].lon, state); state.json_write_float(ops[i].lon);
json_write_float(fp, ops[i].lat, state); state.json_write_float(ops[i].lat);
json_end_array(fp, state); state.json_end_array();
sstate = 1; sstate = 1;
} }
} else { } else {
json_write_array(fp, state); state.json_write_array();
json_write_float(fp, ops[i].lon, state); state.json_write_float(ops[i].lon);
json_write_float(fp, ops[i].lat, state); state.json_write_float(ops[i].lat);
json_end_array(fp, state); state.json_end_array();
} }
} }
json_end_array(fp, state); state.json_end_array();
json_end_array(fp, state); state.json_end_array();
} }
} else if (feat.type == VT_POLYGON) { } else if (feat.type == VT_POLYGON) {
std::vector<std::vector<lonlat> > rings; 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++; outer++;
} }
// fprintf(fp, "\"area\": %Lf,", area); // fprintf("\"area\": %Lf,", area);
} }
if (outer > 1) { if (outer > 1) {
json_write_string(fp, "type", state); state.json_write_string("type");
json_write_string(fp, "MultiPolygon", state); state.json_write_string("MultiPolygon");
json_write_string(fp, "coordinates", state); state.json_write_string("coordinates");
json_write_array(fp, state); state.json_write_array();
json_write_array(fp, state); state.json_write_array();
json_write_array(fp, state); state.json_write_array();
} else { } else {
json_write_string(fp, "type", state); state.json_write_string("type");
json_write_string(fp, "Polygon", state); state.json_write_string("Polygon");
json_write_string(fp, "coordinates", state); state.json_write_string("coordinates");
json_write_array(fp, state); state.json_write_array();
json_write_array(fp, state); state.json_write_array();
} }
int sstate = 0; 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 (areas[i] >= 0) {
if (sstate != 0) { if (sstate != 0) {
// new multipolygon // new multipolygon
json_end_array(fp, state); state.json_end_array();
json_end_array(fp, state); state.json_end_array();
json_write_array(fp, state); state.json_write_array();
json_write_array(fp, state); state.json_write_array();
} }
sstate = 1; sstate = 1;
} }
if (sstate == 2) { if (sstate == 2) {
// new ring in the same polygon // new ring in the same polygon
json_end_array(fp, state); state.json_end_array();
json_write_array(fp, state); state.json_write_array();
} }
for (size_t j = 0; j < rings[i].size(); j++) { for (size_t j = 0; j < rings[i].size(); j++) {
if (rings[i][j].op != VT_CLOSEPATH) { if (rings[i][j].op != VT_CLOSEPATH) {
json_write_array(fp, state); state.json_write_array();
json_write_float(fp, rings[i][j].lon, state); state.json_write_float(rings[i][j].lon);
json_write_float(fp, rings[i][j].lat, state); state.json_write_float(rings[i][j].lat);
json_end_array(fp, state); state.json_end_array();
} else { } else {
json_write_array(fp, state); state.json_write_array();
json_write_float(fp, rings[i][0].lon, state); state.json_write_float(rings[i][0].lon);
json_write_float(fp, rings[i][0].lat, state); state.json_write_float(rings[i][0].lat);
json_end_array(fp, state); 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) { if (outer > 1) {
json_end_array(fp, state); state.json_end_array();
json_end_array(fp, state); state.json_end_array();
json_end_array(fp, state); state.json_end_array();
} else { } else {
json_end_array(fp, state); state.json_end_array();
json_end_array(fp, state); state.json_end_array();
} }
} }
json_end_hash(fp, state); state.json_end_hash();
json_end_hash(fp, state); state.json_end_hash();
if (comma) { if (comma) {
json_write_newline(fp, state); state.json_write_newline();
json_comma_newline(fp, state); state.json_comma_newline();
} }
} }
} }
+24 -18
View File
@@ -14,12 +14,13 @@ enum json_write_tok {
JSON_WRITE_TOP, JSON_WRITE_TOP,
}; };
struct json_write_state { struct json_writer {
std::vector<json_write_tok> state; std::vector<json_write_tok> state;
bool nospace = false; bool nospace = false;
bool wantnl = false; bool wantnl = false;
FILE *f;
~json_write_state() { ~json_writer() {
if (state.size() > 0) { if (state.size() > 0) {
if (state.size() != 1 || state[0] != JSON_WRITE_TOP) { if (state.size() != 1 || state[0] != JSON_WRITE_TOP) {
fprintf(stderr, "JSON not closed at end\n"); 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 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 #endif