Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
146 lines (121 loc) · 3.89 KB
/
Copy pathutils.js
File metadata and controls
146 lines (121 loc) · 3.89 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
const GOOGLE_TILE_SIZE = 256;
import log2 from './math/log2';
function latLng2World({lat, lng}) {
const sin = Math.sin(lat * Math.PI / 180);
const x = (lng / 360 + 0.5);
let y = (0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI);
y = y < -1 // eslint-disable-line
? -1
: y > 1
? 1
: y;
return {x, y};
}
function world2LatLng({x, y}) {
const n = Math.PI - 2 * Math.PI * y;
return {
lat: (180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)))),
lng: x * 360 - 180,
};
}
// Thank you wiki https://en.wikipedia.org/wiki/Geographic_coordinate_system
function latLng2MetersPerDegree({ lat }) {
const phi = lat * Math.PI / 180;
const metersPerLatDegree = 111132.92 - 559.82 * Math.cos(2 * phi) +
1.175 * Math.cos(4 * phi) - 0.0023 * Math.cos(6 * phi);
const metersPerLngDegree = 111412.84 * Math.cos(phi) -
93.5 * Math.cos(3 * phi) + 0.118 * Math.cos(5 * phi);
return {metersPerLatDegree, metersPerLngDegree};
}
function meters2LatLngBounds(meters, {lat, lng}) {
const {metersPerLatDegree, metersPerLngDegree} = latLng2MetersPerDegree({ lat });
const latDelta = 0.5 * meters / metersPerLatDegree;
const lngDelta = 0.5 * meters / metersPerLngDegree;
return {
nw: {
lat: lat - latDelta,
lng: lng - lngDelta,
},
se: {
lat: lat + latDelta,
lng: lng + lngDelta,
},
};
}
function meters2WorldSize(meters, {lat, lng}) {
const {nw, se} = meters2LatLngBounds(meters, {lat, lng});
const nwWorld = latLng2World(nw);
const seWorld = latLng2World(se);
const w = Math.abs(seWorld.x - nwWorld.x);
const h = Math.abs(seWorld.y - nwWorld.y);
return {w, h};
}
export default {
fitBounds({nw, se}, {width, height}) {
const EPS = 0.000000001;
const nwWorld = latLng2World(nw);
const seWorld = latLng2World(se);
const dx = nwWorld.x < seWorld.x
? seWorld.x - nwWorld.x
: (1 - nwWorld.x) + seWorld.x;
const dy = seWorld.y - nwWorld.y;
if (dx <= 0 && dy <= 0) {
return null;
}
const zoomX = log2(width / GOOGLE_TILE_SIZE / dx);
const zoomY = log2(height / GOOGLE_TILE_SIZE / dy);
const zoom = Math.floor(EPS + Math.min(zoomX, zoomY));
// TODO find center just unproject middle world point
const middle = {
x: nwWorld.x < seWorld.x // eslint-disable-line
? 0.5 * (nwWorld.x + seWorld.x)
: nwWorld.x + seWorld.x - 1 > 0
? 0.5 * (nwWorld.x + seWorld.x - 1)
: 0.5 * (1 + nwWorld.x + seWorld.x),
y: 0.5 * (nwWorld.y + seWorld.y),
};
return {
center: world2LatLng(middle),
zoom,
};
},
// -------------------------------------------------------------------
// Helpers to calc some markers size
meters2ScreenPixels(meters, {lat, lng}, zoom) {
const {w, h} = meters2WorldSize(meters, {lat, lng});
const scale = Math.pow(2, zoom);
const wScreen = w * scale * GOOGLE_TILE_SIZE;
const hScreen = h * scale * GOOGLE_TILE_SIZE;
return {
w: wScreen,
h: hScreen,
};
},
// --------------------------------------------------
// Helper functions for working with svg tiles, (examples coming soon)
tile2LatLng({x, y}, zoom) {
const n = Math.PI - 2 * Math.PI * y / Math.pow(2, zoom);
return ({
lat: (180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)))),
lng: (x / Math.pow(2, zoom) * 360 - 180),
});
},
latLng2Tile({lat, lng}, zoom) {
const worldCoords = latLng2World({lat, lng});
const scale = Math.pow(2, zoom);
return {
x: Math.floor(worldCoords.x * scale),
y: Math.floor(worldCoords.y * scale),
};
},
getTilesIds({from, to}, zoom) {
const scale = Math.pow(2, zoom);
const ids = [];
for (let x = from.x; x !== (to.x + 1) % scale; x = (x + 1) % scale) {
for (let y = from.y; y !== (to.y + 1) % scale; y = (y + 1) % scale) {
ids.push([zoom, x, y]);
}
}
return ids;
},
};