File: test_forward_reference_writer.py

package info (click to toggle)
pyosmium 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,584 kB
  • sloc: python: 4,400; cpp: 2,504; makefile: 20
file content (71 lines) | stat: -rw-r--r-- 1,864 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# SPDX-License-Identifier: BSD-2-Clause
#
# This file is part of pyosmium. (https://osmcode.org/pyosmium/)
#
# Copyright (C) 2025 Sarah Hoffmann <lonvia@denofr.de> and others.
# For a full list of authors see the git log.
import uuid
import pytest

import osmium

from helpers import IDCollector


@pytest.fixture
def ref_file(test_data):
    return test_data("""\
                n1 x0 y0
                n2 x0 y0
                n99 x0 y0
                n100 x0 y0
                w12 Nn1,n2
                w90 Nn10,n11
                r2 Mn99@
                r10 Mn100@,w90@,r2@
                """)


class DummyNode:
    def __init__(self, nid):
        self.id = nid
        self.location = (3, 4)


def test_simple_forward_no_back_reference(ref_file, tmp_path):
    outfile = tmp_path / f"{uuid.uuid4()}.osm"

    with osmium.ForwardReferenceWriter(outfile, ref_file, back_references=False) as writer:
        writer.add_node(DummyNode(2))
        writer.add_node(DummyNode(99))

    ids = IDCollector()
    osmium.apply(outfile, ids)

    assert ids.nodes == [2, 99]
    assert ids.ways == [12]
    assert ids.relations == [2]


def test_simple_forward_with_back_reference(ref_file, tmp_path):
    outfile = str(tmp_path / f"{uuid.uuid4()}.osm")

    with osmium.ForwardReferenceWriter(outfile, ref_file) as writer:
        writer.add_node(DummyNode(2))
        writer.add_node(DummyNode(99))

    ids = IDCollector()

    for obj in osmium.FileProcessor(outfile)\
                     .with_filter(ids)\
                     .with_filter(osmium.filter.EntityFilter(osmium.osm.NODE)):
        if obj.id in (2, 99):
            assert obj.lat == 4
            assert obj.lon == 3
        else:
            assert obj.lat == 0
            assert obj.lon == 0

    assert ids.nodes == [1, 2, 99]
    assert ids.ways == [12]
    assert ids.relations == [2]