test_osm_helper.py 29.6 KB
Newer Older
1 2 3 4 5 6
import os
import unittest

import numpy as np
from lxml import etree

Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
7 8
from osm_helper import (Nd, Node, OsmData, OsmLookup, OsmLookupError,
                        PathToPolygon, PolyObjectWidthId, Tag, Way)
9 10


Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
11
class TestNode(unittest.TestCase):
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    def test_create_node(self):
        node = Node.create_node(1, 3, 5)
        self.assertEqual(node.id, 1)
        self.assertEqual(node.lat, 3)
        self.assertEqual(node.lon, 5)

    def test_equals(self):
        node1 = Node.create_node(1, 3, 5)
        node2 = Node.create_node(2, 4, 4)
        node3 = Node.create_node(1, 3, 5)
        self.assertEquals(node1, node3)
        self.assertNotEqual(node1, node2)

    def test_tags(self):
        node = Node.create_node(1, 3, 5)
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
27
        node.add_tag("foo", "bar")
28 29 30 31
        self.assertListEqual(node.tags, [Tag("rover:id", 1), Tag("foo", "bar")])

    def test_to_xml(self):
        node = Node.create_node(1, 3, 5)
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
32
        node.add_tag("foo", "bar")
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
        str_xml = b'<node id="1" action="modify" visible="true" lon="5" lat="3"><tag k="rover:id" v="1"/><tag k="foo" v="bar"/></node>'
        self.assertEquals(etree.tostring(node.to_xml()), str_xml)


class TestNd(unittest.TestCase):
    def test_Nd(self):
        nd = Nd(3344)
        self.assertEquals(etree.tostring(nd.to_xml()), b'<nd ref="3344"/>')

    def test_equal(self):
        nd1 = Nd(33)
        nd2 = Nd(44)
        nd3 = Nd(33)
        self.assertEqual(nd1, nd3)
        self.assertNotEqual(nd1, nd2)


class TestTag(unittest.TestCase):
    def test_Tag(self):
        tag = Tag("Foo", "Bar")
        self.assertEquals(etree.tostring(tag.to_xml()), b'<tag k="Foo" v="Bar"/>')

    def test_equal(self):
        tag1 = Tag("Foo", "Bar")
        tag2 = Tag("Bar", "Foo")
        tag3 = Tag("Foo", "Bar")
        self.assertEqual(tag1, tag3)
        self.assertNotEqual(tag1, tag2)


class TestWay(unittest.TestCase):
    def test_create_with_refs(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
65
        way = Way.create_with_refs(-3, [1, 2, 3], att={"foo": "bar"}, tags={"baz": 42})
66 67 68
        self.assertEqual(way.id, -3)
        self.assertEqual(way.action, "modify")
        self.assertEqual(way.visible, "true")
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
69
        self.assertDictEqual(way.attr, {"foo": "bar"})
70
        self.assertListEqual(way.nds, [Nd(1), Nd(2), Nd(3)])
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
71
        self.assertListEqual(way.tags, [Tag("rover:id", 3), Tag("baz", 42)])
72 73

    def test_create(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
74 75 76 77 78 79 80 81 82 83
        way = Way.create(
            -3,
            [
                Node.create_node(1, 1, 2),
                Node.create_node(2, 3, 4),
                Node.create_node(3, 5, 6),
            ],
            att={"foo": "bar"},
            tags={"baz": 42},
        )
84 85 86
        self.assertEqual(way.id, -3)
        self.assertEqual(way.action, "modify")
        self.assertEqual(way.visible, "true")
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
87
        self.assertDictEqual(way.attr, {"foo": "bar"})
88
        self.assertListEqual(way.nds, [Nd(1), Nd(2), Nd(3)])
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
89
        self.assertListEqual(way.tags, [Tag("rover:id", 3), Tag("baz", 42)])
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

    def test_add_tag(self):
        way = Way.create_with_refs(-3, [1, 2, 3])
        way.add_tag("Foo", "Bar")
        self.assertListEqual(way.tags, [Tag("rover:id", 3), Tag("Foo", "Bar")])

    def test_to_xml(self):
        way = Way.create_with_refs(-3, [1, 2, 3])
        way.add_tag("Foo", "Bar")
        way_str = b'<way id="-3" action="modify" visible="true"><nd ref="1"/><nd ref="2"/><nd ref="3"/><tag k="rover:id" v="3"/><tag k="Foo" v="Bar"/></way>'
        self.assertEqual(etree.tostring(way.to_xml()), way_str)


class TestPathToPolygon(unittest.TestCase):
    def test_strait_line(self):
        points = [(0.0, 0.0), (2.0, 0.0), (3.0, 0.0)]
        path = PathToPolygon(points, dist=0.1)
        self.assertEqual(len(path.polygon), 7)
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
108 109 110 111 112 113 114
        self.assertTrue((path.polygon[0] == [0.0, 0.1]).all())
        self.assertTrue((path.polygon[1] == [2.0, 0.1]).all())
        self.assertTrue((path.polygon[2] == [3.0, 0.1]).all())
        self.assertTrue((path.polygon[3] == [3.0, -0.1]).all())
        self.assertTrue((path.polygon[4] == [2.0, -0.1]).all())
        self.assertTrue((path.polygon[5] == [0.0, -0.1]).all())
        self.assertTrue((path.polygon[6] == [0.0, 0.1]).all())
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        self.assertTrue((path.polygon[0] == path.polygon[-1]).all())


class TestPolyObjectWidthId(unittest.TestCase):
    def test_shift(self):
        obj = PolyObjectWidthId(3, [(3, 5), (4, 6), (5, 7)])
        obj.shift_points([1, 2])
        self.assertListEqual(obj.points, [(2, 3), (3, 4), (4, 5)])

    def test_closed(self):
        obj1 = PolyObjectWidthId(3, [(3, 5), (4, 6), (5, 7)])
        obj2 = PolyObjectWidthId(3, [(3, 5), (4, 6), (5, 7), (3, 5)])
        self.assertFalse(obj1.closed())
        self.assertTrue(obj2.closed())

    def test_create(self):
        closed1 = PolyObjectWidthId.create_closed(3, [(3, 5), (4, 6), (5, 7)])
        closed2 = PolyObjectWidthId.create_closed(3, [(3, 5), (4, 6), (5, 7), (3, 5)])
        self.assertTrue(closed1.closed())
        self.assertTrue(closed2.closed())
        self.assertEqual(closed1, closed2)

        open1 = PolyObjectWidthId.create_open(3, [(3, 5), (4, 6), (5, 7)])
        open2 = PolyObjectWidthId.create_open(3, [(3, 5), (4, 6), (5, 7), (3, 5)])
        self.assertFalse(open1.closed())
        self.assertFalse(open2.closed())
        self.assertEqual(open1, open2)

    def test_add_template_data(self):
        obj = PolyObjectWidthId.create_closed(3, [(3, 5), (4, 6), (5, 7)])
        obj.add_template_data("Foo", "Bar")
        self.assertDictEqual(obj.template_data, {"id": 3, "Foo": "Bar"})


class TestOsmLookup(unittest.TestCase):
    def test_load(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
151 152 153 154 155
        nodes = [
            Node.create_node(2, 48.16256981067, 11.5868239213).to_xml(),
            Node.create_node(1, 48.16254537153, 11.58697933476).to_xml(),
            Node.create_node(-3, 48.16255438099, 11.58692204178).to_xml(),
        ]
156 157 158 159 160 161 162 163 164 165 166 167
        lookup = OsmLookup()
        lookup.load(nodes)

        self.assertDictEqual(lookup.latlon_to_node_errors, {})
        self.assertEqual(len(lookup.zone_map), 1)
        self.assertTupleEqual(list(lookup.zone_map.keys())[0], (32, "U"))

        self.assertEqual(len(lookup.latlon_to_node), 3)
        self.assertEqual(len(lookup.node_to_utm), 3)
        self.assertEqual(len(lookup.node_to_latlon), 3)

    def test_multiple_load(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
168 169 170 171 172
        nodes = [
            Node.create_node(2, 48.16256981067, 11.5868239213).to_xml(),
            Node.create_node(1, 48.16254537153, 11.58697933476).to_xml(),
            Node.create_node(-3, 48.16255438099, 11.58692204178).to_xml(),
        ]
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
        lookup = OsmLookup()
        lookup.load(nodes)
        self.assertEqual(len(lookup.latlon_to_node), 3)
        self.assertEqual(len(lookup.node_to_utm), 3)
        self.assertEqual(len(lookup.node_to_latlon), 3)
        self.assertTupleEqual(list(lookup.zone_map.keys())[0], (32, "U"))
        self.assertDictEqual(lookup.latlon_to_node_errors, {})

        # should be cleaned
        lookup.load(nodes)
        self.assertEqual(len(lookup.latlon_to_node), 3)
        self.assertEqual(len(lookup.node_to_utm), 3)
        self.assertEqual(len(lookup.node_to_latlon), 3)
        self.assertTupleEqual(list(lookup.zone_map.keys())[0], (32, "U"))
        self.assertDictEqual(lookup.latlon_to_node_errors, {})

    def test_add(self):
        node1 = Node.create_node(2, 48.16256981067, 11.5868239213)
        node2 = Node.create_node(1, 48.16254537153, 11.58697933476)

        lookup = OsmLookup()
        lookup.add(2, (node1.lat, node1.lon), (3, 5))
        self.assertEqual(len(lookup.node_to_latlon), 1)
        self.assertEqual(len(lookup.node_to_utm), 1)
        self.assertEqual(len(lookup.latlon_to_node), 1)

        lookup.add(2, (node1.lat, node1.lon), (3, 5))
        self.assertEqual(len(lookup.node_to_latlon), 1)
        self.assertEqual(len(lookup.node_to_utm), 1)
        self.assertEqual(len(lookup.latlon_to_node), 1)

        lookup.add(1, (node2.lat, node2.lon), (3, 7))
        self.assertEqual(len(lookup.node_to_latlon), 2)
        self.assertEqual(len(lookup.node_to_utm), 2)
        self.assertEqual(len(lookup.latlon_to_node), 2)

    def test_remove(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
210 211 212 213 214
        nodes = [
            Node.create_node(2, 48.16256981067, 11.5868239213).to_xml(),
            Node.create_node(1, 48.16254537153, 11.58697933476).to_xml(),
            Node.create_node(-3, 48.16255438099, 11.58692204178).to_xml(),
        ]
215 216 217 218 219 220 221 222 223 224 225 226 227
        lookup = OsmLookup()
        lookup.load(nodes)

        self.assertEqual(len(lookup.latlon_to_node), 3)
        self.assertEqual(len(lookup.node_to_utm), 3)
        self.assertEqual(len(lookup.node_to_latlon), 3)

        lookup.remove(2)
        self.assertEqual(len(lookup.latlon_to_node), 2)
        self.assertEqual(len(lookup.node_to_utm), 2)
        self.assertEqual(len(lookup.node_to_latlon), 2)

    def test_multiple_utm_zones(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
228 229 230 231 232
        nodes = [
            Node.create_node(2, 48.16256981067, 11.5868239213).to_xml(),
            Node.create_node(1, 48.16254537153, 11.58697933476).to_xml(),
            Node.create_node(-3, 48.16255438099, 33.58692204178).to_xml(),
        ]  # wrong zone
233 234 235 236
        lookup = OsmLookup()
        self.assertRaises(RuntimeError, lookup.load, nodes)

    def test_duplicated_ids_negative(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
237 238 239 240 241
        nodes = [
            Node.create_node(2, 48.16256981067, 11.5868239213).to_xml(),
            Node.create_node(-3, 48.16254537153, 11.58697933476).to_xml(),
            Node.create_node(-3, 48.16255438099, 11.58692204178).to_xml(),
        ]
242 243 244 245 246
        lookup = OsmLookup(strict=True)
        self.assertRaises(OsmLookupError, lookup.load, nodes)
        self.assertEqual(len(lookup.latlon_to_node_errors), 0)

    def test_duplicated_ids_positive(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
247 248 249 250 251
        nodes = [
            Node.create_node(2, 48.16256981067, 11.5868239213).to_xml(),
            Node.create_node(3, 48.16254537153, 11.58697933476).to_xml(),
            Node.create_node(3, 48.16255438099, 11.58692204178).to_xml(),
        ]
252 253 254 255 256
        lookup = OsmLookup(strict=True)
        lookup.load(nodes)
        self.assertEqual(len(lookup.latlon_to_node_errors), 0)

    def test_duplicated_latlon(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
257 258 259 260 261
        nodes = [
            Node.create_node(2, 48.16256981067, 11.5868239213).to_xml(),
            Node.create_node(1, 48.16254537153, 11.58697933476).to_xml(),
            Node.create_node(-3, 48.16254537153, 11.58697933476).to_xml(),
        ]
262 263 264 265 266 267 268
        lookup = OsmLookup(strict=True)
        self.assertRaises(OsmLookupError, lookup.load, nodes)
        self.assertEqual(len(lookup.latlon_to_node_errors), 1)


class TestOsmData(unittest.TestCase):
    def setUp(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
        self.valid001 = OsmData(
            input_file=os.path.join(os.path.dirname(__file__), "./valid001.osm")
        )
        self.valid002 = OsmData(
            input_file=os.path.join(os.path.dirname(__file__), "./valid002.osm")
        )
        self.valid003 = OsmData(
            input_file=os.path.join(os.path.dirname(__file__), "./valid003.osm")
        )
        self.valid004 = OsmData(
            input_file=os.path.join(os.path.dirname(__file__), "./valid004.osm")
        )
        self.invalid001 = OsmData(
            input_file=os.path.join(os.path.dirname(__file__), "./invalid001.osm")
        )
        self.invalid002 = OsmData(
            input_file=os.path.join(os.path.dirname(__file__), "./invalid002.osm"),
            strict=False,
        )
        self.invalid003 = OsmData(
            input_file=os.path.join(os.path.dirname(__file__), "./invalid003.osm"),
            strict=False,
        )
        self.invalid004 = OsmData(
            input_file=os.path.join(os.path.dirname(__file__), "./invalid004.osm"),
            strict=False,
        )
        self.invalid005 = OsmData(
            input_file=os.path.join(os.path.dirname(__file__), "./invalid005.osm"),
            strict=False,
        )
300 301 302 303 304 305 306

    def test_base_point(self):
        self.assertTupleEqual(self.valid001.base_point, (48.16077, 11.58451))

    def test_nodes(self):
        nodes = self.valid001.nodes
        self.assertEqual(len(nodes), 6)
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
307 308 309 310 311 312
        self.assertEqual(nodes[0].get("id"), "-127405")
        self.assertEqual(nodes[1].get("id"), "-127407")
        self.assertEqual(nodes[2].get("id"), "-127425")
        self.assertEqual(nodes[3].get("id"), "1318876746")
        self.assertEqual(nodes[4].get("id"), "1324414637")
        self.assertEqual(nodes[5].get("id"), "1324414707")
313 314 315 316 317 318 319 320

    def test_utm_zone(self):
        self.assertTupleEqual(self.valid001.utm_zone, (32, "U"))

    def test_utm_zone_string(self):
        self.assertEqual(self.valid001.utm_zone_string, "UTM Zone 32U")

    def test__xpath_k_v_tags(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
321 322 323 324
        self.assertEqual(
            OsmData._xpath_k_v_tags([("Foo", "Bar"), ("Baz", "Tar")]),
            "(./tag[@k='Foo' and @v='Bar'] and ./tag[@k='Baz' and @v='Tar'])",
        )
325 326

    def test__xpath_k_v_tag(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
327 328 329
        self.assertEqual(
            OsmData._xpath_k_v_tag("Foo", "Bar"), "./tag[@k='Foo' and @v='Bar']"
        )
330 331 332 333

    def test__add_ignore(self):
        self.assertEqual(OsmData._add_ignore([]), "")

Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
334 335 336 337
        self.assertEqual(
            OsmData._add_ignore([("Foo", "Bar"), ("Baz", "Tar")]),
            " and not (./tag[@k='Foo' and @v='Bar'] and ./tag[@k='Baz' and @v='Tar'])",
        )
338 339

    def test__tag_matcher(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
        self.assertTupleEqual(
            OsmData._tag_matcher(
                [("Foo", "Bar"), ("Bazz",)], [], truth_operator=("and", "and")
            ),
            ("./tag[@k='Bazz'] and ./tag[@k='Foo' and @v='Bar']", ""),
        )
        self.assertTupleEqual(
            OsmData._tag_matcher(
                [("Foo", "Bar"), ("Bazz",)], [], truth_operator=("or", "and")
            ),
            ("./tag[@k='Bazz'] or ./tag[@k='Foo' and @v='Bar']", ""),
        )

        self.assertTupleEqual(
            OsmData._tag_matcher(
                [], [("Foo", "Bar"), ("Bazz",)], truth_operator=("and", "and")
            ),
            ("", "./tag[@k='Bazz'] and ./tag[@k='Foo' and @v='Bar']"),
        )
        self.assertTupleEqual(
            OsmData._tag_matcher(
                [], [("Foo", "Bar"), ("Bazz",)], truth_operator=("and", "or")
            ),
            ("", "./tag[@k='Bazz'] or ./tag[@k='Foo' and @v='Bar']"),
        )

        self.assertTupleEqual(
            OsmData._tag_matcher([], [], truth_operator=("and", "or")), ("", "")
        )

        self.assertTupleEqual(
            OsmData._tag_matcher(
                [("A", "A"), ("B", "B")],
                [("C", "C"), ("D", "D")],
                truth_operator=("or", "and"),
            ),
            (
                "./tag[@k='A' and @v='A'] or ./tag[@k='B' and @v='B']",
                "./tag[@k='C' and @v='C'] and ./tag[@k='D' and @v='D']",
            ),
        )
381 382 383 384 385 386 387 388 389 390 391

    def test_filter_tag(self):
        ret = self.valid001.filter_tag(include=[("Foo", "Bar")])
        self.assertEqual(len(ret), 0)

        ret = self.valid001.filter_tag(include=[("test", "1")])
        self.assertEqual(len(ret), 1)

        ret = self.valid001.filter_tag(include=[("test",)])
        self.assertEqual(len(ret), 2)

Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
392 393 394
        ret = self.valid001.filter_tag(
            include=[("test",)], exclude=[("rover:id", "-133507")]
        )
395 396
        self.assertEqual(len(ret), 1)

Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
397 398 399
        ret = self.valid001.filter_tag(
            include=[("test",)], exclude=[("rover:id",), ("building", "yes")]
        )
400 401
        self.assertEqual(len(ret), 2)

Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
402 403 404 405 406
        ret = self.valid001.filter_tag(
            include=[("test",)],
            exclude=[("rover:id",), ("building", "yes")],
            truth_operator=("and", "or"),
        )
407 408
        self.assertEqual(len(ret), 0)

Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
409 410 411
        ret = self.valid001.filter_tag(
            exclude=[("rover:id",), ("building", "yes")], truth_operator=("and", "or")
        )
412 413
        self.assertEqual(len(ret), 0)

Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
414 415 416
        ret = self.valid001.filter_tag(
            include=[("rover:id",), ("building", "yes")], truth_operator=("and", "and")
        )
417 418
        self.assertEqual(len(ret), 0)

Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
419 420 421
        ret = self.valid001.filter_tag(
            include=[("rover:id",), ("building", "yes")], truth_operator=("or", "and")
        )
422 423 424 425 426 427 428 429
        self.assertEqual(len(ret), 2)

        ret = self.valid001.filter_tag(None, None)
        self.assertEqual(len(ret), 2)

    def test_filter_for_buildings(self):
        ret = self.valid001.filter_for_buildings()
        self.assertEqual(len(ret), 1)
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
430
        self.assertEqual(ret[0].get("id"), "173168628")
431 432 433 434 435 436 437 438 439 440

    def test_filter_for_barrier(self):
        pass

    def test_filter_for_buildings_in_relations(self):
        pass

    def test_tags_to_dict(self):
        element = self.valid001.filter_tag([("rover:id", "-133507")])[0]
        self.assertDictEqual(OsmData.tags_to_dict(element), {})
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
441 442 443 444 445 446 447 448 449 450 451 452 453
        self.assertDictEqual(
            OsmData.tags_to_dict(element, ""),
            {
                "test": "1",
                "rover:id": "-133507",
                "rover:obstacle": "yes",
                "rover:obstacle:type": "polygon",
            },
        )
        self.assertDictEqual(
            OsmData.tags_to_dict(element, "rover:"),
            {"id": "-133507", "obstacle": "yes", "obstacle:type": "polygon"},
        )
454 455 456 457 458 459 460

    def test_utm_lookup(self):
        self.assertNotEqual(self.valid001.utm_lookup(1318876746), ())
        self.assertNotEqual(self.valid001.utm_lookup(-127425), ())
        self.assertRaises(OsmLookupError, self.valid001.utm_lookup, 666)

    def test_latlon_lookup(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
461 462 463 464 465 466
        self.assertEqual(
            self.valid001.latlon_lookup(1318876746), (48.1608727, 11.5891088)
        )
        self.assertEqual(
            self.valid001.latlon_lookup(-127425), (48.16255438099, 11.58692204178)
        )
467 468 469 470
        self.assertRaises(OsmLookupError, self.valid001.latlon_lookup, 666)

    def test_tag_update_or_create(self):
        element = self.valid001.filter_tag([("rover:id", "-133507")])[0]
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
471 472 473
        self.assertEqual(element.xpath("./tag[@k='test']/@v")[0], "1")
        OsmData.tag_update_or_create(element, "test", "3")
        self.assertEqual(element.xpath("./tag[@k='test']/@v")[0], "3")
474

Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
475 476
        OsmData.tag_update_or_create(element, "test5", 42)
        self.assertEqual(element.xpath("./tag[@k='test5']/@v")[0], "42")
477

Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
478 479
        OsmData.tag_update_or_create(element, "test5", 69)
        self.assertEqual(element.xpath("./tag[@k='test5']/@v")[0], "69")
480 481

        self.assertEqual(len(element.xpath("./tag[@k='test2']")), 0)
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
482 483
        OsmData.tag_update_or_create(element, "test2", "Foo")
        self.assertEqual(element.xpath("./tag[@k='test2']/@v")[0], "Foo")
484 485 486 487 488 489 490 491 492

    def test_element_contains_tag(self):
        element = self.valid001.filter_tag([("rover:id", "-133507")])[0]
        self.assertTrue(OsmData.element_contains_tag(element, "rover:id", "-133507"))
        self.assertTrue(OsmData.element_contains_tag(element, "test", "1"))
        self.assertFalse(OsmData.element_contains_tag(element, "building", "yes"))

    def test_element(self):
        element = self.valid001.element("/osm/bounds")
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
493 494 495
        self.assertEqual(
            element.get("origin"), "CGImap 0.7.5 (31203 thorn-02.openstreetmap.org)"
        )
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
        self.assertRaises(OsmLookupError, self.valid001.element, "/foo/bar")
        self.assertRaises(OsmLookupError, self.valid001.element, "/osm/way")

    def test_elements(self):
        elements = self.valid001.elements("/osm/way")
        self.assertEqual(len(elements), 2)
        elements = self.valid001.elements("/osm/bounds")
        self.assertEqual(len(elements), 1)
        self.assertRaises(OsmLookupError, self.valid001.elements, "/foo/bar")

    def test_node(self):
        node = self.valid001.node(1318876746)
        self.assertEqual(node.get("user"), "KonB")
        self.assertRaises(OsmLookupError, self.valid001.node, 1318876746, rover_id=True)

        self.assertRaises(OsmLookupError, self.valid001.node, 666)
        node = self.valid001.node(333, rover_id=True)
        self.assertEqual(node.get("id"), "-127405")

    def test_node_exists(self):
        self.assertTrue(self.valid001.node_exists(1318876746))
        self.assertFalse(self.valid001.node_exists(1318876746, rover_id=True))
        self.assertTrue(self.valid001.node_exists(-127407))
        self.assertFalse(self.valid001.node_exists(-666))
        self.assertTrue(self.valid001.node(333, rover_id=True))
        self.assertTrue(self.valid001.node(-127405, rover_id=False))

    def test_node_add(self):

        self.assertEqual(len(self.valid001.nodes), 6)
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
526 527 528
        self.assertEqual(
            self.valid001.node_add((48.16256981067, 11.5868239213)), -127405
        )
529 530 531 532 533 534 535
        self.assertEqual(len(self.valid001.nodes), 6)

        self.assertRaises(RuntimeError, self.valid001.node_add, (45.66, 11.66))
        self.assertEqual(len(self.valid001.nodes), 6)

        node_id = self.valid001.node_add((48.16, 11.59))
        self.assertEqual(self.valid001.latlon_lookup(node_id), (48.16, 11.59))
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
536 537 538
        self.assertEqual(
            self.valid001.lookup.latlon_to_node.get((48.16, 11.59)), node_id
        )
539 540 541 542 543 544 545 546 547 548 549 550 551 552
        self.assertEqual(len(self.valid001.nodes), 7)

    def test_node_remove(self):
        node_id = 1318876746
        self.assertTrue(self.valid001.node_exists(node_id))
        self.valid001.utm_lookup(node_id)
        self.valid001.latlon_lookup(node_id)

        self.valid001.node_remove(node_id)
        self.assertFalse(self.valid001.node_exists(node_id))
        self.assertRaises(OsmLookupError, self.valid001.utm_lookup, node_id)
        self.assertRaises(OsmLookupError, self.valid001.latlon_lookup, node_id)

    def test_nodes_find_with_tags(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
553 554 555 556
        self.assertEqual(len(self.valid001.nodes_find_with_tags([("rover:id",)])), 1)
        self.assertEqual(
            len(self.valid001.nodes_find_with_tags(exc_tag=[("rover:id",)])), 5
        )
557 558 559 560 561 562 563 564 565 566

    def test_node_add_tag(self):
        node_id = -127405
        node = self.valid001.node(node_id)
        self.assertListEqual(node.xpath("./tag[@k='foo']/@v"), [])
        self.assertListEqual(self.valid001.nodes_find_with_tags([("foo", "bar")]), [])
        self.valid001.node_add_tag(node_id, "foo", "bar")
        self.assertEqual(node.xpath("./tag[@k='foo']/@v"), ["bar"])

    def test_nodes_not_used(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
567 568 569 570 571 572 573
        self.assertSetEqual(
            self.valid001.nodes_not_used([-127405, 1318876746, 1324414637]), set()
        )
        self.assertSetEqual(
            self.invalid001.nodes_not_used([-127433, -127405, 1318876746, 1324414637]),
            {-127433},
        )
574 575

    def test_nodes_to_latlon(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
576 577 578 579
        self.assertListEqual(
            self.valid001.nodes_to_latlon([-127405, 1318876746]),
            [(48.16256981067, 11.5868239213), (48.1608727, 11.5891088)],
        )
580 581 582 583

    def test_nodes_to_utm(self):
        self.assertListEqual(
            self.valid001.nodes_to_utm([-127405, 1318876746]),
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
584 585 586 587
            [
                (692351.2680216449, 5337605.595388093),
                (692527.5088385276, 5337422.705304472),
            ],
588 589 590
        )

    def test_way(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
591
        self.assertEqual(self.valid001.way(173168628).get("id"), "173168628")
592 593 594
        self.assertRaises(OsmLookupError, self.valid001.way, 555)

        self.assertRaises(OsmLookupError, self.valid001.way, 555, rover_id=True)
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
595 596 597
        self.assertEqual(
            self.valid001.way(-133507, rover_id=True).get("id"), "-999069167"
        )
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623

    def test_way_exists(self):
        self.assertTrue(self.valid001.way_exists(173168628))
        self.assertFalse(self.valid001.way_exists(173168628, rover_id=True))

        self.assertFalse(self.valid001.way_exists(-133507, rover_id=False))
        self.assertTrue(self.valid001.way_exists(-133507, rover_id=True))

    def test_way_add_1(self):
        way = Way.create_with_refs(3, [-127405, 1318876746, 1324414637])
        self.valid001.way_add(way)
        self.assertTrue(self.valid001.way_exists(3))

    def test_way_add_2(self):
        # duplicated way id
        way = Way.create_with_refs(173168628, [-127405, 1318876746, 1324414637])
        self.assertRaises(OsmLookupError, self.valid001.way_add, way)

    def test_way_add_3(self):
        # missing reference
        way = Way.create_with_refs(3, [6, 1318876746, 1324414637])
        self.assertRaises(OsmLookupError, self.valid001.way_add, way)

    def test_way_create_from_polyline(self):
        utm_coords = self.valid002.lookup.node_to_utm.values()
        line = self.valid002.way(-999069173)
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
624 625
        self.assertEqual(line.xpath("./tag[@k='rover:id']/@v"), ["-133513"])
        self.assertEqual(line.xpath("./tag[@k='rover:obstacle:type']/@v"), ["line"])
626 627 628

        new_way, _, _ = self.valid002.way_create_from_polyline(utm_coords)
        self.assertTrue(self.valid002.way_exists(new_way.id))
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
629
        self.assertEqual(len(new_way.nds), 9)  # start == end
630

Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
631
        self.assertEqual(len(self.valid002.nodes), 4 + 8)  # only
632 633 634 635 636 637

    def test_way_is_closed(self):
        self.assertTrue(self.valid001.way_is_closed(173168628))
        self.assertFalse(self.valid002.way_is_closed(-999069173))

    def test_way_node_refs(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
638 639 640 641
        self.assertListEqual(
            self.valid001.way_node_refs(-999069167),
            [-127405, -127407, -127425, -127405],
        )
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
        self.assertRaises(OsmLookupError, self.valid001.way_node_refs, 666)

    def test_way_remove(self):
        self.assertEqual(len(self.valid001.nodes), 6)
        self.assertEqual(len(self.valid001.ways), 2)
        self.valid001.way_remove(-999069167)
        self.assertEqual(len(self.valid001.nodes), 3)
        self.assertEqual(len(self.valid001.ways), 1)

        self.assertFalse(self.valid001.way_exists(-999069167))
        self.assertRaises(OsmLookupError, self.valid001.utm_lookup, -127405)
        self.assertRaises(OsmLookupError, self.valid001.latlon_lookup, -127407)

    def test_way_add_tag(self):
        pass

    def test_ways_find_with_tags(self):
        pass

    def test_create_convex_hull(self):
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
662 663 664
        el = self.valid003.ways_find_with_tags(
            inc_tag=[("rover:obstacle:hull",), ("rover:obstacle:ignore", "yes")]
        )
665 666
        self.assertEqual(len(el), 0)
        hull_id = self.valid003.create_convex_hull([39057410, 36942797])
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
667 668 669
        el = self.valid003.ways_find_with_tags(
            inc_tag=[("rover:obstacle:hull",), ("rover:obstacle:ignore", "yes")]
        )
670 671
        self.assertEqual(len(el), 2)

Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
        self.assertListEqual(
            self.valid003.way_node_refs(hull_id),
            [
                429583851,
                429583850,
                429583279,
                429583282,
                429583298,
                429583853,
                429583852,
                429583851,
            ],
        )
        self.assertEqual(
            len(
                self.valid003.ways_find_with_tags(
                    inc_tag=[
                        ("rover:id", str(np.abs(hull_id))),
                        ("rover:obstacle", "convex-hull"),
                        ("rover:obstacle:hull", "-1"),
                    ]
                )
            ),
            1,
        )
697 698 699

    def test_lint_add_ids(self):
        self.assertEqual(len(self.invalid002.xml.xpath("/osm/way[@id < 0]")), 1)
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
700 701 702 703 704 705 706 707
        self.assertEqual(
            len(
                self.invalid002.xml.xpath(
                    "/osm/way[@id < 0 and not (./tag[@k='rover:id'])]"
                )
            ),
            1,
        )
708 709

        self.assertEqual(len(self.invalid002.xml.xpath("/osm/node[@id < 0]")), 4)
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
710 711 712 713 714 715 716 717
        self.assertEqual(
            len(
                self.invalid002.xml.xpath(
                    "/osm/node[@id < 0 and not (./tag[@k='rover:id'])]"
                )
            ),
            3,
        )
718 719 720 721

        self.invalid002.lint_add_ids(dry_run=True)

        self.assertEqual(len(self.invalid002.xml.xpath("/osm/way[@id < 0]")), 1)
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
722 723 724 725 726 727 728 729
        self.assertEqual(
            len(
                self.invalid002.xml.xpath(
                    "/osm/way[@id < 0 and not (./tag[@k='rover:id'])]"
                )
            ),
            1,
        )
730 731

        self.assertEqual(len(self.invalid002.xml.xpath("/osm/node[@id < 0]")), 4)
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
732 733 734 735 736 737 738 739
        self.assertEqual(
            len(
                self.invalid002.xml.xpath(
                    "/osm/node[@id < 0 and not (./tag[@k='rover:id'])]"
                )
            ),
            3,
        )
740 741 742 743

        self.invalid002.lint_add_ids(dry_run=False)

        self.assertEqual(len(self.invalid002.xml.xpath("/osm/way[@id < 0]")), 1)
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
744 745 746 747 748 749 750 751
        self.assertEqual(
            len(
                self.invalid002.xml.xpath(
                    "/osm/way[@id < 0 and not (./tag[@k='rover:id'])]"
                )
            ),
            0,
        )
752 753

        self.assertEqual(len(self.invalid002.xml.xpath("/osm/node[@id < 0]")), 4)
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
754 755 756 757 758 759 760 761
        self.assertEqual(
            len(
                self.invalid002.xml.xpath(
                    "/osm/node[@id < 0 and not (./tag[@k='rover:id'])]"
                )
            ),
            0,
        )
762 763 764

    def test_lint_unique_ids(self):
        ways, nodes = self.invalid003.lint_unique_ids()
Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
765 766
        self.assertSetEqual(ways, {"-999069167"})
        self.assertSetEqual(nodes, {"-127405"})
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790

        ways, nodes = self.valid001.lint_unique_ids()
        self.assertSetEqual(ways, set())
        self.assertSetEqual(nodes, set())

    def test_lint_check_obstacles(self):
        invalid_polygons, multiple_used_nodes = self.invalid004.lint_check_obstacles()
        self.assertEqual(len(invalid_polygons), 2)
        self.assertEqual(len(multiple_used_nodes), 2)

        invalid_polygons, multiple_used_nodes = self.valid004.lint_check_obstacles()
        self.assertEqual(len(invalid_polygons), 0)
        self.assertEqual(len(multiple_used_nodes), 0)

    def test_lint_cleanup_unused_nodes(self):
        self.assertRaises(OsmLookupError, self.invalid003.lint_cleanup_unused_nodes)

        self.assertEqual(len(self.invalid005.xml.xpath("/osm/node")), 7)
        self.invalid005.lint_cleanup_unused_nodes(dry_run=True)
        self.assertEqual(len(self.invalid005.xml.xpath("/osm/node")), 7)
        self.invalid005.lint_cleanup_unused_nodes(dry_run=False)
        self.assertEqual(len(self.invalid005.xml.xpath("/osm/node")), 6)


Stefan Schuhbaeck's avatar
Stefan Schuhbaeck committed
791
if __name__ == "__main__":
792
    unittest.main()