Skip to content

Commit a89afda

Browse files
committed
Allow calling as_multilinestring/as_multipolygon from ways
1 parent 76c36c6 commit a89afda

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

src/output-flex.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -885,9 +885,18 @@ int output_flex_t::app_as_polygon()
885885

886886
int output_flex_t::app_as_multilinestring()
887887
{
888-
check_context_and_state("as_multilinestring", "process_relation() function",
889-
m_calling_context !=
890-
calling_context::process_relation);
888+
check_context_and_state(
889+
"as_multilinestring", "process_way/relation() functions",
890+
m_calling_context != calling_context::process_way &&
891+
m_calling_context != calling_context::process_relation);
892+
893+
if (m_calling_context == calling_context::process_way) {
894+
m_way_cache.add_nodes(middle());
895+
896+
auto *geom = create_lua_geometry_object(lua_state());
897+
geom::create_linestring(geom, m_way_cache.get());
898+
return 1;
899+
}
891900

892901
m_relation_cache.add_members(middle());
893902

@@ -900,9 +909,19 @@ int output_flex_t::app_as_multilinestring()
900909

901910
int output_flex_t::app_as_multipolygon()
902911
{
903-
check_context_and_state("as_multipolygon", "process_relation() function",
904-
m_calling_context !=
905-
calling_context::process_relation);
912+
check_context_and_state(
913+
"as_multipolygon", "process_way/relation() functions",
914+
m_calling_context != calling_context::process_way &&
915+
m_calling_context != calling_context::process_relation);
916+
917+
if (m_calling_context == calling_context::process_way) {
918+
m_way_cache.add_nodes(middle());
919+
920+
auto *geom = create_lua_geometry_object(lua_state());
921+
geom::create_polygon(geom, m_way_cache.get());
922+
923+
return 1;
924+
}
906925

907926
m_relation_cache.add_members(middle());
908927

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Feature: Creating (multi)linestring features from way and relations
2+
3+
Scenario:
4+
Given the grid
5+
| 1 | 2 | |
6+
| 4 | | 3 |
7+
| | 5 | 6 |
8+
And the OSM data
9+
"""
10+
w20 Thighway=motorway Nn1,n2,n3
11+
w21 Thighway=motorway Nn4,n5,n6
12+
r30 Ttype=route,route=road Mw20@
13+
r31 Ttype=route,route=road Mw20@,w21@
14+
"""
15+
And the lua style
16+
"""
17+
local lines = osm2pgsql.define_table({
18+
name = 'osm2pgsql_test_lines',
19+
ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
20+
columns = {
21+
{ column = 'geom', type = 'geometry', projection = 4326 },
22+
}
23+
})
24+
25+
function osm2pgsql.process_way(object)
26+
if object.tags.highway == 'motorway' then
27+
lines:insert({
28+
geom = object:as_multilinestring()
29+
})
30+
end
31+
end
32+
33+
function osm2pgsql.process_relation(object)
34+
if object.tags.type == 'route' then
35+
lines:insert({
36+
geom = object:as_multilinestring()
37+
})
38+
end
39+
end
40+
"""
41+
When running osm2pgsql flex
42+
43+
Then table osm2pgsql_test_lines contains exactly
44+
| osm_type | osm_id | ST_GeometryType(geom) | ST_AsText(ST_GeometryN(geom, 1)) | ST_AsText(ST_GeometryN(geom, 2)) |
45+
| W | 20 | ST_LineString | 1, 2, 3 | NULL |
46+
| W | 21 | ST_LineString | 4, 5, 6 | NULL |
47+
| R | 30 | ST_LineString | 1, 2, 3 | NULL |
48+
| R | 31 | ST_MultiLineString | 1, 2, 3 | 4, 5, 6 |
49+

tests/bdd/flex/geometry-processing.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Feature: Tests for Lua geometry processing functions
7474
geomsimple = object:as_linestring():simplify(0.1)
7575
})
7676
77-
local g = object:as_polygon()
77+
local g = object:as_multipolygon()
7878
tables.polygons:insert({
7979
name = object.tags.name,
8080
geom = g,

0 commit comments

Comments
 (0)