Skip to content

Commit 04d6b9e

Browse files
committed
allow list of RelationMembers in member list for writers
Also adds tests for adding osmium objects to the writer. Fixes #194.
1 parent 7ab5ec6 commit 04d6b9e

File tree

2 files changed

+105
-42
lines changed

2 files changed

+105
-42
lines changed

lib/simple_writer.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,16 @@ class SimpleWriter
217217
osmium::builder::RelationMemberListBuilder rml_builder(buffer, builder);
218218

219219
for (auto m: it) {
220-
auto member = m.cast<py::tuple>();
221-
auto type = member[0].cast<std::string>();
222-
auto id = member[1].cast<osmium::object_id_type>();
223-
auto role = member[2].cast<std::string>();
224-
rml_builder.add_member(osmium::char_to_item_type(type[0]), id, role.c_str());
220+
if (py::isinstance<osmium::RelationMember>(m)) {
221+
auto &member = m.cast<osmium::RelationMember &>();
222+
rml_builder.add_member(member.type(), member.ref(), member.role());
223+
} else {
224+
auto member = m.cast<py::tuple>();
225+
auto type = member[0].cast<std::string>();
226+
auto id = member[1].cast<osmium::object_id_type>();
227+
auto role = member[2].cast<std::string>();
228+
rml_builder.add_member(osmium::char_to_item_type(type[0]), id, role.c_str());
229+
}
225230
}
226231
}
227232

test/test_writer.py

Lines changed: 95 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def __init__(self, **params):
4040
setattr(self, k, v)
4141

4242

43-
@pytest.fixture(params=[
43+
@pytest.mark.parametrize('osmobj, out_attr', [
4444
(O(id=None), '0 v0 dV c0 t i0 u T'),
4545
(O(visible=None), '0 v0 dV c0 t i0 u T'),
4646
(O(version=None), '0 v0 dV c0 t i0 u T'),
@@ -59,23 +59,21 @@ def __init__(self, **params):
5959
(O(timestamp=mkdate(2009, 4, 14, 20, 58, 35)), '0 v0 dV c0 t2009-04-14T20:58:35Z i0 u T'),
6060
(O(timestamp='1970-01-01T00:00:01Z'), '0 v0 dV c0 t1970-01-01T00:00:01Z i0 u T')
6161
])
62-
def attr_sample(request):
63-
return request.param
62+
class TestWriteAttributes:
63+
def test_node_simple_attr(self, test_writer, osmobj, out_attr):
64+
with test_writer('n' + out_attr + ' x y') as w:
65+
w.add_node(osmobj)
6466

65-
def test_node_simple_attr(test_writer, attr_sample):
66-
with test_writer('n' + attr_sample[1] + ' x y') as w:
67-
w.add_node(attr_sample[0])
67+
def test_way_simple_attr(self, test_writer, osmobj, out_attr):
68+
with test_writer('w' + out_attr + ' N') as w:
69+
w.add_way(osmobj)
6870

69-
def test_way_simple_attr(test_writer, attr_sample):
70-
with test_writer('w' + attr_sample[1] + ' N') as w:
71-
w.add_way(attr_sample[0])
71+
def test_relation_simple_attr(self, test_writer, osmobj, out_attr):
72+
with test_writer('r' + out_attr + ' M') as w:
73+
w.add_relation(osmobj)
7274

73-
def test_relation_simple_attr(test_writer, attr_sample):
74-
with test_writer('r' + attr_sample[1] + ' M') as w:
75-
w.add_relation(attr_sample[0])
7675

77-
78-
@pytest.fixture(params = [
76+
@pytest.mark.parametrize('tags,out', [
7977
(None, 'T'),
8078
([], 'T'),
8179
({}, 'T'),
@@ -84,33 +82,26 @@ def test_relation_simple_attr(test_writer, attr_sample):
8482
({'test' : 'drive'}, 'Ttest=drive'),
8583
(OrderedDict((('a', 'b'), ('c', '3'))), 'Ta=b,c=3'),
8684
])
87-
def tags_sample(request):
88-
return request.param
89-
90-
def test_node_tags(test_writer, tags_sample):
91-
with test_writer('n0 v0 dV c0 t i0 u ' + tags_sample[1] + ' x y') as w:
92-
w.add_node(O(tags=tags_sample[0]))
93-
94-
def test_way_tags(test_writer, tags_sample):
95-
with test_writer('w0 v0 dV c0 t i0 u ' + tags_sample[1] + ' N') as w:
96-
w.add_way(O(tags=tags_sample[0]))
97-
98-
def test_relation_tags(test_writer, tags_sample):
99-
with test_writer('r0 v0 dV c0 t i0 u ' + tags_sample[1] + ' M') as w:
100-
w.add_relation(O(tags=tags_sample[0]))
85+
class TestWriteTags:
86+
def test_node_tags(self, test_writer, tags, out):
87+
with test_writer('n0 v0 dV c0 t i0 u ' + out + ' x y') as w:
88+
w.add_node(O(tags=tags))
10189

90+
def test_way_tags(self, test_writer, tags, out):
91+
with test_writer('w0 v0 dV c0 t i0 u ' + out + ' N') as w:
92+
w.add_way(O(tags=tags))
10293

103-
def test_location_tuple(test_writer):
104-
with test_writer('n0 v0 dV c0 t i0 u T x1.1234561 y0.1234561') as w:
105-
w.add_node(O(location=(1.1234561, 0.1234561)))
94+
def test_relation_tags(self, test_writer, tags, out):
95+
with test_writer('r0 v0 dV c0 t i0 u ' + out + ' M') as w:
96+
w.add_relation(O(tags=tags))
10697

107-
def test_location_rounding(test_writer):
108-
with test_writer('n0 v0 dV c0 t i0 u T x30.46 y50.37') as w:
109-
w.add_node(O(location=(30.46, 50.37)))
11098

111-
def test_location_none(test_writer):
112-
with test_writer('n0 v0 dV c0 t i0 u T x y') as w:
113-
w.add_node(O(location=None))
99+
@pytest.mark.parametrize("location,out", [((1.1234561, 0.1234561), 'x1.1234561 y0.1234561'),
100+
((30.46, 50.37), 'x30.46 y50.37'),
101+
(None, 'x y')])
102+
def test_location(test_writer, location, out):
103+
with test_writer('n0 v0 dV c0 t i0 u T ' + out) as w:
104+
w.add_node(O(location=location))
114105

115106

116107
def test_node_list(test_writer):
@@ -132,3 +123,70 @@ def test_relation_members(test_writer):
132123
def test_relation_members_None(test_writer):
133124
with test_writer('r0 v0 dV c0 t i0 u T M') as w:
134125
w.add_relation(O(members=None))
126+
127+
128+
def test_node_object(test_writer, simple_handler):
129+
node_opl = 'n235 v1 dV c0 t i0 u Telephant=yes x98.7 y-3.45'
130+
131+
with test_writer(node_opl) as w:
132+
simple_handler(node_opl, node=lambda o: w.add_node(o))
133+
134+
135+
def test_location_object(test_writer, simple_handler):
136+
node_opl = 'n235 v1 dV c0 t i0 u Telephant=yes x98.7 y-3.45'
137+
138+
with test_writer('n0 v0 dV c0 t i0 u T x98.7 y-3.45') as w:
139+
simple_handler(node_opl, node=lambda o: w.add_node(O(location=o.location)))
140+
141+
142+
def test_tag_object(test_writer, simple_handler):
143+
node_opl = 'n235 v1 dV c0 t i0 u Telephant=yes x98.7 y-3.45'
144+
145+
with test_writer('n0 v0 dV c0 t i0 u Telephant=yes x y') as w:
146+
simple_handler(node_opl, node=lambda o: w.add_node(O(tags=o.tags)))
147+
148+
149+
def test_way_object(test_writer, simple_handler):
150+
way_opl = 'w45 v14 dV c0 t i0 u Thighway=top Nn23,n56,n34,n23'
151+
152+
with test_writer(way_opl) as w:
153+
simple_handler(way_opl, way=lambda o: w.add_way(o))
154+
155+
156+
def test_nodelist_object(test_writer, simple_handler):
157+
way_opl = 'w45 v14 dV c0 t i0 u Thighway=top Nn23,n56,n34,n23'
158+
159+
with test_writer('w0 v0 dV c0 t i0 u T Nn23,n56,n34,n23') as w:
160+
simple_handler(way_opl, way=lambda o: w.add_way(O(nodes=o.nodes)))
161+
162+
163+
def test_noderef_object(test_writer, simple_handler):
164+
way_opl = 'w45 v14 dV c0 t i0 u Thighway=top Nn23,n56,n34,n23'
165+
166+
with test_writer('w0 v0 dV c0 t i0 u T Nn56,n34') as w:
167+
simple_handler(way_opl,
168+
way=lambda o: w.add_way(O(nodes=[n for n in o.nodes if n.ref != 23])))
169+
170+
171+
def test_relation_object(test_writer, simple_handler):
172+
rel_opl = 'r2 v0 dV c0 t i0 u Ttype=multipolygon Mw1@,w2@,w3@inner'
173+
174+
with test_writer(rel_opl) as w:
175+
simple_handler(rel_opl, relation=lambda o: w.add_relation(o))
176+
177+
178+
def test_memberlist_object(test_writer, simple_handler):
179+
rel_opl = 'r2 v0 dV c0 t i0 u Ttype=multipolygon Mw1@,w2@,w3@inner'
180+
181+
with test_writer('r0 v0 dV c0 t i0 u T Mw1@,w2@,w3@inner') as w:
182+
simple_handler(rel_opl,
183+
relation=lambda o: w.add_relation(O(members=o.members)))
184+
185+
186+
def test_member_object(test_writer, simple_handler):
187+
rel_opl = 'r2 v0 dV c0 t i0 u Ttype=multipolygon Mw1@,w2@,w3@inner'
188+
189+
with test_writer('r0 v0 dV c0 t i0 u T Mw1@,w2@') as w:
190+
simple_handler(rel_opl,
191+
relation=lambda o: w.add_relation(O(members=[m for m in o.members
192+
if m.role != 'inner'])))

0 commit comments

Comments
 (0)