Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_bbox() function to geometries in Lua #2222

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/flex-lua-geom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

#include "flex-lua-geom.hpp"
#include "geom-box.hpp"
#include "geom-functions.hpp"
#include "geom-pole-of-inaccessibility.hpp"
#include "lua-utils.hpp"
Expand Down Expand Up @@ -242,6 +243,23 @@ int geom_simplify(lua_State *lua_state)
return 1;
}

int geom_get_bbox(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);

try {
auto const box = geom::envelope(*input_geometry);
lua_pushnumber(lua_state, box.min_x());
lua_pushnumber(lua_state, box.min_y());
lua_pushnumber(lua_state, box.max_x());
lua_pushnumber(lua_state, box.max_y());
} catch (...) {
return luaL_error(lua_state, "Unknown error in 'get_bbox()'.\n");
}

return 4;
}

int geom_srid(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
Expand Down Expand Up @@ -298,6 +316,7 @@ void init_geometry_class(lua_State *lua_state)
luaX_add_table_func(lua_state, "area", geom_area);
luaX_add_table_func(lua_state, "length", geom_length);
luaX_add_table_func(lua_state, "centroid", geom_centroid);
luaX_add_table_func(lua_state, "get_bbox", geom_get_bbox);
luaX_add_table_func(lua_state, "geometry_n", geom_geometry_n);
luaX_add_table_func(lua_state, "geometry_type", geom_geometry_type);
luaX_add_table_func(lua_state, "is_null", geom_is_null);
Expand Down
15 changes: 15 additions & 0 deletions tests/bdd/flex/bbox.feature
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ Feature: Test get_bbox() function
function osm2pgsql.process_node(object)
local row = { geom = object:as_point() }
row.min_x, row.min_y, row.max_x, row.max_y = object:get_bbox()
local min_x, min_y, max_x, max_y = object:as_point():get_bbox()
assert(row.min_x == min_x)
assert(row.min_y == min_y)
assert(row.max_x == max_x)
assert(row.max_y == max_y)
points:insert(row)
end
"""
Expand Down Expand Up @@ -53,6 +58,11 @@ Feature: Test get_bbox() function
function osm2pgsql.process_way(object)
local row = { geom = object:as_linestring() }
row.min_x, row.min_y, row.max_x, row.max_y = object:get_bbox()
local min_x, min_y, max_x, max_y = object:as_linestring():get_bbox()
assert(row.min_x == min_x)
assert(row.min_y == min_y)
assert(row.max_x == max_x)
assert(row.max_y == max_y)
highways:insert(row)
end
"""
Expand Down Expand Up @@ -93,6 +103,11 @@ Feature: Test get_bbox() function
row.min_x, row.min_y, row.max_x, row.max_y = object:get_bbox()
for sgeom in object:as_multilinestring():line_merge():geometries() do
row.geom = sgeom
local min_x, min_y, max_x, max_y = sgeom:get_bbox()
assert(row.min_x == min_x)
assert(row.min_y == min_y)
assert(row.max_x == max_x)
assert(row.max_y == max_y)
rels:insert(row)
end
end
Expand Down
Loading