-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.ts
More file actions
618 lines (538 loc) · 20.7 KB
/
Copy pathdecoder.ts
File metadata and controls
618 lines (538 loc) · 20.7 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// Copyright (C) 2026, Stichting Mapcode Foundation (http://www.mapcode.com)
//
// 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.
// Faithful TypeScript port of com.mapcode.Decoder (Java).
//
// The shared unicode tables (DECODE_CHARS, ASCII2LANGUAGE, UNICODE2ASCII), the
// aeuUnpack parsing state machine and decodeUTF16 already live in
// alphabet-converter.ts (extracted in Task 8). They are imported here rather than
// duplicated. Only the decode ALGORITHM lives in this file.
import { Point } from "./point.js";
import { Territory } from "./territory.js";
import { Data } from "./data.js";
import { DataModel } from "./data-model.js";
import { Common } from "./common.js";
import { Boundary } from "./boundary.js";
import { MapcodeZone } from "./mapcode-zone.js";
import { UnknownMapcodeError } from "./errors.js";
import { DECODE_CHARS, ASCII2LANGUAGE, UNICODE2ASCII, aeuUnpack, decodeUTF16 } from "./alphabet-converter.js";
// Re-export the shared tables so the DecoderTest table tests can assert their shapes.
export { DECODE_CHARS, ASCII2LANGUAGE, UNICODE2ASCII };
const DATA_MODEL = DataModel.getInstance();
const createBoundaryForTerritoryRecord = Boundary.createBoundaryForTerritoryRecord;
// Java: int / int truncates toward zero.
function intDiv(a: number, b: number): number {
return Math.trunc(a / b);
}
export class Decoder {
private constructor() {
// Prevent instantiation.
}
// ----------------------------------------------------------------------
// Method called from public API.
// ----------------------------------------------------------------------
static decodeToMapcodeZone(argMapcode: string, argTerritory: Territory): MapcodeZone {
let mapcode = argMapcode;
let territory = argTerritory;
let precisionPostfix = "";
const positionOfDash = mapcode.indexOf("-");
if (positionOfDash > 0) {
precisionPostfix = decodeUTF16(mapcode.substring(positionOfDash + 1).trim());
if (precisionPostfix.includes("Z")) {
throw new UnknownMapcodeError(
"Invalid character Z, mapcode=" + argMapcode + ", territory=" + argTerritory.toString(),
);
}
// Cut the precision postfix from the mapcode.
mapcode = mapcode.substring(0, positionOfDash);
}
// TODO: Explain what AEU unpack does.
mapcode = aeuUnpack(mapcode).trim();
if (mapcode.length === 0) {
throw new UnknownMapcodeError(
"Failed to AEU unpack, mapcode=" + argMapcode + ", territory=" + argTerritory.toString(),
);
}
const codexLen = mapcode.length - 1;
// *** long codes in states are handled by the country
if (codexLen >= 9) {
// International codes are 9 characters.
territory = Territory.AAA;
} else {
const parentTerritory = territory.getParentTerritory();
if (
(codexLen >= 8 &&
(parentTerritory === Territory.USA ||
parentTerritory === Territory.CAN ||
parentTerritory === Territory.AUS ||
parentTerritory === Territory.BRA ||
parentTerritory === Territory.CHN ||
parentTerritory === Territory.RUS)) ||
(codexLen >= 7 && (parentTerritory === Territory.IND || parentTerritory === Territory.MEX))
) {
territory = parentTerritory as Territory;
}
}
const territoryNumber = territory.getNumber();
const fromTerritoryRecord = DATA_MODEL.getDataFirstRecord(territoryNumber);
const uptoTerritoryRecord = DATA_MODEL.getDataLastRecord(territoryNumber);
// Determine the codex pattern as 2-digits: length-of-left-part * 10 + length-of-right-part.
const positionOfDot = mapcode.indexOf(".");
const codex = positionOfDot * 10 + (codexLen - positionOfDot);
let mapcodeZone = new MapcodeZone();
for (let territoryRecord = fromTerritoryRecord; territoryRecord <= uptoTerritoryRecord; territoryRecord++) {
const codexOfTerritory = Data.getCodex(territoryRecord);
const boundaryOfTerritory = createBoundaryForTerritoryRecord(territoryRecord);
if (Data.getTerritoryRecordType(territoryRecord) === Data.TERRITORY_RECORD_TYPE_NONE) {
if (Data.isNameless(territoryRecord)) {
// i = nameless
if (
(codexOfTerritory === 21 && codex === 22) ||
(codexOfTerritory === 22 && codex === 32) ||
(codexOfTerritory === 13 && codex === 23)
) {
mapcodeZone = Decoder.decodeNameless(mapcode, territoryRecord, precisionPostfix);
break;
}
} else {
// i = grid without headerletter
if (codexOfTerritory === codex || (codex === 22 && codexOfTerritory === 21)) {
mapcodeZone = Decoder.decodeGrid(
mapcode,
boundaryOfTerritory.getLonMicroDegMin(),
boundaryOfTerritory.getLatMicroDegMin(),
boundaryOfTerritory.getLonMicroDegMax(),
boundaryOfTerritory.getLatMicroDegMax(),
territoryRecord,
precisionPostfix,
);
// first of all, make sure the zone fits the country
mapcodeZone = mapcodeZone.restrictZoneTo(createBoundaryForTerritoryRecord(uptoTerritoryRecord));
if (Data.isRestricted(territoryRecord) && !mapcodeZone.isEmpty()) {
let nrZoneOverlaps = 0;
let j: number;
const result = mapcodeZone.getCenter();
// see if midpoint of mapcode zone is in any sub-area...
for (j = territoryRecord - 1; j >= fromTerritoryRecord; j--) {
if (!Data.isRestricted(j)) {
if (createBoundaryForTerritoryRecord(j).containsPoint(result)) {
nrZoneOverlaps++;
break;
}
}
}
if (nrZoneOverlaps === 0) {
// see if mapcode zone OVERLAPS any sub-area...
let zfound = new MapcodeZone();
for (j = fromTerritoryRecord; j < territoryRecord; j++) {
// try all smaller rectangles j
if (!Data.isRestricted(j)) {
const z = mapcodeZone.restrictZoneTo(createBoundaryForTerritoryRecord(j));
if (!z.isEmpty()) {
nrZoneOverlaps++;
if (nrZoneOverlaps === 1) {
// first fit! remember...
zfound = MapcodeZone.copy(z);
} else {
// nrZoneOverlaps > 1; more than one hit
break; // give up!
}
}
}
}
if (nrZoneOverlaps === 1) {
// intersected exactly ONE sub-area?
mapcodeZone = MapcodeZone.copy(zfound); // use the intersection found...
}
}
if (nrZoneOverlaps === 0) {
mapcodeZone = new MapcodeZone();
}
}
break;
}
}
} else if (Data.getTerritoryRecordType(territoryRecord) === Data.TERRITORY_RECORD_TYPE_PIPE) {
// i = grid with headerletter
if (
codex === codexOfTerritory + 10 &&
Data.headerLetter(territoryRecord).charAt(0) === mapcode.charAt(0)
) {
mapcodeZone = Decoder.decodeGrid(
mapcode.substring(1),
boundaryOfTerritory.getLonMicroDegMin(),
boundaryOfTerritory.getLatMicroDegMin(),
boundaryOfTerritory.getLonMicroDegMax(),
boundaryOfTerritory.getLatMicroDegMax(),
territoryRecord,
precisionPostfix,
);
break;
}
} else {
// i = autoheader (PLUS or STAR)
if ((codex === 23 && codexOfTerritory === 22) || (codex === 33 && codexOfTerritory === 23)) {
mapcodeZone = Decoder.decodeAutoHeader(mapcode, territoryRecord, precisionPostfix);
break;
}
}
}
mapcodeZone = mapcodeZone.restrictZoneTo(createBoundaryForTerritoryRecord(uptoTerritoryRecord));
return mapcodeZone;
}
// ----------------------------------------------------------------------
// Private methods.
// ----------------------------------------------------------------------
private static decodeGrid(
str: string,
minx: number,
miny: number,
maxx: number,
maxy: number,
m: number,
extrapostfix: string,
): MapcodeZone {
let result = str;
let relx: number;
let rely: number;
const codexlen = result.length - 1; // length ex dot
let prelen = result.indexOf("."); // dot position
if (prelen === 1 && codexlen === 5) {
prelen++;
result = result.substring(0, 1) + result.charAt(2) + "." + result.substring(3);
}
const postlen = codexlen - prelen;
let divx: number;
let divy: number;
divy = DATA_MODEL.getSmartDiv(m);
if (divy === 1) {
divx = Common.X_SIDE[prelen];
divy = Common.Y_SIDE[prelen];
} else {
divx = intDiv(Common.NC[prelen], divy);
}
if (prelen === 4 && divx === 961 && divy === 961) {
result = result.substring(0, 1) + result.charAt(2) + result.charAt(1) + result.substring(3);
}
let v = Decoder.decodeBase31(result);
if (divx !== divy && prelen > 2) {
// D==6
const d = Decoder.decodeSixWide(v, divx, divy);
relx = d.getLonMicroDeg();
rely = d.getLatMicroDeg();
} else {
relx = intDiv(v, divy);
rely = divy - 1 - (v % divy);
}
const ygridsize = intDiv(maxy - miny + divy - 1, divy);
const xgridsize = intDiv(maxx - minx + divx - 1, divx);
rely = miny + rely * ygridsize;
relx = minx + relx * xgridsize;
const yp = Common.Y_SIDE[postlen];
const dividery = intDiv(ygridsize + yp - 1, yp);
const xp = Common.X_SIDE[postlen];
const dividerx = intDiv(xgridsize + xp - 1, xp);
let rest = result.substring(prelen + 1);
// decode relative (postfix vs rely, relx)
let difx: number;
let dify: number;
if (postlen === 3) {
const d = Decoder.decodeTriple(rest);
difx = d.getLonMicroDeg();
dify = d.getLatMicroDeg();
} else {
if (postlen === 4) {
rest = String(rest.charAt(0)) + rest.charAt(2) + rest.charAt(1) + rest.charAt(3);
}
v = Decoder.decodeBase31(rest);
difx = intDiv(v, yp);
dify = v % yp;
}
dify = yp - 1 - dify;
const cornery = rely + dify * dividery;
const cornerx = relx + difx * dividerx;
const pt = Point.fromMicroDeg(cornery, cornerx);
if (!createBoundaryForTerritoryRecord(m).containsPoint(pt)) {
return new MapcodeZone(); // already out of range
}
const decodeMaxx = relx + xgridsize < maxx ? relx + xgridsize : maxx;
const decodeMaxy = rely + ygridsize < maxy ? rely + ygridsize : maxy;
return Decoder.decodeExtension(cornery, cornerx, dividerx << 2, dividery, extrapostfix, 0, decodeMaxy, decodeMaxx); // grid
}
private static decodeNameless(str: string, firstrec: number, extrapostfix: string): MapcodeZone {
let result = str;
const codexm = Data.getCodex(firstrec);
if (codexm === 22) {
result = result.substring(0, 3) + result.substring(4);
} else {
result = result.substring(0, 2) + result.substring(3);
}
const a = Common.countCityCoordinatesForCountry(codexm, firstrec, firstrec);
const p = intDiv(31, a);
const r = 31 % a;
let v = 0;
let nrX: number;
let swapletters = false;
if (codexm !== 21 && a <= 31) {
const offset = DECODE_CHARS[result.charCodeAt(0)];
if (offset < r * (p + 1)) {
nrX = intDiv(offset, p + 1);
} else {
swapletters = p === 1 && codexm === 22;
nrX = r + intDiv(offset - r * (p + 1), p);
}
} else if (codexm !== 21 && a < 62) {
nrX = DECODE_CHARS[result.charCodeAt(0)];
if (nrX < 62 - a) {
swapletters = codexm === 22;
} else {
nrX = nrX + nrX - 62 + a;
}
} else {
// codex==21 || A>=62
const basePower = codexm === 21 ? 961 * 961 : 961 * 961 * 31;
let basePowerA = intDiv(basePower, a);
if (a === 62) {
basePowerA++;
} else {
basePowerA = 961 * intDiv(basePowerA, 961);
}
// decode and determine x
v = Decoder.decodeBase31(result);
nrX = intDiv(v, basePowerA);
v %= basePowerA;
}
if (swapletters && !Data.isSpecialShape(firstrec + nrX)) {
result = result.substring(0, 2) + result.charAt(3) + result.charAt(2) + result.charAt(4);
}
if (codexm !== 21 && a <= 31) {
v = Decoder.decodeBase31(result);
if (nrX > 0) {
v -= (nrX * p + (nrX < r ? nrX : r)) * 961 * 961;
}
} else if (codexm !== 21 && a < 62) {
v = Decoder.decodeBase31(result.substring(1));
if (nrX >= 62 - a && v >= 16 * 961 * 31) {
v -= 16 * 961 * 31;
nrX++;
}
}
if (nrX > a) {
// past end!
return new MapcodeZone();
}
const territoryRecord = firstrec + nrX;
let side = DATA_MODEL.getSmartDiv(territoryRecord);
let xSIDE = side;
const boundary = createBoundaryForTerritoryRecord(territoryRecord);
const maxx = boundary.getLonMicroDegMax();
const maxy = boundary.getLatMicroDegMax();
const minx = boundary.getLonMicroDegMin();
const miny = boundary.getLatMicroDegMin();
let dx: number;
let dy: number;
if (Data.isSpecialShape(territoryRecord)) {
xSIDE *= side;
side = 1 + intDiv(maxy - miny, 90);
xSIDE = intDiv(xSIDE, side);
const d = Decoder.decodeSixWide(v, xSIDE, side);
dx = d.getLonMicroDeg();
dy = side - 1 - d.getLatMicroDeg();
} else {
dy = v % side;
dx = intDiv(v, side);
}
if (dx >= xSIDE) {
// else out-of-range!
return new MapcodeZone(); // return undefined (out of range!)
}
const dividerx4 = Common.xDivider(miny, maxy); // 4 times too large!
const dividery = 90;
const cornerx = minx + intDiv(dx * dividerx4, 4);
const cornery = maxy - dy * dividery;
return Decoder.decodeExtension(
cornery,
cornerx,
dividerx4,
-dividery,
extrapostfix,
(dx * dividerx4) % 4,
miny,
maxx,
); // nameless
}
private static decodeAutoHeader(input: string, m: number, extrapostfix: string): MapcodeZone {
// returns empty zone in case of error
let storageStart = 0;
const codexm = Data.getCodex(m);
let value = Decoder.decodeBase31(input); // decode top (before dot)
value *= 961 * 31;
const triple = Decoder.decodeTriple(input.substring(input.length - 3));
// decode bottom 3 chars
let i = m;
// eslint-disable-next-line no-constant-condition
while (true) {
if (
Data.getTerritoryRecordType(i) < Data.TERRITORY_RECORD_TYPE_PLUS ||
Data.getCodex(i) !== codexm
) {
return new MapcodeZone(); // return undefined
}
const maxx = createBoundaryForTerritoryRecord(i).getLonMicroDegMax();
const maxy = createBoundaryForTerritoryRecord(i).getLatMicroDegMax();
const minx = createBoundaryForTerritoryRecord(i).getLonMicroDegMin();
const miny = createBoundaryForTerritoryRecord(i).getLatMicroDegMin();
let h = intDiv(maxy - miny + 89, 90);
const xdiv = Common.xDivider(miny, maxy);
let w = intDiv((maxx - minx) * 4 + xdiv - 1, xdiv);
h = 176 * intDiv(h + 176 - 1, 176);
w = 168 * intDiv(w + 168 - 1, 168);
let product = intDiv(w, 168) * intDiv(h, 176) * 961 * 31;
if (Data.getTerritoryRecordType(i) === Data.TERRITORY_RECORD_TYPE_PLUS) {
const goodRounder = codexm >= 23 ? 961 * 961 * 31 : 961 * 961;
product = intDiv(storageStart + product + goodRounder - 1, goodRounder) * goodRounder - storageStart;
}
if (value >= storageStart && value < storageStart + product) {
// code belongs here?
const dividerx = intDiv(maxx - minx + w - 1, w);
const dividery = intDiv(maxy - miny + h - 1, h);
value -= storageStart;
value = intDiv(value, 961 * 31);
let vx = intDiv(value, intDiv(h, 176));
vx = vx * 168 + triple.getLonMicroDeg();
const vy = (value % intDiv(h, 176)) * 176 + triple.getLatMicroDeg();
const cornery = maxy - vy * dividery;
const cornerx = minx + vx * dividerx;
if (cornerx < minx || cornerx >= maxx || cornery < miny || cornery > maxy) {
return new MapcodeZone(); // corner out of bounds
}
return Decoder.decodeExtension(cornery, cornerx, dividerx << 2, -dividery, extrapostfix, 0, miny, maxx); // autoheader
}
storageStart += product;
i++;
}
}
private static decodeTriple(str: string): Point {
const c1 = DECODE_CHARS[str.charCodeAt(0)];
const x = Decoder.decodeBase31(str.substring(1));
if (c1 < 24) {
return Point.fromMicroDeg(intDiv(c1, 6) * 34 + (x % 34), (c1 % 6) * 28 + intDiv(x, 34));
}
return Point.fromMicroDeg((x % 40) + 136, intDiv(x, 40) + 24 * (c1 - 24));
}
private static decodeSixWide(v: number, width: number, height: number): Point {
let d: number;
let col = intDiv(v, height * 6);
const maxcol = intDiv(width - 4, 6);
if (col >= maxcol) {
col = maxcol;
d = width - maxcol * 6;
} else {
d = 6;
}
const w = v - col * height * 6;
return Point.fromMicroDeg(height - 1 - intDiv(w, d), col * 6 + (w % d));
}
// lowest level decode routine: decode up to dot or EOS; returns negative on error.
private static decodeBase31(code: string): number {
let value = 0;
for (let k = 0; k < code.length; k++) {
const c = code.charCodeAt(k);
if (c === ".".charCodeAt(0)) {
return value;
}
if (DECODE_CHARS[c] < 0) {
return -1;
}
value = (value * 31 + DECODE_CHARS[c]) | 0;
}
return value;
}
private static decodeExtension(
y: number,
x: number,
dividerx0: number,
dividery0: number,
extrapostfix: string,
lonOffset4: number,
extremeLatMicroDeg: number,
maxLonMicroDeg: number,
): MapcodeZone {
const mapcodeZone = new MapcodeZone();
let dividerx4 = dividerx0;
let dividery = dividery0;
let processor = 1;
let lon32 = 0;
let lat32 = 0;
let odd = false;
let idx = 0;
// decode up to 8 characters
const len = extrapostfix.length > 8 ? 8 : extrapostfix.length;
while (idx < len) {
let c1 = extrapostfix.charCodeAt(idx);
idx++;
c1 = DECODE_CHARS[c1];
if (c1 < 0 || c1 === 30) {
return new MapcodeZone();
}
const y1 = intDiv(c1, 5);
const x1 = c1 % 5;
let y2: number;
let x2: number;
if (idx < len) {
let c2 = extrapostfix.charCodeAt(idx);
idx++;
c2 = DECODE_CHARS[c2];
if (c2 < 0 || c2 === 30) {
return new MapcodeZone();
}
y2 = intDiv(c2, 6);
x2 = c2 % 6;
} else {
odd = true;
y2 = 0;
x2 = 0;
}
processor *= 30;
lon32 = lon32 * 30 + x1 * 6 + x2;
lat32 = lat32 * 30 + y1 * 5 + y2;
}
while (processor < Point.MAX_PRECISION_FACTOR) {
dividerx4 *= 30;
dividery *= 30;
processor *= 30;
}
const lon4 = x * 4 * Point.MAX_PRECISION_FACTOR + lon32 * dividerx4 + lonOffset4 * Point.MAX_PRECISION_FACTOR;
const lat1 = y * Point.MAX_PRECISION_FACTOR + lat32 * dividery;
// determine the range of coordinates that are encoded to this mapcode
if (odd) {
mapcodeZone.setFromFractions(lat1, lon4, 5 * dividery, 6 * dividerx4);
} else {
mapcodeZone.setFromFractions(lat1, lon4, dividery, dividerx4);
}
// FORCE_RECODE - restrict the coordinate range to the extremes that were provided
if (mapcodeZone.getLonFractionMax() > maxLonMicroDeg * Point.LON_MICRODEG_TO_FRACTIONS_FACTOR) {
mapcodeZone.setLonFractionMax(maxLonMicroDeg * Point.LON_MICRODEG_TO_FRACTIONS_FACTOR);
}
if (dividery >= 0) {
if (mapcodeZone.getLatFractionMax() > extremeLatMicroDeg * Point.LAT_MICRODEG_TO_FRACTIONS_FACTOR) {
mapcodeZone.setLatFractionMax(extremeLatMicroDeg * Point.LAT_MICRODEG_TO_FRACTIONS_FACTOR);
}
} else {
if (mapcodeZone.getLatFractionMin() < extremeLatMicroDeg * Point.LAT_MICRODEG_TO_FRACTIONS_FACTOR) {
mapcodeZone.setLatFractionMin(extremeLatMicroDeg * Point.LAT_MICRODEG_TO_FRACTIONS_FACTOR);
}
}
return mapcodeZone;
}
}