forked from GeoDaCenter/geoda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basemap.h
446 lines (392 loc) · 12.9 KB
/
Basemap.h
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**
* GeoDa TM, Copyright (C) 2011-2015 by Luc Anselin - all rights reserved
*
* This file is part of GeoDa.
*
* GeoDa is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GeoDa 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GeoDa_Basemap_h
#define GeoDa_Basemap_h
#include <wx/tokenzr.h>
#include <wx/math.h>
#include <wx/dcgraph.h>
#include <utility>
#include <iostream>
#include <fstream>
#include <ogr_spatialref.h>
#include "../Algorithms/threadpool.h"
using namespace std;
namespace Gda {
/**
* BasemapItem is for "Basemap Source Configuration" dialog
* Each basemap source can be represented in the form of:
* GroupName.Name,url
* For example:
* Carto.Light,https://a.basemaps.cartocdn.com/light_all/{z}/{x}/{y}@2x.png
* Carto.Dark,https://a.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}@2x.png
* GeoDa will create a menu from a collection of BasemapItems
*/
class BasemapItem {
public:
BasemapItem() {}
BasemapItem(wxString _group, wxString _name, wxString _url) {
group = _group;
name = _name;
url = _url;
}
~BasemapItem() {}
BasemapItem& operator=(const BasemapItem& other) {
group = other.group;
name = other.name;
url = other.url;
return *this;
}
bool operator==(const BasemapItem& other) {
return (group == other.group && name == other.name && url == other.url);
}
void Reset() {
group = "";
name = "";
url = "";
}
wxString group;
wxString name;
wxString url;
};
/**
* BasemapGroup is a collection of BasemapItems.
* GeoDa will create a menu from an instance of BasemapGroup
* For example:
* Carto
* |__Light (https://a.basemaps.cartocdn.com/light_all/{z}/{x}/{y}@2x.png)
* |__Dark (https://a.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}@2x.png)
*/
class BasemapGroup {
public:
BasemapGroup() {}
BasemapGroup(wxString _name) {
name = _name;
}
~BasemapGroup() {}
BasemapGroup& operator=(const BasemapGroup& other) {
name = other.name;
items = other.items;
return *this;
}
void AddItem(BasemapItem item) {
items.push_back(item);
}
wxString name;
vector<BasemapItem> items;
};
// Return an instance of BasemapItem based on the basemap_source, which is
// at idx-th row of basemap_sources (e.g. GdaConst::gda_basemap_sources)
BasemapItem GetBasemapSelection(int idx, wxString basemap_sources);
// Construct a std::vector of BasemapGroup (which is a collection of
// BasemapItems) using basemap_sources (e.g. GdaConst::gda_basemap_sources
// or the value in "Basemap Sources:" TextCtrl in Basemap Configuration Dialog
vector<BasemapGroup> ExtractBasemapResources(wxString basemap_sources) ;
// inline function return separator for local file path
inline char separator() {
#ifdef __WIN32__
return '\\';
#else
return '/';
#endif
}
inline bool is_file_exist(const char *fileName) {
std::ifstream infile(fileName);
return infile.good() && (infile.peek() != std::ifstream::traits_type::eof());
}
inline double log2(double n) {
return log(n) / log(2.0);
}
class LatLng {
public:
LatLng() {}
LatLng(double _lat, double _lng) {
lat = _lat;
lng = _lng;
if (lat > 85.0511) lat = 85.0511;
if (lat < -85.0511) lat = -85.0511;
}
~LatLng(){}
double lat;
double lng;
double GetLatDeg() {return lat;}
double GetLngDeg() {return lng;}
double GetLatRad() {return lat * M_PI / 180.0;}
double GetLngRad() {return lng * M_PI / 180.0;}
};
class XYFraction {
public:
XYFraction(){}
XYFraction(int _x, int _y) {
x = _x;
y = _y;
xint = _x;
yint = _y;
xfrac = .0;
yfrac = .0;
}
XYFraction(double _x, double _y);
~XYFraction(){}
double x;
double y;
double xfrac;
double yfrac;
double xint;
double yint;
int GetXInt() { return (int)xint;}
int GetYInt() { return (int)yint;}
double GetXFrac() { return xfrac;}
double GetYFrac() { return yfrac;}
};
class Screen {
public:
Screen(){}
Screen(int _w, int _h) { width=_w; height= _h;}
~Screen(){}
int width;
int height;
};
class MapLayer {
public:
double north;
double south;
double west;
double east;
OGRCoordinateTransformation *poCT;
OGRCoordinateTransformation *poCT_rev;
MapLayer(){
poCT = NULL;
poCT_rev = NULL;
}
MapLayer(MapLayer* _map) {
north = _map->north;
south = _map->south;
west = _map->west;
east = _map->east;
poCT = NULL;
poCT_rev = NULL;
}
MapLayer(double _n, double _w, double _s, double _e){
north = _n;
south = _s;
west = _w;
east = _e;
poCT = NULL;
poCT_rev = NULL;
}
MapLayer(double _n, double _w, double _s, double _e,
OGRCoordinateTransformation *_poCT){
north = _n;
south = _s;
west = _w;
east = _e;
poCT = _poCT;
poCT_rev = NULL;
if (poCT!= NULL) {
if (poCT->Transform(1, &_w, &_n)) {
west = _w;
north = _n;
}
if (poCT->Transform(1, &_e, &_s)) {
east = _e;
south = _s;
}
OGRSpatialReference* s1 = poCT->GetTargetCS();
OGRSpatialReference* s2 = poCT->GetSourceCS();
#ifdef __PROJ6__
s1->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
s2->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
#endif
poCT_rev = OGRCreateCoordinateTransformation(s1, s2);
}
}
MapLayer(LatLng& nw, LatLng& se){
north = nw.lat;
west = nw.lng;
south = se.lat;
east = se.lng;
poCT = NULL;
poCT_rev = NULL;
}
~MapLayer(){
}
void GetWestNorthEastSouth(double& w, double& n, double& e, double& s) {
w = west;
n = north;
e = east;
s = south;
if (poCT_rev) {
poCT_rev->Transform(1, &w, &n);
poCT_rev->Transform(1, &e, &s);
}
}
double GetWidth() {
if (east >= west)
return east - west;
else
return 180 - east + 180 + west;
}
double GetHeight() { return north - south; }
bool IsWGS84Valid() { return north < 90 && south > -90 && east > -180 && west < 180;}
bool Pan(double lat, double lng) {
north += lat;
south += lat;
west += lng;
east += lng;
if (north > 90 || south < -90 || east > 180 || west < -180)
return false;
return true;
}
void UpdateExtent(double _w, double _s, double _e, double _n) {
north = _n;
south = _s;
east = _e;
west = _w;
}
void ZoomIn() {
// 2X by default
double w = east - west;
double h = north - south;
double offsetW = w / 4.0;
double offsetH = h / 4.0;
west = west + offsetW;
east = east - offsetW;
north = north - offsetH;
south = south + offsetH;
}
void ZoomOut() {
// 2X by default
double w = east - west;
double h = north - south;
double offsetW = w / 2.0;
double offsetH = h / 2.0;
west = west - offsetW;
east = east + offsetW;
north = north + offsetH;
south = south - offsetH;
}
MapLayer* operator=(const MapLayer* other) {
north = other->north;
south = other->south;
west = other->west;
east = other->east;
poCT = NULL;
poCT_rev = NULL;
if (other) {
if (other->poCT) {
OGRSpatialReference* s1 = other->poCT->GetSourceCS();
OGRSpatialReference* s2 = other->poCT->GetTargetCS();
#ifdef __PROJ6__
s1->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
s2->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
#endif
poCT = OGRCreateCoordinateTransformation(s1, s2);
}
if (other->poCT_rev) {
OGRSpatialReference* s1 = other->poCT_rev->GetSourceCS();
OGRSpatialReference* s2 = other->poCT_rev->GetTargetCS();
#ifdef __PROJ6__
s1->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
s2->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
#endif
poCT_rev = OGRCreateCoordinateTransformation(s1, s2);
}
}
return this;
}
};
// only for Web mercator projection
class Basemap {
int nn; // pow(2.0, zoom)
thread_pool pool;
boost::mutex mutex;
bool start_download;
int n_tasks;
int complete_tasks;
wxString GetRandomSubdomain(wxString url);
int GetOptimalZoomLevel(double paddingFactor=1.2);
int GetEasyZoomLevel();
void GetTiles();
void DownloadTile(int x, int y);
public:
Basemap() {}
Basemap(BasemapItem& basemap_item,
Screen* _screen,
MapLayer* _map,
MapLayer* _origMap,
wxString _cachePath,
OGRCoordinateTransformation* _poCT,
double scale_factor = 1.0);
~Basemap();
static const char* USER_AGENT;
OGRCoordinateTransformation *poCT;
BasemapItem basemap_item;
wxString basemapName;
wxString basemapUrl;
wxString imageSuffix;
wxString cachePath;
wxString nokia_id;
wxString nokia_code;
int startX;
int startY;
int endX;
int endY;
int offsetX;
int offsetY;
int widthP; // width of all tiles in pixel
int heightP; // height of all tiles in pixel
int leftP;
int topP;
double scale_factor;
bool isTileDrawn;
bool isTileReady;
bool isPan;
int panX;
int panY;
int zoom;
Screen* screen;
MapLayer* map;
MapLayer* origMap;
double Deg2Rad (double degree) { return degree * M_PI / 180.0; }
double Rad2Deg (double radians) { return radians * 180.0 / M_PI;}
XYFraction* LatLngToXY(LatLng &latlng);
XYFraction* LatLngToRawXY(LatLng &latlng);
LatLng* XYToLatLng(XYFraction &xy, bool isLL=false);
void LatLngToXY(double lng, double lat, int &x, int &y);
wxString GetTileUrl(int x, int y);
wxString GetTilePath(int x, int y);
bool Draw(wxBitmap* buffer);
void Extent(double _n, double _w, double _s, double _e,
OGRCoordinateTransformation *_poCT);
void ResizeScreen(int _width, int _height);
void ZoomIn(int mouseX, int mouseY);
void ZoomOut(int mouseX, int mouseY);
bool Zoom(bool is_zoomin, int x0, int y0, int x1, int y1);
void Pan(int x0, int y0, int x1, int y1);
void Reset(int map_type);
void Reset();
void Refresh();
bool IsDownloading();
void SetReady(bool flag);
bool IsExtentChanged();
void SetupMapType(BasemapItem& basemap_item);
void CleanCache();
wxString GetContentType();
};
}
#endif