Factoring out tilestats management from GeoJSON file reading

This commit is contained in:
Erica Fischer
2024-08-21 12:46:21 -07:00
parent 40bb4ff732
commit c91e07aaa6
2 changed files with 82 additions and 49 deletions
+9
View File
@@ -20,6 +20,7 @@ std::string filter;
bool preserve_input_order = false; bool preserve_input_order = false;
std::unordered_map<std::string, attribute_op> attribute_accum; std::unordered_map<std::string, attribute_op> attribute_accum;
std::vector<std::string> unidecode_data; std::vector<std::string> unidecode_data;
std::string assign_to_bins;
std::set<std::string> keep; std::set<std::string> keep;
@@ -53,6 +54,7 @@ int main(int argc, char **argv) {
{"line-simplification", required_argument, 0, 'S'}, {"line-simplification", required_argument, 0, 'S'},
{"tiny-polygon-size", required_argument, 0, 's' & 0x1F}, {"tiny-polygon-size", required_argument, 0, 's' & 0x1F},
{"source-tile", required_argument, 0, 't'}, {"source-tile", required_argument, 0, 't'},
{"assign-to-bins", required_argument, 0, 'b' & 0x1F},
{0, 0, 0, 0}, {0, 0, 0, 0},
}; };
@@ -119,6 +121,10 @@ int main(int argc, char **argv) {
simplification = atof(optarg); simplification = atof(optarg);
break; break;
case 'b' & 0x1F:
assign_to_bins = optarg;
break;
default: default:
fprintf(stderr, "Unrecognized flag -%c\n", i); fprintf(stderr, "Unrecognized flag -%c\n", i);
usage(argv); usage(argv);
@@ -128,6 +134,9 @@ int main(int argc, char **argv) {
std::vector<input_tile> its; std::vector<input_tile> its;
int nz, nx, ny; int nz, nx, ny;
if (assign_to_bins.size() != 0) {
}
if (outtile == NULL) { // single input if (outtile == NULL) { // single input
if (argc - optind != 3) { if (argc - optind != 3) {
fprintf(stderr, "Wrong number of arguments\n"); fprintf(stderr, "Wrong number of arguments\n");
+73 -49
View File
@@ -85,18 +85,11 @@ static std::vector<mvt_geometry> to_feature(drawvec &geom) {
return out; return out;
} }
// Reads from the postfilter std::vector<mvt_layer> parse_layers(FILE *fp, int z, unsigned x, unsigned y, int extent) {
std::vector<mvt_layer> parse_layers(int fd, int z, unsigned x, unsigned y, std::vector<std::map<std::string, layermap_entry>> *layermaps, size_t tiling_seg, std::vector<std::vector<std::string>> *layer_unmaps, int extent) {
std::map<std::string, mvt_layer> ret; std::map<std::string, mvt_layer> ret;
std::shared_ptr<std::string> tile_stringpool = std::make_shared<std::string>(); std::shared_ptr<std::string> tile_stringpool = std::make_shared<std::string>();
FILE *f = fdopen(fd, "r"); json_pull *jp = json_begin_file(fp);
if (f == NULL) {
perror("fdopen filter output");
exit(EXIT_OPEN);
}
json_pull *jp = json_begin_file(f);
while (1) { while (1) {
json_object *j = json_read(jp); json_object *j = json_read(jp);
if (j == NULL) { if (j == NULL) {
@@ -229,40 +222,6 @@ std::vector<mvt_layer> parse_layers(int fd, int z, unsigned x, unsigned y, std::
feature.has_id = true; feature.has_id = true;
} }
std::map<std::string, layermap_entry> &layermap = (*layermaps)[tiling_seg];
if (layermap.count(layername) == 0) {
layermap_entry lme = layermap_entry(layermap.size());
lme.minzoom = z;
lme.maxzoom = z;
layermap.insert(std::pair<std::string, layermap_entry>(layername, lme));
if (lme.id >= (*layer_unmaps)[tiling_seg].size()) {
(*layer_unmaps)[tiling_seg].resize(lme.id + 1);
(*layer_unmaps)[tiling_seg][lme.id] = layername;
}
}
auto ts = layermap.find(layername);
if (ts == layermap.end()) {
fprintf(stderr, "Internal error: layer %s not found\n", layername.c_str());
exit(EXIT_IMPOSSIBLE);
}
if (z < ts->second.minzoom) {
ts->second.minzoom = z;
}
if (z > ts->second.maxzoom) {
ts->second.maxzoom = z;
}
if (feature.type == mvt_point) {
ts->second.points++;
} else if (feature.type == mvt_linestring) {
ts->second.lines++;
} else if (feature.type == mvt_polygon) {
ts->second.polygons++;
}
for (size_t i = 0; i < properties->value.object.length; i++) { for (size_t i = 0; i < properties->value.object.length; i++) {
serial_val sv = stringify_value(properties->value.object.values[i], "Filter output", jp->line, j); serial_val sv = stringify_value(properties->value.object.values[i], "Filter output", jp->line, j);
@@ -272,8 +231,6 @@ std::vector<mvt_layer> parse_layers(int fd, int z, unsigned x, unsigned y, std::
if (sv.type != mvt_null) { if (sv.type != mvt_null) {
mvt_value v = stringified_to_mvt_value(sv.type, sv.s.c_str(), tile_stringpool); mvt_value v = stringified_to_mvt_value(sv.type, sv.s.c_str(), tile_stringpool);
l->second.tag(feature, std::string(properties->value.object.keys[i]->value.string.string), v); l->second.tag(feature, std::string(properties->value.object.keys[i]->value.string.string), v);
add_to_tilestats(ts->second.tilestats, std::string(properties->value.object.keys[i]->value.string.string), sv);
} }
} }
@@ -284,18 +241,85 @@ std::vector<mvt_layer> parse_layers(int fd, int z, unsigned x, unsigned y, std::
} }
json_end(jp); json_end(jp);
if (fclose(f) != 0) {
perror("fclose postfilter output");
exit(EXIT_CLOSE);
}
std::vector<mvt_layer> final; std::vector<mvt_layer> final;
for (auto a : ret) { for (auto a : ret) {
final.push_back(a.second); final.push_back(a.second);
} }
return final; return final;
} }
// Reads from the postfilter
std::vector<mvt_layer> parse_layers(int fd, int z, unsigned x, unsigned y, std::vector<std::map<std::string, layermap_entry>> *layermaps, size_t tiling_seg, std::vector<std::vector<std::string>> *layer_unmaps, int extent) {
FILE *f = fdopen(fd, "r");
if (f == NULL) {
perror("fdopen filter output");
exit(EXIT_OPEN);
}
std::vector<mvt_layer> out = parse_layers(f, z, x, y, extent);
if (fclose(f) != 0) {
perror("fclose postfilter output");
exit(EXIT_CLOSE);
}
for (auto const &layer : out) {
std::string layername = layer.name;
std::map<std::string, layermap_entry> &layermap = (*layermaps)[tiling_seg];
if (layermap.count(layername) == 0) {
layermap_entry lme = layermap_entry(layermap.size());
lme.minzoom = z;
lme.maxzoom = z;
layermap.insert(std::pair<std::string, layermap_entry>(layername, lme));
if (lme.id >= (*layer_unmaps)[tiling_seg].size()) {
(*layer_unmaps)[tiling_seg].resize(lme.id + 1);
(*layer_unmaps)[tiling_seg][lme.id] = layername;
}
}
auto ts = layermap.find(layername);
if (ts == layermap.end()) {
fprintf(stderr, "Internal error: layer %s not found\n", layername.c_str());
exit(EXIT_IMPOSSIBLE);
}
if (z < ts->second.minzoom) {
ts->second.minzoom = z;
}
if (z > ts->second.maxzoom) {
ts->second.maxzoom = z;
}
for (auto const &feature : layer.features) {
if (feature.type == mvt_point) {
ts->second.points++;
} else if (feature.type == mvt_linestring) {
ts->second.lines++;
} else if (feature.type == mvt_polygon) {
ts->second.polygons++;
}
for (size_t i = 0; i + 1 < feature.tags.size(); i += 2) {
const std::string &key = layer.keys[feature.tags[i]];
const mvt_value &val = layer.values[feature.tags[i + 1]];
// Nulls can be excluded here because this is the postfilter
// and it is nearly time to create the vector representation
if (val.type != mvt_null) {
add_to_tilestats(ts->second.tilestats, key, mvt_value_to_serial_val(val));
}
}
}
}
return out;
}
// Reads from the prefilter // Reads from the prefilter
serial_feature parse_feature(json_pull *jp, int z, unsigned x, unsigned y, std::vector<std::map<std::string, layermap_entry>> *layermaps, size_t tiling_seg, std::vector<std::vector<std::string>> *layer_unmaps, bool postfilter) { serial_feature parse_feature(json_pull *jp, int z, unsigned x, unsigned y, std::vector<std::map<std::string, layermap_entry>> *layermaps, size_t tiling_seg, std::vector<std::vector<std::string>> *layer_unmaps, bool postfilter) {
serial_feature sf; serial_feature sf;