aboutsummaryrefslogtreecommitdiff
path: root/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/MapCalculus.kt
blob: 4476e39fc1e02afe96616843dacd9b4a6563be5e (plain)
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
/**
 * TrackerMap
 * Copyright (C) 2021-2022  Iván Ávalos <avalos@disroot.org>, Henoch Ojeda <imhenoch@protonmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package mx.trackermap.TrackerMap.utils

class MapCalculus {
    companion object {
        /**
         * WhirlyGlobe library uses height rather than zoom levels, but it supports converting
         * Mapnik denominator scales to height, so we can first convert zoom levels to Mapnik
         * denominator scales, and then convert them to height using WhirlyGlobe.
         * Source: https://github.com/openstreetmap/mapnik-stylesheets/blob/master/zoom-to-scale.txt
         */
        fun zoomLevelToScale(zoom: Int): Double? =
            when (zoom) {
                1 -> 279541132.014
                2 -> 139770566.007
                3 -> 69885283.0036
                4 -> 34942641.5018
                5 -> 17471320.7509
                6 -> 8735660.37545
                7 -> 4367830.18772
                8 -> 2183915.09386
                9 -> 1091957.54693
                10 -> 545978.773466
                11 -> 272989.386733
                12 -> 136494.693366
                13 -> 68247.3466832
                14 -> 34123.6733416
                15 -> 17061.8366708
                16 -> 8530.9183354
                17 -> 4265.4591677
                18 -> 2132.72958385
                19 -> 1066.36479193
                20 -> 533.182395965
                21 -> 266.5911979825
                22 -> 133.29559899125
                23 -> 66.647799495625
                24 -> 33.3238997478125
                else -> null
        }

        fun scaleToZoomLevel(scale: Double): Int {
            fun z(d: Int) = zoomLevelToScale(d)!!
            return when {
                scale > z(1) -> 1
                scale in z(2)..z(1) -> 2
                scale in z(3)..z(2) -> 3
                scale in z(4)..z(3) -> 4
                scale in z(5)..z(4) -> 5
                scale in z(6)..z(5) -> 6
                scale in z(7)..z(6) -> 7
                scale in z(8)..z(7) -> 8
                scale in z(9)..z(8) -> 9
                scale in z(10)..z(9) -> 10
                scale in z(11)..z(10) -> 11
                scale in z(12)..z(11) -> 12
                scale in z(13)..z(12) -> 13
                scale in z(14)..z(13) -> 14
                scale in z(15)..z(14) -> 15
                scale in z(16)..z(15) -> 16
                scale in z(17)..z(16) -> 17
                scale in z(18)..z(17) -> 18
                scale in z(19)..z(18) -> 19
                scale in z(20)..z(19) -> 20
                scale in z(21)..z(20) -> 21
                scale in z(22)..z(21) -> 22
                scale in z(23)..z(22) -> 23
                scale in z(24)..z(23) -> 24
                else -> 24
            }
        }
    }
}