File: test-tile.cpp

package info (click to toggle)
osm2pgsql 2.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,772 kB
  • sloc: cpp: 60,940; python: 1,115; ansic: 763; sh: 25; makefile: 14
file content (154 lines) | stat: -rw-r--r-- 5,314 bytes parent folder | download | duplicates (2)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
151
152
153
154
/**
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * This file is part of osm2pgsql (https://osm2pgsql.org/).
 *
 * Copyright (C) 2006-2025 by the osm2pgsql developer community.
 * For a full list of authors see the git log.
 */

#include <catch.hpp>

#include "tile.hpp"

TEST_CASE("invalid tile", "[NoDB]")
{
    tile_t const tile;
    REQUIRE_FALSE(tile.valid());
}

TEST_CASE("tile access and comparison", "[NoDB]")
{
    tile_t const a{3, 2, 1};
    tile_t const b{3, 2, 1};
    tile_t const c{3, 1, 2};

    REQUIRE(a.valid());
    REQUIRE(b.valid());
    REQUIRE(c.valid());

    REQUIRE(a.zoom() == 3);
    REQUIRE(a.x() == 2);
    REQUIRE(a.y() == 1);
    REQUIRE(a.to_zxy() == "3/2/1");

    REQUIRE(b.zoom() == 3);
    REQUIRE(b.x() == 2);
    REQUIRE(b.y() == 1);
    REQUIRE(b.to_zxy() == "3/2/1");

    REQUIRE(c.zoom() == 3);
    REQUIRE(c.x() == 1);
    REQUIRE(c.y() == 2);
    REQUIRE(c.to_zxy() == "3/1/2");

    REQUIRE(a == b);
    REQUIRE_FALSE(a != b);
    REQUIRE_FALSE(a == c);
    REQUIRE(a != c);

    REQUIRE_FALSE(a < b);
    REQUIRE_FALSE(b < a);

    REQUIRE_FALSE(a < c);
    REQUIRE(c < a);
}

TEST_CASE("tile_t from zxy string", "[NoDB]")
{
    REQUIRE(tile_t::from_zxy("0/0/0") == tile_t(0, 0, 0));
    REQUIRE(tile_t::from_zxy("2/3/3") == tile_t(2, 3, 3));
    REQUIRE(tile_t::from_zxy("31/3/27") == tile_t(31, 3, 27));

    REQUIRE_THROWS(tile_t::from_zxy(""));
    REQUIRE_THROWS(tile_t::from_zxy("a/c/c"));
    REQUIRE_THROWS(tile_t::from_zxy("1a/0/0"));
    REQUIRE_THROWS(tile_t::from_zxy("32/0/0"));
    REQUIRE_THROWS(tile_t::from_zxy("2/4/0"));
    REQUIRE_THROWS(tile_t::from_zxy("2/0/4"));
}

TEST_CASE("tile_t coordinates zoom=0", "[NoDB]")
{
    tile_t const tile{0, 0, 0};

    REQUIRE(tile.xmin() == Approx(-tile_t::HALF_EARTH_CIRCUMFERENCE));
    REQUIRE(tile.ymin() == Approx(-tile_t::HALF_EARTH_CIRCUMFERENCE));
    REQUIRE(tile.xmax() == Approx(tile_t::HALF_EARTH_CIRCUMFERENCE));
    REQUIRE(tile.ymax() == Approx(tile_t::HALF_EARTH_CIRCUMFERENCE));
    REQUIRE(tile.box().min_x() == Approx(-tile_t::HALF_EARTH_CIRCUMFERENCE));
    REQUIRE(tile.box().min_y() == Approx(-tile_t::HALF_EARTH_CIRCUMFERENCE));
    REQUIRE(tile.box().max_x() == Approx(tile_t::HALF_EARTH_CIRCUMFERENCE));
    REQUIRE(tile.box().max_y() == Approx(tile_t::HALF_EARTH_CIRCUMFERENCE));

    // Bounding box with margin will not get larger, because it is always
    // clamped to the full extent of the map.
    REQUIRE(tile.xmin(0.1) == Approx(-tile_t::HALF_EARTH_CIRCUMFERENCE));
    REQUIRE(tile.ymin(0.1) == Approx(-tile_t::HALF_EARTH_CIRCUMFERENCE));
    REQUIRE(tile.xmax(0.1) == Approx(tile_t::HALF_EARTH_CIRCUMFERENCE));
    REQUIRE(tile.ymax(0.1) == Approx(tile_t::HALF_EARTH_CIRCUMFERENCE));
    REQUIRE(tile.box(0.1).min_x() == Approx(-tile_t::HALF_EARTH_CIRCUMFERENCE));
    REQUIRE(tile.box(0.1).min_y() == Approx(-tile_t::HALF_EARTH_CIRCUMFERENCE));
    REQUIRE(tile.box(0.1).max_x() == Approx(tile_t::HALF_EARTH_CIRCUMFERENCE));
    REQUIRE(tile.box(0.1).max_y() == Approx(tile_t::HALF_EARTH_CIRCUMFERENCE));

    REQUIRE(tile.center().x() == Approx(0.0));
    REQUIRE(tile.center().y() == Approx(0.0));
    REQUIRE(tile.center() == geom::point_t{0.0, 0.0});

    REQUIRE(tile.extent() == Approx(tile_t::EARTH_CIRCUMFERENCE));

    geom::point_t const p{12345.6, 7891.0};
    auto const tp = tile.to_tile_coords(p, 256);
    auto const pp = tile.to_world_coords(tp, 256);
    REQUIRE(p.x() == Approx(pp.x()));
    REQUIRE(p.y() == Approx(pp.y()));

    REQUIRE(tile.quadkey() == quadkey_t{0});
}

TEST_CASE("tile_t coordinates zoom=2", "[NoDB]")
{
    tile_t const tile{2, 1, 2};

    double min = -tile_t::HALF_EARTH_CIRCUMFERENCE / 2;
    double max = 0.0;
    REQUIRE(tile.xmin() == Approx(min));
    REQUIRE(tile.ymin() == Approx(min));
    REQUIRE(tile.xmax() == Approx(max));
    REQUIRE(tile.ymax() == Approx(max));
    CHECK(tile.box().min_x() == tile.xmin());
    CHECK(tile.box().min_y() == tile.ymin());
    CHECK(tile.box().max_x() == tile.xmax());
    CHECK(tile.box().max_y() == tile.ymax());

    // Bounding box of tile with 50% margin on all sides.
    min -= tile_t::HALF_EARTH_CIRCUMFERENCE / 4;
    max += tile_t::HALF_EARTH_CIRCUMFERENCE / 4;
    CHECK(tile.xmin(0.5) == Approx(min));
    CHECK(tile.ymin(0.5) == Approx(min));
    CHECK(tile.xmax(0.5) == Approx(max));
    CHECK(tile.ymax(0.5) == Approx(max));
    CHECK(tile.box(0.5).min_x() == tile.xmin(0.5));
    CHECK(tile.box(0.5).min_y() == tile.ymin(0.5));
    CHECK(tile.box(0.5).max_x() == tile.xmax(0.5));
    CHECK(tile.box(0.5).max_y() == tile.ymax(0.5));

    REQUIRE(tile.center().x() == Approx(-tile_t::HALF_EARTH_CIRCUMFERENCE / 4));
    REQUIRE(tile.center().y() == Approx(-tile_t::HALF_EARTH_CIRCUMFERENCE / 4));

    REQUIRE(tile.extent() == Approx(tile_t::HALF_EARTH_CIRCUMFERENCE / 2));

    geom::point_t const p{-tile_t::HALF_EARTH_CIRCUMFERENCE / 4,
                          -tile_t::HALF_EARTH_CIRCUMFERENCE / 8};
    auto const tp = tile.to_tile_coords(p, 4096);
    REQUIRE(tp.x() == Approx(2048));
    REQUIRE(tp.y() == Approx(2048 + 1024));

    auto const pp = tile.to_world_coords(tp, 4096);
    REQUIRE(p.x() == Approx(pp.x()));
    REQUIRE(p.y() == Approx(pp.y()));

    auto const q = tile.quadkey();
    REQUIRE(tile == tile_t::from_quadkey(q, tile.zoom()));
}