QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
MapProvider.cpp
Go to the documentation of this file.
1#include "MapProvider.h"
3
4#include <QtCore/QLocale>
5
6QGC_LOGGING_CATEGORY(MapProviderLog, "QtLocationPlugin.MapProvider")
7
8// QtLocation expects MapIds to start at 1 and be sequential.
9int MapProvider::_mapIdIndex = 1;
10
12 const QString &mapName,
13 const QString &referrer,
14 const QString &imageFormat,
15 quint32 averageSize,
16 QGeoMapType::MapStyle mapStyle)
17 : _mapName(mapName)
18 , _referrer(referrer)
19 , _imageFormat(imageFormat)
20 , _averageSize(averageSize)
21 , _mapStyle(mapStyle)
22 , _language(!QLocale::system().uiLanguages().isEmpty() ? QLocale::system().uiLanguages().constFirst() : "en")
23 , _mapId(_mapIdIndex++)
24{
25 // qCDebug(MapProviderLog) << Q_FUNC_INFO << this << _mapId;
26}
27
29{
30 // qCDebug(MapProviderLog) << Q_FUNC_INFO << this << _mapId;
31}
32
33QUrl MapProvider::getTileURL(int x, int y, int zoom) const
34{
35 return QUrl(_getURL(x, y, zoom));
36}
37
38QString MapProvider::getImageFormat(QByteArrayView image) const
39{
40 if (image.size() < 3) {
41 return QString();
42 }
43
44 static constexpr QByteArrayView pngSignature("\x89\x50\x4E\x47\x0D\x0A\x1A\x0A");
45 if (image.startsWith(pngSignature)) {
46 return QStringLiteral("png");
47 }
48
49 static constexpr QByteArrayView jpegSignature("\xFF\xD8\xFF");
50 if (image.startsWith(jpegSignature)) {
51 return QStringLiteral("jpg");
52 }
53
54 static constexpr QByteArrayView gifSignature("\x47\x49\x46\x38");
55 if (image.startsWith(gifSignature)) {
56 return QStringLiteral("gif");
57 }
58
59 return _imageFormat;
60}
61
62QString MapProvider::_tileXYToQuadKey(int tileX, int tileY, int levelOfDetail) const
63{
64 QString quadKey;
65 for (int i = levelOfDetail; i > 0; i--) {
66 char digit = '0';
67 const int mask = 1 << (i - 1);
68
69 if ((tileX & mask) != 0) {
70 digit++;
71 }
72 if ((tileY & mask) != 0) {
73 digit += 2;
74 }
75
76 (void) quadKey.append(digit);
77 }
78
79 return quadKey;
80}
81
82int MapProvider::_getServerNum(int x, int y, int max) const
83{
84 return (x + 2 * y) % max;
85}
86
87int MapProvider::long2tileX(double lon, int z) const
88{
89 return static_cast<int>(floor((lon + 180.0) / 360.0 * pow(2.0, z)));
90}
91
92int MapProvider::lat2tileY(double lat, int z) const
93{
94 return static_cast<int>(floor((1.0 - log(tan(lat * M_PI / 180.0) + 1.0 / cos(lat * M_PI / 180.0)) / M_PI) / 2.0 * pow(2.0, z)));
95}
96
97double MapProvider::tileX2long(int x, int z) const
98{
99 return x / std::pow(2.0, z) * 360.0 - 180.0;
100}
101
102double MapProvider::tileY2lat(int y, int z) const
103{
104 const double n = M_PI - 2.0 * M_PI * y / std::pow(2.0, z);
105 return qRadiansToDegrees(std::atan(std::sinh(n)));
106}
107
108QGCTileSet MapProvider::getTileCount(int zoom, double topleftLon,
109 double topleftLat, double bottomRightLon,
110 double bottomRightLat) const
111{
112 QGCTileSet set;
113 set.tileX0 = long2tileX(topleftLon, zoom);
114 set.tileY0 = lat2tileY(topleftLat, zoom);
115 set.tileX1 = long2tileX(bottomRightLon, zoom);
116 set.tileY1 = lat2tileY(bottomRightLat, zoom);
117
118 set.tileCount = (static_cast<quint64>(set.tileX1) -
119 static_cast<quint64>(set.tileX0) + 1) *
120 (static_cast<quint64>(set.tileY1) -
121 static_cast<quint64>(set.tileY0) + 1);
122
123 set.tileSize = getAverageSize() * set.tileCount;
124 return set;
125}
126
127// Resolution math: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Resolution_and_Scale
#define QGC_LOGGING_CATEGORY(name, categoryStr)
int _getServerNum(int x, int y, int max) const
QString _tileXYToQuadKey(int tileX, int tileY, int levelOfDetail) const
const QString _imageFormat
Definition MapProvider.h:72
virtual double tileY2lat(int y, int z) const
virtual ~MapProvider()
virtual QGCTileSet getTileCount(int zoom, double topleftLon, double topleftLat, double bottomRightLon, double bottomRightLat) const
quint32 getAverageSize() const
Definition MapProvider.h:44
virtual int long2tileX(double lon, int z) const
QString getImageFormat(QByteArrayView image) const
virtual double tileX2long(int x, int z) const
QUrl getTileURL(int x, int y, int zoom) const
virtual int lat2tileY(double lat, int z) const
virtual QString _getURL(int x, int y, int zoom) const =0
quint64 tileCount
Definition QGCTileSet.h:29
quint64 tileSize
Definition QGCTileSet.h:30