/
FootpathsProviderDefault.ts
85 lines (71 loc) · 2.94 KB
/
FootpathsProviderDefault.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
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
import { inject, injectable } from "inversify";
import LDFetch from "ldfetch";
import { Footpath, IFootpathIndex } from "../../entities/footpaths/footpath";
import TileCoordinate from "../../entities/tiles/TileCoordinate";
import ILocation from "../../interfaces/ILocation";
import { LDLoader } from "../../loader/ldloader";
import { IndexThingView } from "../../loader/views";
import TYPES from "../../types";
import { PLANNER } from "../../uri/constants";
import URI from "../../uri/uri";
import { lat_to_tile, long_to_tile } from "../../util/Tiles";
import IStop from "../stops/IStop";
import IFootpathsProvider from "./IFootpathsProvider";
const ZOOM = 12;
interface ITiledFootpathIndex {
[id: string]: Promise<IFootpathIndex>;
}
@injectable()
export default class FootpathsProviderDefault implements IFootpathsProvider {
protected ldFetch: LDFetch;
protected ldLoader: LDLoader;
protected paths: ITiledFootpathIndex;
constructor(
@inject(TYPES.LDFetch) ldFetch: LDFetch,
) {
this.ldFetch = ldFetch;
this.ldLoader = new LDLoader();
this.paths = {};
}
public async get(stop: IStop): Promise<IFootpathIndex> {
const tileId = this.getIdForLocation(ZOOM, stop);
if (!this.paths[tileId]) {
this.paths[tileId] = this.getByUrl(tileId);
}
return this.paths[tileId];
}
public getIdForLocation(zoom: number, location: ILocation): string {
const y = lat_to_tile(location.latitude, zoom);
const x = long_to_tile(location.longitude, zoom);
const coordinate = new TileCoordinate(zoom, x, y );
return this.getIdForTileCoords(coordinate);
}
public getIdForTileCoords(coordinate: TileCoordinate): string {
return `https://hdelva.be/stops/distances/${coordinate.zoom}/${coordinate.x}/${coordinate.y}`;
}
public getByLocation(zoom: number, location: ILocation): Promise<IFootpathIndex> {
const y = lat_to_tile(location.latitude, zoom);
const x = long_to_tile(location.longitude, zoom);
const coordinate = new TileCoordinate(zoom, x, y);
return this.getByTileCoords(coordinate);
}
public async getByTileCoords(coordinate: TileCoordinate): Promise<IFootpathIndex> {
const url = this.getIdForTileCoords(coordinate);
return await this.getByUrl(url);
}
protected async getByUrl(url: string): Promise<IFootpathIndex> {
const rdfThing = await this.ldFetch.get(url);
const triples = rdfThing.triples;
const [paths] = this.ldLoader.process(triples, [
this.getPathsView(),
]);
return paths;
}
protected getPathsView() {
const nodesView = new IndexThingView(Footpath.create);
nodesView.addMapping(URI.inNS(PLANNER, "source"), "from");
nodesView.addMapping(URI.inNS(PLANNER, "destination"), "to");
nodesView.addMapping(URI.inNS(PLANNER, "distance"), "distance");
return nodesView;
}
}