diff --git a/src/command-line-parser.cpp b/src/command-line-parser.cpp index 4f6b575da..3430099d9 100644 --- a/src/command-line-parser.cpp +++ b/src/command-line-parser.cpp @@ -31,7 +31,9 @@ #include // for number of threads #include -static osmium::Box parse_bbox_param(std::string const &arg) +namespace { + +osmium::Box parse_bbox_param(std::string const &arg) { double minx = NAN; double maxx = NAN; @@ -61,9 +63,8 @@ static osmium::Box parse_bbox_param(std::string const &arg) return osmium::Box{minx, miny, maxx, maxy}; } -static void parse_expire_tiles_param(char const *arg, - uint32_t *expire_tiles_zoom_min, - uint32_t *expire_tiles_zoom) +void parse_expire_tiles_param(char const *arg, uint32_t *expire_tiles_zoom_min, + uint32_t *expire_tiles_zoom) { if (!arg || arg[0] == '-') { throw std::runtime_error{"Missing argument for option --expire-tiles." @@ -110,20 +111,7 @@ static void parse_expire_tiles_param(char const *arg, " tile expiry must be separated by '-'."}; } -void print_version() -{ - fmt::print(stderr, "Build: {}\n", get_build_type()); - fmt::print(stderr, "Compiled using the following library versions:\n"); - fmt::print(stderr, "Libosmium {}\n", LIBOSMIUM_VERSION_STRING); - fmt::print(stderr, "Proj {}\n", get_proj_version()); -#ifdef HAVE_LUAJIT - fmt::print(stderr, "{} ({})\n", LUA_RELEASE, LUAJIT_VERSION); -#else - fmt::print(stderr, "{}\n", LUA_RELEASE); -#endif -} - -static void check_options_non_slim(CLI::App const &app) +void check_options_non_slim(CLI::App const &app) { std::vector const slim_options = { "--flat-nodes", "--middle-schema", @@ -138,7 +126,7 @@ static void check_options_non_slim(CLI::App const &app) } } -static void check_options_output_flex(CLI::App const &app) +void check_options_output_flex(CLI::App const &app) { auto const ignored_options = app.get_options([](CLI::Option const *option) { return option->get_group() == "Pgsql output options" || @@ -154,7 +142,7 @@ static void check_options_output_flex(CLI::App const &app) } } -static void check_options_output_null(CLI::App const &app) +void check_options_output_null(CLI::App const &app) { auto const ignored_options = app.get_options([](CLI::Option const *option) { return option->get_group() == "Pgsql output options" || @@ -172,7 +160,7 @@ static void check_options_output_null(CLI::App const &app) } } -static void check_options_output_pgsql(CLI::App const &app, options_t *options) +void check_options_output_pgsql(CLI::App const &app, options_t *options) { if (app.count("--latlong") + app.count("--merc") + app.count("--proj") > 1) { @@ -196,7 +184,7 @@ static void check_options_output_pgsql(CLI::App const &app, options_t *options) } } -static void check_options(options_t *options) +void check_options(options_t *options) { if (options->append && !options->slim) { throw std::runtime_error{"--append can only be used with slim mode!"}; @@ -223,7 +211,7 @@ static void check_options(options_t *options) } } -static void check_options_expire(options_t *options) { +void check_options_expire(options_t *options) { // Zoom level 31 is the technical limit because we use 32-bit integers for // the x and y index of a tile ID. if (options->expire_tiles_zoom_min > 31) { @@ -246,6 +234,25 @@ static void check_options_expire(options_t *options) { } } +} // anonymous namespace + +void print_version() +{ + fmt::print(stderr, "Build: {}\n", get_build_type()); + fmt::print(stderr, "Compiled using the following library versions:\n"); + fmt::print(stderr, "Libosmium {}\n", LIBOSMIUM_VERSION_STRING); + fmt::print(stderr, "Proj {}\n", get_proj_version()); +#ifdef HAVE_LUA +#ifdef HAVE_LUAJIT + fmt::print(stderr, "{} ({})\n", LUA_RELEASE, LUAJIT_VERSION); +#else + fmt::print(stderr, "{}\n", LUA_RELEASE); +#endif +#else + fmt::print(stderr, "Lua support not included\n"); +#endif +} + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) options_t parse_command_line(int argc, char *argv[]) { diff --git a/src/flex-lua-expire-output.cpp b/src/flex-lua-expire-output.cpp index cc26a2cf2..ab2188cc6 100644 --- a/src/flex-lua-expire-output.cpp +++ b/src/flex-lua-expire-output.cpp @@ -17,7 +17,9 @@ #include -static expire_output_t & +namespace { + +expire_output_t & create_expire_output(lua_State *lua_state, std::string const &default_schema, std::vector *expire_outputs) { @@ -65,6 +67,8 @@ create_expire_output(lua_State *lua_state, std::string const &default_schema, return new_expire_output; } +} // anonymous namespace + int setup_flex_expire_output(lua_State *lua_state, std::string const &default_schema, std::vector *expire_outputs) diff --git a/src/flex-lua-geom.cpp b/src/flex-lua-geom.cpp index 0a4a5f538..47accbaed 100644 --- a/src/flex-lua-geom.cpp +++ b/src/flex-lua-geom.cpp @@ -50,10 +50,12 @@ int geom_gc(lua_State *lua_state) noexcept return 0; } +namespace { + // The following functions are called when their counterparts in Lua are // called on geometry objects. -static int geom_area(lua_State *lua_state) +int geom_area(lua_State *lua_state) { auto const *const input_geometry = unpack_geometry(lua_state); @@ -66,7 +68,7 @@ static int geom_area(lua_State *lua_state) return 1; } -static int geom_spherical_area(lua_State *lua_state) +int geom_spherical_area(lua_State *lua_state) { auto const *const input_geometry = unpack_geometry(lua_state); @@ -84,7 +86,7 @@ static int geom_spherical_area(lua_State *lua_state) return 1; } -static int geom_length(lua_State *lua_state) +int geom_length(lua_State *lua_state) { auto const *const input_geometry = unpack_geometry(lua_state); try { @@ -96,7 +98,7 @@ static int geom_length(lua_State *lua_state) return 1; } -static int geom_centroid(lua_State *lua_state) +int geom_centroid(lua_State *lua_state) { auto const *const input_geometry = unpack_geometry(lua_state); @@ -110,7 +112,7 @@ static int geom_centroid(lua_State *lua_state) return 1; } -static int geom_geometry_n(lua_State *lua_state) +int geom_geometry_n(lua_State *lua_state) { auto const *const input_geometry = unpack_geometry(lua_state); auto const index = static_cast(luaL_checkinteger(lua_state, 2)); @@ -125,7 +127,7 @@ static int geom_geometry_n(lua_State *lua_state) return 1; } -static int geom_geometry_type(lua_State *lua_state) +int geom_geometry_type(lua_State *lua_state) { auto const *const input_geometry = unpack_geometry(lua_state); @@ -139,14 +141,14 @@ static int geom_geometry_type(lua_State *lua_state) return 1; } -static int geom_is_null(lua_State *lua_state) +int geom_is_null(lua_State *lua_state) { auto const *const input_geometry = unpack_geometry(lua_state); lua_pushboolean(lua_state, input_geometry->is_null()); return 1; } -static int geom_reverse(lua_State *lua_state) +int geom_reverse(lua_State *lua_state) { auto const *const input_geometry = unpack_geometry(lua_state); @@ -160,7 +162,7 @@ static int geom_reverse(lua_State *lua_state) return 1; } -static int geom_line_merge(lua_State *lua_state) +int geom_line_merge(lua_State *lua_state) { auto const *const input_geometry = unpack_geometry(lua_state); @@ -174,7 +176,7 @@ static int geom_line_merge(lua_State *lua_state) return 1; } -static int geom_num_geometries(lua_State *lua_state) +int geom_num_geometries(lua_State *lua_state) { auto const *const input_geometry = unpack_geometry(lua_state); lua_pushinteger(lua_state, @@ -182,7 +184,7 @@ static int geom_num_geometries(lua_State *lua_state) return 1; } -static int geom_pole_of_inaccessibility(lua_State *lua_state) +int geom_pole_of_inaccessibility(lua_State *lua_state) { auto const *const input_geometry = unpack_geometry(lua_state); @@ -210,7 +212,7 @@ static int geom_pole_of_inaccessibility(lua_State *lua_state) return 1; } -static int geom_segmentize(lua_State *lua_state) +int geom_segmentize(lua_State *lua_state) { auto const *const input_geometry = unpack_geometry(lua_state); double const max_segment_length = luaL_checknumber(lua_state, 2); @@ -225,7 +227,7 @@ static int geom_segmentize(lua_State *lua_state) return 1; } -static int geom_simplify(lua_State *lua_state) +int geom_simplify(lua_State *lua_state) { auto const *const input_geometry = unpack_geometry(lua_state); double const tolerance = luaL_checknumber(lua_state, 2); @@ -240,7 +242,7 @@ static int geom_simplify(lua_State *lua_state) return 1; } -static int geom_srid(lua_State *lua_state) +int geom_srid(lua_State *lua_state) { auto const *const input_geometry = unpack_geometry(lua_state); lua_pushinteger(lua_state, @@ -251,12 +253,12 @@ static int geom_srid(lua_State *lua_state) // XXX Implementation for Lua __tostring function on geometries. Currently // just returns the type as string. This could be improved, for instance by // showing a WKT representation of the geometry. -static int geom_tostring(lua_State *lua_state) +int geom_tostring(lua_State *lua_state) { return geom_geometry_type(lua_state); } -static int geom_transform(lua_State *lua_state) +int geom_transform(lua_State *lua_state) { auto const *const input_geometry = unpack_geometry(lua_state); auto const srid = static_cast(luaL_checkinteger(lua_state, 2)); @@ -275,6 +277,8 @@ static int geom_transform(lua_State *lua_state) return 1; } +} // anonymous namespace + void init_geometry_class(lua_State *lua_state) { lua_getglobal(lua_state, "osm2pgsql"); diff --git a/src/flex-lua-index.cpp b/src/flex-lua-index.cpp index 3a396a7ec..085b360f5 100644 --- a/src/flex-lua-index.cpp +++ b/src/flex-lua-index.cpp @@ -16,9 +16,11 @@ #include #include -static void check_and_add_column(flex_table_t const &table, - std::vector *columns, - char const *column_name) +namespace { + +void check_and_add_column(flex_table_t const &table, + std::vector *columns, + char const *column_name) { auto const *column = util::find_by_name(table.columns(), column_name); if (!column) { @@ -28,9 +30,9 @@ static void check_and_add_column(flex_table_t const &table, columns->emplace_back(column_name); } -static void check_and_add_columns(flex_table_t const &table, - std::vector *columns, - lua_State *lua_state) +void check_and_add_columns(flex_table_t const &table, + std::vector *columns, + lua_State *lua_state) { if (!luaX_is_array(lua_state)) { throw std::runtime_error{ @@ -46,6 +48,8 @@ static void check_and_add_columns(flex_table_t const &table, }); } +} // anonymous namespace + void flex_lua_setup_index(lua_State *lua_state, flex_table_t *table) { // get method diff --git a/src/flex-lua-table.cpp b/src/flex-lua-table.cpp index 242463d09..30b7a64bd 100644 --- a/src/flex-lua-table.cpp +++ b/src/flex-lua-table.cpp @@ -18,7 +18,9 @@ #include -static void check_tablespace(std::string const &tablespace) +namespace { + +void check_tablespace(std::string const &tablespace) { if (!has_tablespace(tablespace)) { throw fmt_error( @@ -28,9 +30,9 @@ static void check_tablespace(std::string const &tablespace) } } -static flex_table_t &create_flex_table(lua_State *lua_state, - std::string const &default_schema, - std::vector *tables) +flex_table_t &create_flex_table(lua_State *lua_state, + std::string const &default_schema, + std::vector *tables) { std::string const table_name = luaX_get_table_string(lua_state, "name", -1, "The table"); @@ -101,7 +103,7 @@ static flex_table_t &create_flex_table(lua_State *lua_state, return new_table; } -static void parse_create_index(lua_State *lua_state, flex_table_t *table) +void parse_create_index(lua_State *lua_state, flex_table_t *table) { std::string const create_index = luaX_get_table_string( lua_state, "create_index", -1, "The ids field", "auto"); @@ -117,8 +119,7 @@ static void parse_create_index(lua_State *lua_state, flex_table_t *table) } } -static void setup_flex_table_id_columns(lua_State *lua_state, - flex_table_t *table) +void setup_flex_table_id_columns(lua_State *lua_state, flex_table_t *table) { assert(lua_state); assert(table); @@ -180,8 +181,8 @@ static void setup_flex_table_id_columns(lua_State *lua_state, lua_pop(lua_state, 1); // "ids" } -static std::size_t idx_from_userdata(lua_State *lua_state, int idx, - std::size_t expire_outputs_size) +std::size_t idx_from_userdata(lua_State *lua_state, int idx, + std::size_t expire_outputs_size) { void const *const user_data = lua_touserdata(lua_state, idx); @@ -202,10 +203,10 @@ static std::size_t idx_from_userdata(lua_State *lua_state, int idx, return eo; } -static void parse_and_set_expire_options(lua_State *lua_state, - flex_table_column_t *column, - std::size_t expire_outputs_size, - bool append_mode) +void parse_and_set_expire_options(lua_State *lua_state, + flex_table_column_t *column, + std::size_t expire_outputs_size, + bool append_mode) { auto const type = lua_type(lua_state, -1); @@ -300,10 +301,9 @@ static void parse_and_set_expire_options(lua_State *lua_state, }); } -static void -setup_flex_table_columns(lua_State *lua_state, flex_table_t *table, - std::vector *expire_outputs, - bool append_mode) +void setup_flex_table_columns(lua_State *lua_state, flex_table_t *table, + std::vector *expire_outputs, + bool append_mode) { assert(lua_state); assert(table); @@ -369,8 +369,8 @@ setup_flex_table_columns(lua_State *lua_state, flex_table_t *table, lua_pop(lua_state, 1); // "columns" } -static void setup_flex_table_indexes(lua_State *lua_state, flex_table_t *table, - bool updatable) +void setup_flex_table_indexes(lua_State *lua_state, flex_table_t *table, + bool updatable) { assert(lua_state); assert(table); @@ -413,6 +413,8 @@ static void setup_flex_table_indexes(lua_State *lua_state, flex_table_t *table, lua_pop(lua_state, 1); // "indexes" } +} // anonymous namespace + int setup_flex_table(lua_State *lua_state, std::vector *tables, std::vector *expire_outputs, std::string const &default_schema, bool updatable, diff --git a/src/flex-table-column.cpp b/src/flex-table-column.cpp index 54aa984c3..59ecd2b6a 100644 --- a/src/flex-table-column.cpp +++ b/src/flex-table-column.cpp @@ -19,6 +19,8 @@ #include #include +namespace { + struct column_type_lookup { char const *m_name; @@ -54,7 +56,7 @@ static std::vector const column_types = { {"id_type", table_column_type::id_type}, {"id_num", table_column_type::id_num}}}; -static table_column_type get_column_type_from_string(std::string const &type) +table_column_type get_column_type_from_string(std::string const &type) { auto const *column_type = util::find_by_name(column_types, type); if (!column_type) { @@ -64,7 +66,7 @@ static table_column_type get_column_type_from_string(std::string const &type) return column_type->type; } -static std::string lowercase(std::string const &str) +std::string lowercase(std::string const &str) { std::string result; @@ -76,6 +78,8 @@ static std::string lowercase(std::string const &str) return result; } +} // anonymous namespace + flex_table_column_t::flex_table_column_t(std::string name, std::string const &type, std::string sql_type) diff --git a/src/flex-table.cpp b/src/flex-table.cpp index 82114f59a..80a2195b6 100644 --- a/src/flex-table.cpp +++ b/src/flex-table.cpp @@ -255,8 +255,10 @@ void flex_table_t::analyze(pg_conn_t const &db_connection) const analyze_table(db_connection, schema(), name()); } -static void enable_check_trigger(pg_conn_t const &db_connection, - flex_table_t const &table) +namespace { + +void enable_check_trigger(pg_conn_t const &db_connection, + flex_table_t const &table) { std::string checks; @@ -279,6 +281,8 @@ static void enable_check_trigger(pg_conn_t const &db_connection, checks); } +} // anonymous namespace + void table_connection_t::start(pg_conn_t const &db_connection, bool append) const { diff --git a/src/flex-write.cpp b/src/flex-write.cpp index 376e75537..96cd2745e 100644 --- a/src/flex-write.cpp +++ b/src/flex-write.cpp @@ -22,7 +22,9 @@ #include #include -static int sgn(double val) noexcept +namespace { + +int sgn(double val) noexcept { if (val > 0) { return 1; @@ -33,8 +35,8 @@ static int sgn(double val) noexcept return 0; } -static void write_null(db_copy_mgr_t *copy_mgr, - flex_table_column_t const &column) +void write_null(db_copy_mgr_t *copy_mgr, + flex_table_column_t const &column) { if (column.not_null()) { throw not_null_exception{ @@ -45,8 +47,8 @@ static void write_null(db_copy_mgr_t *copy_mgr, copy_mgr->add_null_column(); } -static void write_boolean(db_copy_mgr_t *copy_mgr, - flex_table_column_t const &column, char const *str) +void write_boolean(db_copy_mgr_t *copy_mgr, + flex_table_column_t const &column, char const *str) { if ((std::strcmp(str, "yes") == 0) || (std::strcmp(str, "true") == 0) || std::strcmp(str, "1") == 0) { @@ -63,9 +65,8 @@ static void write_boolean(db_copy_mgr_t *copy_mgr, write_null(copy_mgr, column); } -static void -write_direction(db_copy_mgr_t *copy_mgr, - flex_table_column_t const &column, char const *str) +void write_direction(db_copy_mgr_t *copy_mgr, + flex_table_column_t const &column, char const *str) { if ((std::strcmp(str, "yes") == 0) || (std::strcmp(str, "1") == 0)) { copy_mgr->add_column(1); @@ -112,8 +113,8 @@ void write_integer(db_copy_mgr_t *copy_mgr, write_null(copy_mgr, column); } -static void write_double(db_copy_mgr_t *copy_mgr, - flex_table_column_t const &column, char const *str) +void write_double(db_copy_mgr_t *copy_mgr, + flex_table_column_t const &column, char const *str) { if (*str == '\0') { write_null(copy_mgr, column); @@ -133,11 +134,11 @@ static void write_double(db_copy_mgr_t *copy_mgr, using table_register_type = std::vector; -static void write_json(json_writer_t *writer, lua_State *lua_state, - table_register_type *tables); +void write_json(json_writer_t *writer, lua_State *lua_state, + table_register_type *tables); -static void write_json_table(json_writer_t *writer, lua_State *lua_state, - table_register_type *tables) +void write_json_table(json_writer_t *writer, lua_State *lua_state, + table_register_type *tables) { void const *table_ptr = lua_topointer(lua_state, -1); assert(table_ptr); @@ -176,7 +177,7 @@ static void write_json_table(json_writer_t *writer, lua_State *lua_state, } } -static void write_json_number(json_writer_t *writer, lua_State *lua_state) +void write_json_number(json_writer_t *writer, lua_State *lua_state) { #if LUA_VERSION_NUM >= 503 int okay = 0; @@ -197,8 +198,8 @@ static void write_json_number(json_writer_t *writer, lua_State *lua_state) #endif } -static void write_json(json_writer_t *writer, lua_State *lua_state, - table_register_type *tables) +void write_json(json_writer_t *writer, lua_State *lua_state, + table_register_type *tables) { assert(writer); assert(lua_state); @@ -226,8 +227,8 @@ static void write_json(json_writer_t *writer, lua_State *lua_state, } } -static bool is_compatible(geom::geometry_t const &geom, - table_column_type type) noexcept +bool is_compatible(geom::geometry_t const &geom, + table_column_type type) noexcept { switch (type) { case table_column_type::geometry: @@ -252,6 +253,8 @@ static bool is_compatible(geom::geometry_t const &geom, return false; } +} // anonymous namespace + void flex_write_column(lua_State *lua_state, db_copy_mgr_t *copy_mgr, flex_table_column_t const &column, diff --git a/src/gen/gen-base.cpp b/src/gen/gen-base.cpp index c8737ee4c..d883eb1e2 100644 --- a/src/gen/gen-base.cpp +++ b/src/gen/gen-base.cpp @@ -82,7 +82,9 @@ std::string gen_base_t::context() return gen_name.empty() ? "" : fmt::format(" '{}'", gen_name); } -static pg_result_t dbexec_internal( +namespace { + +pg_result_t dbexec_internal( pg_conn_t const &connection, std::string const &templ, fmt::dynamic_format_arg_store const &format_store) { @@ -95,6 +97,8 @@ static pg_result_t dbexec_internal( } } +} // anonymous namespace + pg_result_t gen_base_t::dbexec(std::string const &templ) { fmt::dynamic_format_arg_store format_store; diff --git a/src/gen/gen-rivers.cpp b/src/gen/gen-rivers.cpp index 160a98330..0f7e8d1c8 100644 --- a/src/gen/gen-rivers.cpp +++ b/src/gen/gen-rivers.cpp @@ -76,10 +76,12 @@ bool operator<(geom::point_t a, edge_t const &b) noexcept return a < b.points[0]; } -static void -follow_chain_and_set_width(edge_t const &edge, std::vector *edges, - std::map const &node_order, - geom::linestring_t *seen) +namespace { + +void follow_chain_and_set_width( + edge_t const &edge, std::vector *edges, + std::map const &node_order, + geom::linestring_t *seen) { assert(!edge.points.empty()); @@ -115,9 +117,8 @@ follow_chain_and_set_width(edge_t const &edge, std::vector *edges, } } -static void assemble_edge(edge_t *edge, std::vector *edges, - std::map const &node_order) - +void assemble_edge(edge_t *edge, std::vector *edges, + std::map const &node_order) { assert(edge); assert(edges); @@ -165,6 +166,19 @@ static void assemble_edge(edge_t *edge, std::vector *edges, } } +std::string const &get_name( + std::unordered_map const &names, osmid_t id) +{ + static std::string const empty; + auto const it = names.find(id); + if (it == names.end()) { + return empty; + } + return it->second; +} + +} // anonymous namespace + /// Get some stats from source table void gen_rivers_t::get_stats() { @@ -178,17 +192,6 @@ void gen_rivers_t::get_stats() m_num_points); } -static std::string const & -get_name(std::unordered_map const &names, osmid_t id) -{ - static std::string const empty; - auto const it = names.find(id); - if (it == names.end()) { - return empty; - } - return it->second; -} - void gen_rivers_t::process() { log_gen("Calculate waterway area width..."); diff --git a/src/gen/gen-tile-builtup.cpp b/src/gen/gen-tile-builtup.cpp index 391a2dacd..e075bb8a9 100644 --- a/src/gen/gen-tile-builtup.cpp +++ b/src/gen/gen-tile-builtup.cpp @@ -21,11 +21,54 @@ #include -static std::size_t round_up(std::size_t value, std::size_t multiple) noexcept +namespace { + +std::size_t round_up(std::size_t value, std::size_t multiple) noexcept { return ((value + multiple - 1U) / multiple) * multiple; } +void save_image_to_table(pg_conn_t *connection, canvas_t const &canvas, + tile_t const &tile, double margin, + std::string const &table, char const *variant, + std::string const &table_prefix) +{ + auto const wkb = to_hex(canvas.to_wkb(tile, margin)); + + connection->exec("INSERT INTO \"{}_{}_{}\" (zoom, x, y, rast)" + " VALUES ({}, {}, {}, '{}')", + table_prefix, table, variant, tile.zoom(), tile.x(), + tile.y(), wkb); +} + +struct param_canvas_t +{ + canvas_t canvas; + std::string table; +}; + +using canvas_list_t = std::vector; + +void draw_from_db(double margin, canvas_list_t *canvas_list, pg_conn_t *conn, + tile_t const &tile) +{ + int prep = 0; + auto const box = tile.box(margin); + for (auto &cc : *canvas_list) { + std::string const statement = "get_geoms_" + fmt::to_string(prep++); + auto const result = + conn->exec_prepared(statement.c_str(), box.min_x(), box.min_y(), + box.max_x(), box.max_y()); + + for (int n = 0; n < result.num_tuples(); ++n) { + auto const geom = ewkb_to_geom(decode_hex(result.get(n, 0))); + cc.canvas.draw(geom, tile); + } + } +} + +} // anonymous namespace + gen_tile_builtup_t::gen_tile_builtup_t(pg_conn_t *connection, bool append, params_t *params) : gen_tile_t(connection, append, params), m_timer_draw(add_timer("draw")), @@ -137,49 +180,6 @@ PREPARE insert_geoms (geometry, int, int) AS } } -static void save_image_to_table(pg_conn_t *connection, canvas_t const &canvas, - tile_t const &tile, double margin, - std::string const &table, char const *variant, - std::string const &table_prefix) -{ - auto const wkb = to_hex(canvas.to_wkb(tile, margin)); - - connection->exec("INSERT INTO \"{}_{}_{}\" (zoom, x, y, rast)" - " VALUES ({}, {}, {}, '{}')", - table_prefix, table, variant, tile.zoom(), tile.x(), - tile.y(), wkb); -} - -namespace { - -struct param_canvas_t -{ - canvas_t canvas; - std::string table; -}; - -} // anonymous namespace - -using canvas_list_t = std::vector; - -static void draw_from_db(double margin, canvas_list_t *canvas_list, - pg_conn_t *conn, tile_t const &tile) -{ - int prep = 0; - auto const box = tile.box(margin); - for (auto &cc : *canvas_list) { - std::string const statement = "get_geoms_" + fmt::to_string(prep++); - auto const result = - conn->exec_prepared(statement.c_str(), box.min_x(), box.min_y(), - box.max_x(), box.max_y()); - - for (int n = 0; n < result.num_tuples(); ++n) { - auto const geom = ewkb_to_geom(decode_hex(result.get(n, 0))); - cc.canvas.draw(geom, tile); - } - } -} - void gen_tile_builtup_t::process(tile_t const &tile) { connection().exec("BEGIN"); diff --git a/src/gen/gen-tile-raster.cpp b/src/gen/gen-tile-raster.cpp index d750adf22..4771b9d6d 100644 --- a/src/gen/gen-tile-raster.cpp +++ b/src/gen/gen-tile-raster.cpp @@ -20,11 +20,59 @@ #include -static std::size_t round_up(std::size_t value, std::size_t multiple) noexcept +namespace { + +std::size_t round_up(std::size_t value, std::size_t multiple) noexcept { return ((value + multiple - 1U) / multiple) * multiple; } +struct param_canvas_t +{ + canvas_t canvas; + std::size_t points = 0; + + param_canvas_t(unsigned int image_extent, unsigned int image_buffer) + : canvas(image_extent, image_buffer) + {} +}; + +using canvas_list_t = std::unordered_map; + +void draw_from_db(double margin, unsigned int image_extent, + unsigned int image_buffer, canvas_list_t *canvas_list, + pg_conn_t *conn, tile_t const &tile) +{ + auto const box = tile.box(margin); + auto const result = conn->exec_prepared( + "get_geoms", box.min_x(), box.min_y(), box.max_x(), box.max_y()); + + for (int n = 0; n < result.num_tuples(); ++n) { + std::string param = result.get_value(n, 1); + auto const geom = ewkb_to_geom(decode_hex(result.get(n, 0))); + + auto const [it, success] = canvas_list->try_emplace( + std::move(param), image_extent, image_buffer); + + it->second.points += it->second.canvas.draw(geom, tile); + } +} + +void save_image_to_table(pg_conn_t *connection, canvas_t const &canvas, + tile_t const &tile, double margin, + std::string const ¶m, char const *variant, + std::string const &table_prefix) +{ + auto const wkb = to_hex(canvas.to_wkb(tile, margin)); + + connection->exec("INSERT INTO \"{}_{}\" (type, zoom, x, y, rast)" + " VALUES ('{}', {}, {}, {}, '{}')", + table_prefix, variant, param, tile.zoom(), tile.x(), + tile.y(), wkb); +} + +} // anonymous namespace + gen_tile_raster_union_t::gen_tile_raster_union_t(pg_conn_t *connection, bool append, params_t *params) : gen_tile_t(connection, append, params), m_timer_draw(add_timer("draw")), @@ -124,54 +172,6 @@ PREPARE insert_geoms (geometry, int, int, text) AS dbexec(prepare); } -static void save_image_to_table(pg_conn_t *connection, canvas_t const &canvas, - tile_t const &tile, double margin, - std::string const ¶m, char const *variant, - std::string const &table_prefix) -{ - auto const wkb = to_hex(canvas.to_wkb(tile, margin)); - - connection->exec("INSERT INTO \"{}_{}\" (type, zoom, x, y, rast)" - " VALUES ('{}', {}, {}, {}, '{}')", - table_prefix, variant, param, tile.zoom(), tile.x(), - tile.y(), wkb); -} - -namespace { - -struct param_canvas_t -{ - canvas_t canvas; - std::size_t points = 0; - - param_canvas_t(unsigned int image_extent, unsigned int image_buffer) - : canvas(image_extent, image_buffer) - {} -}; - -} // anonymous namespace - -using canvas_list_t = std::unordered_map; - -static void draw_from_db(double margin, unsigned int image_extent, - unsigned int image_buffer, canvas_list_t *canvas_list, - pg_conn_t *conn, tile_t const &tile) -{ - auto const box = tile.box(margin); - auto const result = conn->exec_prepared( - "get_geoms", box.min_x(), box.min_y(), box.max_x(), box.max_y()); - - for (int n = 0; n < result.num_tuples(); ++n) { - std::string param = result.get_value(n, 1); - auto const geom = ewkb_to_geom(decode_hex(result.get(n, 0))); - - auto const [it, success] = canvas_list->try_emplace( - std::move(param), image_extent, image_buffer); - - it->second.points += it->second.canvas.draw(geom, tile); - } -} - void gen_tile_raster_union_t::process(tile_t const &tile) { connection().exec("BEGIN"); diff --git a/src/gen/osm2pgsql-gen.cpp b/src/gen/osm2pgsql-gen.cpp index bcb3f0884..fb767edbe 100644 --- a/src/gen/osm2pgsql-gen.cpp +++ b/src/gen/osm2pgsql-gen.cpp @@ -58,8 +58,6 @@ #include #include -constexpr std::size_t const max_force_single_thread = 4; - // Lua can't call functions on C++ objects directly. This macro defines simple // C "trampoline" functions which are called from Lua which get the current // context (the genproc_t object) and call the respective function on the @@ -80,6 +78,10 @@ constexpr std::size_t const max_force_single_thread = 4; } \ } +namespace { + +constexpr std::size_t const max_force_single_thread = 4; + struct tile_extent { uint32_t xmin = 0; @@ -89,18 +91,18 @@ struct tile_extent bool valid = false; }; -static bool table_is_empty(pg_conn_t const &db_connection, - std::string const &schema, std::string const &table) +bool table_is_empty(pg_conn_t const &db_connection, std::string const &schema, + std::string const &table) { auto const result = db_connection.exec("SELECT 1 FROM {} LIMIT 1", qualified_name(schema, table)); return result.num_tuples() == 0; } -static tile_extent get_extent_from_db(pg_conn_t const &db_connection, - std::string const &schema, - std::string const &table, - std::string const &column, uint32_t zoom) +tile_extent get_extent_from_db(pg_conn_t const &db_connection, + std::string const &schema, + std::string const &table, + std::string const &column, uint32_t zoom) { if (table_is_empty(db_connection, schema, table)) { return {}; @@ -128,9 +130,9 @@ static tile_extent get_extent_from_db(pg_conn_t const &db_connection, osmium::geom::mercy_to_tiley(zoom, extent_ymin), true}; } -static tile_extent get_extent_from_db(pg_conn_t const &db_connection, - std::string const &default_schema, - params_t const ¶ms, uint32_t zoom) +tile_extent get_extent_from_db(pg_conn_t const &db_connection, + std::string const &default_schema, + params_t const ¶ms, uint32_t zoom) { auto const schema = params.get_string("schema", default_schema); std::string table; @@ -149,10 +151,9 @@ static tile_extent get_extent_from_db(pg_conn_t const &db_connection, return get_extent_from_db(db_connection, schema, table, geom_column, zoom); } -static void -get_tiles_from_table(pg_conn_t const &connection, std::string const &table, - uint32_t zoom, - std::vector> *tiles) +void get_tiles_from_table(pg_conn_t const &connection, std::string const &table, + uint32_t zoom, + std::vector> *tiles) { auto const result = connection.exec( R"(SELECT x, y FROM "{}" WHERE zoom = {})", table, zoom); @@ -613,6 +614,8 @@ void genproc_t::run() } } +} // anonymous namespace + // NOLINTNEXTLINE(bugprone-exception-escape) int main(int argc, char *argv[]) { diff --git a/src/gen/tracer.cpp b/src/gen/tracer.cpp index 588988ff6..027fb3415 100644 --- a/src/gen/tracer.cpp +++ b/src/gen/tracer.cpp @@ -15,6 +15,19 @@ #include #include +static_assert(sizeof(potrace_word) == 8); + +namespace { + +potrace_word bit_squeeze(potrace_word w, unsigned char const *d) noexcept +{ + return (0x80U & d[0]) | (0x40U & d[1]) | (0x20U & d[2]) | (0x10U & d[3]) | + (0x08U & d[4]) | (0x04U & d[5]) | (0x02U & d[6]) | (0x01U & d[7]) | + w; +} + +} // anonymous namespace + geom::point_t tracer_t::make_point(potrace_dpoint_t const &p) const noexcept { return {p.x - static_cast(m_buffer), @@ -46,15 +59,6 @@ void tracer_t::reset() m_num_points = 0; } -static potrace_word bit_squeeze(potrace_word w, unsigned char const *d) noexcept -{ - return (0x80U & d[0]) | (0x40U & d[1]) | (0x20U & d[2]) | (0x10U & d[3]) | - (0x08U & d[4]) | (0x04U & d[5]) | (0x02U & d[6]) | (0x01U & d[7]) | - w; -} - -static_assert(sizeof(potrace_word) == 8); - void tracer_t::prepare(canvas_t const &canvas) noexcept { std::size_t const size = canvas.size(); diff --git a/src/geom-from-osm.cpp b/src/geom-from-osm.cpp index 3d241349a..71f858f12 100644 --- a/src/geom-from-osm.cpp +++ b/src/geom-from-osm.cpp @@ -29,6 +29,8 @@ geometry_t create_point(osmium::Node const &node) return geometry_t{point_t{node.location()}}; } +namespace { + /** * Fill point list with locations from nodes list. Consecutive identical * locations are collapsed into a single point. @@ -36,8 +38,7 @@ geometry_t create_point(osmium::Node const &node) * Returns true if the result is a valid linestring, i.e. it has more than * one point. */ -static bool fill_point_list(point_list_t *list, - osmium::NodeRefList const &nodes) +bool fill_point_list(point_list_t *list, osmium::NodeRefList const &nodes) { osmium::Location last{}; @@ -53,6 +54,25 @@ static bool fill_point_list(point_list_t *list, return list->size() > 1; } +void fill_polygon(polygon_t *polygon, osmium::Area const &area, + osmium::OuterRing const &outer_ring) +{ + assert(polygon->inners().empty()); + + for (auto const &nr : outer_ring) { + polygon->outer().emplace_back(nr.location()); + } + + for (auto const &inner_ring : area.inner_rings(outer_ring)) { + auto &ring = polygon->inners().emplace_back(); + for (auto const &nr : inner_ring) { + ring.emplace_back(nr.location()); + } + } +} + +} // anonymous namespace + void create_linestring(geometry_t *geom, osmium::Way const &way) { auto &line = geom->set(); @@ -185,23 +205,6 @@ geometry_t create_multilinestring(osmium::memory::Buffer const &buffer, return geom; } -static void fill_polygon(polygon_t *polygon, osmium::Area const &area, - osmium::OuterRing const &outer_ring) -{ - assert(polygon->inners().empty()); - - for (auto const &nr : outer_ring) { - polygon->outer().emplace_back(nr.location()); - } - - for (auto const &inner_ring : area.inner_rings(outer_ring)) { - auto &ring = polygon->inners().emplace_back(); - for (auto const &nr : inner_ring) { - ring.emplace_back(nr.location()); - } - } -} - void create_multipolygon(geometry_t *geom, osmium::Relation const &relation, osmium::memory::Buffer const &buffer) { diff --git a/src/geom-functions.cpp b/src/geom-functions.cpp index 840ff2096..f223f1f32 100644 --- a/src/geom-functions.cpp +++ b/src/geom-functions.cpp @@ -266,10 +266,8 @@ class without_first point_list_t const *m_list; }; // class without_first -} // anonymous namespace - -static void split_linestring(linestring_t const &line, double split_at, - multilinestring_t *output) +void split_linestring(linestring_t const &line, double split_at, + multilinestring_t *output) { double dist = 0; point_t prev_pt{line.front()}; @@ -320,6 +318,8 @@ static void split_linestring(linestring_t const &line, double split_at, } } +} // anonymous namespace + void segmentize(geometry_t *output, geometry_t const &input, double max_segment_length) { @@ -364,7 +364,9 @@ double area(geometry_t const &geom) /****************************************************************************/ -static double spherical_area(polygon_t const &geom) +namespace { + +double spherical_area(polygon_t const &geom) { boost::geometry::strategy::area::spherical<> const spherical_earth{ 6371008.8}; @@ -378,6 +380,8 @@ static double spherical_area(polygon_t const &geom) return boost::geometry::area(sph_geom, spherical_earth); } +} // anonymous namespace + double spherical_area(geometry_t const &geom) { assert(geom.srid() == 4326); @@ -471,23 +475,25 @@ std::vector split_multi(geometry_t &&geom, bool split_multi) /****************************************************************************/ -static void reverse(geom::nullgeom_t * /*output*/, - geom::nullgeom_t const & /*input*/) noexcept +namespace { + +void reverse(geom::nullgeom_t * /*output*/, + geom::nullgeom_t const & /*input*/) noexcept {} -static void reverse(geom::point_t *output, geom::point_t const &input) noexcept +void reverse(geom::point_t *output, geom::point_t const &input) noexcept { *output = input; } -static void reverse(point_list_t *output, point_list_t const &input) +void reverse(point_list_t *output, point_list_t const &input) { output->reserve(input.size()); std::reverse_copy(input.cbegin(), input.cend(), std::back_inserter(*output)); } -static void reverse(geom::polygon_t *output, geom::polygon_t const &input) +void reverse(geom::polygon_t *output, geom::polygon_t const &input) { reverse(&output->outer(), input.outer()); for (auto const &g : input.inners()) { @@ -505,6 +511,8 @@ void reverse(geom::multigeometry_t *output, } } +} // anonymous namespace + void reverse(geometry_t *output, geometry_t const &input) { output->set_srid(input.srid()); @@ -525,13 +533,15 @@ geometry_t reverse(geometry_t const &input) /****************************************************************************/ +namespace { + /** * Add points specified by iterators to the linestring. If linestring is not * empty, do not add the first point returned by *it. */ template -static void add_nodes_to_linestring(linestring_t *linestring, ITERATOR it, - ITERATOR end) +void add_nodes_to_linestring(linestring_t *linestring, ITERATOR it, + ITERATOR end) { if (!linestring->empty()) { assert(it != end); @@ -544,6 +554,8 @@ static void add_nodes_to_linestring(linestring_t *linestring, ITERATOR it, } } +} // anonymous namespace + void line_merge(geometry_t *output, geometry_t const &input) { if (input.is_linestring()) { @@ -728,6 +740,8 @@ geometry_t line_merge(geometry_t const &input) /****************************************************************************/ +namespace { + /** * This helper function is used to calculate centroids of geometry collections. * It first creates a multi geometry that only contains the geometries of @@ -740,7 +754,7 @@ geometry_t line_merge(geometry_t const &input) * Nested geometry collections are not allowed. */ template -static void filtered_centroid(collection_t const &collection, point_t *center) +void filtered_centroid(collection_t const &collection, point_t *center) { multigeometry_t multi; for (auto const &geom : collection) { @@ -758,6 +772,8 @@ static void filtered_centroid(collection_t const &collection, point_t *center) boost::geometry::centroid(multi, *center); } +} // anonymous namespace + geometry_t centroid(geometry_t const &geom) { geom::geometry_t output{point_t{}, geom.srid()}; @@ -785,8 +801,9 @@ geometry_t centroid(geometry_t const &geom) /****************************************************************************/ -static bool simplify(linestring_t *output, linestring_t const &input, - double tolerance) +namespace { + +bool simplify(linestring_t *output, linestring_t const &input, double tolerance) { boost::geometry::simplify(input, *output, tolerance); @@ -798,8 +815,8 @@ static bool simplify(linestring_t *output, linestring_t const &input, return output->size() > 1; } -static bool simplify(multilinestring_t *output, multilinestring_t const &input, - double tolerance) +bool simplify(multilinestring_t *output, multilinestring_t const &input, + double tolerance) { for (auto const &linestring : input) { linestring_t simplified_ls; @@ -811,11 +828,13 @@ static bool simplify(multilinestring_t *output, multilinestring_t const &input, } template -static bool simplify(T * /*output*/, T const & /*input*/, double /*tolerance*/) +bool simplify(T * /*output*/, T const & /*input*/, double /*tolerance*/) { return false; } +} // anonymous namespace + void simplify(geometry_t *output, geometry_t const &input, double tolerance) { output->set_srid(input.srid()); diff --git a/src/geom-pole-of-inaccessibility.cpp b/src/geom-pole-of-inaccessibility.cpp index 35b981eac..de3bd6b66 100644 --- a/src/geom-pole-of-inaccessibility.cpp +++ b/src/geom-pole-of-inaccessibility.cpp @@ -39,9 +39,11 @@ namespace geom { +namespace { + /// Get squared distance from a point p to a segment (a, b). -static double point_to_segment_distance_squared(point_t p, point_t a, point_t b, - double stretch) noexcept +double point_to_segment_distance_squared(point_t p, point_t a, point_t b, + double stretch) noexcept { double x = a.x(); double y = a.y() * stretch; @@ -68,9 +70,9 @@ static double point_to_segment_distance_squared(point_t p, point_t a, point_t b, } /// Get squared distance from a point p to ring. -static bool point_to_ring_distance_squared(point_t point, ring_t const &ring, - bool inside, double stretch, - double *min_dist_squared) noexcept +bool point_to_ring_distance_squared(point_t point, ring_t const &ring, + bool inside, double stretch, + double *min_dist_squared) noexcept { std::size_t const len = ring.size(); @@ -100,8 +102,8 @@ static bool point_to_ring_distance_squared(point_t point, ring_t const &ring, * Signed distance from point to polygon boundary. The result is negative if * the point is outside. */ -static auto point_to_polygon_distance(point_t point, polygon_t const &polygon, - double stretch) +auto point_to_polygon_distance(point_t point, polygon_t const &polygon, + double stretch) { double min_dist_squared = std::numeric_limits::infinity(); @@ -116,8 +118,6 @@ static auto point_to_polygon_distance(point_t point, polygon_t const &polygon, return (inside ? 1 : -1) * std::sqrt(min_dist_squared); } -namespace { - struct Cell { static constexpr double const SQRT2 = 1.4142135623730951; @@ -139,9 +139,7 @@ struct Cell } }; -} // anonymous namespace - -static Cell make_centroid_cell(polygon_t const &polygon, double stretch) +Cell make_centroid_cell(polygon_t const &polygon, double stretch) { point_t centroid{0, 0}; boost::geometry::centroid(polygon, centroid); @@ -149,6 +147,8 @@ static Cell make_centroid_cell(polygon_t const &polygon, double stretch) return {centroid, 0, polygon, stretch}; } +} // anonymous namespace + point_t pole_of_inaccessibility(const polygon_t &polygon, double precision, double stretch) { diff --git a/src/input.cpp b/src/input.cpp index 93aa5c61c..f23731034 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -59,6 +59,8 @@ type_id check_input(type_id const &last, osmium::OSMObject const &object) return check_input(last, {object.type(), object.id()}); } +namespace { + /** * A data source is where we get the OSM objects from, one at a time. It * wraps the osmium::io::Reader. @@ -173,37 +175,6 @@ class queue_element_t }; // class queue_element_t -std::vector -prepare_input_files(std::vector const &input_files, - std::string const &input_format, bool append) -{ - std::vector files; - - for (auto const &filename : input_files) { - osmium::io::File const file{filename, input_format}; - - if (file.format() == osmium::io::file_format::unknown) { - if (input_format.empty()) { - throw fmt_error("Cannot detect file format for '{}'." - " Try using -r.", - filename); - } - throw fmt_error("Unknown file format '{}'.", input_format); - } - - if (!append && file.has_multiple_object_versions()) { - throw std::runtime_error{ - "Reading an OSM change file only works in append mode."}; - } - - log_debug("Reading file: {}", filename); - - files.emplace_back(file); - } - - return files; -} - class input_context_t { public: @@ -261,9 +232,8 @@ class input_context_t bool m_append; }; // class input_context_t -static file_info process_single_file(osmium::io::File const &file, - osmdata_t *osmdata, - progress_display_t *progress, bool append) +file_info process_single_file(osmium::io::File const &file, osmdata_t *osmdata, + progress_display_t *progress, bool append) { file_info finfo; @@ -288,10 +258,9 @@ static file_info process_single_file(osmium::io::File const &file, return finfo; } -static file_info -process_multiple_files(std::vector const &files, - osmdata_t *osmdata, progress_display_t *progress, - bool append) +file_info process_multiple_files(std::vector const &files, + osmdata_t *osmdata, + progress_display_t *progress, bool append) { file_info finfo; @@ -333,6 +302,39 @@ process_multiple_files(std::vector const &files, return finfo; } +} // anonymous namespace + +std::vector +prepare_input_files(std::vector const &input_files, + std::string const &input_format, bool append) +{ + std::vector files; + + for (auto const &filename : input_files) { + osmium::io::File const file{filename, input_format}; + + if (file.format() == osmium::io::file_format::unknown) { + if (input_format.empty()) { + throw fmt_error("Cannot detect file format for '{}'." + " Try using -r.", + filename); + } + throw fmt_error("Unknown file format '{}'.", input_format); + } + + if (!append && file.has_multiple_object_versions()) { + throw std::runtime_error{ + "Reading an OSM change file only works in append mode."}; + } + + log_debug("Reading file: {}", filename); + + files.emplace_back(file); + } + + return files; +} + file_info process_files(std::vector const &files, osmdata_t *osmdata, bool append, bool show_progress) { diff --git a/src/lua-utils.cpp b/src/lua-utils.cpp index bbca3a4ed..1a28f346b 100644 --- a/src/lua-utils.cpp +++ b/src/lua-utils.cpp @@ -193,7 +193,9 @@ int luaX_pcall(lua_State *lua_state, int narg, int nres) #else -static int pcall_error_traceback_handler(lua_State *lua_state) +namespace { + +int pcall_error_traceback_handler(lua_State *lua_state) { assert(lua_state); @@ -210,6 +212,8 @@ static int pcall_error_traceback_handler(lua_State *lua_state) return 1; } +} // anonymous namespace + /// Wrapper function for lua_pcall() showing a stack trace on error. int luaX_pcall(lua_State *lua_state, int narg, int nres) { diff --git a/src/middle-pgsql.cpp b/src/middle-pgsql.cpp index e8a0df5eb..83e229cd9 100644 --- a/src/middle-pgsql.cpp +++ b/src/middle-pgsql.cpp @@ -44,8 +44,10 @@ #include "pgsql-helper.hpp" #include "util.hpp" -static bool check_bucket_index(pg_conn_t const *db_connection, - std::string const &prefix) +namespace { + +bool check_bucket_index(pg_conn_t const *db_connection, + std::string const &prefix) { auto const res = db_connection->exec("SELECT relname FROM pg_class" @@ -55,7 +57,7 @@ static bool check_bucket_index(pg_conn_t const *db_connection, return res.num_tuples() > 0; } -static void send_id_list(pg_conn_t const &db_connection, +void send_id_list(pg_conn_t const &db_connection, std::string const &table, idlist_t const &ids) { std::string data; @@ -69,8 +71,8 @@ static void send_id_list(pg_conn_t const &db_connection, db_connection.copy_end(table); } -static void load_id_list(pg_conn_t const &db_connection, - std::string const &table, idlist_t *ids) +void load_id_list(pg_conn_t const &db_connection, std::string const &table, + idlist_t *ids) { auto const res = db_connection.exec( fmt::format("SELECT DISTINCT id FROM {} ORDER BY id", table)); @@ -79,7 +81,7 @@ static void load_id_list(pg_conn_t const &db_connection, } } -static std::string build_sql(options_t const &options, std::string const &templ) +std::string build_sql(options_t const &options, std::string const &templ) { std::string const using_tablespace{options.tblsslim_index.empty() ? "" @@ -114,8 +116,8 @@ static std::string build_sql(options_t const &options, std::string const &templ) : "")); } -static std::vector -build_sql(options_t const &options, std::vector const &templs) +std::vector build_sql(options_t const &options, + std::vector const &templs) { std::vector out; out.reserve(templs.size()); @@ -127,6 +129,8 @@ build_sql(options_t const &options, std::vector const &templs) return out; } +} // anonymous namespace + middle_pgsql_t::table_desc::table_desc(options_t const &options, table_sql const &ts) : m_create_table(build_sql(options, ts.create_table)), @@ -367,9 +371,7 @@ void set_attributes_on_builder(T *builder, pg_result_t const &result, int num, } } -} // anonymous namespace - -static void tags_to_json(osmium::TagList const &tags, json_writer_t *writer) +void tags_to_json(osmium::TagList const &tags, json_writer_t *writer) { writer->start_object(); @@ -382,8 +384,8 @@ static void tags_to_json(osmium::TagList const &tags, json_writer_t *writer) writer->end_object(); } -static void members_to_json(osmium::RelationMemberList const &members, - json_writer_t *writer) +void members_to_json(osmium::RelationMemberList const &members, + json_writer_t *writer) { writer->start_array(); @@ -418,6 +420,8 @@ static void members_to_json(osmium::RelationMemberList const &members, writer->end_array(); } +} // anonymous namespace + void middle_pgsql_t::copy_attributes(osmium::OSMObject const &obj) { if (obj.timestamp()) { @@ -808,12 +812,13 @@ void middle_pgsql_t::way_set(osmium::Way const &way) m_db_copy.finish_line(); } +namespace { + /** * Build way in buffer from database results. */ -static void build_way(osmid_t id, pg_result_t const &res, int res_num, - int offset, osmium::memory::Buffer *buffer, - bool with_attributes) +void build_way(osmid_t id, pg_result_t const &res, int res_num, int offset, + osmium::memory::Buffer *buffer, bool with_attributes) { osmium::builder::WayBuilder builder{*buffer}; builder.set_id(id); @@ -825,6 +830,8 @@ static void build_way(osmid_t id, pg_result_t const &res, int res_num, pgsql_parse_json_tags(res.get_value(res_num, offset + 1), buffer, &builder); } +} // anonymous namespace + bool middle_query_pgsql_t::way_get(osmid_t id, osmium::memory::Buffer *buffer) const { @@ -1030,14 +1037,18 @@ middle_query_pgsql_t::middle_query_pgsql_t( m_db_connection.set_config("max_parallel_workers_per_gather", "0"); } -static void table_setup(pg_conn_t const &db_connection, - middle_pgsql_t::table_desc const &table) +namespace { + +void table_setup(pg_conn_t const &db_connection, + middle_pgsql_t::table_desc const &table) { log_debug("Setting up table '{}'", table.name()); drop_table_if_exists(db_connection, table.schema(), table.name()); table.create_table(db_connection); } +} // anonymous namespace + void middle_pgsql_t::start() { assert(m_middle_state == middle_state::constructed); @@ -1139,7 +1150,9 @@ void middle_pgsql_t::wait() } } -static table_sql sql_for_users(middle_pgsql_options const &store_options) +namespace { + +table_sql sql_for_users(middle_pgsql_options const &store_options) { table_sql sql{}; @@ -1155,7 +1168,7 @@ static table_sql sql_for_users(middle_pgsql_options const &store_options) return sql; } -static table_sql sql_for_nodes(middle_pgsql_options const &options) +table_sql sql_for_nodes(middle_pgsql_options const &options) { table_sql sql{}; @@ -1183,7 +1196,7 @@ static table_sql sql_for_nodes(middle_pgsql_options const &options) return sql; } -static table_sql sql_for_ways(middle_pgsql_options const &options) +table_sql sql_for_ways(middle_pgsql_options const &options) { table_sql sql{}; @@ -1228,7 +1241,7 @@ static table_sql sql_for_ways(middle_pgsql_options const &options) return sql; } -static table_sql sql_for_relations() +table_sql sql_for_relations() { table_sql sql{}; @@ -1267,6 +1280,8 @@ static table_sql sql_for_relations() return sql; } +} // anonymous namespace + middle_pgsql_t::middle_pgsql_t(std::shared_ptr thread_pool, options_t const *options) : middle_t(std::move(thread_pool)), m_options(options), diff --git a/src/middle-ram.cpp b/src/middle-ram.cpp index 1f39b58f3..82170fbe0 100644 --- a/src/middle-ram.cpp +++ b/src/middle-ram.cpp @@ -25,6 +25,47 @@ #include #include +namespace { + +void add_delta_encoded_way_node_list(std::string *data, + osmium::WayNodeList const &wnl) +{ + assert(data); + + // Add number of nodes in list + protozero::add_varint_to_buffer(data, wnl.size()); + + // Add delta encoded node ids + osmium::DeltaEncode delta; + for (auto const &nr : wnl) { + protozero::add_varint_to_buffer( + data, protozero::encode_zigzag64(delta.update(nr.ref()))); + } +} + +void get_delta_encoded_way_nodes_list(std::string const &data, + std::size_t offset, + osmium::builder::WayBuilder *builder) +{ + assert(builder); + + char const *begin = data.data() + offset; + char const *const end = data.data() + data.size(); + + auto count = protozero::decode_varint(&begin, end); + + osmium::DeltaDecode delta; + osmium::builder::WayNodeListBuilder wnl_builder{*builder}; + while (count > 0) { + auto const val = + protozero::decode_zigzag64(protozero::decode_varint(&begin, end)); + wnl_builder.add_node_ref(delta.update(val)); + --count; + } +} + +} // anonymous namespace + middle_ram_t::middle_ram_t(std::shared_ptr thread_pool, options_t const *options) : middle_t(std::move(thread_pool)) @@ -133,22 +174,6 @@ bool middle_ram_t::get_object(osmium::item_type type, osmid_t id, return true; } -static void add_delta_encoded_way_node_list(std::string *data, - osmium::WayNodeList const &wnl) -{ - assert(data); - - // Add number of nodes in list - protozero::add_varint_to_buffer(data, wnl.size()); - - // Add delta encoded node ids - osmium::DeltaEncode delta; - for (auto const &nr : wnl) { - protozero::add_varint_to_buffer( - data, protozero::encode_zigzag64(delta.update(nr.ref()))); - } -} - void middle_ram_t::node(osmium::Node const &node) { assert(m_middle_state == middle_state::node); @@ -233,27 +258,6 @@ bool middle_ram_t::way_get(osmid_t id, osmium::memory::Buffer *buffer) const return false; } -static void -get_delta_encoded_way_nodes_list(std::string const &data, std::size_t offset, - osmium::builder::WayBuilder *builder) -{ - assert(builder); - - char const *begin = data.data() + offset; - char const *const end = data.data() + data.size(); - - auto count = protozero::decode_varint(&begin, end); - - osmium::DeltaDecode delta; - osmium::builder::WayNodeListBuilder wnl_builder{*builder}; - while (count > 0) { - auto const val = - protozero::decode_zigzag64(protozero::decode_varint(&begin, end)); - wnl_builder.add_node_ref(delta.update(val)); - --count; - } -} - std::size_t middle_ram_t::rel_members_get(osmium::Relation const &rel, osmium::memory::Buffer *buffer, diff --git a/src/osm2pgsql.cpp b/src/osm2pgsql.cpp index f6bb541bd..b8d7ff510 100644 --- a/src/osm2pgsql.cpp +++ b/src/osm2pgsql.cpp @@ -28,12 +28,14 @@ #include #include +namespace { + /** * Output overall memory usage as debug message. * * This only works on Linux. */ -static void show_memory_usage() +void show_memory_usage() { osmium::MemoryUsage const mem; if (mem.peak() != 0) { @@ -42,7 +44,7 @@ static void show_memory_usage() } } -static file_info run(options_t const &options) +file_info run(options_t const &options) { auto const files = prepare_input_files( options.input_files, options.input_format, options.append); @@ -75,7 +77,7 @@ static file_info run(options_t const &options) return finfo; } -static void check_db(options_t const &options) +void check_db(options_t const &options) { pg_conn_t const db_connection{options.connection_params, "check"}; @@ -92,7 +94,7 @@ static void check_db(options_t const &options) } // This is called in "create" mode to store properties into the database. -static void store_properties(properties_t *properties, options_t const &options) +void store_properties(properties_t *properties, options_t const &options) { properties->set_bool("attributes", options.extra_attributes); @@ -123,8 +125,7 @@ static void store_properties(properties_t *properties, options_t const &options) properties->store(); } -static void store_data_properties(properties_t *properties, - file_info const &finfo) +void store_data_properties(properties_t *properties, file_info const &finfo) { if (finfo.last_timestamp.valid()) { auto const timestamp = finfo.last_timestamp.to_iso(); @@ -142,7 +143,7 @@ static void store_data_properties(properties_t *properties, properties->store(); } -static void check_updatable(properties_t const &properties) +void check_updatable(properties_t const &properties) { if (properties.get_bool("updatable", false)) { return; @@ -153,7 +154,7 @@ static void check_updatable(properties_t const &properties) " updatable database use --slim (without --drop)."}; } -static void check_attributes(properties_t const &properties, options_t *options) +void check_attributes(properties_t const &properties, options_t *options) { bool const with_attributes = properties.get_bool("attributes", false); @@ -172,8 +173,8 @@ static void check_attributes(properties_t const &properties, options_t *options) } } -static void check_and_update_flat_node_file(properties_t *properties, - options_t *options) +void check_and_update_flat_node_file(properties_t *properties, + options_t *options) { auto const flat_node_file_from_import = properties->get_string("flat_node_file", ""); @@ -210,7 +211,7 @@ static void check_and_update_flat_node_file(properties_t *properties, } } -static void check_prefix(properties_t const &properties, options_t *options) +void check_prefix(properties_t const &properties, options_t *options) { auto const prefix = properties.get_string("prefix", "planet_osm"); if (!options->prefix_is_set) { @@ -226,7 +227,7 @@ static void check_prefix(properties_t const &properties, options_t *options) } } -static void check_db_format(properties_t const &properties, options_t *options) +void check_db_format(properties_t const &properties, options_t *options) { auto const format = properties.get_int("db_format", -1); @@ -243,7 +244,7 @@ static void check_db_format(properties_t const &properties, options_t *options) options->middle_database_format = static_cast(format); } -static void check_output(properties_t const &properties, options_t *options) +void check_output(properties_t const &properties, options_t *options) { auto const output = properties.get_string("output", "pgsql"); @@ -262,8 +263,7 @@ static void check_output(properties_t const &properties, options_t *options) options->output_backend, output); } -static void check_and_update_style_file(properties_t *properties, - options_t *options) +void check_and_update_style_file(properties_t *properties, options_t *options) { auto const style_file_from_import = properties->get_string("style", ""); @@ -296,8 +296,7 @@ static void check_and_update_style_file(properties_t *properties, // This is called in "append" mode to check that the command line options are // compatible with the properties stored in the database. -static void check_and_update_properties(properties_t *properties, - options_t *options) +void check_and_update_properties(properties_t *properties, options_t *options) { check_updatable(*properties); check_attributes(*properties, options); @@ -308,7 +307,7 @@ static void check_and_update_properties(properties_t *properties, check_and_update_style_file(properties, options); } -static void set_option_defaults(options_t *options) +void set_option_defaults(options_t *options) { if (options->output_backend.empty()) { options->output_backend = "pgsql"; @@ -325,6 +324,8 @@ static void set_option_defaults(options_t *options) } } +} // anonymous namespace + // NOLINTNEXTLINE(bugprone-exception-escape) int main(int argc, char *argv[]) { diff --git a/src/output-flex.cpp b/src/output-flex.cpp index 290dc9d98..6c7e6f68c 100644 --- a/src/output-flex.cpp +++ b/src/output-flex.cpp @@ -122,9 +122,11 @@ prepared_lua_function_t::prepared_lua_function_t(lua_State *lua_state, throw fmt_error("osm2pgsql.{} must be a function.", name); } -static void push_osm_object_to_lua_stack(lua_State *lua_state, - osmium::OSMObject const &object, - bool with_attributes) +namespace { + +void push_osm_object_to_lua_stack(lua_State *lua_state, + osmium::OSMObject const &object, + bool with_attributes) { assert(lua_state); @@ -200,13 +202,103 @@ static void push_osm_object_to_lua_stack(lua_State *lua_state, * Helper function to push the lon/lat of the specified location onto the * Lua stack */ -static void push_location(lua_State *lua_state, - osmium::Location location) noexcept +void push_location(lua_State *lua_state, osmium::Location location) noexcept { lua_pushnumber(lua_state, location.lon()); lua_pushnumber(lua_state, location.lat()); } +// Check that the first element on the Lua stack is a "type_name" +// parameter and return its internal index. +std::size_t idx_from_param(lua_State *lua_state, char const *type_name) +{ + assert(lua_gettop(lua_state) >= 1); + + void const *const user_data = lua_touserdata(lua_state, 1); + + if (user_data == nullptr || !lua_getmetatable(lua_state, 1)) { + throw fmt_error("First parameter must be of type {}.", type_name); + } + + luaL_getmetatable(lua_state, type_name); + if (!lua_rawequal(lua_state, -1, -2)) { + throw fmt_error("First parameter must be of type {}.", type_name); + } + lua_pop(lua_state, 2); // remove the two metatables + + return *static_cast(user_data); +} + +template +typename CONTAINER::value_type const &get_from_idx_param(lua_State *lua_state, + CONTAINER *container, + char const *type_name) +{ + if (lua_gettop(lua_state) != 1) { + throw fmt_error("Need exactly one parameter of type {}.", type_name); + } + + auto const &item = container->at(idx_from_param(lua_state, type_name)); + lua_remove(lua_state, 1); + return item; +} + +std::size_t get_nodes(middle_query_t const &middle, osmium::Way *way) +{ + constexpr std::size_t const max_missing_nodes = 100; + static std::size_t count_missing_nodes = 0; + + auto const count = middle.nodes_get_list(&way->nodes()); + + if (count_missing_nodes <= max_missing_nodes && + count != way->nodes().size()) { + util::string_joiner_t id_list{','}; + for (auto const &nr : way->nodes()) { + if (!nr.location().valid()) { + id_list.add(fmt::to_string(nr.ref())); + ++count_missing_nodes; + } + } + + log_debug("Missing nodes in way {}: {}", way->id(), id_list()); + + if (count_missing_nodes > max_missing_nodes) { + log_debug("Reported more than {} missing nodes, no further missing " + "nodes will be reported!", + max_missing_nodes); + } + } + + return count; +} + +void flush_tables(std::vector &table_connections) +{ + for (auto &table : table_connections) { + table.flush(); + } +} + +void create_expire_tables(std::vector const &expire_outputs, + connection_params_t const &connection_params) +{ + if (std::all_of(expire_outputs.begin(), expire_outputs.end(), + [](auto const &expire_output) { + return expire_output.table().empty(); + })) { + return; + } + + pg_conn_t const connection{connection_params, "out.flex.expire"}; + for (auto const &expire_output : expire_outputs) { + if (!expire_output.table().empty()) { + expire_output.create_output_table(connection); + } + } +} + +} // anonymous namespace + /** * Helper function checking that Lua function "name" is called in the correct * context and without parameters. @@ -420,41 +512,6 @@ int output_flex_t::app_define_expire_output() m_expire_outputs.get()); } -// Check that the first element on the Lua stack is a "type_name" -// parameter and return its internal index. -static std::size_t idx_from_param(lua_State *lua_state, char const *type_name) -{ - assert(lua_gettop(lua_state) >= 1); - - void const *const user_data = lua_touserdata(lua_state, 1); - - if (user_data == nullptr || !lua_getmetatable(lua_state, 1)) { - throw fmt_error("First parameter must be of type {}.", type_name); - } - - luaL_getmetatable(lua_state, type_name); - if (!lua_rawequal(lua_state, -1, -2)) { - throw fmt_error("First parameter must be of type {}.", type_name); - } - lua_pop(lua_state, 2); // remove the two metatables - - return *static_cast(user_data); -} - -template -static typename CONTAINER::value_type const & -get_from_idx_param(lua_State *lua_state, CONTAINER *container, - char const *type_name) -{ - if (lua_gettop(lua_state) != 1) { - throw fmt_error("Need exactly one parameter of type {}.", type_name); - } - - auto const &item = container->at(idx_from_param(lua_state, type_name)); - lua_remove(lua_state, 1); - return item; -} - flex_table_t const &output_flex_t::get_table_from_param() { return get_from_idx_param(lua_state(), m_tables.get(), @@ -497,35 +554,6 @@ void output_flex_t::way_cache_t::init(osmium::Way *way) m_way = way; } -static std::size_t get_nodes(middle_query_t const &middle, osmium::Way *way) -{ - constexpr std::size_t const max_missing_nodes = 100; - static std::size_t count_missing_nodes = 0; - - auto const count = middle.nodes_get_list(&way->nodes()); - - if (count_missing_nodes <= max_missing_nodes && - count != way->nodes().size()) { - util::string_joiner_t id_list{','}; - for (auto const &nr : way->nodes()) { - if (!nr.location().valid()) { - id_list.add(fmt::to_string(nr.ref())); - ++count_missing_nodes; - } - } - - log_debug("Missing nodes in way {}: {}", way->id(), id_list()); - - if (count_missing_nodes > max_missing_nodes) { - log_debug("Reported more than {} missing nodes, no further missing " - "nodes will be reported!", - max_missing_nodes); - } - } - - return count; -} - std::size_t output_flex_t::way_cache_t::add_nodes(middle_query_t const &middle) { if (m_num_way_nodes == std::numeric_limits::max()) { @@ -931,13 +959,6 @@ void output_flex_t::sync() } } -static void flush_tables(std::vector &table_connections) -{ - for (auto &table : table_connections) { - table.flush(); - } -} - void output_flex_t::after_nodes() { flush_tables(m_table_connections); } void output_flex_t::after_ways() { flush_tables(m_table_connections); } @@ -1106,25 +1127,6 @@ void output_flex_t::start() } } -static void -create_expire_tables(std::vector const &expire_outputs, - connection_params_t const &connection_params) -{ - if (std::all_of(expire_outputs.begin(), expire_outputs.end(), - [](auto const &expire_output) { - return expire_output.table().empty(); - })) { - return; - } - - pg_conn_t const connection{connection_params, "out.flex.expire"}; - for (auto const &expire_output : expire_outputs) { - if (!expire_output.table().empty()) { - expire_output.create_output_table(connection); - } - } -} - output_flex_t::output_flex_t(output_flex_t const *other, std::shared_ptr mid, std::shared_ptr copy_thread) @@ -1211,10 +1213,12 @@ output_flex_t::output_flex_t(std::shared_ptr const &mid, create_expire_tables(*m_expire_outputs, get_options()->connection_params); } +namespace { + /** * Define the osm2pgsql.Table class/metatable. */ -static void init_table_class(lua_State *lua_state) +void init_table_class(lua_State *lua_state) { lua_getglobal(lua_state, "osm2pgsql"); if (luaL_newmetatable(lua_state, osm2pgsql_table_name) != 1) { @@ -1241,7 +1245,7 @@ static void init_table_class(lua_State *lua_state) /** * Define the osm2pgsql.ExpireOutput class/metatable. */ -static void init_expire_output_class(lua_State *lua_state) +void init_expire_output_class(lua_State *lua_state) { lua_getglobal(lua_state, "osm2pgsql"); if (luaL_newmetatable(lua_state, osm2pgsql_expire_output_name) != 1) { @@ -1270,6 +1274,8 @@ static void init_expire_output_class(lua_State *lua_state) lua_pop(lua_state, 2); } +} // anonymous namespace + void output_flex_t::init_lua(std::string const &filename) { m_lua_state.reset(luaL_newstate(), diff --git a/src/output-pgsql.cpp b/src/output-pgsql.cpp index cf4274071..9a8b34d06 100644 --- a/src/output-pgsql.cpp +++ b/src/output-pgsql.cpp @@ -45,9 +45,10 @@ #include "wildcmp.hpp" #include "wkb.hpp" -static double calculate_area(bool reproject_area, - geom::geometry_t const &geom4326, - geom::geometry_t const &projected_geom) +namespace { + +double calculate_area(bool reproject_area, geom::geometry_t const &geom4326, + geom::geometry_t const &projected_geom) { static thread_local auto const proj3857 = reprojection::create_projection(3857); @@ -59,6 +60,37 @@ static double calculate_area(bool reproject_area, return geom::area(projected_geom); } +// The roles of all available member ways of a relation are available in the +// Lua "filter_tags_relation_member" callback function. This function extracts +// the roles from all ways in the buffer and returns the list. +rolelist_t get_rolelist(osmium::Relation const &rel, + osmium::memory::Buffer const &buffer) +{ + rolelist_t roles; + + auto it = buffer.select().cbegin(); + auto const end = buffer.select().cend(); + + if (it == end) { + return roles; + } + + for (auto const &member : rel.members()) { + if (member.type() == osmium::item_type::way && + member.ref() == it->id()) { + roles.emplace_back(member.role()); + ++it; + if (it == end) { + break; + } + } + } + + return roles; +} + +} // anonymous namespace + void output_pgsql_t::pgsql_out_way(osmium::Way const &way, taglist_t *tags, bool polygon, bool roads) { @@ -203,35 +235,6 @@ void output_pgsql_t::way_add(osmium::Way *way) } } -// The roles of all available member ways of a relation are available in the -// Lua "filter_tags_relation_member" callback function. This function extracts -// the roles from all ways in the buffer and returns the list. -static rolelist_t get_rolelist(osmium::Relation const &rel, - osmium::memory::Buffer const &buffer) -{ - rolelist_t roles; - - auto it = buffer.select().cbegin(); - auto const end = buffer.select().cend(); - - if (it == end) { - return roles; - } - - for (auto const &member : rel.members()) { - if (member.type() == osmium::item_type::way && - member.ref() == it->id()) { - roles.emplace_back(member.role()); - ++it; - if (it == end) { - break; - } - } - } - - return roles; -} - /* This is the workhorse of pgsql_add_relation, split out because it is used as the callback for iterate relations */ void output_pgsql_t::pgsql_process_relation(osmium::Relation const &rel) { diff --git a/src/pgsql-capabilities.cpp b/src/pgsql-capabilities.cpp index 8123b7478..6ea96e737 100644 --- a/src/pgsql-capabilities.cpp +++ b/src/pgsql-capabilities.cpp @@ -16,21 +16,26 @@ #include -static database_capabilities_t &capabilities() noexcept +namespace { + +database_capabilities_t &capabilities() noexcept { static database_capabilities_t c; return c; } +} // anonymous namespace + database_capabilities_t &database_capabilities_for_testing() noexcept { return capabilities(); } -static void init_set_from_query(std::set *set, - pg_conn_t const &db_connection, - char const *table, char const *column, - char const *condition = "true") +namespace { + +void init_set_from_query(std::set *set, + pg_conn_t const &db_connection, char const *table, + char const *column, char const *condition = "true") { set->clear(); // Clear existing in case this is called multiple times @@ -42,7 +47,7 @@ static void init_set_from_query(std::set *set, } /// Get all config settings from the database. -static void init_settings(pg_conn_t const &db_connection) +void init_settings(pg_conn_t const &db_connection) { capabilities().settings.clear(); // In case this is called multiple times @@ -54,7 +59,7 @@ static void init_settings(pg_conn_t const &db_connection) } } -static void init_database_name(pg_conn_t const &db_connection) +void init_database_name(pg_conn_t const &db_connection) { auto const res = db_connection.exec("SELECT current_catalog"); @@ -66,7 +71,7 @@ static void init_database_name(pg_conn_t const &db_connection) capabilities().database_name = res.get(0, 0); } -static void init_postgis_version(pg_conn_t const &db_connection) +void init_postgis_version(pg_conn_t const &db_connection) { auto const res = db_connection.exec( "SELECT regexp_split_to_table(extversion, '\\.') FROM" @@ -84,6 +89,8 @@ static void init_postgis_version(pg_conn_t const &db_connection) std::stoi(std::string{res.get(1, 0)})}; } +} // anonymous namespace + void init_database_capabilities(pg_conn_t const &db_connection) { init_settings(db_connection); diff --git a/src/pgsql.cpp b/src/pgsql.cpp index 5b1637486..04c5c033d 100644 --- a/src/pgsql.cpp +++ b/src/pgsql.cpp @@ -28,8 +28,10 @@ std::size_t pg_result_t::affected_rows() const noexcept std::atomic pg_conn_t::connection_id{0}; -static PGconn *open_connection(connection_params_t const &connection_params, - std::string_view context, std::uint32_t id) +namespace { + +PGconn *open_connection(connection_params_t const &connection_params, + std::string_view context, std::uint32_t id) { std::vector keywords; std::vector values; @@ -49,6 +51,19 @@ static PGconn *open_connection(connection_params_t const &connection_params, return PQconnectdbParams(keywords.data(), values.data(), 1); } +std::string concat_params(int num_params, char const *const *param_values) +{ + util::string_joiner_t joiner{','}; + + for (int i = 0; i < num_params; ++i) { + joiner.add(param_values[i] ? param_values[i] : ""); + } + + return joiner(); +} + +} // anonymous namespace + pg_conn_t::pg_conn_t(connection_params_t const &connection_params, std::string_view context) : m_connection_id(connection_id.fetch_add(1)) @@ -179,18 +194,6 @@ void pg_conn_t::copy_end(std::string_view context) const } } -static std::string concat_params(int num_params, - char const *const *param_values) -{ - util::string_joiner_t joiner{','}; - - for (int i = 0; i < num_params; ++i) { - joiner.add(param_values[i] ? param_values[i] : ""); - } - - return joiner(); -} - pg_result_t pg_conn_t::exec_prepared_internal(char const *stmt, int num_params, char const *const *param_values, int *param_lengths, diff --git a/src/progress-display.cpp b/src/progress-display.cpp index 5fd21a290..ae8aa946d 100644 --- a/src/progress-display.cpp +++ b/src/progress-display.cpp @@ -12,7 +12,9 @@ #include "progress-display.hpp" #include "util.hpp" -static double count_per_second(std::size_t count, uint64_t elapsed) noexcept +namespace { + +double count_per_second(std::size_t count, uint64_t elapsed) noexcept { if (count == 0) { return 0.0; @@ -25,7 +27,7 @@ static double count_per_second(std::size_t count, uint64_t elapsed) noexcept return static_cast(count) / static_cast(elapsed); } -static std::string cps_display(std::size_t count, uint64_t elapsed) +std::string cps_display(std::size_t count, uint64_t elapsed) { double const cps = count_per_second(count, elapsed); @@ -35,6 +37,8 @@ static std::string cps_display(std::size_t count, uint64_t elapsed) return fmt::format("{:.0f}/s", cps); } +} // anonymous namespace + void progress_display_t::print_summary() const { std::time_t const now = std::time(nullptr); diff --git a/src/taginfo.cpp b/src/taginfo.cpp index 45be7186d..64e7e34f5 100644 --- a/src/taginfo.cpp +++ b/src/taginfo.cpp @@ -64,10 +64,12 @@ unsigned parse_tag_flags(std::string const &flags, int lineno) return temp_flags; } +namespace { + /** * Get the tag type. For unknown types, 0 will be returned. */ -static unsigned get_tag_type(std::string const &tag) +unsigned get_tag_type(std::string const &tag) { static std::map const tagtypes = { {"smallint", FLAG_INT_TYPE}, {"integer", FLAG_INT_TYPE}, @@ -83,6 +85,8 @@ static unsigned get_tag_type(std::string const &tag) return 0; } +} // anonymous namespace + bool read_style_file(std::string const &filename, export_list *exlist) { bool enable_way_area = true; diff --git a/src/tagtransform-c.cpp b/src/tagtransform-c.cpp index e606d2915..9e96f949f 100644 --- a/src/tagtransform-c.cpp +++ b/src/tagtransform-c.cpp @@ -44,9 +44,7 @@ constexpr std::array const layers = { {"primary", 37, true}, {"trunk", 38, true}, {"motorway", 39, true}}}; -} // anonymous namespace - -static void add_z_order(taglist_t *tags, bool *roads) +void add_z_order(taglist_t *tags, bool *roads) { std::string const *const layer = tags->get("layer"); std::string const *const highway = tags->get("highway"); @@ -91,6 +89,13 @@ static void add_z_order(taglist_t *tags, bool *roads) tags->add_tag("z_order", fmt::to_string(z_order)); } +bool starts_with(char const *input, std::string const &test) noexcept +{ + return std::strncmp(input, test.c_str(), test.size()) == 0; +} + +} // anonymous namespace + c_tagtransform_t::c_tagtransform_t(options_t const *options, export_list exlist) : m_options(options), m_export_list(std::move(exlist)) {} @@ -100,11 +105,6 @@ std::unique_ptr c_tagtransform_t::clone() const return std::make_unique(m_options, m_export_list); } -static bool starts_with(char const *input, std::string const& test) noexcept -{ - return std::strncmp(input, test.c_str(), test.size()) == 0; -} - bool c_tagtransform_t::check_key(std::vector const &infos, char const *k, bool *filter, unsigned int *flags) diff --git a/src/tagtransform-lua.cpp b/src/tagtransform-lua.cpp index 95eb99715..2f69b7f93 100644 --- a/src/tagtransform-lua.cpp +++ b/src/tagtransform-lua.cpp @@ -45,10 +45,12 @@ void lua_tagtransform_t::check_lua_function_exists(char const *func_name) lua_pop(lua_state(), 1); } +namespace { + /** * Read tags from the Lua table on the stack and write them to out_tags */ -static void get_out_tags(lua_State *lua_state, taglist_t *out_tags) +void get_out_tags(lua_State *lua_state, taglist_t *out_tags) { luaX_for_each(lua_state, [&]() { auto const key_type = lua_type(lua_state, -2); @@ -77,6 +79,8 @@ static void get_out_tags(lua_State *lua_state, taglist_t *out_tags) lua_pop(lua_state, 1); } +} // anonymous namespace + bool lua_tagtransform_t::filter_tags(osmium::OSMObject const &o, bool *polygon, bool *roads, taglist_t *out_tags) { diff --git a/src/tile.cpp b/src/tile.cpp index 3351ed75e..8b5c2a443 100644 --- a/src/tile.cpp +++ b/src/tile.cpp @@ -28,10 +28,12 @@ geom::point_t tile_t::center() const noexcept return to_world_coords({0.5, 0.5}, 1); } +namespace { + // Quadkey implementation uses bit interleaving code from // https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/master/2018/01/08/interleave.c -static uint64_t interleave_uint32_with_zeros(uint32_t input) noexcept +uint64_t interleave_uint32_with_zeros(uint32_t input) noexcept { uint64_t word = input; word = (word ^ (word << 16U)) & 0x0000ffff0000ffffULL; @@ -42,7 +44,7 @@ static uint64_t interleave_uint32_with_zeros(uint32_t input) noexcept return word; } -static uint32_t deinterleave_lowuint32(uint64_t word) noexcept +uint32_t deinterleave_lowuint32(uint64_t word) noexcept { word &= 0x5555555555555555ULL; word = (word ^ (word >> 1U)) & 0x3333333333333333ULL; @@ -53,6 +55,8 @@ static uint32_t deinterleave_lowuint32(uint64_t word) noexcept return static_cast(word); } +} // anonymous namespace + quadkey_t tile_t::quadkey() const noexcept { return quadkey_t{interleave_uint32_with_zeros(m_x) |