Skip to content

Commit bc87ea2

Browse files
committed
Flex: Process untagged objects in their own callbacks
This happens regardless of whether the -x/--extra-attributes option is set.
1 parent 0c7b201 commit bc87ea2

File tree

5 files changed

+90
-7
lines changed

5 files changed

+90
-7
lines changed

.luacheckrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ stds.osm2pgsql = {
2020
process_relation = {
2121
read_only = false
2222
},
23+
process_untagged_node = {
24+
read_only = false
25+
},
26+
process_untagged_way = {
27+
read_only = false
28+
},
29+
process_untagged_relation = {
30+
read_only = false
31+
},
2332
select_relation_members = {
2433
read_only = false
2534
},

flex-config/untagged.lua

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
-- This config example file is released into the Public Domain.
2+
--
3+
-- Most of the time we are only interested in nodes, ways, and relations that
4+
-- have tags. But we can also get the untagged ones if necessary by using
5+
-- the processing functions 'process_untagged_node', 'process_untagged_way',
6+
-- and 'process_untagged_relation', respectively.
7+
8+
local nodes = osm2pgsql.define_node_table('nodes', {
9+
{ column = 'tags', type = 'jsonb' },
10+
{ column = 'geom', type = 'point' },
11+
})
12+
13+
local ways = osm2pgsql.define_way_table('ways', {
14+
{ column = 'tags', type = 'jsonb' },
15+
{ column = 'geom', type = 'linestring' },
16+
})
17+
18+
function osm2pgsql.process_node(object)
19+
nodes:insert({
20+
tags = object.tags,
21+
geom = object:as_point(),
22+
})
23+
end
24+
25+
function osm2pgsql.process_untagged_node(object)
26+
nodes:insert({
27+
geom = object:as_point(),
28+
})
29+
end
30+
31+
-- If you want to use the same function in both cases, that's also quite easy.
32+
-- For instance like this:
33+
34+
local function do_way(object)
35+
ways:insert({
36+
tags = object.tags,
37+
geom = object:as_linestring(),
38+
})
39+
end
40+
41+
osm2pgsql.process_way = do_way
42+
osm2pgsql.process_untagged_way = do_way
43+
44+

src/osmdata.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ osmdata_t::osmdata_t(std::shared_ptr<middle_t> mid,
3131
: m_mid(std::move(mid)), m_output(std::move(output)),
3232
m_connection_params(options.connection_params), m_bbox(options.bbox),
3333
m_num_procs(options.num_procs), m_append(options.append),
34-
m_droptemp(options.droptemp), m_with_extra_attrs(options.extra_attributes)
34+
m_droptemp(options.droptemp),
35+
m_with_extra_attrs(options.extra_attributes ||
36+
options.output_backend == "flex")
3537
{
3638
assert(m_mid);
3739
assert(m_output);

src/output-flex.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,36 +1047,45 @@ void output_flex_t::wait()
10471047

10481048
void output_flex_t::node_add(osmium::Node const &node)
10491049
{
1050-
if (!m_process_node) {
1050+
auto const &func =
1051+
node.tags().empty() ? m_process_untagged_node : m_process_node;
1052+
1053+
if (!func) {
10511054
return;
10521055
}
10531056

10541057
m_context_node = &node;
1055-
get_mutex_and_call_lua_function(m_process_node, node);
1058+
get_mutex_and_call_lua_function(func, node);
10561059
m_context_node = nullptr;
10571060
}
10581061

10591062
void output_flex_t::way_add(osmium::Way *way)
10601063
{
10611064
assert(way);
10621065

1063-
if (!m_process_way) {
1066+
auto const &func =
1067+
way->tags().empty() ? m_process_untagged_way : m_process_way;
1068+
1069+
if (!func) {
10641070
return;
10651071
}
10661072

10671073
m_way_cache.init(way);
1068-
get_mutex_and_call_lua_function(m_process_way, m_way_cache.get());
1074+
get_mutex_and_call_lua_function(func, m_way_cache.get());
10691075
}
10701076

10711077
void output_flex_t::relation_add(osmium::Relation const &relation)
10721078
{
1073-
if (!m_process_relation) {
1079+
auto const &func = relation.tags().empty() ? m_process_untagged_relation
1080+
: m_process_relation;
1081+
1082+
if (!func) {
10741083
return;
10751084
}
10761085

10771086
m_relation_cache.init(relation);
10781087
select_relation_members();
1079-
get_mutex_and_call_lua_function(m_process_relation, relation);
1088+
get_mutex_and_call_lua_function(func, relation);
10801089
}
10811090

10821091
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,
11711180
m_area_buffer(1024, osmium::memory::Buffer::auto_grow::yes),
11721181
m_process_node(other->m_process_node), m_process_way(other->m_process_way),
11731182
m_process_relation(other->m_process_relation),
1183+
m_process_untagged_node(other->m_process_untagged_node),
1184+
m_process_untagged_way(other->m_process_untagged_way),
1185+
m_process_untagged_relation(other->m_process_untagged_relation),
11741186
m_select_relation_members(other->m_select_relation_members),
11751187
m_after_nodes(other->m_after_nodes), m_after_ways(other->m_after_ways),
11761188
m_after_relations(other->m_after_relations)
@@ -1402,9 +1414,19 @@ void output_flex_t::init_lua(std::string const &filename,
14021414
lua_state(), calling_context::process_way, "process_way"};
14031415
m_process_relation = prepared_lua_function_t{
14041416
lua_state(), calling_context::process_relation, "process_relation"};
1417+
1418+
m_process_untagged_node = prepared_lua_function_t{
1419+
lua_state(), calling_context::process_node, "process_untagged_node"};
1420+
m_process_untagged_way = prepared_lua_function_t{
1421+
lua_state(), calling_context::process_way, "process_untagged_way"};
1422+
m_process_untagged_relation =
1423+
prepared_lua_function_t{lua_state(), calling_context::process_relation,
1424+
"process_untagged_relation"};
1425+
14051426
m_select_relation_members = prepared_lua_function_t{
14061427
lua_state(), calling_context::select_relation_members,
14071428
"select_relation_members", 1};
1429+
14081430
m_after_nodes = prepared_lua_function_t{lua_state(), calling_context::main,
14091431
"after_nodes"};
14101432
m_after_ways = prepared_lua_function_t{lua_state(), calling_context::main,

src/output-flex.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,13 @@ class output_flex_t : public output_t
301301
prepared_lua_function_t m_process_node{};
302302
prepared_lua_function_t m_process_way{};
303303
prepared_lua_function_t m_process_relation{};
304+
305+
prepared_lua_function_t m_process_untagged_node{};
306+
prepared_lua_function_t m_process_untagged_way{};
307+
prepared_lua_function_t m_process_untagged_relation{};
308+
304309
prepared_lua_function_t m_select_relation_members{};
310+
305311
prepared_lua_function_t m_after_nodes{};
306312
prepared_lua_function_t m_after_ways{};
307313
prepared_lua_function_t m_after_relations{};

0 commit comments

Comments
 (0)