Skip to content

Commit 8db072c

Browse files
committed
Make attributes of OSM objects always available to Lua
I did some benchmarking and it turns out there is real no measurable difference between putting the data in there and not doing it. So we'll just always do it. This means that it doesn't depend on the -x/--extra-attributes command line option any more. Note that the attributes will still be nil when coming from the middle in update mode if -x/--extra-attributes was not used on import.
1 parent a76ea12 commit 8db072c

File tree

3 files changed

+23
-41
lines changed

3 files changed

+23
-41
lines changed

src/init.lua

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,6 @@ end
168168

169169
-- This will be the metatable for the OSM objects given to the process callback
170170
-- functions.
171-
local inner_metatable = {
172-
__index = function(table, key)
173-
if key == 'version' or key == 'timestamp' or
174-
key == 'changeset' or key == 'uid' or key == 'user' then
175-
return nil
176-
end
177-
error("unknown field '" .. key .. "'", 2)
178-
end
179-
}
180-
181171
object_metatable = {
182172
__index = {
183173
grab_tag = function(data, tag)
@@ -191,8 +181,6 @@ object_metatable = {
191181
}
192182
}
193183

194-
setmetatable(object_metatable.__index, inner_metatable)
195-
196184
-- This is used to iterate over (multi)geometries.
197185
function osm2pgsql.Geometry.geometries(geom)
198186
local i = 0

src/output-flex.cpp

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,14 @@ prepared_lua_function_t::prepared_lua_function_t(lua_State *lua_state,
126126
namespace {
127127

128128
void push_osm_object_to_lua_stack(lua_State *lua_state,
129-
osmium::OSMObject const &object,
130-
bool with_attributes)
129+
osmium::OSMObject const &object)
131130
{
132131
assert(lua_state);
133132

134133
/**
135-
* Table will always have at least 3 fields (id, type, tags). And 5 more if
136-
* with_attributes is true (version, timestamp, changeset, uid, user). For
137-
* ways there are 2 more (is_closed, nodes), for relations 1 more (members).
134+
* Table will always have at least 8 fields (id, type, tags, version,
135+
* timestamp, changeset, uid, user). For ways there are 2 more (is_closed,
136+
* nodes), for relations 1 more (members).
138137
*/
139138
constexpr int const max_table_size = 10;
140139

@@ -145,23 +144,21 @@ void push_osm_object_to_lua_stack(lua_State *lua_state,
145144
luaX_add_table_str(lua_state, "type",
146145
osmium::item_type_to_name(object.type()));
147146

148-
if (with_attributes) {
149-
if (object.version() != 0U) {
150-
luaX_add_table_int(lua_state, "version", object.version());
151-
}
152-
if (object.timestamp().valid()) {
153-
luaX_add_table_int(lua_state, "timestamp",
154-
object.timestamp().seconds_since_epoch());
155-
}
156-
if (object.changeset() != 0U) {
157-
luaX_add_table_int(lua_state, "changeset", object.changeset());
158-
}
159-
if (object.uid() != 0U) {
160-
luaX_add_table_int(lua_state, "uid", object.uid());
161-
}
162-
if (object.user()[0] != '\0') {
163-
luaX_add_table_str(lua_state, "user", object.user());
164-
}
147+
if (object.version() != 0U) {
148+
luaX_add_table_int(lua_state, "version", object.version());
149+
}
150+
if (object.timestamp().valid()) {
151+
luaX_add_table_int(lua_state, "timestamp",
152+
object.timestamp().seconds_since_epoch());
153+
}
154+
if (object.changeset() != 0U) {
155+
luaX_add_table_int(lua_state, "changeset", object.changeset());
156+
}
157+
if (object.uid() != 0U) {
158+
luaX_add_table_int(lua_state, "uid", object.uid());
159+
}
160+
if (object.user()[0] != '\0') {
161+
luaX_add_table_str(lua_state, "user", object.user());
165162
}
166163

167164
if (object.type() == osmium::item_type::way) {
@@ -692,8 +689,7 @@ int output_flex_t::table_insert()
692689
lua_pushboolean(lua_state(), false);
693690
lua_pushstring(lua_state(), "null value in not null column.");
694691
lua_pushstring(lua_state(), e.column().name().c_str());
695-
push_osm_object_to_lua_stack(lua_state(), object,
696-
get_options()->extra_attributes);
692+
push_osm_object_to_lua_stack(lua_state(), object);
697693
table_connection.increment_not_null_error_counter();
698694
return 4;
699695
}
@@ -820,9 +816,7 @@ void output_flex_t::call_lua_function(prepared_lua_function_t func,
820816
m_calling_context = func.context();
821817

822818
lua_pushvalue(lua_state(), func.index()); // the function to call
823-
push_osm_object_to_lua_stack(
824-
lua_state(), object,
825-
get_options()->extra_attributes); // the single argument
819+
push_osm_object_to_lua_stack(lua_state(), object); // the single argument
826820

827821
luaX_set_context(lua_state(), this);
828822
if (luaX_pcall(lua_state(), 1, func.nresults())) {

tests/bdd/flex/extra-attributes.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ Feature: Tests for including extra attributes
3636
| --slim |
3737

3838
Then table osm2pgsql_test_attr contains
39-
| type | way_id | tags->'highway' | tags->'osm_version' | version | changeset | timestamp | uid | "user" |
40-
| way | 20 | primary | NULL | NULL | NULL | NULL | NULL | NULL |
39+
| type | way_id | tags->'highway' | tags->'osm_version' | version | changeset | timestamp | uid | "user" |
40+
| way | 20 | primary | NULL | 1 | 31 | 1578832496 | 17 | test |
4141

4242
Given the grid
4343
| | |

0 commit comments

Comments
 (0)