Skip to content

Commit 7ed5f2f

Browse files
authored
Merge pull request #1850 from joto/refactor-output-flex
Refactoring: Move more functions out of huge output-flex.cpp
2 parents 0b89c46 + 86c647e commit 7ed5f2f

File tree

3 files changed

+86
-82
lines changed

3 files changed

+86
-82
lines changed

src/geom-transform.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,80 @@ void init_geom_transform(geom_transform_t *transform, lua_State *lua_state)
173173
lua_pop(lua_state, 1);
174174
}
175175
}
176+
177+
std::unique_ptr<geom_transform_t>
178+
get_transform(lua_State *lua_state, flex_table_column_t const &column)
179+
{
180+
assert(lua_state);
181+
assert(lua_gettop(lua_state) == 1);
182+
183+
std::unique_ptr<geom_transform_t> transform{};
184+
185+
lua_getfield(lua_state, -1, column.name().c_str());
186+
int const ltype = lua_type(lua_state, -1);
187+
188+
// Field not set, return null transform
189+
if (ltype == LUA_TNIL) {
190+
lua_pop(lua_state, 1); // geom field
191+
return transform;
192+
}
193+
194+
// Field set to anything but a Lua table is not allowed
195+
if (ltype != LUA_TTABLE) {
196+
lua_pop(lua_state, 1); // geom field
197+
throw std::runtime_error{
198+
"Invalid geometry transformation for column '{}'."_format(
199+
column.name())};
200+
}
201+
202+
lua_getfield(lua_state, -1, "create");
203+
char const *create_type = lua_tostring(lua_state, -1);
204+
if (create_type == nullptr) {
205+
throw std::runtime_error{
206+
"Missing geometry transformation for column '{}'."_format(
207+
column.name())};
208+
}
209+
210+
transform = create_geom_transform(create_type);
211+
lua_pop(lua_state, 1); // 'create' field
212+
init_geom_transform(transform.get(), lua_state);
213+
if (!transform->is_compatible_with(column.type())) {
214+
throw std::runtime_error{
215+
"Geometry transformation is not compatible "
216+
"with column type '{}'."_format(column.type_name())};
217+
}
218+
219+
lua_pop(lua_state, 1); // geom field
220+
221+
return transform;
222+
}
223+
224+
geom_transform_t const *get_default_transform(flex_table_column_t const &column,
225+
osmium::item_type object_type)
226+
{
227+
static geom_transform_point_t const default_transform_node_to_point{};
228+
static geom_transform_line_t const default_transform_way_to_line{};
229+
static geom_transform_area_t const default_transform_way_to_area{};
230+
231+
switch (object_type) {
232+
case osmium::item_type::node:
233+
if (column.type() == table_column_type::point) {
234+
return &default_transform_node_to_point;
235+
}
236+
break;
237+
case osmium::item_type::way:
238+
if (column.type() == table_column_type::linestring) {
239+
return &default_transform_way_to_line;
240+
}
241+
if (column.type() == table_column_type::polygon) {
242+
return &default_transform_way_to_area;
243+
}
244+
break;
245+
default:
246+
break;
247+
}
248+
249+
throw std::runtime_error{
250+
"Missing geometry transformation for column '{}'."_format(
251+
column.name())};
252+
}

src/geom-transform.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,10 @@ std::unique_ptr<geom_transform_t> create_geom_transform(char const *type);
122122

123123
void init_geom_transform(geom_transform_t *transform, lua_State *lua_state);
124124

125+
std::unique_ptr<geom_transform_t>
126+
get_transform(lua_State *lua_state, flex_table_column_t const &column);
127+
128+
geom_transform_t const *get_default_transform(flex_table_column_t const &column,
129+
osmium::item_type object_type);
130+
125131
#endif // OSM2PGSQL_GEOM_TRANSFORM_HPP

src/output-flex.cpp

Lines changed: 3 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,84 +1002,6 @@ int output_flex_t::table_cluster()
10021002
return 1;
10031003
}
10041004

1005-
static std::unique_ptr<geom_transform_t>
1006-
get_transform(lua_State *lua_state, flex_table_column_t const &column)
1007-
{
1008-
assert(lua_state);
1009-
assert(lua_gettop(lua_state) == 1);
1010-
1011-
std::unique_ptr<geom_transform_t> transform{};
1012-
1013-
lua_getfield(lua_state, -1, column.name().c_str());
1014-
int const ltype = lua_type(lua_state, -1);
1015-
1016-
// Field not set, return null transform
1017-
if (ltype == LUA_TNIL) {
1018-
lua_pop(lua_state, 1); // geom field
1019-
return transform;
1020-
}
1021-
1022-
// Field set to anything but a Lua table is not allowed
1023-
if (ltype != LUA_TTABLE) {
1024-
lua_pop(lua_state, 1); // geom field
1025-
throw std::runtime_error{
1026-
"Invalid geometry transformation for column '{}'."_format(
1027-
column.name())};
1028-
}
1029-
1030-
lua_getfield(lua_state, -1, "create");
1031-
char const *create_type = lua_tostring(lua_state, -1);
1032-
if (create_type == nullptr) {
1033-
throw std::runtime_error{
1034-
"Missing geometry transformation for column '{}'."_format(
1035-
column.name())};
1036-
}
1037-
1038-
transform = create_geom_transform(create_type);
1039-
lua_pop(lua_state, 1); // 'create' field
1040-
init_geom_transform(transform.get(), lua_state);
1041-
if (!transform->is_compatible_with(column.type())) {
1042-
throw std::runtime_error{
1043-
"Geometry transformation is not compatible "
1044-
"with column type '{}'."_format(column.type_name())};
1045-
}
1046-
1047-
lua_pop(lua_state, 1); // geom field
1048-
1049-
return transform;
1050-
}
1051-
1052-
static geom_transform_t const *
1053-
get_default_transform(flex_table_column_t const &column,
1054-
osmium::item_type object_type)
1055-
{
1056-
static geom_transform_point_t const default_transform_node_to_point{};
1057-
static geom_transform_line_t const default_transform_way_to_line{};
1058-
static geom_transform_area_t const default_transform_way_to_area{};
1059-
1060-
switch (object_type) {
1061-
case osmium::item_type::node:
1062-
if (column.type() == table_column_type::point) {
1063-
return &default_transform_node_to_point;
1064-
}
1065-
break;
1066-
case osmium::item_type::way:
1067-
if (column.type() == table_column_type::linestring) {
1068-
return &default_transform_way_to_line;
1069-
}
1070-
if (column.type() == table_column_type::polygon) {
1071-
return &default_transform_way_to_area;
1072-
}
1073-
break;
1074-
default:
1075-
break;
1076-
}
1077-
1078-
throw std::runtime_error{
1079-
"Missing geometry transformation for column '{}'."_format(
1080-
column.name())};
1081-
}
1082-
10831005
geom::geometry_t output_flex_t::run_transform(reprojection const &proj,
10841006
geom_transform_t const *transform,
10851007
osmium::Node const &node)
@@ -1140,18 +1062,17 @@ void output_flex_t::add_row(table_connection_t *table_connection,
11401062
"Need two parameters: The osm2pgsql.Table and the row data."};
11411063
}
11421064

1065+
auto const &proj = table_connection->proj();
1066+
auto const type = table.geom_column().type();
1067+
11431068
auto const geom_transform = get_transform(lua_state(), table.geom_column());
11441069
assert(lua_gettop(lua_state()) == 1);
11451070

11461071
geom_transform_t const *transform = geom_transform.get();
1147-
11481072
if (!transform) {
11491073
transform = get_default_transform(table.geom_column(), object.type());
11501074
}
11511075

1152-
auto const &proj = table_connection->proj();
1153-
auto const type = table.geom_column().type();
1154-
11551076
// The geometry returned by run_transform() is in 4326 if it is a
11561077
// (multi)polygon. If it is a point or linestring, it is already in the
11571078
// target geometry.

0 commit comments

Comments
 (0)