Skip to content

Commit f8c78ae

Browse files
authored
Merge pull request #2222 from joto/geom-get-bbox
Add get_bbox() function to geometries in Lua
2 parents d80730d + ac8bc1d commit f8c78ae

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/flex-lua-geom.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
#include "flex-lua-geom.hpp"
11+
#include "geom-box.hpp"
1112
#include "geom-functions.hpp"
1213
#include "geom-pole-of-inaccessibility.hpp"
1314
#include "lua-utils.hpp"
@@ -242,6 +243,23 @@ int geom_simplify(lua_State *lua_state)
242243
return 1;
243244
}
244245

246+
int geom_get_bbox(lua_State *lua_state)
247+
{
248+
auto const *const input_geometry = unpack_geometry(lua_state);
249+
250+
try {
251+
auto const box = geom::envelope(*input_geometry);
252+
lua_pushnumber(lua_state, box.min_x());
253+
lua_pushnumber(lua_state, box.min_y());
254+
lua_pushnumber(lua_state, box.max_x());
255+
lua_pushnumber(lua_state, box.max_y());
256+
} catch (...) {
257+
return luaL_error(lua_state, "Unknown error in 'get_bbox()'.\n");
258+
}
259+
260+
return 4;
261+
}
262+
245263
int geom_srid(lua_State *lua_state)
246264
{
247265
auto const *const input_geometry = unpack_geometry(lua_state);
@@ -298,6 +316,7 @@ void init_geometry_class(lua_State *lua_state)
298316
luaX_add_table_func(lua_state, "area", geom_area);
299317
luaX_add_table_func(lua_state, "length", geom_length);
300318
luaX_add_table_func(lua_state, "centroid", geom_centroid);
319+
luaX_add_table_func(lua_state, "get_bbox", geom_get_bbox);
301320
luaX_add_table_func(lua_state, "geometry_n", geom_geometry_n);
302321
luaX_add_table_func(lua_state, "geometry_type", geom_geometry_type);
303322
luaX_add_table_func(lua_state, "is_null", geom_is_null);

tests/bdd/flex/bbox.feature

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ Feature: Test get_bbox() function
1818
function osm2pgsql.process_node(object)
1919
local row = { geom = object:as_point() }
2020
row.min_x, row.min_y, row.max_x, row.max_y = object:get_bbox()
21+
local min_x, min_y, max_x, max_y = object:as_point():get_bbox()
22+
assert(row.min_x == min_x)
23+
assert(row.min_y == min_y)
24+
assert(row.max_x == max_x)
25+
assert(row.max_y == max_y)
2126
points:insert(row)
2227
end
2328
"""
@@ -53,6 +58,11 @@ Feature: Test get_bbox() function
5358
function osm2pgsql.process_way(object)
5459
local row = { geom = object:as_linestring() }
5560
row.min_x, row.min_y, row.max_x, row.max_y = object:get_bbox()
61+
local min_x, min_y, max_x, max_y = object:as_linestring():get_bbox()
62+
assert(row.min_x == min_x)
63+
assert(row.min_y == min_y)
64+
assert(row.max_x == max_x)
65+
assert(row.max_y == max_y)
5666
highways:insert(row)
5767
end
5868
"""
@@ -93,6 +103,11 @@ Feature: Test get_bbox() function
93103
row.min_x, row.min_y, row.max_x, row.max_y = object:get_bbox()
94104
for sgeom in object:as_multilinestring():line_merge():geometries() do
95105
row.geom = sgeom
106+
local min_x, min_y, max_x, max_y = sgeom:get_bbox()
107+
assert(row.min_x == min_x)
108+
assert(row.min_y == min_y)
109+
assert(row.max_x == max_x)
110+
assert(row.max_y == max_y)
96111
rels:insert(row)
97112
end
98113
end

0 commit comments

Comments
 (0)