forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtagtransform-lua.cpp
More file actions
213 lines (179 loc) · 6.72 KB
/
tagtransform-lua.cpp
File metadata and controls
213 lines (179 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2025 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "format.hpp"
#include "lua-utils.hpp"
#include "tagtransform-lua.hpp"
#include <stdexcept>
lua_tagtransform_t::lua_tagtransform_t(std::string const *tag_transform_script,
bool extra_attributes)
: m_lua_file(tag_transform_script), m_extra_attributes(extra_attributes)
{
m_lua_state.reset(luaL_newstate());
luaL_openlibs(lua_state());
if (luaL_dofile(lua_state(), m_lua_file->c_str())) {
throw fmt_error("Lua tag transform style error: {}.",
lua_tostring(lua_state(), -1));
}
check_lua_function_exists(NODE_FUNC);
check_lua_function_exists(WAY_FUNC);
check_lua_function_exists(REL_FUNC);
check_lua_function_exists(REL_MEM_FUNC);
}
std::unique_ptr<tagtransform_t> lua_tagtransform_t::clone() const
{
return std::make_unique<lua_tagtransform_t>(m_lua_file, m_extra_attributes);
}
void lua_tagtransform_t::check_lua_function_exists(char const *func_name)
{
lua_getglobal(lua_state(), func_name);
if (!lua_isfunction(lua_state(), -1)) {
throw fmt_error("Tag transform style does not contain a function {}.",
func_name);
}
lua_pop(lua_state(), 1);
}
namespace {
/**
* Read tags from the Lua table on the stack and write them to out_tags
*/
void get_out_tags(lua_State *lua_state, taglist_t *out_tags)
{
luaX_for_each(lua_state, [&]() {
auto const key_type = lua_type(lua_state, -2);
// They key must be a string, otherwise the lua_tostring() function
// below will change it to a string and the Lua array iteration will
// break.
if (key_type != LUA_TSTRING) {
throw fmt_error("Basic tag processing found incorrect data type"
" '{}', use a string.",
lua_typename(lua_state, key_type));
}
auto const value_type = lua_type(lua_state, -1);
// They key must be a string or number (which will automatically be
// converted to a string).
if (value_type != LUA_TSTRING && value_type != LUA_TNUMBER) {
throw fmt_error("Basic tag processing found incorrect data type"
" '{}', use a string.",
lua_typename(lua_state, value_type));
}
char const *const key = lua_tostring(lua_state, -2);
char const *const value = lua_tostring(lua_state, -1);
out_tags->add_tag(key, value);
});
lua_pop(lua_state, 1);
}
} // anonymous namespace
bool lua_tagtransform_t::filter_tags(osmium::OSMObject const &o, bool *polygon,
bool *roads, taglist_t *out_tags)
{
switch (o.type()) {
case osmium::item_type::node:
lua_getglobal(lua_state(), NODE_FUNC);
break;
case osmium::item_type::way:
lua_getglobal(lua_state(), WAY_FUNC);
break;
case osmium::item_type::relation:
lua_getglobal(lua_state(), REL_FUNC);
break;
default:
throw std::runtime_error{"Unknown OSM type."};
}
lua_newtable(lua_state()); /* key value table */
lua_Integer sz = 0;
for (auto const &t : o.tags()) {
lua_pushstring(lua_state(), t.key());
lua_pushstring(lua_state(), t.value());
lua_rawset(lua_state(), -3);
++sz;
}
if (m_extra_attributes && o.version() > 0) {
taglist_t tags;
tags.add_attributes(o);
for (auto const &t : tags) {
luaX_pushstring(lua_state(), t.key);
luaX_pushstring(lua_state(), t.value);
lua_rawset(lua_state(), -3);
++sz;
}
}
lua_pushinteger(lua_state(), sz);
if (lua_pcall(lua_state(), 2, (o.type() == osmium::item_type::way) ? 4 : 2,
0)) {
/* lua function failed */
throw fmt_error("Failed to execute lua function for"
" basic tag processing: {}.",
lua_tostring(lua_state(), -1));
}
if (o.type() == osmium::item_type::way) {
if (roads) {
*roads = (int)lua_tointeger(lua_state(), -1);
}
lua_pop(lua_state(), 1);
if (polygon) {
*polygon = (int)lua_tointeger(lua_state(), -1);
}
lua_pop(lua_state(), 1);
}
get_out_tags(lua_state(), out_tags);
bool const filter = lua_tointeger(lua_state(), -1);
lua_pop(lua_state(), 1);
return filter;
}
bool lua_tagtransform_t::filter_rel_member_tags(
taglist_t const &rel_tags, osmium::memory::Buffer const &members,
rolelist_t const &member_roles, bool *make_boundary, bool *make_polygon,
bool *roads, taglist_t *out_tags)
{
size_t const num_members = member_roles.size();
lua_getglobal(lua_state(), REL_MEM_FUNC);
lua_newtable(lua_state()); /* relations key value table */
for (auto const &rel_tag : rel_tags) {
luaX_pushstring(lua_state(), rel_tag.key);
luaX_pushstring(lua_state(), rel_tag.value);
lua_rawset(lua_state(), -3);
}
lua_newtable(lua_state()); /* member tags table */
int idx = 1;
for (auto const &w : members.select<osmium::Way>()) {
lua_pushnumber(lua_state(), idx++);
lua_newtable(lua_state()); /* member key value table */
for (auto const &member_tag : w.tags()) {
lua_pushstring(lua_state(), member_tag.key());
lua_pushstring(lua_state(), member_tag.value());
lua_rawset(lua_state(), -3);
}
lua_rawset(lua_state(), -3);
}
lua_newtable(lua_state()); /* member roles table */
for (size_t i = 0; i < num_members; ++i) {
lua_pushnumber(lua_state(), static_cast<lua_Number>(i + 1));
lua_pushstring(lua_state(), member_roles[i]);
lua_rawset(lua_state(), -3);
}
lua_pushnumber(lua_state(), static_cast<lua_Number>(num_members));
if (lua_pcall(lua_state(), 4, 6, 0)) {
/* lua function failed */
throw fmt_error("Failed to execute lua function for"
" relation tag processing: {}.",
lua_tostring(lua_state(), -1));
}
*roads = (int)lua_tointeger(lua_state(), -1);
lua_pop(lua_state(), 1);
*make_polygon = (int)lua_tointeger(lua_state(), -1);
lua_pop(lua_state(), 1);
*make_boundary = (int)lua_tointeger(lua_state(), -1);
lua_pop(lua_state(), 1);
// obsolete member superseded is ignored.
lua_pop(lua_state(), 1);
get_out_tags(lua_state(), out_tags);
bool const filter = lua_tointeger(lua_state(), -1);
lua_pop(lua_state(), 1);
return filter;
}