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
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2023-05-15
 * Description : geolocation engine based on Marble.
 *               (c) 2007-2022 Marble Team
 *               https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
 *
 * SPDX-FileCopyrightText: 2023-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * ============================================================ */

#include "TileId.h"

// Local includes

#include "GeoDataCoordinates.h"
#include "digikam_debug.h"

namespace Marble
{

TileId::TileId(QString const& mapThemeId, int zoomLevel, int tileX, int tileY)
    : m_mapThemeIdHash(qHash(mapThemeId)), m_zoomLevel(zoomLevel), m_tileX(tileX), m_tileY(tileY)
{
}

TileId::TileId(uint mapThemeIdHash, int zoomLevel, int tileX, int tileY)
    : m_mapThemeIdHash(mapThemeIdHash), m_zoomLevel(zoomLevel), m_tileX(tileX), m_tileY(tileY)
{
}

TileId::TileId()
    : m_mapThemeIdHash(0), m_zoomLevel(0), m_tileX(0), m_tileY(0)
{
}

TileId TileId::fromCoordinates(const GeoDataCoordinates& coords, int zoomLevel)
{
    if (zoomLevel < 0)
    {
        return TileId();
    }

    const int maxLat = 90 * 1000000;
    const int maxLon = 180 * 1000000;
    int lat = GeoDataCoordinates::normalizeLat(coords.latitude(GeoDataCoordinates::Degree), GeoDataCoordinates::Degree) * 1000000;
    int lon = GeoDataCoordinates::normalizeLon(coords.longitude(GeoDataCoordinates::Degree), GeoDataCoordinates::Degree) * 1000000;
    int x = 0;<--- Shadow variable
    int y = 0;<--- Shadow variable

    for (int i = 0; i < zoomLevel; ++i)
    {
        const int deltaLat = maxLat >> i;

        if (lat <= (maxLat - deltaLat))
        {
            y += 1 << (zoomLevel - i - 1);
            lat += deltaLat;
        }

        const int deltaLon = maxLon >> i;

        if (lon >= (maxLon - deltaLon))
        {
            x += 1 << (zoomLevel - i - 1);
        }

        else
        {
            lon += deltaLon;
        }
    }

    return TileId(0, zoomLevel, x, y);
}

} // namespace Marble

#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const Marble::TileId& id)
{
    return dbg << QString::fromUtf8("Marble::TileId(%1, %2, %3, %4)").arg(id.mapThemeIdHash())
           .arg(id.zoomLevel())
           .arg(id.x())
           .arg(id.y());
}
#endif