Flex: Process untagged objects in their own callbacks

This happens regardless of whether the -x/--extra-attributes option is
set.
This commit is contained in:
Jochen Topf
2024-08-16 14:20:50 +02:00
parent 0c7b201b76
commit bc87ea2c8b
5 changed files with 90 additions and 7 deletions

View File

@ -20,6 +20,15 @@ stds.osm2pgsql = {
process_relation = {
read_only = false
},
process_untagged_node = {
read_only = false
},
process_untagged_way = {
read_only = false
},
process_untagged_relation = {
read_only = false
},
select_relation_members = {
read_only = false
},

44
flex-config/untagged.lua Normal file
View File

@ -0,0 +1,44 @@
-- This config example file is released into the Public Domain.
--
-- Most of the time we are only interested in nodes, ways, and relations that
-- have tags. But we can also get the untagged ones if necessary by using
-- the processing functions 'process_untagged_node', 'process_untagged_way',
-- and 'process_untagged_relation', respectively.
local nodes = osm2pgsql.define_node_table('nodes', {
{ column = 'tags', type = 'jsonb' },
{ column = 'geom', type = 'point' },
})
local ways = osm2pgsql.define_way_table('ways', {
{ column = 'tags', type = 'jsonb' },
{ column = 'geom', type = 'linestring' },
})
function osm2pgsql.process_node(object)
nodes:insert({
tags = object.tags,
geom = object:as_point(),
})
end
function osm2pgsql.process_untagged_node(object)
nodes:insert({
geom = object:as_point(),
})
end
-- If you want to use the same function in both cases, that's also quite easy.
-- For instance like this:
local function do_way(object)
ways:insert({
tags = object.tags,
geom = object:as_linestring(),
})
end
osm2pgsql.process_way = do_way
osm2pgsql.process_untagged_way = do_way

View File

@ -31,7 +31,9 @@ osmdata_t::osmdata_t(std::shared_ptr<middle_t> mid,
: m_mid(std::move(mid)), m_output(std::move(output)),
m_connection_params(options.connection_params), m_bbox(options.bbox),
m_num_procs(options.num_procs), m_append(options.append),
m_droptemp(options.droptemp), m_with_extra_attrs(options.extra_attributes)
m_droptemp(options.droptemp),
m_with_extra_attrs(options.extra_attributes ||
options.output_backend == "flex")
{
assert(m_mid);
assert(m_output);

View File

@ -1047,12 +1047,15 @@ void output_flex_t::wait()
void output_flex_t::node_add(osmium::Node const &node)
{
if (!m_process_node) {
auto const &func =
node.tags().empty() ? m_process_untagged_node : m_process_node;
if (!func) {
return;
}
m_context_node = &node;
get_mutex_and_call_lua_function(m_process_node, node);
get_mutex_and_call_lua_function(func, node);
m_context_node = nullptr;
}
@ -1060,23 +1063,29 @@ void output_flex_t::way_add(osmium::Way *way)
{
assert(way);
if (!m_process_way) {
auto const &func =
way->tags().empty() ? m_process_untagged_way : m_process_way;
if (!func) {
return;
}
m_way_cache.init(way);
get_mutex_and_call_lua_function(m_process_way, m_way_cache.get());
get_mutex_and_call_lua_function(func, m_way_cache.get());
}
void output_flex_t::relation_add(osmium::Relation const &relation)
{
if (!m_process_relation) {
auto const &func = relation.tags().empty() ? m_process_untagged_relation
: m_process_relation;
if (!func) {
return;
}
m_relation_cache.init(relation);
select_relation_members();
get_mutex_and_call_lua_function(m_process_relation, relation);
get_mutex_and_call_lua_function(func, relation);
}
void output_flex_t::delete_from_table(table_connection_t *table_connection,
@ -1171,6 +1180,9 @@ output_flex_t::output_flex_t(output_flex_t const *other,
m_area_buffer(1024, osmium::memory::Buffer::auto_grow::yes),
m_process_node(other->m_process_node), m_process_way(other->m_process_way),
m_process_relation(other->m_process_relation),
m_process_untagged_node(other->m_process_untagged_node),
m_process_untagged_way(other->m_process_untagged_way),
m_process_untagged_relation(other->m_process_untagged_relation),
m_select_relation_members(other->m_select_relation_members),
m_after_nodes(other->m_after_nodes), m_after_ways(other->m_after_ways),
m_after_relations(other->m_after_relations)
@ -1402,9 +1414,19 @@ void output_flex_t::init_lua(std::string const &filename,
lua_state(), calling_context::process_way, "process_way"};
m_process_relation = prepared_lua_function_t{
lua_state(), calling_context::process_relation, "process_relation"};
m_process_untagged_node = prepared_lua_function_t{
lua_state(), calling_context::process_node, "process_untagged_node"};
m_process_untagged_way = prepared_lua_function_t{
lua_state(), calling_context::process_way, "process_untagged_way"};
m_process_untagged_relation =
prepared_lua_function_t{lua_state(), calling_context::process_relation,
"process_untagged_relation"};
m_select_relation_members = prepared_lua_function_t{
lua_state(), calling_context::select_relation_members,
"select_relation_members", 1};
m_after_nodes = prepared_lua_function_t{lua_state(), calling_context::main,
"after_nodes"};
m_after_ways = prepared_lua_function_t{lua_state(), calling_context::main,

View File

@ -301,7 +301,13 @@ private:
prepared_lua_function_t m_process_node{};
prepared_lua_function_t m_process_way{};
prepared_lua_function_t m_process_relation{};
prepared_lua_function_t m_process_untagged_node{};
prepared_lua_function_t m_process_untagged_way{};
prepared_lua_function_t m_process_untagged_relation{};
prepared_lua_function_t m_select_relation_members{};
prepared_lua_function_t m_after_nodes{};
prepared_lua_function_t m_after_ways{};
prepared_lua_function_t m_after_relations{};