Skip to content

Commit eaf269b

Browse files
committed
Add function to compare geometries for equality from Lua
This can, for instance, be used to check whether there are consecutive nodes with the same location in a way as seen in the test.
1 parent ce7d24a commit eaf269b

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

src/flex-lua-geom.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ geom::geometry_t *unpack_geometry(lua_State *lua_state, int n) noexcept
3939

4040
namespace {
4141

42+
int geom_equals(lua_State *lua_state)
43+
{
44+
auto const *const geometry1 = unpack_geometry(lua_state, 1);
45+
auto const *const geometry2 = unpack_geometry(lua_state, 2);
46+
47+
lua_pushboolean(lua_state, *geometry1 == *geometry2);
48+
49+
return 1;
50+
}
51+
4252
/**
4353
* This function is called by Lua garbage collection when a geometry object
4454
* needs cleaning up. It calls the destructor of the C++ object. After that
@@ -335,7 +345,8 @@ void init_geometry_class(lua_State *lua_state)
335345
{
336346
luaX_set_up_metatable(
337347
lua_state, "Geometry", OSM2PGSQL_GEOMETRY_CLASS,
338-
{{"__gc", geom_gc},
348+
{{"__eq", geom_equals},
349+
{"__gc", geom_gc},
339350
{"__len", geom_num_geometries},
340351
{"__tostring", geom_tostring},
341352
{"area", geom_area},

tests/bdd/flex/geometry-linestring.feature

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,37 @@ Feature: Creating linestring features from way
7272
Geometry data for geometry column 'geom' has the wrong type (LINESTRING).
7373
"""
7474

75+
Scenario:
76+
Given the grid
77+
| 1 | 2 |
78+
And the OSM data
79+
"""
80+
w20 Thighway=motorway Nn1,n1,n2
81+
"""
82+
And the lua style
83+
"""
84+
local points = osm2pgsql.define_way_table('osm2pgsql_test', {
85+
{ column = 'geom', type = 'point', projection = 4326 },
86+
{ column = 'dupl', type = 'boolean' },
87+
})
88+
89+
function osm2pgsql.process_way(object)
90+
if #object.nodes > 1 then
91+
local prev = object:as_point(1)
92+
points:insert({ geom = prev, dupl = false })
93+
for n = 2, #object.nodes do
94+
local geom = object:as_point(n)
95+
points:insert({ geom = geom, dupl = (prev == geom) })
96+
end
97+
end
98+
end
99+
100+
"""
101+
When running osm2pgsql flex
102+
103+
Then table osm2pgsql_test contains exactly
104+
| way_id | geom!geo | dupl |
105+
| 20 | 1 | False |
106+
| 20 | 1 | True |
107+
| 20 | 2 | False |
108+

0 commit comments

Comments
 (0)