From f7593b01b681e0a0da15bfa26493196e57ae43f4 Mon Sep 17 00:00:00 2001 From: Erica Fischer Date: Mon, 22 Jan 2024 15:31:18 -0800 Subject: [PATCH] Starting to factor out attribute accumulation into its own file --- Makefile | 2 +- attribute.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ attribute.hpp | 17 +++++++++++ main.cpp | 76 ++----------------------------------------------- tile.cpp | 1 + tile.hpp | 11 +------- 6 files changed, 100 insertions(+), 85 deletions(-) create mode 100644 attribute.cpp create mode 100644 attribute.hpp diff --git a/Makefile b/Makefile index 36bba20b..3e21845a 100644 --- a/Makefile +++ b/Makefile @@ -58,7 +58,7 @@ C = $(wildcard *.c) $(wildcard *.cpp) INCLUDES = -I/usr/local/include -I. LIBS = -L/usr/local/lib -tippecanoe: geojson.o jsonpull/jsonpull.o tile.o pool.o mbtiles.o geometry.o projection.o memfile.o mvt.o serial.o main.o text.o dirtiles.o pmtiles_file.o plugin.o read_json.o write_json.o geobuf.o flatgeobuf.o evaluator.o geocsv.o csv.o geojson-loop.o json_logger.o visvalingam.o compression.o clip.o sort.o +tippecanoe: geojson.o jsonpull/jsonpull.o tile.o pool.o mbtiles.o geometry.o projection.o memfile.o mvt.o serial.o main.o text.o dirtiles.o pmtiles_file.o plugin.o read_json.o write_json.o geobuf.o flatgeobuf.o evaluator.o geocsv.o csv.o geojson-loop.o json_logger.o visvalingam.o compression.o clip.o sort.o attribute.o $(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread tippecanoe-enumerate: enumerate.o diff --git a/attribute.cpp b/attribute.cpp new file mode 100644 index 00000000..2a01c2b2 --- /dev/null +++ b/attribute.cpp @@ -0,0 +1,78 @@ +#include +#include +#include "attribute.hpp" +#include "errors.hpp" +#include "jsonpull/jsonpull.h" + +void set_attribute_accum(std::map &attribute_accum, std::string name, std::string type) { + attribute_op t; + + if (type == "sum") { + t = op_sum; + } else if (type == "product") { + t = op_product; + } else if (type == "mean") { + t = op_mean; + } else if (type == "max") { + t = op_max; + } else if (type == "min") { + t = op_min; + } else if (type == "concat") { + t = op_concat; + } else if (type == "comma") { + t = op_comma; + } else { + fprintf(stderr, "Attribute method (%s) must be sum, product, mean, max, min, concat, or comma\n", type.c_str()); + exit(EXIT_ARGS); + } + + attribute_accum.insert(std::pair(name, t)); +} + +void set_attribute_accum(std::map &attribute_accum, const char *arg, char **argv) { + if (*arg == '{') { + json_pull *jp = json_begin_string(arg); + json_object *o = json_read_tree(jp); + + if (o == NULL) { + fprintf(stderr, "%s: -E%s: %s\n", *argv, arg, jp->error); + exit(EXIT_JSON); + } + + if (o->type != JSON_HASH) { + fprintf(stderr, "%s: -E%s: not a JSON object\n", *argv, arg); + exit(EXIT_JSON); + } + + for (size_t i = 0; i < o->value.object.length; i++) { + json_object *k = o->value.object.keys[i]; + json_object *v = o->value.object.values[i]; + + if (k->type != JSON_STRING) { + fprintf(stderr, "%s: -E%s: key %zu not a string\n", *argv, arg, i); + exit(EXIT_JSON); + } + if (v->type != JSON_STRING) { + fprintf(stderr, "%s: -E%s: value %zu not a string\n", *argv, arg, i); + exit(EXIT_JSON); + } + + set_attribute_accum(attribute_accum, k->value.string.string, v->value.string.string); + } + + json_free(o); + json_end(jp); + return; + } + + const char *s = strchr(arg, ':'); + if (s == NULL) { + fprintf(stderr, "-E%s option must be in the form -Ename:method\n", arg); + exit(EXIT_ARGS); + } + + std::string name = std::string(arg, s - arg); + std::string type = std::string(s + 1); + + set_attribute_accum(attribute_accum, name, type); +} diff --git a/attribute.hpp b/attribute.hpp new file mode 100644 index 00000000..1dd206e0 --- /dev/null +++ b/attribute.hpp @@ -0,0 +1,17 @@ +#ifndef ATTRIBUTE_HPP +#define ATTRIBUTE_HPP + +enum attribute_op { + op_sum, + op_product, + op_mean, + op_concat, + op_comma, + op_max, + op_min, +}; + +void set_attribute_accum(std::map &attribute_accum, std::string name, std::string type); +void set_attribute_accum(std::map &attribute_accum, const char *arg, char **argv); + +#endif diff --git a/main.cpp b/main.cpp index 038495e0..a359bacb 100644 --- a/main.cpp +++ b/main.cpp @@ -66,6 +66,7 @@ #include "errors.hpp" #include "read_json.hpp" #include "sort.hpp" +#include "attribute.hpp" static int low_detail = 12; static int full_detail = -1; @@ -2871,79 +2872,6 @@ void set_attribute_type(std::map &attribute_types, const char attribute_types.insert(std::pair(name, t)); } -void set_attribute_accum(std::map &attribute_accum, std::string name, std::string type) { - attribute_op t; - - if (type == "sum") { - t = op_sum; - } else if (type == "product") { - t = op_product; - } else if (type == "mean") { - t = op_mean; - } else if (type == "max") { - t = op_max; - } else if (type == "min") { - t = op_min; - } else if (type == "concat") { - t = op_concat; - } else if (type == "comma") { - t = op_comma; - } else { - fprintf(stderr, "Attribute method (%s) must be sum, product, mean, max, min, concat, or comma\n", type.c_str()); - exit(EXIT_ARGS); - } - - attribute_accum.insert(std::pair(name, t)); -} - -void set_attribute_accum(std::map &attribute_accum, const char *arg) { - if (*arg == '{') { - json_pull *jp = json_begin_string(arg); - json_object *o = json_read_tree(jp); - - if (o == NULL) { - fprintf(stderr, "%s: -E%s: %s\n", *av, arg, jp->error); - exit(EXIT_JSON); - } - - if (o->type != JSON_HASH) { - fprintf(stderr, "%s: -E%s: not a JSON object\n", *av, arg); - exit(EXIT_JSON); - } - - for (size_t i = 0; i < o->value.object.length; i++) { - json_object *k = o->value.object.keys[i]; - json_object *v = o->value.object.values[i]; - - if (k->type != JSON_STRING) { - fprintf(stderr, "%s: -E%s: key %zu not a string\n", *av, arg, i); - exit(EXIT_JSON); - } - if (v->type != JSON_STRING) { - fprintf(stderr, "%s: -E%s: value %zu not a string\n", *av, arg, i); - exit(EXIT_JSON); - } - - set_attribute_accum(attribute_accum, k->value.string.string, v->value.string.string); - } - - json_free(o); - json_end(jp); - return; - } - - const char *s = strchr(arg, ':'); - if (s == NULL) { - fprintf(stderr, "-E%s option must be in the form -Ename:method\n", arg); - exit(EXIT_ARGS); - } - - std::string name = std::string(arg, s - arg); - std::string type = std::string(s + 1); - - set_attribute_accum(attribute_accum, name, type); -} - void set_attribute_value(const char *arg) { if (*arg == '{') { json_pull *jp = json_begin_string(arg); @@ -3694,7 +3622,7 @@ int main(int argc, char **argv) { break; case 'E': - set_attribute_accum(attribute_accum, optarg); + set_attribute_accum(attribute_accum, optarg, argv); break; default: { diff --git a/tile.cpp b/tile.cpp index bdd9df56..99ab65b9 100644 --- a/tile.cpp +++ b/tile.cpp @@ -43,6 +43,7 @@ #include "errors.hpp" #include "compression.hpp" #include "protozero/varint.hpp" +#include "attribute.hpp" extern "C" { #include "jsonpull/jsonpull.h" diff --git a/tile.hpp b/tile.hpp index cecff006..6155df0b 100644 --- a/tile.hpp +++ b/tile.hpp @@ -8,18 +8,9 @@ #include #include "mbtiles.hpp" #include "serial.hpp" +#include "attribute.hpp" #include "jsonpull/jsonpull.h" -enum attribute_op { - op_sum, - op_product, - op_mean, - op_concat, - op_comma, - op_max, - op_min, -}; - struct atomic_strategy { std::atomic dropped_by_rate; std::atomic dropped_by_gamma;