-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmapcode.js
2496 lines (2233 loc) · 90.1 KB
/
mapcode.js
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2003-2018 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.
*/
var iso3166alpha = [
'VAT', 'MCO', 'GIB', 'TKL', 'CCK', 'BLM', 'NRU', 'TUV', 'MAC', 'SXM',
'MAF', 'NFK', 'PCN', 'BVT', 'BMU', 'IOT', 'SMR', 'GGY', 'AIA', 'MSR',
'JEY', 'CXR', 'WLF', 'VGB', 'LIE', 'ABW', 'MHL', 'ASM', 'COK', 'SPM',
'NIU', 'KNA', 'CYM', 'BES', 'MDV', 'SHN', 'MLT', 'GRD', 'VIR', 'MYT',
'SJM', 'VCT', 'HMD', 'BRB', 'ATG', 'CUW', 'SYC', 'PLW', 'MNP', 'AND',
'GUM', 'IMN', 'LCA', 'FSM', 'SGP', 'TON', 'DMA', 'BHR', 'KIR', 'TCA',
'STP', 'HKG', 'MTQ', 'FRO', 'GLP', 'COM', 'MUS', 'REU', 'LUX', 'WSM',
'SGS', 'PYF', 'CPV', 'TTO', 'BRN', 'ATF', 'PRI', 'CYP', 'LBN', 'JAM',
'GMB', 'QAT', 'FLK', 'VUT', 'MNE', 'BHS', 'TLS', 'SWZ', 'KWT', 'FJI',
'NCL', 'SVN', 'ISR', 'PSE', 'SLV', 'BLZ', 'DJI', 'MKD', 'RWA', 'HTI',
'BDI', 'GNQ', 'ALB', 'SLB', 'ARM', 'LSO', 'BEL', 'MDA', 'GNB', 'TWN',
'BTN', 'CHE', 'NLD', 'DNK', 'EST', 'DOM', 'SVK', 'CRI', 'BIH', 'HRV',
'TGO', 'LVA', 'LTU', 'LKA', 'GEO', 'IRL', 'SLE', 'PAN', 'CZE', 'GUF',
'ARE', 'AUT', 'AZE', 'SRB', 'JOR', 'PRT', 'HUN', 'KOR', 'ISL', 'GTM',
'CUB', 'BGR', 'LBR', 'HND', 'BEN', 'ERI', 'MWI', 'PRK', 'NIC', 'GRC',
'TJK', 'BGD', 'NPL', 'TUN', 'SUR', 'URY', 'KHM', 'SYR', 'SEN', 'KGZ',
'BLR', 'GUY', 'LAO', 'ROU', 'GHA', 'UGA', 'GBR', 'GIN', 'ECU', 'ESH',
'GAB', 'NZL', 'BFA', 'PHL', 'ITA', 'OMN', 'POL', 'CIV', 'NOR', 'MYS',
'VNM', 'FIN', 'COG', 'DEU', 'JPN', 'ZWE', 'PRY', 'IRQ', 'MAR', 'UZB',
'SWE', 'PNG', 'CMR', 'TKM', 'ESP', 'THA', 'YEM', 'FRA', 'ALA', 'KEN',
'BWA', 'MDG', 'UKR', 'SSD', 'CAF', 'SOM', 'AFG', 'MMR', 'ZMB', 'CHL',
'TUR', 'PAK', 'MOZ', 'NAM', 'VEN', 'NGA', 'TZA', 'EGY', 'MRT', 'BOL',
'ETH', 'COL', 'ZAF', 'MLI', 'AGO', 'NER', 'TCD', 'PER', 'MNG', 'IRN',
'LBY', 'SDN', 'IDN', 'MX-DIF', 'MX-TLA',
'MX-MOR', 'MX-AGU', 'MX-CL', 'MX-QUE', 'MX-HID',
'MX-MX', 'MX-TAB', 'MX-NAY', 'MX-GUA', 'MX-PUE',
'MX-YUC', 'MX-ROO', 'MX-SIN', 'MX-CAM', 'MX-MIC',
'MX-SLP', 'MX-GRO', 'MX-NLE', 'MX-BCN', 'MX-VER',
'MX-CHP', 'MX-BCS', 'MX-ZAC', 'MX-JAL', 'MX-TAM',
'MX-OAX', 'MX-DUR', 'MX-COA', 'MX-SON', 'MX-CHH',
'GRL', 'SAU', 'COD', 'DZA', 'KAZ',
'ARG', 'IN-DD', 'IN-DN', 'IN-CH', 'IN-AN',
'IN-LD', 'IN-DL', 'IN-ML', 'IN-NL', 'IN-MN',
'IN-TR', 'IN-MZ', 'IN-SK', 'IN-PB', 'IN-HR',
'IN-AR', 'IN-AS', 'IN-BR', 'IN-UT', 'IN-GA',
'IN-KL', 'IN-TN', 'IN-HP', 'IN-JK', 'IN-CT',
'IN-JH', 'IN-KA', 'IN-RJ', 'IN-OR', 'IN-GJ',
'IN-WB', 'IN-MP', 'IN-TG', 'IN-AP', 'IN-MH',
'IN-UP', 'IN-PY', 'AU-NSW', 'AU-ACT', 'AU-JBT',
'AU-NT', 'AU-SA', 'AU-TAS', 'AU-VIC', 'AU-WA',
'AU-QLD', 'BR-DF', 'BR-SE', 'BR-AL', 'BR-RJ',
'BR-ES', 'BR-RN', 'BR-PB', 'BR-SC', 'BR-PE',
'BR-AP', 'BR-CE', 'BR-AC', 'BR-PR', 'BR-RR',
'BR-RO', 'BR-SP', 'BR-PI', 'BR-TO', 'BR-RS',
'BR-MA', 'BR-GO', 'BR-MS', 'BR-BA', 'BR-MG',
'BR-MT', 'BR-PA', 'BR-AM', 'US-DC', 'US-RI',
'US-DE', 'US-CT', 'US-NJ', 'US-NH', 'US-VT',
'US-MA', 'US-HI', 'US-MD', 'US-WV', 'US-SC',
'US-ME', 'US-IN', 'US-KY', 'US-TN', 'US-VA',
'US-OH', 'US-PA', 'US-MS', 'US-LA', 'US-AL',
'US-AR', 'US-NC', 'US-NY', 'US-IA', 'US-IL',
'US-GA', 'US-WI', 'US-FL', 'US-MO', 'US-OK',
'US-ND', 'US-WA', 'US-SD', 'US-NE', 'US-KS',
'US-ID', 'US-UT', 'US-MN', 'US-MI', 'US-WY',
'US-OR', 'US-CO', 'US-NV', 'US-AZ', 'US-NM',
'US-MT', 'US-CA', 'US-TX', 'US-AK', 'CA-BC',
'CA-AB', 'CA-ON', 'CA-QC', 'CA-SK', 'CA-MB',
'CA-NL', 'CA-NB', 'CA-NS', 'CA-PE', 'CA-YT',
'CA-NT', 'CA-NU', 'IND', 'AUS', 'BRA',
'USA', 'MEX', 'RU-MOW', 'RU-SPE', 'RU-KGD',
'RU-IN', 'RU-AD', 'RU-SE', 'RU-KB', 'RU-KC',
'RU-CE', 'RU-CU', 'RU-IVA', 'RU-LIP', 'RU-ORL',
'RU-TUL', 'RU-BE', 'RU-VLA', 'RU-KRS', 'RU-KLU',
'RU-TT', 'RU-BRY', 'RU-YAR', 'RU-RYA', 'RU-AST',
'RU-MOS', 'RU-SMO', 'RU-DA', 'RU-VOR', 'RU-NGR',
'RU-PSK', 'RU-KOS', 'RU-STA', 'RU-KDA', 'RU-KL',
'RU-TVE', 'RU-LEN', 'RU-ROS', 'RU-VGG', 'RU-VLG',
'RU-MUR', 'RU-KR', 'RU-NEN', 'RU-KO', 'RU-ARK',
'RU-MO', 'RU-NIZ', 'RU-PNZ', 'RU-KI', 'RU-ME',
'RU-ORE', 'RU-ULY', 'RU-PM', 'RU-BA', 'RU-UD',
'RU-TA', 'RU-SAM', 'RU-SAR', 'RU-YAN', 'RU-KM',
'RU-SVE', 'RU-TYU', 'RU-KGN', 'RU-CH', 'RU-BU',
'RU-ZAB', 'RU-IRK', 'RU-NVS', 'RU-TOM', 'RU-OMS',
'RU-KK', 'RU-KEM', 'RU-AL', 'RU-ALT', 'RU-TY',
'RU-KYA', 'RU-MAG', 'RU-CHU', 'RU-KAM', 'RU-SAK',
'RU-PO', 'RU-YEV', 'RU-KHA', 'RU-AMU', 'RU-SA',
'CAN', 'RUS', 'CN-SH', 'CN-TJ', 'CN-BJ',
'CN-HI', 'CN-NX', 'CN-CQ', 'CN-ZJ', 'CN-JS',
'CN-FJ', 'CN-AH', 'CN-LN', 'CN-SD', 'CN-SX',
'CN-JX', 'CN-HA', 'CN-GZ', 'CN-GD', 'CN-HB',
'CN-JL', 'CN-HE', 'CN-SN', 'CN-NM', 'CN-HL',
'CN-HN', 'CN-GX', 'CN-SC', 'CN-YN', 'CN-XZ',
'CN-GS', 'CN-QH', 'CN-XJ', 'CHN', 'UMI',
'CPT', 'ATA', 'AAA'
];
var aliases = "2UK=2UT,2CG=2CT,1GU=GUM,1UM=UMI,1VI=VIR,1AS=ASM,1MP=MNP,4CX=CXR,4CC=CCK,4NF=NFK,4HM=HMD," +
"COL=5CL,5ME=5MX,MEX=5MX,5AG=AGU,5BC=BCN,5BS=BCS,5CM=CAM,5CS=CHP,5CH=CHH,5CO=COA,5DF=DIF,5DG=DUR," +
"5GT=GUA,5GR=GRO,5HG=HID,5JA=JAL,5MI=MIC,5MO=MOR,5NA=NAY,5NL=NLE,5OA=OAX,5PB=PUE,5QE=QUE,5QR=ROO," +
"5SL=SLP,5SI=SIN,5SO=SON,5TB=TAB,5TL=TLA,5VE=VER,5YU=YUC,5ZA=ZAC,811=8BJ,812=8TJ,813=8HE,814=8SX," +
"815=8NM,821=8LN,822=8JL,823=8HL,831=8SH,832=8JS,833=8ZJ,834=8AH,835=8FJ,836=8JX,837=8SD,841=8HA," +
"842=8HB,843=8HN,844=8GD,845=8GX,846=8HI,850=8CQ,851=8SC,852=8GZ,853=8YN,854=8XZ,861=8SN,862=8GS," +
"863=8QH,864=8NX,865=8XJ,871=TWN,891=HKG,892=MAC,8TW=TWN,8HK=HKG,8MC=MAC,BEL=7BE,KIR=7KI,PRI=7PO," +
"CHE=7CH,KHM=7KM,PER=7PM,TAM=7TT,0US=USA,0AU=AUS,0RU=RUS,0CN=CHN,TAA=SHN,ASC=SHN,DGA=IOT,WAK=MHL," +
"JTN=UMI,MID=1HI,1PR=PRI,5TM=TAM,TAM=TAM,2OD=2OR,";
var dependency = [
27, 410, 50, 410, 26, 410, 53, 410, 48, 410, 47, 410, 76, 410, 529, 410, 38, 410,
21, 408, 4, 408, 42, 408, 11, 408,
18, 166, 14, 166, 15, 166, 23, 166, 32, 166, 82, 166, 2, 166, 17, 166, 51, 166, 20, 166, 19, 166, 12, 166,
35, 166, 70, 166, 59, 166,
61, 528, 8, 528, 109, 528,
63, 113, 265, 113,
198, 181,
530, 197, 129, 197, 71, 197, 75, 197, 64, 197, 62, 197, 90, 197, 67, 197, 10, 197, 29, 197, 5, 197, 22, 197,
13, 178, 40, 178,
25, 112, 33, 112, 45, 112, 9, 112,
28, 171, 30, 171, 3, 171,
77, 210,
-1];
var usa_from = 343;
var usa_upto = 393;
var ccode_usa = 410;
var ind_from = 271;
var ind_upto = 306;
var ccode_ind = 407;
var can_from = 394;
var can_upto = 406;
var ccode_can = 495;
var aus_from = 307;
var aus_upto = 315;
var ccode_aus = 408;
var mex_from = 233;
var mex_upto = 264;
var ccode_mex = 411;
var bra_from = 316;
var bra_upto = 342;
var ccode_bra = 409;
var chn_from = 497;
var chn_upto = 527;
var ccode_chn = 528;
var rus_from = 412;
var rus_upto = 494;
var ccode_rus = 496;
var ccode_earth = 532;
var parents3 = "USA,IND,CAN,AUS,MEX,BRA,RUS,CHN,";
var parents2 = "US,IN,CA,AU,MX,BR,RU,CN,";
var ccode_start = 112; // NLD
var mapcode_cversion = "2.0.2";
var mapcode_dataversion = "2.3.0";
// *************************** mapcode_org *********************
var mapcode_javaversion = '2.4.2/Data' + mapcode_dataversion;
/// PRIVATE returns string without leading spaces and plus-signs, and trailing spaces
function trim(str) {
return str.replace(/^\s+|\s+$/g, '');
}
/// PRIVATE return 2-letter parent country abbreviation (disam in range 1..8)
function parentname2(disam) {
return parents2.substr(disam * 3 - 3, 2);
}
/// PRIVATE given a parent country abbreviation, return disam (in range 1-8) or negative if error
function parentletter(territoryAlphaCode) {
var p = -1;
var srch = String(territoryAlphaCode).toUpperCase() + ',';
var len = srch.length;
if (len == 3) {
p = parents2.indexOf(srch);
} else if (len == 4) {
p = parents3.indexOf(srch);
}
if (p < 0) {
return -2;
}
return 1 + (p / len);
}
/// PRIVATE given an ISO abbreviation, set disambiguation for future calls to iso2ccode(); returns nonzero in case of error
var disambiguate = 1; // GLOBAL
function set_disambiguate(territoryAlphaCode) {
var p = parentletter(territoryAlphaCode);
if (p < 0) {
return -2;
}
disambiguate = p;
return 0;
}
/// PRIVATE returns alias of ISO abbreviation (if any), or return empty
function alias2iso(territoryAlphaCode) {
var rx;
if (territoryAlphaCode.length == 2) {
rx = '[0-9]' + territoryAlphaCode;
} else {
rx = territoryAlphaCode;
}
rx = new RegExp(rx + '=', '');
var p = aliases.search(rx);
if (p >= 0) {
return aliases.substring(p + 4, p + 7);
}
return '';
}
/// PRIVATE given ISO code, return territoryNumber (or negative if error)
function findISO(territoryAlphaCode) {
for (var i = 0; i < iso3166alpha.length; i++) {
if (territoryAlphaCode == iso3166alpha[i]) {
return i;
}
}
return -1;
}
/// PRIVATE given ISO code, return territoryNumber (or negative if error)
function iso2ccode(territoryAlphaCode) {
if (typeof territoryAlphaCode == "undefined") {
return undefined;
}
territoryAlphaCode = trim(String(territoryAlphaCode)).toUpperCase();
var sp = territoryAlphaCode.indexOf(" ");
if (sp > 0) {
territoryAlphaCode = territoryAlphaCode.substr(0, sp);
}
if (!isNaN(territoryAlphaCode)) {
var n = Number(territoryAlphaCode);
if ((n >= 0) && (n <= ccode_earth)) {
return n;
}
}
var i, isoa;
var sep = territoryAlphaCode.lastIndexOf('-');
if (sep >= 0) { // territory!
var prefix = territoryAlphaCode.substring(0, sep);
var properMapcode = territoryAlphaCode.substring(sep + 1);
if (set_disambiguate(prefix) || properMapcode.length < 2) {
return -1;
}
i = findISO(parentname2(disambiguate) + '-' + properMapcode);
if (i >= 0) {
return i;
}
// recognise alias
if (properMapcode.length == 3) {
isoa = alias2iso(properMapcode);
} else {
isoa = alias2iso(disambiguate + '' + properMapcode);
}
if (isoa) {
if (isoa.charAt(0) == disambiguate) {
properMapcode = isoa.substring(1);
} else {
properMapcode = isoa;
i = findISO(properMapcode);
if (i >= 0) {
return i;
}
}
}
return findISO(parentname2(disambiguate) + '-' + properMapcode);
}
// first rewrite alias in context
if (territoryAlphaCode.length == 2) {
isoa = alias2iso(disambiguate + '' + territoryAlphaCode);
if (isoa) {
if (isoa.charAt(0) == disambiguate) {
territoryAlphaCode = isoa.substring(1);
} else {
territoryAlphaCode = isoa;
}
}
}
// no prefix. check if a normal territory
if (territoryAlphaCode.length == 3) {
i = findISO(territoryAlphaCode);
if (i >= 0) {
return i;
}
}
// no prefix, check in context
i = findISO(parentname2(disambiguate) + '-' + territoryAlphaCode);
if (i >= 0) {
return i;
}
if (territoryAlphaCode.length >= 2) {
i = findISO(parentname2(disambiguate) + '-' + territoryAlphaCode);
if (i >= 0) {
return i;
}
// find in ANY context
var hyphenated = '-' + territoryAlphaCode;
for (i = 0; i < iso3166alpha.length; i++) {
if (iso3166alpha[i].indexOf(hyphenated) > 0) {
if (iso3166alpha[i].substring(iso3166alpha[i].indexOf(hyphenated)) == hyphenated) {
return i;
}
}
}
}
// all else failed, try non-disambiguated alphacode
isoa = alias2iso(territoryAlphaCode); // or try ANY alias
if (isoa) {
if (isoa.charCodeAt(0) <= 57) { // starts with digit
territoryAlphaCode = parentname2(isoa.charCodeAt(0) - 48) + '-' + isoa.substring(1);
} else {
territoryAlphaCode = isoa;
}
return iso2ccode(territoryAlphaCode);
}
return -1;
}
/// PUBLIC given an alphacode (such as US-AL), returns the territory number (or negative).
/// A contextTerritoryNumber helps to interpret ambiguous (abbreviated) AlphaCodes, such as "AL"
function getTerritoryNumber(territoryAlphaCode, contextTerritory) {
if (contextTerritory) {
var contextTerritoryNumber = getTerritoryNumber(contextTerritory);
set_disambiguate(iso3166alpha[contextTerritoryNumber]);
}
return iso2ccode(territoryAlphaCode);
}
/// PUBLIC return full name of territory
function getTerritoryFullname(territory) {
var territoryNumber = getTerritoryNumber(territory);
if (territoryNumber == undefined || territoryNumber < 0 || territoryNumber > ccode_earth) {
return undefined;
}
var idx = isofullname[territoryNumber].indexOf(' (');
if (idx > 0) {
return isofullname[territoryNumber].substr(0, idx);
}
return isofullname[territoryNumber];
}
/// PUBLIC return parent country of subdivision (negative if territory is not a subdivision)
function getParentOf(territory) {
var territoryNumber = getTerritoryNumber(territory);
if (territoryNumber >= usa_from && territoryNumber <= usa_upto) {
return ccode_usa;
}
if (territoryNumber >= ind_from && territoryNumber <= ind_upto) {
return ccode_ind;
}
if (territoryNumber >= can_from && territoryNumber <= can_upto) {
return ccode_can;
}
if (territoryNumber >= aus_from && territoryNumber <= aus_upto) {
return ccode_aus;
}
if (territoryNumber >= mex_from && territoryNumber <= mex_upto) {
return ccode_mex;
}
if (territoryNumber >= bra_from && territoryNumber <= bra_upto) {
return ccode_bra;
}
if (territoryNumber >= rus_from && territoryNumber <= rus_upto) {
return ccode_rus;
}
if (territoryNumber >= chn_from && territoryNumber <= chn_upto) {
return ccode_chn;
}
return -199;
}
/// PUBLIC returns true iff territoryNumber is a state
function isSubdivision(territory) {
return getParentOf(territory) >= 0;
}
/// PUBLIC returns true iff territoryNumber is a country that has states
function hasSubdivision(territory) {
return (parents3.indexOf(getTerritoryAlphaCode(getTerritoryNumber(territory), 0)) >= 0);
}
/// PRIVATE returns true iff x in range (all values in millionths)
function isInRangeX(x, minx, maxx) {
if (minx <= x && x < maxx) {
return true;
}
if (x < minx) {
x += 360000000;
} else {
x -= 360000000;
}
return (minx <= x && x < maxx);
}
/// PRIVATE returns true iff coordinate inside rectangle (all values in millionths)
function fitsInside(coord, mm) {
return (mm.miny <= coord.y && coord.y < mm.maxy && isInRangeX(coord.x, mm.minx, mm.maxx));
}
/// PRIVATE returns true iff coordinate inside rectangle with some room to spare outside (all values in millionths)
function fitsInsideWithRoom(coord, mm) {
if (((mm.miny - 60) > coord.y) || (coord.y >= (mm.maxy + 60))) {
return false;
}
var xroom = xDivider4(mm.miny, mm.maxy) / 4;
return isInRangeX(coord.x, mm.minx - xroom, mm.maxx + xroom);
}
/// PRIVATE returns true iff coordinate inside rectangle with some room to spare inside (all values in millionths)
function fitsWellInside(coord, mm) {
if (((mm.miny + 60) > coord.y) || (coord.y >= (mm.maxy - 60))) {
return false;
}
var xroom = xDivider4(mm.miny, mm.maxy) / 4;
return isInRangeX(coord.x, mm.minx + xroom, mm.maxx - xroom);
}
/// PRIVATE returns true iff coordinate near the rectangle border
function isNearBorderOf(coord, mm) {
return fitsInsideWithRoom(coord, mm) && (!fitsWellInside(coord, mm));
}
/// PUBLIC return the AlphaCode (usually an ISO 3166 code) of a territory
/// format: 0=local (often ambiguous), 1=international (full and unambiguous, DEFAULT), 2=shortest (shortest non-ambiguous abbreviation)
function getTerritoryAlphaCode(territory, format) {
if (format == undefined) {
format = 1;
}
var territoryNumber = getTerritoryNumber(territory);
if (territoryNumber < 0 || territoryNumber > ccode_earth) {
return '';
}
var full = iso3166alpha[territoryNumber];
var hyphen = full.indexOf("-");
if (format == 1 || hyphen <= 0) {
return full;
}
var short = full.substr(hyphen + 1);
if (format == 0) {
return short;
}
// shortest POSSIBLE
// see if short occurs multiple times, if not, don't bother with parent
var count = 0;
var i = aliases.indexOf(short + '=');
if (i >= 0) {
count = 2;
} else {
for (i = 0; i < iso3166alpha.length; i++) {
var pos = iso3166alpha[i].indexOf("-" + short);
if ((pos >= 0) && (iso3166alpha[i].substr(pos + 1) == short)) {
count++;
}
}
}
return (count == 1) ? short : full;
}
/// PRIVATE low-level routines for data access
function dataFirstRecord(territoryNumber) {
return data_start[territoryNumber];
}
function dataLastRecord(territoryNumber) {
return data_start[++territoryNumber] - 1;
}
function minmaxSetup(i) {
var d = data_maxy[i];
if (d < 10) {
var shortmaxy = [0, 122309, 27539, 27449, 149759, 2681190, 60119, 62099, 491040, 86489];
d = shortmaxy[d];
}
return {
minx: data_minx[i],
miny: data_miny[i],
maxx: data_minx[i] + data_maxx[i],
maxy: data_miny[i] + d
};
}
/// low-level tables for mapcode encoding and decoding
var xdivider19 = [
360, 360, 360, 360, 360, 360, 361, 361, 361, 361, // 5.2429 degrees
362, 362, 362, 363, 363, 363, 364, 364, 365, 366, // 10.4858 degrees
366, 367, 367, 368, 369, 370, 370, 371, 372, 373, // 15.7286 degrees
374, 375, 376, 377, 378, 379, 380, 382, 383, 384, // 20.9715 degrees
386, 387, 388, 390, 391, 393, 394, 396, 398, 399, // 26.2144 degrees
401, 403, 405, 407, 409, 411, 413, 415, 417, 420, // 31.4573 degrees
422, 424, 427, 429, 432, 435, 437, 440, 443, 446, // 36.7002 degrees
449, 452, 455, 459, 462, 465, 469, 473, 476, 480, // 41.9430 degrees
484, 488, 492, 496, 501, 505, 510, 515, 520, 525, // 47.1859 degrees
530, 535, 540, 546, 552, 558, 564, 570, 577, 583, // 52.4288 degrees
590, 598, 605, 612, 620, 628, 637, 645, 654, 664, // 57.6717 degrees
673, 683, 693, 704, 715, 726, 738, 751, 763, 777, // 62.9146 degrees
791, 805, 820, 836, 852, 869, 887, 906, 925, 946, // 68.1574 degrees
968, 990, 1014, 1039, 1066, 1094, 1123, 1154, 1187, 1223, // 73.4003 degrees
1260, 1300, 1343, 1389, 1438, 1490, 1547, 1609, 1676, 1749, // 78.6432 degrees
1828, 1916, 2012, 2118, 2237, 2370, 2521, 2691, 2887, 3114, // 83.8861 degrees
3380, 3696, 4077, 4547, 5139, 5910, 6952, 8443, 10747, 14784, // 89.1290 degrees
23681, 59485];
var nc = [1, 31, 961, 29791, 923521, 28629151, 887503681];
var xside = [0, 5, 31, 168, 961, 168 * 31, 29791, 165869, 923521, 5141947, 28629151];
var yside = [0, 6, 31, 176, 961, 176 * 31, 29791, 165869, 923521, 5141947, 28629151];
var decodeChar = [
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, -2, 10, 11, 12, -3, 13, 14, 15, 1, 16, 17, 18, 19, 20, 0,
21, 22, 23, 24, 25, -4, 26, 27, 28, 29, 30, -1, -1, -1, -1, -1,
-1, -2, 10, 11, 12, -3, 13, 14, 15, 1, 16, 17, 18, 19, 20, 0,
21, 22, 23, 24, 25, -4, 26, 27, 28, 29, 30, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1];
var encodeChar = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z',
'A', 'E', 'U'];
/// PRIVATE given a minimum and maximum latitude, returns a relative stretch factor (in 360th) for the longitude
function xDivider4(miny, maxy) {
if (miny >= 0) {
return xdivider19[(miny) >> 19];
}
if (maxy >= 0) {
return xdivider19[0];
}
return xdivider19[(-maxy) >> 19];
}
/// PRIVATE lowest level encode/decode routines
function decodeBase31(str) {
var value = 0;
var i;
for (i = 0; i < str.length; i++) {
var c = str.charCodeAt(i);
if (c == 46) // dot!
{
return value;
}
if (decodeChar[c] < 0) {
return NaN;
}
value = value * 31 + decodeChar[c];
}
return value;
}
function decodeTriple(input) {
var triplex, tripley;
var c1 = decodeChar[input.charCodeAt(0)];
var x = decodeBase31(input.substr(1));
if (c1 < 24) {
triplex = (c1 % 6) * 28 + Math.floor(x / 34);
tripley = Math.floor(c1 / 6) * 34 + Math.floor(x % 34);
}
else {
tripley = (x % 40) + 136;
triplex = Math.floor(x / 40) + 24 * (c1 - 24);
}
return {y: tripley, x: triplex}
}
function decodeSixWide(v, width, height) {
var D = 6;
var col = Math.floor(v / (height * 6));
var maxcol = Math.floor((width - 4) / 6);
if (col >= maxcol) {
col = maxcol;
D = width - maxcol * 6;
}
var w = v - (col * height * 6);
var x6 = col * 6 + (w % D);
var y6 = height - 1 - Math.floor(w / D);
return {y: y6, x: x6}
}
function encodeSixWide(x, y, width, height) {
var D = 6;
var col = Math.floor(x / 6);
var maxcol = Math.floor((width - 4) / 6);
if (col >= maxcol) {
col = maxcol;
D = width - maxcol * 6;
}
return (height * 6 * col) + (height - 1 - y) * D + (x - col * 6);
}
var getDebugInfo; // caller must set this to 1 to get debug info about first successful encode
var mcInfo;
/*
type: 1=topdown nameless, 2=sixwide nameless, 3=regulargrid 4=irregular grid 5=rounded groups 6=unrounded groups
record: rectangle record used to encode
rectangles:
rectEncompassing : encompassing rectangle of country or subdivision
rectArea : area of the encoding record
rectSubarea : subArea (as determined by prefix)
rectZone : subArea zone (only exists for huge regular grids, e.g. Antarctica)
rectRegion : region in Subarea
rectCell : cell for the mapcode (usually 10x10 meters)
rectCell1 : precision 1 cell (usually 2x2 meters)
rectCell2 : precision 2 cell (usually 33x33 cm)
other debug information:
form = alphanumeric description of mapcode form
dotPosition = position to insert dot in mapcode
prefixDivx,prefixDivy = how prefix divides the area (if any)
*/
function asDegreeRect(minx, miny, dx, dy) {
return {minx: minx / 1000000, miny: miny / 1000000, maxx: (minx + dx) / 1000000, maxy: (miny + dy) / 1000000};
}
/// high-precision extension routines
function maxMapcodePrecision() {
return 8;
}
function encodeExtension(result, enc, extrax4, extray, dividerx4, dividery, extraDigits, ydirection) {
if (!extraDigits) {
return result;
}
if (extraDigits > maxMapcodePrecision()) {
extraDigits = maxMapcodePrecision();
}
// the following are all perfect integers
var factorx = 810000 * dividerx4; // 810000 = 30^4
var factory = 810000 * dividery;
var valx = (810000 * extrax4) + (enc.fraclon);
var valy = (810000 * extray) + (enc.fraclat * ydirection);
// protect against floating point errors
if (valx < 0) {
valx = 0;
} else if (valx >= factorx) {
valx = factorx - 1;
}
if (valy < 0) {
valy = 0;
} else if (valy >= factory) {
valy = factory - 1;
}
result += '-';
for (; ;) {
factorx /= 30;
var gx = Math.floor(valx / factorx);
factory /= 30;
var gy = Math.floor(valy / factory);
var column1 = Math.floor(gx / 6);
var row1 = Math.floor(gy / 5);
result += encodeChar[row1 * 5 + column1];
if (--extraDigits == 0) {
break;
}
var column2 = (gx % 6);
var row2 = (gy % 5);
result += encodeChar[row2 * 6 + column2];
if (--extraDigits == 0) {
break;
}
valx -= factorx * gx;
valy -= factory * gy;
}
return result;
}
// ***** MapcodeZone *****
// cconstruct and return empty MapcodeZone
function mzEmpty() {
return {fminx: 0, fmaxx: 0, fminy: 0, fmaxy: 0};
}
// construct and return copy of MapcodeZone
function mzCopy(zone) {
return {
fminx: zone.fminx,
fmaxx: zone.fmaxx,
fminy: zone.fminy,
fmaxy: zone.fmaxy
};
}
// return true iff MapcodeZone is empty
function mzIsEmpty(zone) {
return ((zone.fmaxx <= zone.fminx) || (zone.fmaxy <= zone.fminy));
}
// comstruct MapcodeZone based on coordinate and deltas (in Fractions)
function mzSetFromFractions(y, x, yDelta, xDelta) {
if (yDelta < 0) {
return {
fminx: x,
fmaxx: x + xDelta,
fminy: y + 1 + yDelta, // y+yDelta can NOT be represented
fmaxy: y + 1 // y CAN be represented
};
}
else {
return {
fminx: x,
fmaxx: x + xDelta,
fminy: y,
fmaxy: y + yDelta
};
}
}
function mzMidPointFractions(zone) {
return {
y: Math.floor((zone.fminy + zone.fmaxy) / 2),
x: Math.floor((zone.fminx + zone.fmaxx) / 2)
};
}
function convertFractionsToCoord32(p) {
p.y = Math.floor(p.y / 810000);
p.x = Math.floor(p.x / 3240000);
return p;
}
function wrap(p) {
if (p.x >= (180 * 3240000 * 1000000)) {
p.x -= (360 * 3240000 * 1000000);
}
if (p.x < (-180 * 3240000 * 1000000)) {
p.x += (360 * 3240000 * 1000000);
}
return p;
}
function convertFractionsToDegrees(p) {
p.y /= (810000 * 1000000);
p.x /= (3240000 * 1000000);
return p;
}
function mzRestrictZoneTo(zone, mm) {
var z = mzCopy(zone);
var miny = mm.miny * 810000;
if (z.fminy < miny) {
z.fminy = miny;
}
var maxy = mm.maxy * 810000;
if (z.fmaxy > maxy) {
z.fmaxy = maxy;
}
if (z.fminy < z.fmaxy) {
var minx = mm.minx * 3240000;
var maxx = mm.maxx * 3240000;
if ((maxx < 0) && (z.fminx > 0)) {
minx += (360000000 * 3240000);
maxx += (360000000 * 3240000);
}
else if ((minx > 1) && (z.fmaxx < 0)) {
minx -= (360000000 * 3240000);
maxx -= (360000000 * 3240000);
}
if (z.fminx < minx) {
z.fminx = minx;
}
if (z.fmaxx > maxx) {
z.fmaxx = maxx;
}
}
return z;
}
// returns (possibly empty) MapcodeZone
function decodeExtension(extensionchars, coord32, dividerx4, dividery, lon_offset4, extremeLatMicroDeg, maxLonMicroDeg) {
var processor = 1;
var lon32 = 0;
var lat32 = 0;
var odd = 0;
var idx = 0;
if (extensionchars.length > 8) {
return mzEmpty(); // too many digits
}
while (idx < extensionchars.length) {
var column1, row1, column2, row2;
var c1 = decodeChar[extensionchars.charCodeAt(idx++)];
if (c1 < 0 || c1 == 30) {
return mzEmpty();
}
row1 = Math.floor(c1 / 5);
column1 = (c1 % 5);
if (idx < extensionchars.length) {
var c2 = decodeChar[extensionchars.charCodeAt(idx++)];
if (c2 < 0 || c2 == 30) {
return mzEmpty();
}
row2 = Math.floor(c2 / 6);
column2 = (c2 % 6);
} else { //
row2 = 0;
odd = 1;
column2 = 0;
} //
processor *= 30;
lon32 = lon32 * 30 + column1 * 6 + column2;
lat32 = lat32 * 30 + row1 * 5 + row2;
} //
while (processor < 810000.0) {
dividerx4 *= 30;
dividery *= 30;
processor *= 30;
}
var lon4 = (coord32.x * 3240000.0) + (lon32 * dividerx4) + (lon_offset4 * 810000.0);
var lat1 = (coord32.y * 810000.0) + (lat32 * dividery);
// determine the range of coordinates that are encode to this mapcode
var mapcodeZone;
if (odd) { // odd
mapcodeZone = mzSetFromFractions(lat1, lon4, 5 * dividery, 6 * dividerx4);
} else { // not odd
mapcodeZone = mzSetFromFractions(lat1, lon4, dividery, dividerx4);
} // not odd
// FORCE_RECODE - restrict the coordinate range to the extremes that were provided
if (mapcodeZone.fmaxx > (maxLonMicroDeg * 3240000.0)) {
mapcodeZone.fmaxx = (maxLonMicroDeg * 3240000.0);
}
if (dividery >= 0) {
if (mapcodeZone.fmaxy > (extremeLatMicroDeg * 810000.0)) {
mapcodeZone.fmaxy = (extremeLatMicroDeg * 810000.0);
}
} else {
if (mapcodeZone.fminy < (extremeLatMicroDeg * 810000.0)) {
mapcodeZone.fminy = (extremeLatMicroDeg * 810000.0);
}
}
return mapcodeZone;
}
function decodeGrid(input, extensionchars, m) {
var relx, rely;
var prefixlength = input.indexOf('.');
var postfixlength = input.length - 1 - prefixlength;
if (prefixlength == 1 && postfixlength == 4) {
prefixlength++;
postfixlength--;
input = input.charAt(0) + input.charAt(2) + '.' + input.substring(3);
}
var divx;
var divy = smartdiv(m);
if (divy == 1) {
divx = xside[prefixlength];
divy = yside[prefixlength];
} else {
divx = Math.floor(nc[prefixlength] / divy);
}
if (prefixlength == 4 && divx == 961 && divy == 961) {
input = input.charAt(0) + input.charAt(2) + input.charAt(1) + input.substr(3);
}
var v = decodeBase31(input);
if (divx != divy && prefixlength > 2) // D==6
{
var rel = decodeSixWide(v, divx, divy);
relx = rel.x;
rely = rel.y;
}
else {
relx = Math.floor(v / divy);
rely = v % divy;
rely = divy - 1 - rely;
}
var mm = minmaxSetup(m);
var ygridsize = Math.floor((mm.maxy - mm.miny + divy - 1) / divy);
var xgridsize = Math.floor((mm.maxx - mm.minx + divx - 1) / divx);
rely = mm.miny + (rely * ygridsize);
relx = mm.minx + (relx * xgridsize);
var xp = xside[postfixlength];
var dividerx = Math.floor((xgridsize + xp - 1) / xp);
var yp = yside[postfixlength];
var dividery = Math.floor((ygridsize + yp - 1) / yp);
var rest = input.substr(prefixlength + 1);
// decoderelative (postfix vs rely,relx)
var difx;
var dify;
if (postfixlength == 3) {
var d = decodeTriple(rest);
difx = d.x;
dify = d.y;
}
else {
if (postfixlength == 4) {
rest = rest.charAt(0) + rest.charAt(2) + rest.charAt(1) + rest.charAt(3);
}
v = decodeBase31(rest);
difx = Math.floor(v / yp);
dify = Math.floor(v % yp);
}
dify = yp - 1 - dify;
var corner = { // in microdegrees
y: rely + (dify * dividery),
x: relx + (difx * dividerx)
};
if (!fitsInside(corner, mm)) {
return mzEmpty();
}
var decodeMaxx = ((relx + xgridsize) < mm.maxx) ? (relx + xgridsize) : mm.maxx;
var decodeMaxy = ((rely + ygridsize) < mm.maxy) ? (rely + ygridsize) : mm.maxy;
return decodeExtension(extensionchars, corner, dividerx << 2, dividery,
0, decodeMaxy, decodeMaxx); // grid
}
function encodeBase31(value, nrchars) {
var result = '';
while (nrchars-- > 0) {
result = encodeChar[value % 31] + result;
value = Math.floor(value / 31);
}
return result;
}
function encodeTriple(difx, dify, dividerx, dividery) {
var rx, ry, cx, cy;
if (dify < 4 * 34) {
rx = Math.floor(difx / 28);
ry = Math.floor(dify / 34);
cx = (difx % 28);
cy = (dify % 34);
if (getDebugInfo) {
mcInfo.rectRegion = asDegreeRect((1000000 * mcInfo.rectSubarea.minx) + ((28 * rx * dividerx)),
(1000000 * (mcInfo.rectSubarea.maxy)) - (((34 * ry) + 34) * dividery), 28 * dividerx, 34 * dividery);
}
return encodeChar[(rx + 6 * ry)] + encodeBase31(cx * 34 + cy, 2);
}
else {
rx = Math.floor(difx / 24);
cx = (difx % 24);
if (getDebugInfo) {
mcInfo.rectRegion = asDegreeRect((1000000 * mcInfo.rectSubarea.minx) + (24 * rx * dividerx),
1000000 * mcInfo.rectSubarea.miny, 24 * dividerx, 40 * dividery);
}
return encodeChar[rx + 24] + encodeBase31(cx * 40 + (dify - 136), 2);
}
}
function encodeGrid(enc, m, mm, headerletter, extraDigits) {
var orgcodex = coDex(m);
var codex = orgcodex;
if (codex == 21) {
codex = 22;
}
if (codex == 14) {
codex = 23;
}
var prefixlength = Math.floor(codex / 10);