gridpin 0.10.0

Offline geocoder: addresses to coordinates from a country sheet
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
//! Optional answer fields, asked for with `--with` (CLI), `with=` (server, Python) and kept out of `Hit`.
//!
//! Without the flag an answer is byte for byte what it was: the fields are computed at output and land in one
//! object `extras` (JSON) or in `gp_<name>` columns after the usual ones (CSV). Every surface passes the same
//! spec string to `Extras::new`, so a field added here reaches all of them at once.
//!
//! Fields: `h3[:res]` and `geohash[:len]` (cells), `placekey` (the "where" part of a Placekey, which is H3 at
//! resolution 10 in Placekey's own alphabet), `codes` (Plus Code, MGRS, degrees-minutes-seconds), `street_point`
//! (a point on the street in front of the house, from the houses across it), `links` (the point on five public
//! maps), `provenance` (the sheet: release, data date, licence, sources, SHA-256) and `cell:<grid>` (the code of a
//! statistical grid cell, `cells.rs`). Time zones are not here:
//! the zone boundaries every library ships are ODbL data, and putting them in the engine is the owner's call.

use std::collections::BTreeMap;

use anyhow::{bail, Result};
use serde_json::{json, Map, Value};

use crate::codes;
use crate::query::{Hit, Index};

/// What a spec asks for. `Default` asks for nothing.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct With {
    pub h3: Option<u8>,
    pub geohash: Option<usize>,
    pub placekey: bool,
    pub codes: bool,
    pub street_point: bool,
    pub links: bool,
    pub provenance: bool,
    /// `place[:level]`: the answer as a person writes it, at house, street, locality or region level
    pub place: Option<&'static str>,
    /// what happened to each field of the query (`verdict`); needs the query (`for_query`)
    pub verdict: bool,
    /// `postal[:cc]`: the answer laid out as the post of country `cc` writes an address (`postal_formats.tsv`);
    /// without `cc` the default layout. The layout is named by the caller: the engine does not branch on the
    /// sheet's country (ANSWERS §60).
    pub postal: Option<&'static str>,
    /// `crs:EPSG:<code>`: easting and northing in a national projection (`crs.rs`)
    pub crs: Option<u32>,
    /// does the asked address exist: yes, maybe, no and the reason (`exists.rs`); needs the query (`for_query`)
    pub exists: bool,
    /// where the address may be: the candidates of the answer's level with weights, their box and spread
    pub area: bool,
    /// the flat, office or floor the query names ("apt 4B", "Suite 300", "2º B"), from `unit_words.tsv`
    pub unit: bool,
    /// the ISO 20022 postal address (`PstlAdr`): structured, hybrid or none with the reasons (`iso20022.rs`)
    pub iso20022: bool,
    /// the HL7 FHIR `Address` built on the same reading (`parts.rs`): its mode and the resource as JSON
    pub fhir: bool,
    /// how the answer's point was obtained (`point_method`), read from what the answer already says
    pub method: bool,
    /// `cell:<name>`: the code of a statistical grid cell (`cells.rs`: eu1km, de100m, jp1, jp2, jp3); repeatable
    pub cells: Vec<&'static str>,
}

pub const NAMES: [&str; 18] = [
    "h3",
    "geohash",
    "placekey",
    "codes",
    "street_point",
    "links",
    "provenance",
    "place",
    "verdict",
    "postal",
    "crs",
    "exists",
    "area",
    "unit",
    "iso20022",
    "fhir",
    "method",
    "cell",
];

/// How the point of an answer was obtained, read from its `precision` and flags (plan K3.1; the inventory of every
/// place a point is set, 17.11.2026, is in the plan's notes). Nothing is looked up: the answer already says it.
///
/// `registry_point` the stored point of the house (also a reverse answer far from any house: `approximate`);
/// `range_interpolated` a point on a range of numbers of the street (TIGER, flag `range`); `between_neighbours`
/// interpolated between the known numbers around it (`interp`); `nearest_house` the point of a neighbouring number of the
/// street (`near`); `crossing_of_streets` the middle of the closest houses of two streets; `street_centre`; `place_table`
/// the point of the sheet's table of places (flags `place_*`); `commune_centre`; `postcode_centre` a place read from a
/// postcode alone; `postcode_layer` the point of a postcode layer (Brazil's CEP); `place_layer` the POI layer;
/// `own_place` a place of `--aliases`.
pub fn point_method(h: &Hit) -> &'static str {
    let has = |f: &str| h.flags.contains(&f);
    if has(crate::aliases::ALIAS_FLAG) {
        return "own_place";
    }
    if has("poi_layer") {
        return "place_layer";
    }
    if has(crate::postcode_layer::LAYER_FLAG) {
        return "postcode_layer";
    }
    match h.precision {
        "house" if has("range") => "range_interpolated",
        "house" | "approximate" => "registry_point",
        "interp" => "between_neighbours",
        "near" => "nearest_house",
        "intersection" => "crossing_of_streets",
        "street" => "street_centre",
        _ if has("postal_place") => "postcode_centre",
        _ if h.flags.iter().any(|f| f.starts_with("place_")) => "place_table",
        _ => "commune_centre",
    }
}

/// The CSV columns of `--with fhir`: the mode and the `Address` resource as one JSON cell.
pub const FHIR_COLUMNS: [&str; 2] = ["gp_fhir_mode", "gp_fhir_address"];

fn fhir_cells(v: Option<&Value>) -> Vec<String> {
    let mode = v.and_then(|v| v.get("mode")).and_then(Value::as_str);
    let address = v.and_then(|v| v.get("address"));
    vec![
        mode.unwrap_or("").to_string(),
        address.map_or(String::new(), Value::to_string),
    ]
}

/// The CSV columns of `--with iso20022`: the mode, the elements of `PstlAdr`, two address lines and the checks.
pub const ISO_COLUMNS: [&str; 13] = [
    "gp_iso_mode",
    "gp_iso_strt_nm",
    "gp_iso_bldg_nb",
    "gp_iso_flr",
    "gp_iso_room",
    "gp_iso_pst_bx",
    "gp_iso_pst_cd",
    "gp_iso_twn_nm",
    "gp_iso_ctry_sub_dvsn",
    "gp_iso_ctry",
    "gp_iso_adr_line_1",
    "gp_iso_adr_line_2",
    "gp_iso_checks",
];

/// The cells of `ISO_COLUMNS` from the `iso20022` object of an answer.
fn iso_cells(v: Option<&Value>) -> Vec<String> {
    let s = |x: Option<&Value>| x.and_then(Value::as_str).unwrap_or("").to_string();
    let adr = v.and_then(|v| v.get("PstlAdr"));
    let line = |i: usize| s(adr.and_then(|a| a.get("AdrLine")).and_then(|l| l.get(i)));
    let mut out = vec![s(v.and_then(|v| v.get("mode")))];
    for k in [
        "StrtNm",
        "BldgNb",
        "Flr",
        "Room",
        "PstBx",
        "PstCd",
        "TwnNm",
        "CtrySubDvsn",
        "Ctry",
    ] {
        out.push(s(adr.and_then(|a| a.get(k))));
    }
    out.extend([line(0), line(1)]);
    let checks = v.and_then(|v| v.get("checks")).and_then(Value::as_array);
    out.push(checks.map_or(String::new(), |c| {
        c.iter()
            .filter_map(Value::as_str)
            .collect::<Vec<_>>()
            .join(";")
    }));
    out
}

/// Levels of `place`, finest first.
pub const PLACE_LEVELS: [&str; 4] = ["house", "street", "locality", "region"];

impl With {
    /// `tz,h3:9,placekey` style: names separated by commas, `h3` and `geohash` take a level after a colon.
    pub fn parse(spec: &str) -> Result<With> {
        let mut w = With::default();
        for raw in spec.split(',').map(str::trim).filter(|s| !s.is_empty()) {
            let (name, arg) = match raw.split_once(':') {
                Some((n, a)) => (n.trim(), Some(a.trim())),
                None => (raw, None),
            };
            let level = |lo: u32, hi: u32| -> Result<Option<u32>> {
                arg.map(|a| match a.parse::<u32>() {
                    Ok(v) if (lo..=hi).contains(&v) => Ok(v),
                    _ => bail!("with {name}:{a}: the level is a whole number {lo}..={hi}"),
                })
                .transpose()
            };
            if arg.is_some()
                && !matches!(name, "h3" | "geohash" | "place" | "postal" | "crs" | "cell")
            {
                bail!("with {raw}: only h3, geohash, place, postal, crs and cell take a level");
            }
            match name.to_ascii_lowercase().as_str() {
                "h3" => w.h3 = Some(level(0, 15)?.unwrap_or(9) as u8),
                "geohash" => w.geohash = Some(level(1, 12)?.unwrap_or(9) as usize),
                "placekey" => w.placekey = true,
                "codes" => w.codes = true,
                "street_point" => w.street_point = true,
                "links" => w.links = true,
                "provenance" => w.provenance = true,
                "verdict" => w.verdict = true,
                "exists" => w.exists = true,
                "area" => w.area = true,
                "unit" => w.unit = true,
                "iso20022" => w.iso20022 = true,
                "fhir" => w.fhir = true,
                "method" => w.method = true,
                "cell" => {
                    let kinds: Vec<&str> = crate::cells::KINDS.iter().map(|k| k.0).collect();
                    let name = arg.ok_or_else(|| {
                        anyhow::anyhow!("with cell: name the grid, one of {}", kinds.join(", "))
                    })?;
                    let (n, _) = crate::cells::KINDS.iter().find(|(n, _)| *n == name).ok_or_else(|| {
                        anyhow::anyhow!("with cell:{name}: the grids are {}", kinds.join(", "))
                    })?;
                    if !w.cells.contains(n) {
                        w.cells.push(n);
                    }
                }
                "crs" => {
                    let code = arg.ok_or_else(|| anyhow::anyhow!("with crs: name the code, e.g. crs:EPSG:2154"))?;
                    w.crs = Some(crate::crs::parse(code).ok_or_else(|| {
                        anyhow::anyhow!(
                            "with crs:{code}: not a projection of this build (UTM 326zz/327zz/258zz, SIRGAS 31965-31985, \
                             MGA2020 7849-7856, 2154, 3035, 28992, 3857)"
                        )
                    })?);
                }
                "postal" => {
                    // no code: the layout of the sheet's own country, read when the answer is laid out (§278)
                    let cc = arg.unwrap_or(SHEET_LAYOUT);
                    w.postal = Some(postal_layout_key(cc).ok_or_else(|| {
                        anyhow::anyhow!("with postal:{cc}: no layout for {cc:?}; known: {}", postal_layouts().join(", "))
                    })?);
                }
                "place" => {
                    let level = arg.unwrap_or("house");
                    w.place = Some(PLACE_LEVELS.into_iter().find(|l| *l == level).ok_or_else(|| {
                        anyhow::anyhow!("with place:{level}: the level is one of {}", PLACE_LEVELS.join(", "))
                    })?);
                }
                "tz" => bail!(
                    "with tz: not in this build: the time zone boundaries are ODbL data (timezone-boundary-builder, \
                     from OpenStreetMap), and they are not shipped inside the engine"
                ),
                other => bail!("with {other}: unknown field; known: {}", NAMES.join(", ")),
            }
        }
        Ok(w)
    }

    pub fn is_empty(&self) -> bool {
        *self == With::default()
    }

    /// The CSV columns these fields add, in output order.
    pub fn columns(&self) -> Vec<&'static str> {
        let mut c = Vec::new();
        if self.h3.is_some() {
            c.push("gp_h3");
        }
        if self.geohash.is_some() {
            c.push("gp_geohash");
        }
        if self.placekey {
            c.push("gp_placekey_where");
        }
        if self.codes {
            c.extend(["gp_plus_code", "gp_mgrs", "gp_dms"]);
        }
        if self.street_point {
            c.extend(["gp_street_lat", "gp_street_lon", "gp_street_point_m"]);
        }
        if self.links {
            c.extend(LINKS.iter().map(|(col, _, _)| *col));
        }
        if self.place.is_some() {
            c.push("gp_place");
        }
        if self.verdict {
            c.extend([
                "gp_verdict_street",
                "gp_verdict_number",
                "gp_verdict_postcode",
                "gp_verdict_city",
            ]);
        }
        if self.crs.is_some() {
            c.extend(["gp_x", "gp_y", "gp_crs"]);
        }
        if self.exists {
            c.extend(["gp_exists", "gp_exists_reason"]);
        }
        if self.area {
            c.extend(["gp_area_spread_m", "gp_area_candidates"]);
        }
        if self.unit {
            c.extend(["gp_unit", "gp_unit_kind"]);
        }
        if self.iso20022 {
            c.extend(ISO_COLUMNS);
        }
        if self.fhir {
            c.extend(FHIR_COLUMNS);
        }
        if self.method {
            c.push("gp_point_method");
        }
        for name in &self.cells {
            c.extend(crate::cells::column(name));
        }
        if self.postal.is_some() {
            c.push("gp_postal");
        }
        if self.provenance {
            c.extend([
                "gp_sheet_release",
                "gp_data_date",
                "gp_license",
                "gp_sources",
                "gp_sheet_sha256",
            ]);
        }
        c
    }
}

const UNIT_WORDS: &str = include_str!("unit_words.tsv");
/// The unit words of Cyrillic writing (kept as literals: the table file is Latin text).
const UNIT_WORDS_CYRILLIC: [(&str, &str); 3] =
    [("кв", "flat"), ("квартира", "flat"), ("этаж", "floor")];

/// The token after a unit word is a unit: digits with at most two letters, or one or two letters.
fn unit_token(t: &str) -> bool {
    let t = t.trim_matches(|c: char| matches!(c, ',' | ';' | ':' | '.'));
    let digits = t.chars().filter(char::is_ascii_digit).count();
    let letters = t.chars().filter(|c| c.is_alphabetic()).count();
    !t.is_empty()
        && t.chars()
            .all(|c| c.is_alphanumeric() || c == '-' || c == '/')
        && ((digits >= 1 && letters <= 2) || (digits == 0 && (1..=2).contains(&letters)))
}

/// A token written like a postcode, not like a unit number: five digits or more ("33173", "89642-000").
fn postcode_shaped(t: &str) -> bool {
    t.chars().filter(char::is_ascii_digit).count() >= 5
}

/// The flat, office or floor a query names, and its kind: `apt 4B`, `Suite 300`, `kv. 12`, `#12`, `2º B`.
/// Read from the text only; the address path does not change.
pub fn unit_of(q: &str) -> Option<(String, &'static str)> {
    let toks: Vec<&str> = q
        .split(|c: char| c.is_whitespace() || c == ',')
        .filter(|t| !t.is_empty())
        .collect();
    for (i, t) in toks.iter().enumerate() {
        if let Some(rest) = t.strip_prefix('#') {
            if unit_token(rest) {
                return Some((rest.to_string(), "flat"));
            }
        }
        // "2º B", "3º izq": a floor ordinal and the door; an ordinal that opens the line is the street's name
        // ("1ª Avenida", "4º Sub-Distrito de Alegrete"): a floor never comes first
        if let Some(n) = t.strip_suffix('º').or_else(|| t.strip_suffix('ª')) {
            if i > 0 && !n.is_empty() && n.chars().all(|c| c.is_ascii_digit()) {
                let door = toks
                    .get(i + 1)
                    .filter(|d| d.chars().count() <= 4)
                    .map(|d| format!(" {d}"))
                    .unwrap_or_default();
                return Some((format!("{n}º{door}"), "floor"));
            }
        }
        let word = crate::norm::normalize(t);
        let kind = UNIT_WORDS
            .lines()
            .filter(|l| !l.starts_with('#'))
            .filter_map(|l| l.split_once('\t'))
            .chain(UNIT_WORDS_CYRILLIC)
            .find(|(w, _)| *w == word)
            .map(|(_, k)| k);
        if let (Some(kind), Some(next)) = (kind, toks.get(i + 1)) {
            // "Miami, FL 33173": a region code that spells a unit word ("FL", floor), before a postcode; "Interior,
            // 89642-000" the same in Brazil. No flat, floor or door is numbered with a postcode (ANSWERS §308)
            if unit_token(next) && !postcode_shaped(next) {
                let kind: &'static str = match kind {
                    "floor" => "floor",
                    "office" => "office",
                    "room" => "room",
                    "door" => "door",
                    _ => "flat",
                };
                let value = next.trim_matches(|c: char| matches!(c, '.' | ';' | ':'));
                return Some((value.to_string(), kind));
            }
        }
    }
    None
}

/// Candidates asked for `area`.
pub const AREA_K: usize = 5;
/// A candidate with a weight below this does not widen the area.
pub const AREA_MIN_WEIGHT: f64 = 0.1;

/// Where the address may be: the candidates of the top answer's level, weights `exp(score - best)` normalized to
/// a sum of 1, the box and the largest distance between candidates of weight `AREA_MIN_WEIGHT` or more. One
/// candidate: an empty area of spread 0. The answer itself (the first point) is not changed.
pub fn area(hits: &[Hit]) -> Value {
    let Some(top) = hits.first() else {
        return Value::Null;
    };
    let level = crate::query::match_level(top).0;
    let same: Vec<&Hit> = hits
        .iter()
        .filter(|h| crate::query::match_level(h).0 == level)
        .collect();
    let best = same
        .iter()
        .map(|h| f64::from(h.score))
        .fold(f64::NEG_INFINITY, f64::max);
    let raw: Vec<f64> = same
        .iter()
        .map(|h| (f64::from(h.score) - best).exp())
        .collect();
    let total: f64 = raw.iter().sum();
    let weights: Vec<f64> = raw.iter().map(|w| w / total).collect();
    let kept: Vec<(&Hit, f64)> = same
        .iter()
        .copied()
        .zip(weights.iter().copied())
        .filter(|(_, w)| *w >= AREA_MIN_WEIGHT)
        .collect();
    let mut spread = 0.0_f64;
    for (a, _) in &kept {
        for (b, _) in &kept {
            spread = spread.max(codes::haversine_m((a.lat, a.lon), (b.lat, b.lon)));
        }
    }
    let lat = kept.iter().map(|(h, _)| h.lat);
    let lon = kept.iter().map(|(h, _)| h.lon);
    let bbox = json!([
        lon.clone().fold(f64::INFINITY, f64::min),
        lat.clone().fold(f64::INFINITY, f64::min),
        lon.fold(f64::NEG_INFINITY, f64::max),
        lat.fold(f64::NEG_INFINITY, f64::max)
    ]);
    let candidates: Vec<Value> = same
        .iter()
        .zip(&weights)
        .map(|(h, w)| json!({"lat": round7(h.lat), "lon": round7(h.lon), "weight": (w * 1e6).round() / 1e6}))
        .collect();
    json!({
        "level": level,
        "bbox": if same.len() > 1 { bbox } else { Value::Null },
        "candidates": if same.len() > 1 { json!(candidates) } else { json!([]) },
        "spread_m": (spread * 10.0).round() / 10.0,
    })
}

/// Postal layouts by country code (`postal_formats.tsv`): data, not a branch of code per country.
const POSTAL_FORMATS: &str = include_str!("postal_formats.tsv");

fn postal_rows() -> impl Iterator<Item = (&'static str, &'static str)> {
    postal_rows_shaped().map(|(k, t, _)| (k, t))
}

/// Rows with their optional third column: the shape of the postcode ("99-999": 9 a digit, any other character
/// written as it is), for a country whose sheet keeps the code as digits only (Poland, 17.11.2026).
fn postal_rows_shaped() -> impl Iterator<Item = (&'static str, &'static str, Option<&'static str>)>
{
    POSTAL_FORMATS
        .lines()
        .filter(|l| !l.starts_with('#'))
        .filter_map(|l| l.split_once('\t'))
        .map(|(k, rest)| match rest.split_once('\t') {
            Some((t, shape)) => (k, t, Some(shape.trim()).filter(|s| !s.is_empty())),
            None => (k, rest, None),
        })
}

/// The postcode as the post of `layout` writes it: digits only ("00624") put into the row's shape ("00-624") when
/// their count is the shape's; any other code, and a layout without a shape, is returned as it is.
pub fn postcode_as_written(code: &str, layout: Option<&str>) -> String {
    let shape = layout.and_then(|cc| {
        postal_rows_shaped()
            .find(|(k, _, _)| k.eq_ignore_ascii_case(cc))
            .and_then(|(_, _, shape)| shape)
    });
    let Some(shape) = shape else {
        return code.to_string();
    };
    let slots = shape.chars().filter(|c| *c == '9').count();
    if code.is_empty() || code.len() != slots || !code.chars().all(|c| c.is_ascii_digit()) {
        return code.to_string();
    }
    let mut digits = code.chars();
    shape
        .chars()
        .map(|c| {
            if c == '9' {
                digits.next().unwrap_or(c)
            } else {
                c
            }
        })
        .collect()
}

/// The layout names of the table (`default` and country codes).
pub fn postal_layouts() -> Vec<&'static str> {
    postal_rows().map(|(k, _)| k).collect()
}

/// `--with postal` without a code: the layout follows the sheet's country.
pub const SHEET_LAYOUT: &str = "sheet";

/// The layout of `--with postal`: the one named, or for `sheet` the one of the sheet's country (`postal_formats.tsv`
/// row; the default row when the country has none). A lookup in a data table, not a reading rule per country: the
/// owner allowed this one read of the sheet's country, ANSWERS §278.
pub fn postal_layout_of(layout: &'static str, idx: &Index) -> &'static str {
    if layout != SHEET_LAYOUT {
        return layout;
    }
    idx.country()
        .and_then(postal_layout_key)
        .unwrap_or("default")
}

/// The table's own spelling of a layout name, if there is one (`sheet`: the sheet's country, chosen later).
pub fn postal_layout_key(name: &str) -> Option<&'static str> {
    if name == SHEET_LAYOUT {
        return Some(SHEET_LAYOUT);
    }
    postal_rows()
        .map(|(k, _)| k)
        .find(|k| k.eq_ignore_ascii_case(name))
}

/// The template of layout `country`, else the default one.
pub fn postal_template(country: Option<&str>) -> &'static str {
    let rows = postal_rows;
    let find = |cc: &str| {
        rows()
            .find(|(k, _)| k.eq_ignore_ascii_case(cc))
            .map(|(_, t)| t)
    };
    country
        .and_then(find)
        .or_else(|| find("default"))
        .unwrap_or("{street} {number}\\n{postcode} {city}")
}

/// The answer as the post of its country lays out an address: lines separated by "\n". A field the answer does
/// not have is dropped together with the separator before it (or after it, at the start of a line).
pub fn postal(h: &Hit, country: Option<&str>) -> String {
    let postcode = postcode_as_written(&h.postcode, country);
    let field = |name: &str| -> &str {
        match name {
            "number" => h.housenumber.as_deref().unwrap_or(""),
            "street" => &h.street,
            "postcode" => &postcode,
            "city" => &h.commune,
            "region" => h.region.as_deref().unwrap_or(""),
            _ => "", // neighborhood: a sheet keeps no neighbourhood yet
        }
    };
    postal_lines(postal_template(country), field).join("\n")
}

/// The lines of `template` filled by `field`; a line whose fields are all empty is dropped (FHIR `text` takes the
/// town lines of the sheet's layout this way, `parts.rs`).
pub fn postal_lines<'a>(template: &str, field: impl Fn(&str) -> &'a str) -> Vec<String> {
    let mut lines = Vec::new();
    for line in template.split("\\n") {
        // pieces: text between placeholders, and placeholders filled or empty
        let mut out = String::new();
        let mut pending_sep = String::new();
        let mut rest = line;
        while let Some(open) = rest.find('{') {
            pending_sep.push_str(&rest[..open]);
            let close = rest[open..].find('}').map_or(rest.len(), |c| open + c);
            let value = field(&rest[open + 1..close]).trim();
            if !value.is_empty() {
                if !out.is_empty() {
                    out.push_str(&pending_sep);
                }
                out.push_str(value);
            }
            pending_sep.clear();
            rest = &rest[(close + 1).min(rest.len())..];
        }
        if !out.is_empty() {
            lines.push(out);
        }
    }
    lines
}

/// (CSV column, JSON key, URL template with {lat} {lon})
const LINKS: [(&str, &str, &str); 5] = [
    (
        "gp_osm_url",
        "osm",
        "https://www.openstreetmap.org/?mlat={lat}&mlon={lon}#map=19/{lat}/{lon}",
    ),
    (
        "gp_google_url",
        "google",
        "https://www.google.com/maps/search/?api=1&query={lat},{lon}",
    ),
    (
        "gp_apple_url",
        "apple",
        "https://maps.apple.com/?ll={lat},{lon}&q={lat},{lon}",
    ),
    (
        "gp_mapillary_url",
        "mapillary",
        "https://www.mapillary.com/app/?lat={lat}&lng={lon}&z=18",
    ),
    (
        "gp_bing_url",
        "bing",
        "https://www.bing.com/maps?cp={lat}~{lon}&lvl=19",
    ),
];

/// A spec read against one sheet: the sheet card is computed once here, not per answer.
pub struct Extras {
    pub with: With,
    card: Option<Map<String, Value>>,
    /// the card of the `--poi` layer, for its `poi_layer` answers (`set_poi`)
    poi_card: Option<Map<String, Value>>,
}

impl Extras {
    /// `None` spec or an empty one gives `None`: the caller then writes answers exactly as before.
    pub fn new(spec: Option<&str>, idx: &Index) -> Result<Option<Extras>> {
        let Some(spec) = spec else { return Ok(None) };
        let with = With::parse(spec)?;
        if with.is_empty() {
            return Ok(None);
        }
        let card = with.provenance.then(|| cached_card(idx));
        Ok(Some(Extras {
            with,
            card,
            poi_card: None,
        }))
    }

    /// The `--poi` layer asked beside the sheet: its answers (`poi_layer`) carry its own card.
    pub fn set_poi(&mut self, poi: &Index) {
        if self.with.provenance {
            self.poi_card = Some(cached_card(poi));
        }
    }

    /// The cells of a line the sheet did not answer, by column: `PstlAdr` can still be hybrid (a town of the sheet)
    /// or a post box, and `none` says why; the FHIR `Address` of the same reading. Empty without `--with iso20022`
    /// or `--with fhir`.
    pub fn cells_without_answer(&self, q: &str, idx: &Index) -> Vec<(&'static str, String)> {
        let mut out = Vec::new();
        if self.with.iso20022 {
            let o = crate::iso20022::build(idx, q, None);
            out.extend(
                ISO_COLUMNS
                    .into_iter()
                    .zip(iso_cells(Some(&crate::iso20022::json(&o, false)))),
            );
        }
        if self.with.fhir {
            let v = crate::parts::fhir_json(idx, q, None);
            out.extend(FHIR_COLUMNS.into_iter().zip(fhir_cells(Some(&v))));
        }
        out
    }

    /// The fields of one answer, keys sorted.
    pub fn of(&self, h: &Hit, idx: &Index) -> Map<String, Value> {
        let w = &self.with;
        let mut out = BTreeMap::<String, Value>::new();
        if let Some(res) = w.h3 {
            out.insert(
                "h3".into(),
                h3_cell(h.lat, h.lon, res).map_or(Value::Null, Value::from),
            );
        }
        if let Some(len) = w.geohash {
            out.insert(
                "geohash".into(),
                codes::geohash_encode(h.lat, h.lon, len).into(),
            );
        }
        if w.placekey {
            out.insert(
                "placekey_where".into(),
                placekey_where(h.lat, h.lon).map_or(Value::Null, Value::from),
            );
        }
        if w.codes {
            out.insert(
                "plus_code".into(),
                codes::plus_code_encode(h.lat, h.lon, 11).into(),
            );
            out.insert(
                "mgrs".into(),
                codes::mgrs_encode(h.lat, h.lon).map_or(Value::Null, Value::from),
            );
            out.insert("dms".into(), codes::dms_format(h.lat, h.lon).into());
        }
        if w.street_point {
            let point = street_point(h, idx);
            out.insert(
                "street_point".into(),
                point.map_or(
                    Value::Null,
                    |(lat, lon)| json!({"lat": round7(lat), "lon": round7(lon)}),
                ),
            );
            out.insert(
                "street_point_m".into(),
                point.map_or(Value::Null, |p| {
                    json!((codes::haversine_m((h.lat, h.lon), p) * 10.0).round() / 10.0)
                }),
            );
        }
        if w.links {
            let links: Map<String, Value> = LINKS
                .iter()
                .map(|(_, key, tpl)| ((*key).to_string(), link(tpl, h.lat, h.lon).into()))
                .collect();
            out.insert("links".into(), Value::Object(links));
        }
        if let Some(level) = w.place {
            out.insert("place".into(), place(h, level, idx));
        }
        if w.verdict {
            out.insert(
                "verdict".into(),
                match (query_now(), asked_now(idx)) {
                    (Some(q), Some(a)) => json!(crate::verdict::field_verdicts_in(idx, &q, &a, h)),
                    _ => Value::Null,
                },
            );
        }
        if w.unit {
            let q = QUERY.with(|c| c.borrow().as_ref().map(|q| q.0.clone()));
            out.insert(
                "unit".into(),
                q.and_then(|q| unit_of(&q))
                    .map_or(Value::Null, |(u, k)| json!({"unit": u, "kind": k})),
            );
        }
        if w.iso20022 {
            let q = QUERY.with(|c| c.borrow().as_ref().map(|q| q.0.clone()));
            out.insert(
                "iso20022".into(),
                q.map_or(Value::Null, |q| {
                    crate::iso20022::json(&crate::iso20022::build(idx, &q, Some(h)), false)
                }),
            );
        }
        if w.method {
            out.insert("point_method".into(), point_method(h).into());
        }
        for name in &w.cells {
            out.insert(
                format!("cell_{name}"),
                crate::cells::code(name, h.lat, h.lon).map_or(Value::Null, Value::from),
            );
        }
        if w.fhir {
            let q = QUERY.with(|c| c.borrow().as_ref().map(|q| q.0.clone()));
            out.insert(
                "fhir".into(),
                q.map_or(Value::Null, |q| crate::parts::fhir_json(idx, &q, Some(h))),
            );
        }
        if w.area {
            let q = QUERY.with(|c| c.borrow().as_ref().map(|q| q.0.clone()));
            out.insert(
                "area".into(),
                q.map_or(Value::Null, |q| {
                    area(&crate::query::query_cascade(idx, None, &q, AREA_K))
                }),
            );
        }
        if w.exists {
            let q = QUERY.with(|c| c.borrow().as_ref().map(|q| q.0.clone()));
            out.insert(
                "exists".into(),
                match (q, asked_now(idx)) {
                    (Some(q), Some(a)) => json!(crate::exists::judge_answer(idx, &q, &a, Some(h))),
                    _ => Value::Null,
                },
            );
        }
        if let Some(code) = w.crs {
            out.insert(
                "crs".into(),
                crate::crs::project(code, h.lat, h.lon).map_or(Value::Null, |(x, y)| {
                    json!({"code": format!("EPSG:{code}"), "x": (x * 100.0).round() / 100.0, "y": (y * 100.0).round() / 100.0})
                }),
            );
        }
        if let Some(layout) = w.postal {
            // a weak answer is a guess: laid out as an address it would reach a label with nothing to say so; the row
            // keeps the answer and gp_weak, the layout stays empty (ANSWERS §298, §300)
            let text = (!crate::query::hit_is_weak(h))
                .then(|| postal(h, Some(postal_layout_of(layout, idx))));
            out.insert("postal".into(), text.map_or(Value::Null, Value::from));
        }
        if let Some(card) = &self.card {
            // the card of the sheet that gave this answer: an answer of `--also` (own_sheet) or `--also-after`
            // (world_place) is not data of the country's sheet, nor under its licence (revision 0.9.0)
            let card = match (answering_sheet(h, idx), &self.poi_card) {
                (Some(other), _) => cached_card(other),
                (None, Some(poi)) if h.flags.contains(&"poi_layer") => poi.clone(),
                (None, _) => card.clone(),
            };
            out.insert("provenance".into(), Value::Object(card));
        }
        out.into_iter().collect()
    }

    /// The CSV cells of `columns()` for one answer, in the order of `columns()`: each cell is keyed by its column
    /// name and laid out by `columns()`, so the header and the cells cannot drift apart (revision 0.9.0: two
    /// hand-kept orders put `gp_x=yes` under `--with exists,crs:…`).
    pub fn cells(&self, h: &Hit, idx: &Index) -> Vec<String> {
        let map = self.of(h, idx);
        let text = |v: Option<&Value>| match v {
            None | Some(Value::Null) => String::new(),
            Some(Value::String(s)) => s.clone(),
            Some(v) => v.to_string(),
        };
        let mut by: BTreeMap<&'static str, String> = BTreeMap::new();
        let w = &self.with;
        if w.h3.is_some() {
            by.insert("gp_h3", text(map.get("h3")));
        }
        if w.geohash.is_some() {
            by.insert("gp_geohash", text(map.get("geohash")));
        }
        if w.placekey {
            by.insert("gp_placekey_where", text(map.get("placekey_where")));
        }
        if w.codes {
            for (col, k) in [
                ("gp_plus_code", "plus_code"),
                ("gp_mgrs", "mgrs"),
                ("gp_dms", "dms"),
            ] {
                by.insert(col, text(map.get(k)));
            }
        }
        if w.street_point {
            let p = map.get("street_point");
            by.insert("gp_street_lat", text(p.and_then(|p| p.get("lat"))));
            by.insert("gp_street_lon", text(p.and_then(|p| p.get("lon"))));
            by.insert("gp_street_point_m", text(map.get("street_point_m")));
        }
        if w.links {
            let l = map.get("links");
            for (col, key, _) in LINKS {
                by.insert(col, text(l.and_then(|l| l.get(key))));
            }
        }
        if w.place.is_some() {
            by.insert(
                "gp_place",
                text(map.get("place").and_then(|p| p.get("short"))),
            );
        }
        if w.verdict {
            let v = map.get("verdict");
            for (col, k) in [
                ("gp_verdict_street", "street"),
                ("gp_verdict_number", "number"),
                ("gp_verdict_postcode", "postcode"),
                ("gp_verdict_city", "city"),
            ] {
                by.insert(col, text(v.and_then(|v| v.get(k))));
            }
        }
        if w.unit {
            let u = map.get("unit");
            by.insert("gp_unit", text(u.and_then(|u| u.get("unit"))));
            by.insert("gp_unit_kind", text(u.and_then(|u| u.get("kind"))));
        }
        if w.iso20022 {
            by.extend(ISO_COLUMNS.into_iter().zip(iso_cells(map.get("iso20022"))));
        }
        if w.fhir {
            by.extend(FHIR_COLUMNS.into_iter().zip(fhir_cells(map.get("fhir"))));
        }
        if w.method {
            by.insert("gp_point_method", text(map.get("point_method")));
        }
        for name in &w.cells {
            if let Some(col) = crate::cells::column(name) {
                by.insert(col, text(map.get(&format!("cell_{name}"))));
            }
        }
        if w.area {
            let a = map.get("area");
            by.insert("gp_area_spread_m", text(a.and_then(|a| a.get("spread_m"))));
            by.insert(
                "gp_area_candidates",
                text(
                    a.and_then(|a| a.get("candidates"))
                        .and_then(Value::as_array)
                        .map(|v| Value::from(v.len()))
                        .as_ref(),
                ),
            );
        }
        if w.exists {
            let e = map.get("exists");
            by.insert("gp_exists", text(e.and_then(|e| e.get("exists"))));
            by.insert("gp_exists_reason", text(e.and_then(|e| e.get("reason"))));
        }
        if w.crs.is_some() {
            let p = map.get("crs");
            for (col, k) in [("gp_x", "x"), ("gp_y", "y"), ("gp_crs", "code")] {
                by.insert(col, text(p.and_then(|p| p.get(k))));
            }
        }
        if w.postal.is_some() {
            by.insert("gp_postal", text(map.get("postal")));
        }
        if w.provenance {
            let p = map.get("provenance");
            for (col, k) in [
                ("gp_sheet_release", "release"),
                ("gp_data_date", "data_date"),
                ("gp_license", "license"),
                ("gp_sources", "sources"),
                ("gp_sheet_sha256", "sha256"),
            ] {
                by.insert(col, text(p.and_then(|p| p.get(k))));
            }
        }
        let cols = w.columns();
        debug_assert_eq!(by.len(), cols.len(), "every column has its cell: {cols:?}");
        cols.into_iter()
            .map(|c| by.remove(c).unwrap_or_default())
            .collect()
    }

    /// The JSON text of one answer: its own fields as `Hit` writes them (same order, same numbers), then
    /// `alias_name` when given, then `extras`.
    pub fn hit_json(&self, h: &Hit, idx: &Index, alias: Option<&str>) -> Result<String> {
        hit_text(h, Some(self), idx, alias)
    }
}

/// One answer as JSON text with or without extras: the single place every surface writes a hit through.
/// Without extras and alias it is exactly `serde_json::to_string(hit)`.
pub fn hit_text(
    h: &Hit,
    extras: Option<&Extras>,
    idx: &Index,
    alias: Option<&str>,
) -> Result<String> {
    let mut s = serde_json::to_string(h)?;
    let mut tail = String::new();
    if let Some(name) = alias {
        tail.push_str(",\"alias_name\":");
        tail.push_str(&serde_json::to_string(name)?);
    }
    if let Some(e) = extras {
        tail.push_str(",\"extras\":");
        tail.push_str(&serde_json::to_string(&Value::Object(e.of(h, idx)))?);
    }
    if !tail.is_empty() {
        debug_assert!(s.ends_with('}'));
        s.pop();
        s.push_str(&tail);
        s.push('}');
    }
    Ok(s)
}

/// A list of answers as a JSON array text.
pub fn hits_text(
    hits: &[Hit],
    extras: Option<&Extras>,
    idx: &Index,
    alias: Option<&str>,
) -> Result<String> {
    let rows = hits
        .iter()
        .map(|h| hit_text(h, extras, idx, alias))
        .collect::<Result<Vec<_>>>()?;
    Ok(format!("[{}]", rows.join(",")))
}

thread_local! {
    /// The query whose answers are being written, and how the engine read it (filled on first use).
    static QUERY: std::cell::RefCell<Option<(String, Option<crate::verdict::Asked>)>> =
        const { std::cell::RefCell::new(None) };
}

/// Write the answers of `q` inside `f`: `verdict` compares them with `q` as the engine read it, read once.
/// Outside such a scope (reverse answers, for one) `verdict` is null.
pub fn for_query<T>(q: &str, f: impl FnOnce() -> T) -> T {
    struct Restore(Option<(String, Option<crate::verdict::Asked>)>);
    impl Drop for Restore {
        fn drop(&mut self) {
            QUERY.with(|c| *c.borrow_mut() = self.0.take());
        }
    }
    let _restore = Restore(QUERY.with(|c| c.borrow_mut().replace((q.to_string(), None))));
    f()
}

fn query_now() -> Option<String> {
    QUERY.with(|c| c.borrow().as_ref().map(|q| q.0.clone()))
}

fn asked_now(idx: &Index) -> Option<crate::verdict::Asked> {
    let q = QUERY.with(|c| c.borrow().clone())?;
    if let Some(a) = q.1 {
        return Some(a);
    }
    let a = crate::verdict::asked(idx, &q.0);
    QUERY.with(|c| {
        if let Some(cur) = c.borrow_mut().as_mut() {
            cur.1 = Some(a.clone());
        }
    });
    Some(a)
}

fn round7(v: f64) -> f64 {
    (v * 1e7).round() / 1e7
}

fn link(tpl: &str, lat: f64, lon: f64) -> String {
    tpl.replace("{lat}", &format!("{:.6}", lat))
        .replace("{lon}", &format!("{:.6}", lon))
}

fn h3_cell(lat: f64, lon: f64, res: u8) -> Option<String> {
    let r = h3o::Resolution::try_from(res).ok()?;
    let ll = h3o::LatLng::new(lat, lon).ok()?;
    Some(ll.to_cell(r).to_string())
}

/// Placekey's alphabet and the words it replaces (placekey-py 0.0.36, `placekey/placekey.py`, MIT).
const PK_ALPHABET: &[u8; 28] = b"23456789bcdfghjkmnpqrstvwxyz";
const PK_REPLACE: [(&str, &str); 13] = [
    ("prn", "pre"),
    ("f4nny", "f4nne"),
    ("tw4t", "tw4e"),
    ("ngr", "ngu"),
    ("dck", "dce"),
    ("vjn", "vju"),
    ("fck", "fce"),
    ("pns", "pne"),
    ("sht", "she"),
    ("kkk", "kke"),
    ("fgt", "fgu"),
    ("dyk", "dye"),
    ("bch", "bce"),
];

/// The "where" part of a Placekey: H3 at resolution 10, shortened and written in Placekey's alphabet
/// ("@5vg-82n-kzz"). The "what" part is issued by Placekey's own service and is not made here.
pub fn placekey_where(lat: f64, lon: f64) -> Option<String> {
    const BASE_RESOLUTION: u32 = 12;
    let ll = h3o::LatLng::new(lat, lon).ok()?;
    let cell = u64::from(ll.to_cell(h3o::Resolution::Ten));
    // placekey-py `_shorten_h3_integer`: add one to the base cell, keep the 52 location bits, drop the
    // digits finer than resolution 12
    let base_cell_shift = 1u64 << (3 * 15);
    let short = (cell.wrapping_add(base_cell_shift) % (1u64 << 52)) >> (3 * (15 - BASE_RESOLUTION));
    let mut digits = Vec::new();
    let mut x = short;
    if x == 0 {
        digits.push(PK_ALPHABET[0]);
    }
    while x > 0 {
        digits.push(PK_ALPHABET[(x % 28) as usize]);
        x /= 28;
    }
    digits.reverse();
    let mut s = String::from_utf8(digits).ok()?;
    for (bad, good) in PK_REPLACE {
        if s.contains(bad) {
            s = s.replace(bad, good);
        }
    }
    while s.len() < 9 {
        s.insert(0, 'a');
    }
    let groups: Vec<&str> = (0..s.len())
        .step_by(3)
        .map(|i| &s[i..(i + 3).min(s.len())])
        .collect();
    Some(format!("@{}", groups.join("-")))
}

pub(crate) fn street_point(h: &Hit, idx: &Index) -> Option<(f64, f64)> {
    if !matches!(h.precision, "house" | "interp" | "near") {
        return None;
    }
    let number: u32 = h
        .housenumber
        .as_deref()?
        .chars()
        .take_while(char::is_ascii_digit)
        .collect::<String>()
        .parse()
        .ok()?;
    idx.street_point(h.lat, h.lon, &h.street, &h.commune, number)
}

/// The answer as a person writes it: `levels` from the house to the region (what the answer has), and `short`,
/// the shortest of them at `level` or coarser that is still one place: the town is added to a street only when
/// another street of that name lies within about 3 km, and to a house the same way.
fn place(h: &Hit, level: &str, idx: &Index) -> Value {
    let house = match (&h.housenumber, h.street.is_empty()) {
        (Some(n), false) if idx.number_first() || number_written_first(idx) => {
            Some(format!("{n} {}", h.street))
        }
        (Some(n), false) => Some(format!("{} {n}", h.street)),
        _ => None,
    };
    let street = (!h.street.is_empty()).then(|| h.street.clone());
    let locality = (!h.commune.is_empty()).then(|| h.commune.clone());
    let region = h.region.clone().filter(|r| !r.is_empty());
    let alone = || {
        street
            .as_ref()
            .is_some_and(|s| idx.street_name_alone_near(h.lat, h.lon, s, &h.commune))
    };
    let with_town = |s: &str| match &locality {
        Some(town) if !alone() => format!("{s}, {town}"),
        _ => s.to_string(),
    };
    let start = PLACE_LEVELS.iter().position(|l| *l == level).unwrap_or(0);
    let short = PLACE_LEVELS[start..].iter().find_map(|l| match *l {
        "house" => house.as_deref().map(with_town),
        "street" => street.as_deref().map(with_town),
        "locality" => locality.as_ref().map(|t| match &region {
            Some(r) => format!("{t}, {r}"),
            None => t.clone(),
        }),
        _ => region.clone(),
    });
    json!({
        "short": short,
        "levels": {"house": house, "street": street, "locality": locality, "region": region},
    })
}

/// The sheet beside `idx` that gave `h`, when it is not `idx` itself: the sheet of one's own places for an
/// `own_sheet` answer, the sheet asked after for a `world_place` one. The flags are set only by those two paths
/// (`query.rs`), so an answer of the country's sheet never names another sheet.
fn answering_sheet<'a>(h: &Hit, idx: &'a Index) -> Option<&'a Index> {
    if h.flags.contains(&"own_sheet") {
        idx.also()
    } else if h.flags.contains(&"world_place") {
        idx.also_after()
    } else {
        None
    }
}

/// The post of the sheet's country writes the number before the street (`postal_formats.tsv`, the first line
/// of the layout `--with postal` takes for the sheet): "37 Rue Sainte-Croix", not "Rue Sainte-Croix 37". The
/// same lookup in a data table as `postal_layout_of` (ANSWERS §278), not a reading rule per country.
pub(crate) fn number_written_first(idx: &Index) -> bool {
    let line = postal_template(Some(postal_layout_of(SHEET_LAYOUT, idx)))
        .split("\\n")
        .next()
        .unwrap_or("");
    matches!((line.find("{number}"), line.find("{street}")), (Some(n), Some(s)) if n < s)
}

/// The card of a sheet, hashed once per mapped file: the server builds `Extras` per request.
fn cached_card(idx: &Index) -> Map<String, Value> {
    use std::collections::HashMap;
    use std::sync::{Mutex, OnceLock};
    type Cards = HashMap<(usize, usize), Map<String, Value>>;
    static CARDS: OnceLock<Mutex<Cards>> = OnceLock::new();
    let bytes = idx.file_bytes();
    let key = (bytes.as_ptr() as usize, bytes.len());
    let cards = CARDS.get_or_init(|| Mutex::new(HashMap::new()));
    if let Some(card) = cards.lock().expect("card cache").get(&key) {
        return card.clone();
    }
    let card = provenance_card(idx);
    cards.lock().expect("card cache").insert(key, card.clone());
    card
}

fn provenance_card(idx: &Index) -> Map<String, Value> {
    use sha2::{Digest, Sha256};
    let meta: BTreeMap<&str, &str> = idx
        .meta()
        .iter()
        .map(|(k, v)| (k.as_str(), v.as_str()))
        .collect();
    let sha: String = Sha256::digest(idx.file_bytes())
        .iter()
        .map(|b| format!("{b:02x}"))
        .collect();
    let reg = crate::sheets::registry();
    let release = reg
        .sheets
        .iter()
        .find(|s| s.sha256 == sha)
        .map(|_| reg.release.clone());
    let get = |k: &str| meta.get(k).map(|v| Value::from(*v)).unwrap_or(Value::Null);
    let mut card = Map::new();
    card.insert("release".into(), release.map_or(Value::Null, Value::from));
    card.insert("country".into(), get("country"));
    card.insert("data_date".into(), get("source_release"));
    card.insert("license".into(), get("license"));
    card.insert("sources".into(), get("sources"));
    card.insert("attribution".into(), get("attribution"));
    card.insert("sha256".into(), sha.into());
    card
}

#[cfg(test)]
mod tests {

    #[test]
    fn a_postcode_after_a_unit_word_is_not_a_unit() {
        // "FL" is Florida before its ZIP, "Interior" a Brazilian address part before its CEP (ANSWERS §308)
        assert_eq!(unit_of("11481 SW 84th St, Miami, FL 33173, USA"), None);
        assert_eq!(unit_of("Rua X 5, Interior, 89642-000"), None);
        // an ordinal that opens the line names the street; after the house it is the floor
        assert_eq!(
            unit_of("1ª Avenida do Centro Administrativo 150, Salvador"),
            None
        );
        assert_eq!(
            unit_of("Calle Mayor 5, 2º B, Madrid"),
            Some(("2º B".to_string(), "floor"))
        );
        // the near misses: a floor and a suite of four digits stay units
        assert_eq!(
            unit_of("350 Fifth Ave, Fl 3, New York"),
            Some(("3".to_string(), "floor"))
        );
        assert_eq!(
            unit_of("101 West Ohio Street, Suite 1200, Indianapolis"),
            Some(("1200".to_string(), "office"))
        );
    }

    use super::*;

    #[test]
    fn every_point_method_is_read_from_the_precision_and_flags_the_answer_carries() {
        let h = |precision: &'static str, flags: &[&'static str]| Hit {
            precision,
            flags: flags.to_vec(),
            ..addr("Rue A", "1", "", "Ville", None)
        };
        for (precision, flags, want) in [
            ("house", &[][..], "registry_point"),
            ("approximate", &[][..], "registry_point"),
            ("house", &["range"][..], "range_interpolated"),
            ("interp", &[][..], "between_neighbours"),
            ("near", &[][..], "nearest_house"),
            ("intersection", &[][..], "crossing_of_streets"),
            ("street", &["street_exact"][..], "street_centre"),
            ("city", &["postal_place"][..], "postcode_centre"),
            ("city", &["place_exact"][..], "place_table"),
            ("city", &[][..], "commune_centre"),
            ("house", &["poi_layer"][..], "place_layer"),
            (
                "street",
                &["postcode_layer", "postal_place"][..],
                "postcode_layer",
            ),
            ("house", &["alias", "poi_layer"][..], "own_place"),
        ] {
            assert_eq!(
                point_method(&h(precision, flags)),
                want,
                "{precision} {flags:?}"
            );
        }
    }

    fn addr(street: &str, number: &str, postcode: &str, city: &str, region: Option<&str>) -> Hit {
        Hit {
            lat: 0.0,
            lon: 0.0,
            precision: "house",
            score: 1.0,
            confidence: 1.0,
            street: street.into(),
            housenumber: (!number.is_empty()).then(|| number.into()),
            commune: city.into(),
            postcode: postcode.into(),
            flags: vec![],
            region: region.map(String::from),
            distance_m: None,
        }
    }

    #[test]
    fn unit_is_read_from_the_query_and_a_word_inside_a_street_name_is_not_one() {
        let u = |q: &str| unit_of(q).map(|(u, k)| (u, k.to_string()));
        let p = |a: &str, b: &str| Some((a.to_string(), b.to_string()));
        assert_eq!(u("Main St 12, apt 4B, Springfield"), p("4B", "flat"));
        assert_eq!(
            u("Suite 300, 1 Market St, San Francisco"),
            p("300", "office")
        );
        assert_eq!(u("Calle Mayor 12, 2º B, Madrid"), p("2º B", "floor"));
        assert_eq!(u("ul. Lenina 5, kv. 12"), p("12", "flat"));
        assert_eq!(u("12 Rue X #7"), p("7", "flat"));
        assert_eq!(u("ул. Ленина 5, кв. 12"), p("12", "flat"));
        assert_eq!(u("Unit Street 5, Springfield"), None);
        assert_eq!(u("Floor Avenue 12"), None);
    }

    #[test]
    fn area_weights_the_candidates_of_one_level_and_measures_their_spread() {
        let mut far = addr("Main Street", "1", "", "Springfield", None);
        far.lat = 39.8;
        far.lon = -89.6;
        far.score = 5.0;
        let mut near = far.clone();
        near.lat = 42.1;
        near.lon = -72.6;
        near.score = 4.0;
        let a = area(&[far.clone(), near.clone()]);
        let w: Vec<f64> = a["candidates"]
            .as_array()
            .unwrap()
            .iter()
            .map(|c| c["weight"].as_f64().unwrap())
            .collect();
        assert!((w.iter().sum::<f64>() - 1.0).abs() < 1e-5);
        assert!(w[0] > w[1], "the better score weighs more: {w:?}");
        assert!(a["spread_m"].as_f64().unwrap() > 50_000.0);
        let one = area(&[far]);
        assert_eq!(one["spread_m"], 0.0);
        assert_eq!(one["candidates"], json!([]));
    }

    #[test]
    fn a_polish_postcode_kept_as_digits_is_written_with_its_dash() {
        // the Polish sheet keeps "00624"; the post, ISO 20022 and FHIR write "00-624" (17.11.2026)
        let pl = addr("Marszałkowska", "1", "00624", "Warszawa", None);
        assert_eq!(postal(&pl, Some("pl")), "Marszałkowska 1\n00-624 Warszawa");
        assert_eq!(postcode_as_written("00624", Some("PL")), "00-624");
        // a code of another length, letters, an empty code and a layout without a shape stay as they are
        assert_eq!(postcode_as_written("0062", Some("pl")), "0062");
        assert_eq!(postcode_as_written("00-624", Some("pl")), "00-624");
        assert_eq!(postcode_as_written("", Some("pl")), "");
        assert_eq!(postcode_as_written("01304", Some("br")), "01304");
        assert_eq!(postcode_as_written("10115", Some("de")), "10115");
        assert_eq!(postcode_as_written("00624", None), "00624");
        assert_eq!(
            postal_template(Some("pl")),
            "{street} {number}\\n{postcode} {city}"
        );
    }

    #[test]
    fn postal_lays_the_answer_out_as_the_country_writes_it() {
        let de = addr("Hauptstraße", "5", "10115", "Berlin", None);
        let us = addr(
            "Main Street",
            "100",
            "90012",
            "Los Angeles",
            Some("California"),
        );
        let br = addr("Rua Augusta", "1500", "01304", "São Paulo", Some("SP"));
        let cases = [
            (&de, Some("de"), "Hauptstraße 5\n10115 Berlin"),
            (&de, Some("fr"), "5 Hauptstraße\n10115 Berlin"),
            (
                &us,
                Some("us"),
                "100 Main Street\nLos Angeles, California 90012",
            ),
            (&br, Some("br"), "Rua Augusta, 1500\nSão Paulo - SP\n01304"),
            (&de, Some("zz"), "Hauptstraße 5\n10115 Berlin"),
            (&de, None, "Hauptstraße 5\n10115 Berlin"),
        ];
        for (h, cc, want) in cases {
            assert_eq!(postal(h, cc), want, "{cc:?}");
        }
    }

    #[test]
    fn postal_drops_a_missing_field_with_its_separator() {
        // a street answer: no number; the US line keeps "city postcode" without a dangling comma
        let us = addr("Main Street", "", "90012", "Los Angeles", None);
        assert_eq!(postal(&us, Some("us")), "Main Street\nLos Angeles 90012");
        let mx = addr(
            "Calle Moneda",
            "4",
            "06000",
            "Cuauhtémoc",
            Some("Ciudad de México"),
        );
        assert_eq!(
            postal(&mx, Some("mx")),
            "Calle Moneda 4\n06000 Cuauhtémoc, Ciudad de México"
        );
    }

    #[test]
    fn postal_is_a_column_only_when_asked() {
        assert!(!With::parse("h3").unwrap().columns().contains(&"gp_postal"));
        assert_eq!(With::parse("postal").unwrap().columns(), ["gp_postal"]);
        assert_eq!(With::parse("postal:US").unwrap().postal, Some("us"));
        assert!(
            With::parse("postal:zz").is_err(),
            "an unknown layout is an error, not the default"
        );
    }

    #[test]
    fn a_spec_reads_names_and_levels_and_refuses_the_rest() {
        let w = With::parse("h3, geohash:7,placekey,codes,street_point,links,provenance").unwrap();
        assert_eq!(w.h3, Some(9), "h3 without a level is 9");
        assert_eq!(w.geohash, Some(7));
        assert!(w.placekey && w.codes && w.street_point && w.links && w.provenance);
        assert_eq!(With::parse("h3:12").unwrap().h3, Some(12));
        assert!(With::parse("").unwrap().is_empty());
        for bad in ["h3:16", "geohash:0", "placekey:3", "zip", "h3:x"] {
            assert!(With::parse(bad).is_err(), "{bad}");
        }
        let tz = With::parse("tz").unwrap_err().to_string();
        assert!(tz.contains("ODbL"), "{tz}");
    }

    #[test]
    fn the_columns_follow_the_spec_in_a_fixed_order() {
        let w = With::parse("links,h3").unwrap();
        let cols = w.columns();
        assert_eq!(cols[0], "gp_h3");
        assert_eq!(cols.len(), 1 + LINKS.len());
        assert!(With::default().columns().is_empty());
    }

    #[test]
    fn h3_is_the_cell_of_the_h3_documentation() {
        // h3 documentation, "latLngToCell": 37.769377, -122.388903 at resolution 9
        assert_eq!(
            h3_cell(37.769377, -122.388903, 9).unwrap(),
            "89283082e73ffff"
        );
        assert_ne!(
            h3_cell(37.769377, -122.388903, 8).unwrap(),
            "89283082e73ffff"
        );
    }

    #[test]
    fn the_placekey_where_part_matches_placekey_py() {
        // placekey-py 0.0.36 README: geo_to_placekey(0.0, 0.0) is "@dvt-smp-tvz" (H3 8a754e64992ffff), and the
        // service answers 37.7371, -122.44283 with the where part "@5vg-82n-kzz"
        assert_eq!(h3_cell(0.0, 0.0, 10).unwrap(), "8a754e64992ffff");
        assert_eq!(placekey_where(0.0, 0.0).unwrap(), "@dvt-smp-tvz");
        assert_eq!(placekey_where(37.7371, -122.44283).unwrap(), "@5vg-82n-kzz");
    }

    #[test]
    fn links_put_the_point_in_each_template() {
        let l = link(LINKS[0].2, 44.8125, 20.4612);
        assert_eq!(l, "https://www.openstreetmap.org/?mlat=44.812500&mlon=20.461200#map=19/44.812500/20.461200");
    }
}