forked from mapbox/tippecanoe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
projection.c
63 lines (51 loc) · 1.53 KB
/
projection.c
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
#include <math.h>
#include "projection.h"
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
void latlon2tile(double lat, double lon, int zoom, long long *x, long long *y) {
// Must limit latitude somewhere to prevent overflow.
// 89.9 degrees latitude is 0.621 worlds beyond the edge of the flat earth,
// hopefully far enough out that there are few expectations about the shape.
if (lat < -89.9) {
lat = -89.9;
}
if (lat > 89.9) {
lat = 89.9;
}
if (lon < -360) {
lon = -360;
}
if (lon > 360) {
lon = 360;
}
double lat_rad = lat * M_PI / 180;
unsigned long long n = 1LL << zoom;
long long llx = n * ((lon + 180) / 360);
long long lly = n * (1 - (log(tan(lat_rad) + 1 / cos(lat_rad)) / M_PI)) / 2;
*x = llx;
*y = lly;
}
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
void tile2latlon(long long x, long long y, int zoom, double *lat, double *lon) {
unsigned long long n = 1LL << zoom;
*lon = 360.0 * x / n - 180.0;
*lat = atan(sinh(M_PI * (1 - 2.0 * y / n))) * 180.0 / M_PI;
}
unsigned long long encode(unsigned int wx, unsigned int wy) {
long long out = 0;
int i;
for (i = 0; i < 32; i++) {
long long v = ((wx >> (32 - (i + 1))) & 1) << 1;
v |= (wy >> (32 - (i + 1))) & 1;
v = v << (64 - 2 * (i + 1));
out |= v;
}
return out;
}
void decode(unsigned long long index, unsigned *wx, unsigned *wy) {
*wx = *wy = 0;
int i;
for (i = 0; i < 32; i++) {
*wx |= ((index >> (64 - 2 * (i + 1) + 1)) & 1) << (32 - (i + 1));
*wy |= ((index >> (64 - 2 * (i + 1) + 0)) & 1) << (32 - (i + 1));
}
}