-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathTileHelper.cpp
More file actions
162 lines (141 loc) · 4.67 KB
/
TileHelper.cpp
File metadata and controls
162 lines (141 loc) · 4.67 KB
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
155
156
157
158
159
160
161
162
#include "stdafx.h"
#include "tilehelper.h"
#include "CustomProjection.h"
// ************************************************************
// GetTileSizeProj()
// ************************************************************
double TileHelper::GetTileSizeProj(VARIANT_BOOL isSameProjection, BaseProvider* provider, IGeoProjection* wgsToMapTransformation,
PointLatLng& location, int zoom)
{
CustomProjection* customProj = dynamic_cast<CustomProjection*>(provider->get_Projection());
if (isSameProjection && customProj)
{
SizeLatLng size;
customProj->GetTileSizeProj(zoom, size);
return size.WidthLng;
}
CPoint point;
provider->get_Projection()->FromLatLngToXY(location, zoom, point);
SizeLatLng size;
provider->get_Projection()->GetTileSizeLatLon(point, zoom, size);
PointLatLng newLoc;
provider->get_Projection()->FromXYToLatLng(point, zoom, newLoc);
double xMin, xMax, yMin, yMax;
xMin = newLoc.Lng;
xMax = xMin + size.WidthLng;
if (abs(xMax) > 180.0)
xMax = xMin - size.WidthLng;
yMin = newLoc.Lat;
yMax = yMin + size.HeightLat;
if (abs(yMax) > 90.0) {
yMax = yMin - size.HeightLat;
}
if (wgsToMapTransformation)
{
VARIANT_BOOL vb1, vb2;
m_globalSettings.suppressGdalErrors = true;
wgsToMapTransformation->Transform(&xMin, &yMin, &vb1);
wgsToMapTransformation->Transform(&xMax, &yMax, &vb2);
m_globalSettings.suppressGdalErrors = false;
if (vb1 && vb2)
{
double val = xMax - xMin;
return val > 0 ? val : -1.0;
}
return -1.0;
}
else
{
return xMax - xMin;
}
}
// ************************************************************
// Transform()
// ************************************************************
// Transforms tile bounds to map projection.
bool TileHelper::Transform(TileCore* tile, IGeoProjection* mapProjection, bool isSameProjection, RectLatLng& result)
{
if (isSameProjection)
{
// projection for tiles matches map projection (most often it's Google Mercator; EPSG:3857)
PointLatLng pnt;
CustomProjection* customProj = dynamic_cast<CustomProjection*>(tile->get_Projection());
if (customProj)
{
customProj->FromXYToProj(CPoint(tile->tileX(), tile->tileY() + 1), tile->zoom(), pnt);
SizeLatLng size;
customProj->GetTileSizeProj(tile->zoom(), size);
result.xLng = pnt.Lng;
result.yLat = pnt.Lat;
result.WidthLng = size.WidthLng;
result.HeightLat = size.HeightLat;
return true;
}
}
else
{
if (mapProjection)
{
RectLatLng* bounds = tile->get_GeographicBounds();
double xMin = bounds->xLng;
double yMax = bounds->yLat;
double xMax = bounds->MaxLng();
double yMin = bounds->MinLat();
double xTL, xTR, xBL, xBR;
double yTL, yTR, yBL, yBR;
xTL = xBL = xMin;
xTR = xBR = xMax;
yTL = yTR = yMax;
yBL = yBR = yMin;
VARIANT_BOOL vb;
mapProjection->Transform(&xTL, &yTL, &vb);
if (!vb) return false;
mapProjection->Transform(&xTR, &yTR, &vb);
if (!vb) return false;
mapProjection->Transform(&xBL, &yBL, &vb);
if (!vb) return false;
mapProjection->Transform(&xBR, &yBR, &vb);
if (!vb) return false;
result.xLng = (xBL + xTL)/2.0;
result.yLat = (yTL + yTR)/2.0;
result.WidthLng = (xTR + xBR)/2.0 - result.xLng;
result.HeightLat = result.yLat - (yBR + yBL)/2.0;
return true;
}
else
{
// we are working with WGS84 decimal degrees
RectLatLng* bounds = tile->get_GeographicBounds();
result.xLng = bounds->xLng;
result.yLat = bounds->yLat;
result.WidthLng = bounds->WidthLng;
result.HeightLat = bounds->HeightLat;
return true;
}
}
return false;
}
// ************************************************************
// GetTileSize()
// ************************************************************
// Returns tiles size of the specified zoom level under current map scale
double TileHelper::GetTileSize(BaseProjection* proj, PointLatLng& location, int zoom, double pixelPerDegree)
{
CPoint point;
proj->FromLatLngToXY(location, zoom, point);
SizeLatLng size;
proj->GetTileSizeLatLon(point, zoom, size);
return (size.WidthLng + size.HeightLat) * pixelPerDegree / 2.0;
}
// ************************************************************
// GetTileSizeByWidth()
// ************************************************************
// Returns tiles size of the specified zoom level under current map scale
double TileHelper::GetTileSizeByWidth(BaseProjection* proj, PointLatLng& location, int zoom, double pixelPerDegree)
{
CPoint point;
proj->FromLatLngToXY(location, zoom, point);
SizeLatLng size;
proj->GetTileSizeLatLon(point, zoom, size);
return size.WidthLng * pixelPerDegree;
}