-
Notifications
You must be signed in to change notification settings - Fork 2
/
mapycz.deno.ts
126 lines (121 loc) · 2.11 KB
/
mapycz.deno.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
// The authors disclaim copyright to this source code (they are ashamed to
// admit they wrote it)
const alphabet = [
"0",
"A",
"B",
"C",
"D",
"2",
"E",
"F",
"G",
"H",
"4",
"I",
"J",
"K",
"L",
"M",
"N",
"6",
"O",
"P",
"Q",
"R",
"S",
"T",
"8",
"U",
"V",
"W",
"X",
"Y",
"Z",
"-",
"1",
"a",
"b",
"c",
"d",
"3",
"e",
"f",
"g",
"h",
"5",
"i",
"j",
"k",
"l",
"m",
"n",
"7",
"o",
"p",
"q",
"r",
"s",
"t",
"9",
"u",
"v",
"w",
"x",
"y",
"z",
".",
];
function serializeNumber(delta: number, orig: number) {
let code = "";
if (delta >= -1024 && delta < 1024) {
code += alphabet[(delta + 1024) >> 6];
code += alphabet[(delta + 1024) & 63];
} else if (delta >= -32768 && delta < 32768) {
const value = 131072 | (delta + 32768);
code += alphabet[(value >> 12) & 63];
code += alphabet[(value >> 6) & 63];
code += alphabet[value & 63];
} else {
const value = 805306368 | (orig & 268435455);
code += alphabet[(value >> 24) & 63];
code += alphabet[(value >> 18) & 63];
code += alphabet[(value >> 12) & 63];
code += alphabet[(value >> 6) & 63];
code += alphabet[value & 63];
}
return code;
}
function coordsToString(coords: { lat: number; lon: number }[]): string {
let ox = 0;
let oy = 0;
let result = "";
for (const { lat, lon } of coords) {
const x = Math.round(((lon + 180) * (1 << 28)) / 360);
const y = Math.round(((lat + 90) * (1 << 28)) / 180);
const dx = x - ox;
const dy = y - oy;
result += serializeNumber(dx, x);
result += serializeNumber(dy, y);
ox = x;
oy = y;
}
return result;
}
export function getUrlForPoints(points: { lat: number; lon: number }[]) {
return `https://en.mapy.cz/zakladni?vlastni-body&uc=${
coordsToString(
points,
)
}`;
}
export function getImageForPoints(points: { lat: number; lon: number }[]) {
return (
`https://en.mapy.cz/screenshoter?` +
new URLSearchParams({
url: getUrlForPoints(points) + "&p=3&l=0",
width: "1200",
height: "630",
}).toString()
);
}