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");
+57 -33
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,6 +222,52 @@ std::vector<mvt_layer> parse_layers(int fd, int z, unsigned x, unsigned y, std::
feature.has_id = true; feature.has_id = true;
} }
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);
// Nulls can be excluded here because this is the postfilter
// and it is nearly time to create the vector representation
if (sv.type != mvt_null) {
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.features.push_back(feature);
}
json_free(j);
}
json_end(jp);
std::vector<mvt_layer> final;
for (auto a : ret) {
final.push_back(a.second);
}
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]; std::map<std::string, layermap_entry> &layermap = (*layermaps)[tiling_seg];
if (layermap.count(layername) == 0) { if (layermap.count(layername) == 0) {
layermap_entry lme = layermap_entry(layermap.size()); layermap_entry lme = layermap_entry(layermap.size());
@@ -255,6 +294,7 @@ std::vector<mvt_layer> parse_layers(int fd, int z, unsigned x, unsigned y, std::
ts->second.maxzoom = z; ts->second.maxzoom = z;
} }
for (auto const &feature : layer.features) {
if (feature.type == mvt_point) { if (feature.type == mvt_point) {
ts->second.points++; ts->second.points++;
} else if (feature.type == mvt_linestring) { } else if (feature.type == mvt_linestring) {
@@ -263,37 +303,21 @@ std::vector<mvt_layer> parse_layers(int fd, int z, unsigned x, unsigned y, std::
ts->second.polygons++; ts->second.polygons++;
} }
for (size_t i = 0; i < properties->value.object.length; i++) { for (size_t i = 0; i + 1 < feature.tags.size(); i += 2) {
serial_val sv = stringify_value(properties->value.object.values[i], "Filter output", jp->line, j); 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 // Nulls can be excluded here because this is the postfilter
// and it is nearly time to create the vector representation // and it is nearly time to create the vector representation
if (sv.type != mvt_null) { if (val.type != mvt_null) {
mvt_value v = stringified_to_mvt_value(sv.type, sv.s.c_str(), tile_stringpool); add_to_tilestats(ts->second.tilestats, key, mvt_value_to_serial_val(val));
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);
} }
} }
l->second.features.push_back(feature); return out;
}
json_free(j);
}
json_end(jp);
if (fclose(f) != 0) {
perror("fclose postfilter output");
exit(EXIT_CLOSE);
}
std::vector<mvt_layer> final;
for (auto a : ret) {
final.push_back(a.second);
}
return final;
} }
// Reads from the prefilter // Reads from the prefilter