-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.ts
138 lines (122 loc) · 3.32 KB
/
helpers.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
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
import area from '@turf/area';
import bboxClip from '@turf/bbox-clip';
import bboxPolygon from '@turf/bbox-polygon';
import { FeatureCollection } from '@turf/helpers';
import { BBox, mercator, Tile } from 'idly-common/lib/geo';
import parser from 'idly-faster-osm-parser';
import { IDLY_NS } from '../constants';
export function addSource(layer: any, source: string) {
return {
...layer,
source,
id: getNameSpacedLayerId(layer.id, source),
};
}
export function getNameSpacedLayerId(layerId: string, source: string) {
return source + IDLY_NS + layerId;
}
export function reverseGetNameSpacedLayerId(id: string) {
return id.split(IDLY_NS)[1];
}
export function tilesFilterSmall(
tiles: Tile[],
viewportBbox: BBox,
minimumOverlap: number
): Tile[] {
const final = tiles
.map(tile => {
const tilePolygon = bboxPolygon(mercator.bbox(tile.x, tile.y, tile.z));
const tileInsideBbox = bboxClip(tilePolygon, viewportBbox);
const fractionVisible =
area(tileInsideBbox) / area(bboxPolygon(viewportBbox));
return { fractionVisible, tile };
})
.sort((a, b) => b.fractionVisible - a.fractionVisible)
.reduce(
(prev, { fractionVisible, tile }) => {
if (prev.sum >= minimumOverlap) {
return prev;
}
prev.tiles.push(tile);
prev.sum += fractionVisible;
return prev;
},
{ sum: 0, tiles: [] as Tile[] }
);
console.log(
'thresh',
minimumOverlap,
'orig',
tiles.length,
'removed',
tiles.length - final.tiles.length
);
return final.tiles;
}
export function blankFC(): FeatureCollection<any, any> {
return {
type: 'FeatureCollection',
features: [],
};
}
const cache: any = new Map();
export async function fetchTileXml(
x: number,
y: number,
zoom: number
): Promise<any> {
// return parser(rsp);
const bboxStr = mercator.bbox(x, y, zoom).join(',');
if (cache.has(bboxStr)) {
return cache.get(bboxStr);
}
const response = await fetch(
`https://www.openstreetmap.org/api/0.6/map?bbox=${bboxStr}`
);
if (!response.ok) {
throw new Error(response.statusText);
}
const entities = parser(await response.text());
cache.set(bboxStr, entities);
return entities;
}
export function bboxify(e: any, factor: number) {
return [
[e.point.x - factor, e.point.y - factor],
[e.point.x + factor, e.point.y + factor],
];
}
export const hideVersion = (index?: string) =>
index && modifiedIdParse(index)[0];
export const modifiedIdParse = (index: string) => index.split('#');
export const distance = (
[x1, y1]: [number, number],
[x2, y2]: [number, number]
) => Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
export function tapLog<T>(fn: T) {
if (typeof fn !== 'function') {
console.log('Log-- expression', fn);
return fn;
}
return (...args: any[]) => {
console.log('Log-- input', ...args);
const result = (fn as any).apply(null, args);
console.log('Log-- output', result);
return result;
};
}
export function bindThis(_: any, key: any, { value: fn }: any) {
return {
configurable: true,
get() {
const value = fn.bind(this);
Object.defineProperty(this, key, {
value,
configurable: true,
writable: true,
});
return value;
},
};
}
export function isRelation(id?: string) {}