Skip to content

Commit

Permalink
Use the standard JSON writer when writing metadata.json
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Mar 16, 2018
1 parent ebb0334 commit 3c827b1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 66 deletions.
51 changes: 10 additions & 41 deletions mbtiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,38 +88,6 @@ void mbtiles_write_tile(sqlite3 *outdb, int z, int tx, int ty, const char *data,
}
}

static void quote(std::string &buf, std::string const &s) {
for (size_t i = 0; i < s.size(); i++) {
unsigned char ch = s[i];

if (ch == '\\' || ch == '\"') {
buf.push_back('\\');
buf.push_back(ch);
} else if (ch < ' ') {
char tmp[7];
sprintf(tmp, "\\u%04x", ch);
buf.append(std::string(tmp));
} else {
buf.push_back(ch);
}
}
}

void aprintf(std::string *buf, const char *format, ...) {
va_list ap;
char *tmp;

va_start(ap, format);
if (vasprintf(&tmp, format, ap) < 0) {
fprintf(stderr, "memory allocation failure\n");
exit(EXIT_FAILURE);
}
va_end(ap);

buf->append(tmp, strlen(tmp));
free(tmp);
}

bool type_and_string::operator<(const type_and_string &o) const {
if (string < o.string) {
return true;
Expand Down Expand Up @@ -505,7 +473,10 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *outdir, const char *fnam
exit(EXIT_FAILURE);
}

fprintf(fp, "{\n");
json_writer state(fp);

state.json_write_hash();
state.json_write_newline();

sqlite3_stmt *stmt;
bool first = true;
Expand All @@ -520,19 +491,17 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *outdir, const char *fnam
exit(EXIT_FAILURE);
}

quote(key, k);
quote(value, v);

if (!first) {
fprintf(fp, ",\n");
}
fprintf(fp, " \"%s\": \"%s\"", key.c_str(), value.c_str());
state.json_comma_newline();
state.json_write_string(k);
state.json_write_string(v);
first = false;
}
sqlite3_finalize(stmt);
}

fprintf(fp, "\n}\n");
state.json_write_newline();
state.json_end_hash();
state.json_write_newline();
fclose(fp);
}
}
Expand Down
2 changes: 0 additions & 2 deletions mbtiles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *outdir, const char *fnam

void mbtiles_close(sqlite3 *outdb, const char *pgm);

void aprintf(std::string *buf, const char *format, ...);

std::map<std::string, layermap_entry> merge_layermaps(std::vector<std::map<std::string, layermap_entry> > const &maps);
std::map<std::string, layermap_entry> merge_layermaps(std::vector<std::map<std::string, layermap_entry> > const &maps, bool trunc);

Expand Down
20 changes: 10 additions & 10 deletions tests/raw-tiles/compare/metadata.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "tests/raw-tiles/raw-tiles",
"description": "tests/raw-tiles/raw-tiles",
"version": "2",
"minzoom": "0",
"maxzoom": "14",
"center": "-122.662354,45.514045,14",
"bounds": "-122.682427,45.512331,-122.654961,45.569975",
"type": "overlay",
"format": "pbf",
"json": "{\"vector_layers\": [ { \"id\": \"hackspots\", \"description\": \"\", \"minzoom\": 0, \"maxzoom\": 14, \"fields\": {\"Address\": \"String\", \"Name\": \"String\", \"Notes\": \"String\"} } ],\"tilestats\": {\"layerCount\": 1,\"layers\": [{\"layer\": \"hackspots\",\"count\": 4,\"geometry\": \"Point\",\"attributeCount\": 3,\"attributes\": [{\"attribute\": \"Address\",\"count\": 4,\"type\": \"string\",\"values\": [\"1507 N Rosa Parks Way Portland, OR 97217\",\"201 SE 12th Ave, Portland, OR 97214\",\"4637 N Albina Ave Portland, OR 97217\",\"915 SE Hawthorne Blvd. Portland, OR 97214\"]},{\"attribute\": \"Name\",\"count\": 4,\"type\": \"string\",\"values\": [\"Albina Press\",\"Arbor Lodge\",\"Lucky Labrador Brew Pub\",\"Three Friends Coffeehouse\"]},{\"attribute\": \"Notes\",\"count\": 3,\"type\": \"string\",\"values\": [\"\",\"Dog friendly\",\"usually busy, outlets on side wall only\"]}]}]}}"
"name": "tests/raw-tiles/raw-tiles",
"description": "tests/raw-tiles/raw-tiles",
"version": "2",
"minzoom": "0",
"maxzoom": "14",
"center": "-122.662354,45.514045,14",
"bounds": "-122.682427,45.512331,-122.654961,45.569975",
"type": "overlay",
"format": "pbf",
"json": "{\"vector_layers\": [ { \"id\": \"hackspots\", \"description\": \"\", \"minzoom\": 0, \"maxzoom\": 14, \"fields\": {\"Address\": \"String\", \"Name\": \"String\", \"Notes\": \"String\"} } ],\"tilestats\": {\"layerCount\": 1,\"layers\": [{\"layer\": \"hackspots\",\"count\": 4,\"geometry\": \"Point\",\"attributeCount\": 3,\"attributes\": [{\"attribute\": \"Address\",\"count\": 4,\"type\": \"string\",\"values\": [\"1507 N Rosa Parks Way Portland, OR 97217\",\"201 SE 12th Ave, Portland, OR 97214\",\"4637 N Albina Ave Portland, OR 97217\",\"915 SE Hawthorne Blvd. Portland, OR 97214\"]},{\"attribute\": \"Name\",\"count\": 4,\"type\": \"string\",\"values\": [\"Albina Press\",\"Arbor Lodge\",\"Lucky Labrador Brew Pub\",\"Three Friends Coffeehouse\"]},{\"attribute\": \"Notes\",\"count\": 3,\"type\": \"string\",\"values\": [\"\",\"Dog friendly\",\"usually busy, outlets on side wall only\"]}]}]}}"
}
15 changes: 15 additions & 0 deletions tile-join.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ struct stats {
double minlat, minlon, maxlat, maxlon;
};

void aprintf(std::string *buf, const char *format, ...) {
va_list ap;
char *tmp;

va_start(ap, format);
if (vasprintf(&tmp, format, ap) < 0) {
fprintf(stderr, "memory allocation failure\n");
exit(EXIT_FAILURE);
}
va_end(ap);

buf->append(tmp, strlen(tmp));
free(tmp);
}

void handle(std::string message, int z, unsigned x, unsigned y, std::map<std::string, layermap_entry> &layermap, std::vector<std::string> &header, std::map<std::string, std::vector<std::string>> &mapping, std::set<std::string> &exclude, std::set<std::string> &keep_layers, std::set<std::string> &remove_layers, int ifmatched, mvt_tile &outtile, json_object *filter) {
mvt_tile tile;
int features_added = 0;
Expand Down
24 changes: 12 additions & 12 deletions write_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,18 @@ void json_writer::json_comma_newline() {
}

void json_writer::aprintf(const char *format, ...) {
va_list ap;
char *tmp;

va_start(ap, format);
if (vasprintf(&tmp, format, ap) < 0) {
fprintf(stderr, "memory allocation failure\n");
exit(EXIT_FAILURE);
}
va_end(ap);

adds(std::string(tmp, strlen(tmp)));
free(tmp);
va_list ap;
char *tmp;

va_start(ap, format);
if (vasprintf(&tmp, format, ap) < 0) {
fprintf(stderr, "memory allocation failure\n");
exit(EXIT_FAILURE);
}
va_end(ap);

adds(std::string(tmp, strlen(tmp)));
free(tmp);
}

void json_writer::addc(char c) {
Expand Down
2 changes: 1 addition & 1 deletion write_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct json_writer {
void json_write_newline();
void json_comma_newline();

private:
private:
void json_adjust();
void aprintf(const char *format, ...);
void addc(char c);
Expand Down

0 comments on commit 3c827b1

Please sign in to comment.