/
RoutableTile.ts
55 lines (44 loc) · 1.63 KB
/
RoutableTile.ts
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
import ILocation from "../../interfaces/ILocation";
import TileCoordinate from "./TileCoordinate";
function tile_to_lat(coordinate: TileCoordinate) {
// from https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
const n = Math.PI - 2 * Math.PI * coordinate.y / Math.pow(2, coordinate.zoom);
return (180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n))));
}
function tile_to_long(coordinate: TileCoordinate) {
// from https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
return (coordinate.x / Math.pow(2, coordinate.zoom) * 360 - 180);
}
export class RoutableTile {
public id: string;
public coordinate?: TileCoordinate;
protected nodes: Set<string>;
protected ways: Set<string>;
constructor(id: string, nodes: Set<string>, ways: Set<string>) {
this.id = id;
this.nodes = nodes;
this.ways = ways;
}
public getWays() {
return this.ways;
}
public getNodes() {
return this.nodes;
}
public contains(location: ILocation): boolean {
const top = tile_to_lat(this.coordinate);
const left = tile_to_long(this.coordinate);
const next = new TileCoordinate(this.coordinate.zoom, this.coordinate.x + 1, this.coordinate.y + 1);
const bottom = tile_to_lat(next);
const right = tile_to_long(next);
if (location.latitude > top || location.latitude < bottom) {
return false;
} else if (location.longitude < left || location.longitude > right) {
return false;
}
return true;
}
}
export interface IRoutableTileIndex {
[id: string]: Promise<RoutableTile>;
}