Skip to content

Commit

Permalink
Flex: Process untagged objects in their own callbacks
Browse files Browse the repository at this point in the history
This happens regardless of whether the -x/--extra-attributes option is
set.
  • Loading branch information
joto committed Aug 16, 2024
1 parent 8db072c commit dce55a4
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
44 changes: 44 additions & 0 deletions flex-config/untagged.lua
Original file line number Diff line number Diff line change
@@ -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:

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


4 changes: 3 additions & 1 deletion src/osmdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 28 additions & 6 deletions src/output-flex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1047,36 +1047,45 @@ 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;
}

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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions src/output-flex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,13 @@ class output_flex_t : public output_t
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{};
Expand Down

0 comments on commit dce55a4

Please sign in to comment.