Fix tilestats with truncated strings. Trim tilestats if TileJSON is huge

This commit is contained in:
Eric Fischer
2017-08-15 11:23:03 -07:00
parent 5fc261020c
commit 205c28bb64
3 changed files with 1954 additions and 49 deletions
+25 -7
View File
@@ -137,7 +137,7 @@ bool type_and_string::operator!=(const type_and_string &o) const {
return false;
}
std::string tilestats(std::map<std::string, layermap_entry> const &layermap1) {
std::string tilestats(std::map<std::string, layermap_entry> const &layermap1, size_t elements) {
// Consolidate layers/attributes whose names are truncated
std::vector<std::map<std::string, layermap_entry>> lmv;
lmv.push_back(layermap1);
@@ -192,7 +192,7 @@ std::string tilestats(std::map<std::string, layermap_entry> const &layermap1) {
size_t attrs = 0;
for (auto attribute : layer.second.file_keys) {
if (attrs == 100) {
if (attrs == elements) {
break;
}
if (attrs != 0) {
@@ -240,21 +240,27 @@ std::string tilestats(std::map<std::string, layermap_entry> const &layermap1) {
size_t vals = 0;
for (auto value : attribute.second.sample_values) {
if (vals == 100) {
if (vals == elements) {
break;
}
if (value.type == mvt_double || value.type == mvt_bool) {
if (vals != 0) {
out.append(",\n");
}
vals++;
if (value.type == mvt_double || value.type == mvt_bool) {
out.append("\t\t\t\t\t\t");
out.append(value.string);
} else {
std::string trunc = truncate16(value.string, 256);
if (trunc.size() == value.string.size()) {
if (vals != 0) {
out.append(",\n");
}
vals++;
out.append("\t\t\t\t\t\t\"");
quote(out, value.string.c_str());
out.append("\"");
@@ -407,7 +413,11 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *outdir, const char *fnam
sqlite3_free(sql);
if (vector) {
std::string buf("{");
size_t elements = 100;
std::string buf;
while (1) {
buf = "{";
aprintf(&buf, "\"vector_layers\": [ ");
std::vector<std::string> lnames;
@@ -457,12 +467,20 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *outdir, const char *fnam
aprintf(&buf, " ]");
if (do_tilestats) {
aprintf(&buf, ",\"tilestats\": %s", tilestats(layermap).c_str());
if (do_tilestats && elements > 0) {
aprintf(&buf, ",\"tilestats\": %s", tilestats(layermap, elements).c_str());
}
aprintf(&buf, "}");
if (buf.size() >= 60 * 1024 && elements > 0) {
elements = elements / 2;
continue;
} else {
break;
}
}
sql = sqlite3_mprintf("INSERT INTO metadata (name, value) VALUES ('json', %Q);", buf.c_str());
if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
fprintf(stderr, "set json: %s\n", err);
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long