-
Notifications
You must be signed in to change notification settings - Fork 4
/
coords.js
80 lines (66 loc) · 3.02 KB
/
coords.js
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
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const EARTH_RADIUS_METERS = 6378137;
const EQUATOR_CIRCUMFERENCE_METERS = 2 * Math.PI * EARTH_RADIUS_METERS;
const MAX_LAT = 85.051129;
const INVERSE_PRECISION_METERS = 0.1;
const INVERSE_MAX_ITERATIONS = 100;
class Coords {
static degreesToRadians(degrees) {
return degrees / 180 * Math.PI;
}
static mercatorYFromLatDegrees(latDegrees) {
return Math.log(Math.tan(Math.PI / 4.0 + Coords.degreesToRadians(latDegrees) / 2.0)) / Math.PI;
}
static mercatorXFromLonDegrees(lonDegrees) {
return lonDegrees / 180;
}
static latDegreesFromMercatorY(mercatorY) {
return (2.0 * Math.atan(Math.exp(Math.PI * mercatorY))*180/Math.PI - 90);
}
static lonDegreesFromMercatorX(mercatorX) {
return mercatorX * 180;
}
static circumferenceAtLatRadians(latRadians) {
return EQUATOR_CIRCUMFERENCE_METERS * Math.cos(latRadians);
}
static oneMeterInMercatorAtLatRadians(latRadians) {
// The constant number "2" is the extent of Mercator Coordinates, i.e.:
// max(MercatorX) - min(MercatorX) = 1 - (-1) = 2.
return 2 / Coords.circumferenceAtLatRadians(latRadians);
}
static oneMeterInMercatorAtLatDegrees(latDegrees) {
return Coords.oneMeterInMercatorAtLatRadians(Coords.degreesToRadians(latDegrees));
}
constructor(sceneOriginLonLatDegrees) {
this.sceneOriginLonLatDegrees = sceneOriginLonLatDegrees;
this.sceneOriginMercator = new THREE.Vector2(
Coords.mercatorXFromLonDegrees(sceneOriginLonLatDegrees.x),
Coords.mercatorYFromLatDegrees(sceneOriginLonLatDegrees.y));
this.oneMeterInMercatorAtSceneOriginLatitude =
Coords.oneMeterInMercatorAtLatDegrees(this.sceneOriginLonLatDegrees.y);
}
lonLatDegreesToSceneCoords(lonLatDegrees) {
return new THREE.Vector2(
(Coords.mercatorXFromLonDegrees(lonLatDegrees.x) - this.sceneOriginMercator.x) / this.oneMeterInMercatorAtSceneOriginLatitude,
-(Coords.mercatorYFromLatDegrees(lonLatDegrees.y) - this.sceneOriginMercator.y) / this.oneMeterInMercatorAtSceneOriginLatitude,
);
}
sceneCoordsToLatLonDegrees(scenePoint) {
const oneMeter = Coords.oneMeterInMercatorAtLatDegrees(this.sceneOriginLonLatDegrees.y);
return new THREE.Vector2(Coords.lonDegreesFromMercatorX(this.sceneOriginMercator.x + scenePoint.x * this.oneMeterInMercatorAtSceneOriginLatitude),
Coords.latDegreesFromMercatorY(this.sceneOriginMercator.y - scenePoint.y * this.oneMeterInMercatorAtSceneOriginLatitude));
}
}
export {Coords};