Skip to content

Commit

Permalink
Make attributes of OSM objects always available to Lua
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joto committed Aug 16, 2024
1 parent a76ea12 commit 8db072c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 41 deletions.
12 changes: 0 additions & 12 deletions src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,6 @@ end

-- This will be the metatable for the OSM objects given to the process callback
-- functions.
local inner_metatable = {
__index = function(table, key)
if key == 'version' or key == 'timestamp' or
key == 'changeset' or key == 'uid' or key == 'user' then
return nil
end
error("unknown field '" .. key .. "'", 2)
end
}

object_metatable = {
__index = {
grab_tag = function(data, tag)
Expand All @@ -191,8 +181,6 @@ object_metatable = {
}
}

setmetatable(object_metatable.__index, inner_metatable)

-- This is used to iterate over (multi)geometries.
function osm2pgsql.Geometry.geometries(geom)
local i = 0
Expand Down
48 changes: 21 additions & 27 deletions src/output-flex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,14 @@ prepared_lua_function_t::prepared_lua_function_t(lua_State *lua_state,
namespace {

void push_osm_object_to_lua_stack(lua_State *lua_state,
osmium::OSMObject const &object,
bool with_attributes)
osmium::OSMObject const &object)
{
assert(lua_state);

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

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

if (with_attributes) {
if (object.version() != 0U) {
luaX_add_table_int(lua_state, "version", object.version());
}
if (object.timestamp().valid()) {
luaX_add_table_int(lua_state, "timestamp",
object.timestamp().seconds_since_epoch());
}
if (object.changeset() != 0U) {
luaX_add_table_int(lua_state, "changeset", object.changeset());
}
if (object.uid() != 0U) {
luaX_add_table_int(lua_state, "uid", object.uid());
}
if (object.user()[0] != '\0') {
luaX_add_table_str(lua_state, "user", object.user());
}
if (object.version() != 0U) {
luaX_add_table_int(lua_state, "version", object.version());
}
if (object.timestamp().valid()) {
luaX_add_table_int(lua_state, "timestamp",
object.timestamp().seconds_since_epoch());
}
if (object.changeset() != 0U) {
luaX_add_table_int(lua_state, "changeset", object.changeset());
}
if (object.uid() != 0U) {
luaX_add_table_int(lua_state, "uid", object.uid());
}
if (object.user()[0] != '\0') {
luaX_add_table_str(lua_state, "user", object.user());
}

if (object.type() == osmium::item_type::way) {
Expand Down Expand Up @@ -692,8 +689,7 @@ int output_flex_t::table_insert()
lua_pushboolean(lua_state(), false);
lua_pushstring(lua_state(), "null value in not null column.");
lua_pushstring(lua_state(), e.column().name().c_str());
push_osm_object_to_lua_stack(lua_state(), object,
get_options()->extra_attributes);
push_osm_object_to_lua_stack(lua_state(), object);
table_connection.increment_not_null_error_counter();
return 4;
}
Expand Down Expand Up @@ -820,9 +816,7 @@ void output_flex_t::call_lua_function(prepared_lua_function_t func,
m_calling_context = func.context();

lua_pushvalue(lua_state(), func.index()); // the function to call
push_osm_object_to_lua_stack(
lua_state(), object,
get_options()->extra_attributes); // the single argument
push_osm_object_to_lua_stack(lua_state(), object); // the single argument

luaX_set_context(lua_state(), this);
if (luaX_pcall(lua_state(), 1, func.nresults())) {
Expand Down
4 changes: 2 additions & 2 deletions tests/bdd/flex/extra-attributes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ Feature: Tests for including extra attributes
| --slim |

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

Given the grid
| | |
Expand Down

0 comments on commit 8db072c

Please sign in to comment.