terrio 0.5.0

Pure-Rust geospatial raster, vector and point cloud data I/O: read, write, convert and reproject GIS formats without C dependencies
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
//! SpatioTemporal Asset Catalog (STAC) documents as datasets, as GDAL's STACIT driver and its
//! GeoJSON driver read them.
//!
//! - A STAC Item, ItemCollection (such as a STAC API search result), Collection or Catalog (local
//!   or remote JSON) opens as a vector layer of item footprints with their properties, read as
//!   GDAL reads STAC GeoJSON: the properties, the item's other members (`stac_version`, `links`,
//!   `stac_extensions`, `collection`) and each asset's members as `assets.<asset>.<member>`.
//!   Search results are followed through their `next` links, and collections through their
//!   `item` and `items` links, up to 1000 items by default (`max_items=N`, 0 for no limit).
//! - `STAC:"item.json":asset` (or `item.json!asset`) opens an asset of an item, its href
//!   resolved against the item's location. Cloud Optimized GeoTIFFs are read in place.
//! - `STACIT:"source":asset=name` mosaics an asset of the items of a search or item collection,
//!   as GDAL's STACIT driver does: one subdataset per collection, asset and CRS
//!   (`collection=`, `asset=` and `crs=` filters), items drawn in ascending datetime order onto
//!   a grid at the items' average resolution (`resolution=highest|lowest|average`), read by
//!   window from the items' files.
//!
//! - Search pages are followed through their `next` links whether they are `GET` or `POST`
//!   requests (with the link's `body`, `merge` and `headers`), and `STACIT:"url":method=POST`
//!   sends a search URL's query parameters as a `POST` body, as pystac-client does.
//! - Assets on Microsoft Planetary Computer (Azure Blob Storage hrefs of items read from its API
//!   or linking to it) are signed with its SAS tokens, as `planetary_computer.sign()` does (see
//!   [`SignMode`]): `STACIT:` names take a `sign=` option, `STAC:` names follow
//!   `TERRIO_STAC_SIGN`. An item file without links to Planetary Computer is signed only with
//!   `planetary_computer`.
//!
//! [`crate::info::info_path`] lists an item's assets, or a collection's mosaics, as subdatasets.

use crate::cog::{LazyRaster, Window, WindowOptions, WindowSource};
use crate::crs::SpatialRef;
use crate::formats::{RasterDriver, VectorDriver};
use crate::info::{Info, InfoOptions, Subdataset};
use crate::ops::vrt::SourceRead;
use crate::{Dataset, Error, RasterDataset, Result, VectorDataset};
use serde_json::{Value, json};
use std::collections::BTreeMap;
use wbraster::DataType;
use wbraster::raster::RasterData;

/// The most items read by default, as in GDAL's STACIT driver.
pub const DEFAULT_MAX_ITEMS: usize = 1000;

fn bad(msg: impl std::fmt::Display) -> Error {
    Error::InvalidData(format!("STAC: {msg}"))
}

fn is_url(s: &str) -> bool {
    let l = s.to_ascii_lowercase();
    l.starts_with("http://") || l.starts_with("https://")
}

/// A location's bytes: a local file, a URL or object storage, or a zip archive member.
pub(crate) fn fetch(loc: &str) -> Result<Vec<u8>> {
    use crate::vfs::Location;
    match Location::parse(loc) {
        Location::Local(p) => Ok(std::fs::read(p)?),
        #[cfg(remote)]
        l @ (Location::Url(_) | Location::S3 { .. }) => {
            let remote = l.remote().ok_or_else(|| bad(format!("'{loc}' is not remote")))?;
            crate::http::get(&remote)
        }
        _ => {
            let local = crate::vfs::resolve(loc)?;
            Ok(std::fs::read(local.path())?)
        }
    }
}

fn fetch_json(loc: &str) -> Result<Value> {
    serde_json::from_slice(&fetch(loc)?).map_err(|e| bad(format!("{loc}: {e}")))
}

/// A page of search results to request: a `GET` of a URL, or a `POST` of a JSON body.
#[derive(Debug, Clone, PartialEq)]
struct Request {
    url: String,
    /// The `POST` body, `None` for a `GET`.
    body: Option<Value>,
    headers: Vec<(String, String)>,
}

impl Request {
    fn get(url: &str) -> Self {
        Self { url: url.to_string(), body: None, headers: Vec::new() }
    }

    fn fetch(&self) -> Result<Value> {
        let bytes = match &self.body {
            None if self.headers.is_empty() => fetch(&self.url)?,
            #[cfg(remote)]
            None => crate::http::get_with_headers(&self.url, &self.headers)?,
            #[cfg(remote)]
            Some(body) => crate::http::post_json(&self.url, &serde_json::to_vec(body).map_err(bad)?, &self.headers)?,
            #[cfg(not(remote))]
            _ => return Err(bad(format!("{}: POST requests and request headers need the `remote` feature", self.url))),
        };
        serde_json::from_slice(&bytes).map_err(|e| bad(format!("{}: {e}", self.url)))
    }
}

/// The request of a page's `next` link: a `GET` link if the page has one, else a `POST` link,
/// its body merged into the request's when the link says `merge` (STAC API's pagination).
fn next_request(doc: &Value, cur: &Request) -> Option<Request> {
    let links: Vec<&Value> = doc["links"]
        .as_array()
        .into_iter()
        .flatten()
        .filter(|l| {
            l["rel"] == "next" && l["type"].as_str().is_none_or(|t| t == "application/geo+json" || t == "application/json" || t.is_empty())
        })
        .collect();
    let method = |l: &Value| l["method"].as_str().unwrap_or("GET").to_ascii_uppercase();
    let link = links.iter().find(|l| method(l) == "GET").or_else(|| links.iter().find(|l| method(l) == "POST"))?;
    let url = resolve_href(&cur.url, link["href"].as_str()?);
    // A header of several values (an array) is sent once per value, as STAC API says.
    let link_headers: Vec<(String, String)> = link["headers"]
        .as_object()
        .into_iter()
        .flatten()
        .flat_map(|(k, v)| match v {
            Value::Array(values) => values.iter().filter_map(|x| Some((k.clone(), x.as_str()?.to_string()))).collect::<Vec<_>>(),
            other => other.as_str().map(|x| vec![(k.clone(), x.to_string())]).unwrap_or_default(),
        })
        .collect();
    // The previous page's headers (keys, perhaps) go only to the same origin: as they are when the
    // link has none, under the link's when it says `merge`.
    let same_origin = crate::vfs::url_origin(&url).is_some() && crate::vfs::url_origin(&url) == crate::vfs::url_origin(&cur.url);
    let headers = match (same_origin, link_headers.is_empty(), link["merge"] == true) {
        (true, true, _) => cur.headers.clone(),
        (true, false, true) => {
            let mut h: Vec<(String, String)> =
                cur.headers.iter().filter(|(k, _)| !link_headers.iter().any(|(l, _)| l.eq_ignore_ascii_case(k))).cloned().collect();
            h.extend(link_headers);
            h
        }
        _ => link_headers,
    };
    if method(link) == "GET" {
        return Some(Request { url, body: None, headers });
    }
    let body = match (&cur.body, &link["body"]) {
        (Some(Value::Object(prev)), Value::Object(b)) if link["merge"] == true && same_origin => {
            let mut m = prev.clone();
            m.extend(b.iter().map(|(k, v)| (k.clone(), v.clone())));
            Value::Object(m)
        }
        (_, Value::Object(b)) => Value::Object(b.clone()),
        // A link without a body repeats the search, to its own origin only.
        (Some(prev), _) if same_origin => prev.clone(),
        _ => json!({}),
    };
    Some(Request { url, body: Some(body), headers })
}

/// Percent-decodes a URL query component (`+` is a space).
fn url_decode(s: &str) -> String {
    let b = s.as_bytes();
    let mut out = Vec::with_capacity(b.len());
    let mut i = 0;
    while i < b.len() {
        let hex = b.get(i + 1..i + 3).and_then(|h| std::str::from_utf8(h).ok()).and_then(|h| u8::from_str_radix(h, 16).ok());
        match (b[i], hex) {
            (b'+', _) => out.push(b' '),
            (b'%', Some(v)) => {
                out.push(v);
                i += 2;
            }
            (c, _) => out.push(c),
        }
        i += 1;
    }
    String::from_utf8_lossy(&out).into_owned()
}

/// A `datetime` search parameter with its dates and months as RFC 3339 instants, as
/// pystac-client sends it (`2023-07` is `2023-07-01T00:00:00Z/2023-07-31T23:59:59Z`): servers
/// reject bare dates in `POST` bodies.
fn rfc3339_interval(v: &str) -> String {
    let parts: Vec<&str> = v.split('/').map(str::trim).collect();
    let days_in = |y: i64, m: i64| match m {
        2 if (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 => 29,
        2 => 28,
        4 | 6 | 9 | 11 => 30,
        _ => 31,
    };
    // The first and last instants of a year, month or day; `None` for anything else.
    let expand = |p: &str| -> Option<(String, String)> {
        let n: Vec<i64> = p.split('-').map(|x| x.parse().ok()).collect::<Option<_>>()?;
        let ok = p.len() == [4, 7, 10][n.len().clamp(1, 3) - 1];
        match n[..] {
            [y] if ok => Some((format!("{y:04}-01-01T00:00:00Z"), format!("{y:04}-12-31T23:59:59Z"))),
            [y, m] if ok && (1..=12).contains(&m) => {
                Some((format!("{y:04}-{m:02}-01T00:00:00Z"), format!("{y:04}-{m:02}-{:02}T23:59:59Z", days_in(y, m))))
            }
            [y, m, d] if ok && (1..=12).contains(&m) && (1..=days_in(y, m)).contains(&d) => {
                Some((format!("{y:04}-{m:02}-{d:02}T00:00:00Z"), format!("{y:04}-{m:02}-{d:02}T23:59:59Z")))
            }
            _ => None,
        }
    };
    match parts[..] {
        [one] => match expand(one) {
            Some((a, b)) => format!("{a}/{b}"),
            None => one.to_string(),
        },
        [a, b] => {
            let a = expand(a).map_or_else(|| a.to_string(), |(s, _)| s);
            let b = expand(b).map_or_else(|| b.to_string(), |(_, e)| e);
            format!("{a}/{b}")
        }
        _ => v.to_string(),
    }
}

/// The `POST` request of a STAC API search URL's query parameters, as pystac-client builds it
/// (`bbox`, `collections` and `ids` as arrays, `limit` as a number, `intersects`, `query` and
/// `filter` as JSON, `sortby` and `fields` as their objects).
fn post_request(source: &str) -> Request {
    let (url, query) = source.split_once('?').unwrap_or((source, ""));
    let mut body = serde_json::Map::new();
    for kv in query.split('&').filter(|kv| !kv.is_empty()) {
        let (k, v) = kv.split_once('=').unwrap_or((kv, ""));
        let (k, v) = (url_decode(k), url_decode(v));
        let list = || Value::Array(v.split(',').map(|s| Value::String(s.trim().to_string())).collect());
        let value = match k.as_str() {
            "bbox" => Value::Array(v.split(',').filter_map(|n| n.trim().parse::<f64>().ok()).map(Value::from).collect()),
            "collections" | "ids" => list(),
            "limit" => v.parse::<u64>().map(Value::from).unwrap_or(Value::String(v)),
            "datetime" => Value::String(rfc3339_interval(&v)),
            "sortby" => Value::Array(
                v.split(',')
                    .map(|f| {
                        let f = f.trim();
                        let (dir, field) = match f.strip_prefix('-') {
                            Some(r) => ("desc", r),
                            None => ("asc", f.strip_prefix('+').unwrap_or(f)),
                        };
                        json!({"field": field, "direction": dir})
                    })
                    .collect(),
            ),
            "fields" => {
                let (mut include, mut exclude) = (Vec::new(), Vec::new());
                for f in v.split(',').map(str::trim).filter(|f| !f.is_empty()) {
                    match f.strip_prefix('-') {
                        Some(r) => exclude.push(r.to_string()),
                        None => include.push(f.strip_prefix('+').unwrap_or(f).to_string()),
                    }
                }
                json!({"include": include, "exclude": exclude})
            }
            _ => match serde_json::from_str::<Value>(&v) {
                Ok(j @ (Value::Object(_) | Value::Array(_))) => j,
                _ => Value::String(v),
            },
        };
        body.insert(k, value);
    }
    // A text filter (CQL2 text) needs its language said in a POST body.
    if body.get("filter").is_some_and(Value::is_string) && !body.contains_key("filter-lang") {
        body.insert("filter-lang".into(), json!("cql2-text"));
    }
    Request { url: url.to_string(), body: Some(Value::Object(body)), headers: Vec::new() }
}

/// An href resolved against the location of the document holding it.
pub(crate) fn resolve_href(base: &str, href: &str) -> String {
    if href.contains("://") || href.starts_with("/vsi") {
        return href.to_string();
    }
    if is_url(base) {
        if href.starts_with('/') {
            let (scheme, rest) = base.split_once("://").unwrap_or(("https", base));
            let host = rest.split(['/', '?', '#']).next().unwrap_or(rest);
            return format!("{scheme}://{host}{href}");
        }
    } else if href.starts_with('/') || std::path::Path::new(href).is_absolute() {
        return href.to_string();
    }
    let dir = base.split(['?', '#']).next().unwrap_or(base);
    let dir = match dir.rfind(['/', '\\']) {
        Some(i) => &dir[..i],
        None => "",
    };
    let mut parts: Vec<&str> = if dir.is_empty() { Vec::new() } else { dir.split('/').collect() };
    for seg in href.split('/') {
        match seg {
            "." | "" => {}
            ".." if parts.len() > 1 && parts.last() != Some(&"..") => {
                parts.pop();
            }
            s => parts.push(s),
        }
    }
    parts.join("/")
}

/// What kind of STAC document a JSON value is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Kind {
    Item,
    ItemCollection,
    Collection,
    Catalog,
}

fn kind(v: &Value) -> Option<Kind> {
    let is_item = |f: &Value| f["type"] == "Feature" && f.get("stac_version").is_some();
    match v["type"].as_str()? {
        "Feature" if is_item(v) => Some(Kind::Item),
        "FeatureCollection" if v.get("stac_version").is_some() || v["features"].as_array().is_some_and(|f| f.iter().any(is_item)) => {
            Some(Kind::ItemCollection)
        }
        "Collection" if v.get("stac_version").is_some() => Some(Kind::Collection),
        "Catalog" if v.get("stac_version").is_some() => Some(Kind::Catalog),
        _ => None,
    }
}

/// How asset hrefs are signed before they are opened.
///
/// Set by `TERRIO_STAC_SIGN` (`auto`, the default, `planetary_computer` or `none`) or a
/// `STACIT:` name's `sign=` option. `auto` signs the Azure Blob Storage hrefs of items read from
/// Planetary Computer's STAC API (or linking to it); `planetary_computer` signs every Azure Blob
/// Storage href. Signing appends a SAS token from Planetary Computer's token endpoint (one per
/// storage account and container, renewed before it expires), sent with
/// `PC_SDK_SUBSCRIPTION_KEY` as its API key when that is set, as the `planetary-computer` Python
/// package does.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum SignMode {
    /// Sign the assets of Planetary Computer items.
    #[default]
    Auto,
    /// Sign every Azure Blob Storage asset with a Planetary Computer token.
    PlanetaryComputer,
    /// Never sign.
    None,
}

impl SignMode {
    fn parse(v: &str) -> Option<Self> {
        match v.trim().to_ascii_lowercase().replace('-', "_").as_str() {
            "auto" | "" => Some(Self::Auto),
            "planetary_computer" | "planetarycomputer" | "pc" | "yes" | "on" | "true" => Some(Self::PlanetaryComputer),
            "none" | "no" | "off" | "false" => Some(Self::None),
            _ => None,
        }
    }

    /// The mode set by `TERRIO_STAC_SIGN`.
    fn from_env() -> Self {
        std::env::var("TERRIO_STAC_SIGN").ok().and_then(|v| Self::parse(&v)).unwrap_or_default()
    }
}

const PC_HOST: &str = "planetarycomputer.microsoft.com";

/// The storage account and container of an Azure Blob Storage URL.
fn blob_container(href: &str) -> Option<(&str, &str)> {
    let rest = href.strip_prefix("https://")?;
    let (host, path) = rest.split_once('/')?;
    // An account name is lowercase letters and digits: a host with user information, a port or
    // a fragment (`https://evil.example#@a.blob.core.windows.net/c`) is not an account's.
    let account = host
        .strip_suffix(".blob.core.windows.net")
        .filter(|a| !a.is_empty() && a.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit()))?;
    let container = path
        .split(['/', '?', '#'])
        .next()
        .filter(|c| !c.is_empty() && c.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-'))?;
    Some((account, container))
}

/// Whether an item comes from Planetary Computer: read from its API, or linking to it.
fn from_planetary_computer(item: &Value, base: &str) -> bool {
    let host = |u: &str| crate::vfs::url_origin(u).map(|(_, h)| h);
    host(base).is_some_and(|h| h == PC_HOST)
        || item["links"].as_array().into_iter().flatten().any(|l| l["href"].as_str().and_then(host).is_some_and(|h| h == PC_HOST))
}

/// An asset href of `item` (read from `base`), signed as `mode` says.
fn sign_href(href: String, item: &Value, base: &str, mode: SignMode) -> Result<String> {
    if needs_signing(&href, item, base, mode) { signed(href) } else { Ok(href) }
}

/// Whether an asset href of `item` (read from `base`) is to be signed as `mode` says: an
/// unsigned Azure Blob Storage href.
fn needs_signing(href: &str, item: &Value, base: &str, mode: SignMode) -> bool {
    let sign = match mode {
        SignMode::None => false,
        SignMode::PlanetaryComputer => true,
        SignMode::Auto => from_planetary_computer(item, base),
    };
    // Already signed (a SAS token's signature is `sig=`).
    sign && blob_container(href).is_some() && !href.split_once('?').is_some_and(|(_, q)| q.split('&').any(|kv| kv.starts_with("sig=")))
}

/// An Azure Blob Storage href with a current Planetary Computer SAS token appended.
fn signed(href: String) -> Result<String> {
    let Some((account, container)) = blob_container(&href) else { return Ok(href) };
    let token = pc_token(account, container)?;
    Ok(format!("{href}{}{token}", if href.contains('?') { '&' } else { '?' }))
}

/// A Planetary Computer SAS token for a storage account and container, cached until shortly
/// before it expires.
#[cfg(remote)]
fn pc_token(account: &str, container: &str) -> Result<String> {
    use std::sync::Mutex;
    use std::time::{Duration, Instant};
    static TOKENS: Mutex<BTreeMap<(String, String), (String, Instant)>> = Mutex::new(BTreeMap::new());
    let key = (account.to_string(), container.to_string());
    if let Ok(tokens) = TOKENS.lock()
        && let Some((token, until)) = tokens.get(&key)
        && Instant::now() < *until
    {
        return Ok(token.clone());
    }
    let base = std::env::var("TERRIO_PC_SAS_URL").unwrap_or_else(|_| format!("https://{PC_HOST}/api/sas/v1/token"));
    let url = format!("{}/{account}/{container}", base.trim_end_matches('/'));
    // The API key goes over https only.
    let headers: Vec<(String, String)> = std::env::var("PC_SDK_SUBSCRIPTION_KEY")
        .ok()
        .filter(|k| !k.trim().is_empty() && url.starts_with("https://"))
        .map(|k| ("Ocp-Apim-Subscription-Key".to_string(), k.trim().to_string()))
        .into_iter()
        .collect();
    let v: Value = serde_json::from_slice(&crate::http::get_with_headers(&url, &headers)?).map_err(|e| bad(format!("{url}: {e}")))?;
    let token = v["token"].as_str().filter(|t| !t.is_empty()).ok_or_else(|| bad(format!("{url}: no token in the response")))?.to_string();
    // Tokens last about an hour or more ("msft:expiry"); renew well before.
    // No expiry given: half an hour. One already past (a clock off) or close: not cached.
    let lifetime = match v["msft:expiry"].as_str() {
        None => Some(Duration::from_secs(1800)),
        Some(t) => seconds_until(t).filter(|s| *s > 300).map(|s| Duration::from_secs(s - 300)),
    };
    if let Some(lifetime) = lifetime
        && let Ok(mut tokens) = TOKENS.lock()
    {
        tokens.insert(key, (token.clone(), Instant::now() + lifetime));
    }
    Ok(token)
}

#[cfg(not(remote))]
fn pc_token(account: &str, container: &str) -> Result<String> {
    Err(bad(format!("signing {account}/{container} assets needs the `remote` feature")))
}

/// The seconds from now to a UTC time `YYYY-MM-DDTHH:MM:SSZ`.
#[cfg(remote)]
fn seconds_until(t: &str) -> Option<u64> {
    let n = |r: std::ops::Range<usize>| t.get(r)?.parse::<i64>().ok();
    crate::http::seconds_until_utc(n(0..4)?, n(5..7)?, n(8..10)?, n(11..13)?, n(14..16)?, n(17..19)?)
}

/// Items with the location each was read from (their hrefs are relative to it).
struct Items {
    items: Vec<(Value, String)>,
    /// Whether the item limit stopped the reading.
    truncated: bool,
}

/// Reads the items of a STAC document at `loc` (already parsed as `doc`), following search
/// pages and collection links, up to `max_items` items (0: no limit).
fn load_items(doc: Value, loc: &str, max_items: usize) -> Result<Items> {
    load_items_from(doc, &Request::get(loc), max_items)
}

/// [`load_items`] of the response to a request (whose next pages may be `POST` requests too).
fn load_items_from(doc: Value, request: &Request, max_items: usize) -> Result<Items> {
    let loc = request.url.as_str();
    let mut out = Items { items: Vec::new(), truncated: false };
    let full = |out: &Items| max_items > 0 && out.items.len() >= max_items;
    match kind(&doc) {
        Some(Kind::Item) => out.items.push((doc, loc.to_string())),
        Some(Kind::ItemCollection) => read_pages(doc, request.clone(), max_items, &mut out)?,
        Some(Kind::Collection | Kind::Catalog) => {
            let mut queue = vec![(doc, loc.to_string(), 0usize)];
            while let Some((doc, base, depth)) = queue.pop() {
                for link in doc["links"].as_array().into_iter().flatten() {
                    if full(&out) {
                        out.truncated = true;
                        return Ok(out);
                    }
                    let Some(href) = link["href"].as_str() else { continue };
                    let href = resolve_href(&base, href);
                    match link["rel"].as_str() {
                        Some("item") => {
                            let item = fetch_json(&href)?;
                            if kind(&item) == Some(Kind::Item) {
                                out.items.push((item, href));
                            }
                        }
                        Some("items") => read_pages(fetch_json(&href)?, Request::get(&href), max_items, &mut out)?,
                        Some("child") if depth < 8 => queue.push((fetch_json(&href)?, href, depth + 1)),
                        _ => {}
                    }
                }
            }
        }
        None => return Err(bad(format!("'{loc}' is not a STAC item, item collection, collection or catalog"))),
    }
    Ok(out)
}

/// Reads the items of a page of results and of the pages its `next` links lead to.
fn read_pages(mut doc: Value, mut cur: Request, max_items: usize, out: &mut Items) -> Result<()> {
    let mut pages = 0;
    loop {
        pages += 1;
        for f in doc["features"].as_array().into_iter().flatten() {
            if max_items > 0 && out.items.len() >= max_items {
                out.truncated = true;
                return Ok(());
            }
            out.items.push((f.clone(), cur.url.clone()));
        }
        // https://github.com/radiantearth/stac-api-spec/tree/release/v1.0.0/item-search#pagination
        let Some(next) = next_request(&doc, &cur) else { return Ok(()) };
        if next == cur || pages > 10_000 {
            return Ok(());
        }
        doc = next.fetch()?;
        cur = next;
    }
}

/// The footprints of items as a vector layer, read as GDAL reads STAC GeoJSON.
fn footprints(items: &[(Value, String)], name: &str) -> Result<wbvector::Layer> {
    let features: Vec<&Value> = items.iter().map(|(v, _)| v).collect();
    let fc = json!({"type": "FeatureCollection", "features": features});
    let bytes = serde_json::to_vec(&fc).map_err(bad)?;
    crate::drivers::vector::geojson::parse(&bytes, name)
}

/// The layer name of a STAC document: the file name's stem, or the collection's id.
fn layer_name(doc: &Value, loc: &str) -> String {
    if matches!(kind(doc), Some(Kind::Collection | Kind::Catalog))
        && let Some(id) = doc["id"].as_str()
    {
        return id.to_string();
    }
    let path = loc.split(['?', '#']).next().unwrap_or(loc);
    let file = path.trim_end_matches('/').rsplit(['/', '\\']).next().unwrap_or(path);
    let stem = file.split('.').next().unwrap_or(file);
    if stem.is_empty() { "items".into() } else { stem.to_string() }
}

/// Whether an asset can hold raster or vector data (not metadata, thumbnails or text).
fn is_data_asset(asset: &Value) -> bool {
    let t = asset["type"].as_str().unwrap_or_default();
    if matches!(t, "application/json" | "application/xml" | "text/plain" | "text/html") {
        return false;
    }
    !asset["roles"].as_array().into_iter().flatten().any(|r| matches!(r.as_str(), Some("thumbnail" | "info" | "metadata" | "overview")))
}

// ---------------------------------------------------------------------------------------------
// Dataset names

/// A parsed `STAC:` or `STACIT:` dataset name.
#[derive(Debug, Clone, PartialEq)]
enum Name {
    /// An asset of an item: `STAC:"item.json":asset` or `item.json!asset`.
    Asset { item: String, asset: String },
    /// A mosaic of the items of a search or collection: `STACIT:"source":key=value,...`.
    Mosaic { source: String, filters: Filters },
}

/// The filters and options of a `STACIT:` name.
#[derive(Debug, Clone, Default, PartialEq)]
struct Filters {
    collection: Option<String>,
    asset: Option<String>,
    crs: Option<String>,
    resolution: Option<String>,
    max_items: Option<usize>,
    /// `method=POST`: send the search URL's query as a `POST` body.
    post: bool,
    sign: Option<SignMode>,
    /// An option with a value it does not take (`sign=nope`, `method=PUT`).
    invalid: Option<String>,
}

impl Filters {
    /// The options a mosaic's name keeps from these (`method=POST`, `sign=`), for the names of
    /// the subdatasets it suggests.
    fn kept_options(&self) -> String {
        let mut out = String::new();
        if self.post {
            out.push_str(",method=POST");
        }
        match self.sign {
            Some(SignMode::Auto) => out.push_str(",sign=auto"),
            Some(SignMode::PlanetaryComputer) => out.push_str(",sign=planetary_computer"),
            Some(SignMode::None) => out.push_str(",sign=none"),
            None => {}
        }
        out
    }

    /// An error for an option with a value it does not take.
    fn check(&self) -> Result<()> {
        match &self.invalid {
            Some(o) => Err(Error::InvalidArgument(format!("STACIT: invalid option '{o}'"))),
            None => Ok(()),
        }
    }
}

/// Splits `"quoted":rest` or `unquoted:rest` (the rest after the last colon when it looks like
/// `key=value` options or an asset name).
fn split_source(rest: &str, options_have_equals: bool) -> (String, Option<String>) {
    if let Some(q) = rest.strip_prefix('"')
        && let Some(end) = q.find('"')
    {
        let tail = q[end + 1..].strip_prefix(':').map(str::to_string).filter(|s| !s.is_empty());
        return (q[..end].to_string(), tail);
    }
    if let Some((src, tail)) = rest.rsplit_once(':')
        && !tail.contains('/')
        && !tail.is_empty()
        && (!options_have_equals || tail.contains('='))
        && !src.is_empty()
        && !src.ends_with(":/")
        && src.len() > 1
    {
        return (src.to_string(), Some(tail.to_string()));
    }
    (rest.to_string(), None)
}

fn parse_name(path: &str) -> Option<Name> {
    let p = path.trim();
    if let Some(rest) = p.strip_prefix("STACIT:").or_else(|| p.strip_prefix("stacit:")) {
        let (source, tail) = split_source(rest, true);
        let mut filters = Filters::default();
        for kv in tail.iter().flat_map(|t| t.split(',')) {
            let (k, v) = kv.split_once('=').unwrap_or((kv, ""));
            match k.trim().to_ascii_lowercase().as_str() {
                "collection" => filters.collection = Some(v.to_string()),
                "asset" => filters.asset = Some(v.to_string()),
                "crs" => filters.crs = Some(v.to_string()),
                "resolution" => filters.resolution = Some(v.to_ascii_uppercase()),
                "max_items" => filters.max_items = v.parse().ok(),
                "method" if v.eq_ignore_ascii_case("POST") || v.eq_ignore_ascii_case("GET") => {
                    filters.post = v.eq_ignore_ascii_case("POST")
                }
                "sign" if SignMode::parse(v).is_some() => filters.sign = SignMode::parse(v),
                "method" | "sign" => filters.invalid = Some(kv.to_string()),
                _ => {}
            }
        }
        return Some(Name::Mosaic { source, filters });
    }
    if let Some(rest) = p.strip_prefix("STAC:").or_else(|| p.strip_prefix("stac:")) {
        let (item, asset) = split_source(rest, false);
        return Some(Name::Asset { item, asset: asset? });
    }
    // item.json!asset
    let lower = p.to_ascii_lowercase();
    for ext in [".json!", ".geojson!"] {
        if let Some(i) = lower.find(ext) {
            let (item, asset) = (&p[..i + ext.len() - 1], &p[i + ext.len()..]);
            if !asset.is_empty() && !asset.contains('/') {
                return Some(Name::Asset { item: item.to_string(), asset: asset.to_string() });
            }
        }
    }
    None
}

/// The href of an asset of an item, resolved against the item's location.
fn asset_href(item_loc: &str, asset: &str) -> Result<String> {
    let doc = fetch_json(item_loc)?;
    let item = match kind(&doc) {
        Some(Kind::Item) => doc,
        _ => return Err(bad(format!("'{item_loc}' is not a STAC item"))),
    };
    let a = &item["assets"][asset];
    let href = a["href"].as_str().ok_or_else(|| {
        let names: Vec<&String> = item["assets"].as_object().map(|o| o.keys().collect()).unwrap_or_default();
        let names: Vec<&str> = names.iter().map(|s| s.as_str()).collect();
        bad(format!("'{item_loc}' has no asset '{asset}' (assets: {})", names.join(", ")))
    })?;
    sign_href(resolve_href(item_loc, href), &item, item_loc, SignMode::from_env())
}

// ---------------------------------------------------------------------------------------------
// STACIT: assets grouped by collection, asset and CRS

/// An item's asset in a mosaic.
#[derive(Debug, Clone)]
struct AssetItem {
    /// The href, unsigned: a signed one is made when the file is opened, so that its token is
    /// current.
    href: String,
    /// Whether the href is signed before it is opened.
    sign: bool,
    datetime: String,
    width: usize,
    height: usize,
    bounds: [f64; 4],
}

impl AssetItem {
    /// Opens the item's file, its href signed now if it is to be signed.
    fn open(&self) -> Result<LazyRaster> {
        if self.sign { LazyRaster::open(&signed(self.href.clone())?) } else { LazyRaster::open(&self.href) }
    }
}

/// Assets of one asset name, by CRS.
#[derive(Debug, Default)]
struct AssetGroup {
    bands: Value,
    by_crs: BTreeMap<String, Vec<AssetItem>>,
}

type Groups = BTreeMap<String, BTreeMap<String, AssetGroup>>;

/// GDAL's sanitized CRS name for the `crs=` filter: runs of non-alphanumeric characters become
/// one underscore.
fn sanitize_crs(v: &str) -> String {
    let mut out = String::new();
    let mut last_alnum = true;
    for ch in v.chars() {
        if !ch.is_ascii_alphanumeric() {
            if last_alnum {
                out.push('_');
            }
            last_alnum = false;
        } else {
            out.push(ch);
            last_alnum = true;
        }
    }
    if out.ends_with('_') {
        out.pop();
    }
    out
}

/// Groups the assets of items as GDAL's STACIT driver does (`ParseAsset()`).
fn group_assets(items: &[(Value, String)], f: &Filters) -> Groups {
    let mut groups: Groups = BTreeMap::new();
    let sign = f.sign.unwrap_or_else(SignMode::from_env);
    let crs_filter = f.crs.as_deref().map(sanitize_crs);
    for (item, base) in items {
        let has_proj = item["stac_extensions"]
            .as_array()
            .into_iter()
            .flatten()
            .any(|e| e.as_str().is_some_and(|s| s == "proj" || s.starts_with("https://stac-extensions.github.io/projection/")));
        let props = &item["properties"];
        if !has_proj || !props.is_object() || !item["assets"].is_object() {
            continue;
        }
        let collection = match &item["collection"] {
            Value::String(s) => s.clone(),
            Value::Null => String::new(),
            v => v.to_string(),
        };
        if f.collection.as_ref().is_some_and(|c| *c != collection) {
            continue;
        }
        for (name, asset) in item["assets"].as_object().into_iter().flatten() {
            if f.asset.as_ref().is_some_and(|a| a != name) {
                continue;
            }
            let t = asset["type"].as_str().unwrap_or_default();
            if matches!(t, "application/json" | "application/xml" | "text/plain")
                || asset["roles"].as_array().into_iter().flatten().any(|r| matches!(r.as_str(), Some("thumbnail" | "info" | "metadata")))
            {
                continue;
            }
            let Some(href) = asset["href"].as_str().filter(|h| !h.is_empty()) else { continue };
            let get = |k: &str| if asset.get(k).is_some() { &asset[k] } else { &props[k] };
            let crs = if !get("proj:code").is_null() && get("proj:code").as_str().is_some() {
                get("proj:code").as_str().unwrap_or_default().to_string()
            } else if !get("proj:epsg").is_null() {
                // Older items give ESRI codes (54009) there too.
                let code = get("proj:epsg");
                let esri = code.as_u64().and_then(|c| u32::try_from(c).ok()).is_some_and(crate::crs::is_esri_code);
                format!("{}:{code}", if esri { "ESRI" } else { "EPSG" })
            } else if let Some(w) = get("proj:wkt2").as_str() {
                w.to_string()
            } else if get("proj:projjson").is_object() {
                get("proj:projjson").to_string()
            } else {
                continue;
            };
            if crs_filter.as_ref().is_some_and(|c| *c != sanitize_crs(&crs)) {
                continue;
            }
            let nums = |k: &str| -> Option<Vec<f64>> { get(k).as_array().map(|a| a.iter().filter_map(Value::as_f64).collect()) };
            let bbox = nums("proj:bbox").filter(|b| b.len() == 4);
            let shape = nums("proj:shape").filter(|s| s.len() == 2);
            let transform = nums("proj:transform").filter(|t| t.len() == 6 || t.len() == 9);
            if let Some(t) = &transform
                && (t[0] <= 0.0
                    || t[1] != 0.0
                    || t[3] != 0.0
                    || t[4] >= 0.0
                    || (t.len() == 9 && (t[6] != 0.0 || t[7] != 0.0 || t[8] != 1.0)))
            {
                continue;
            }
            let (width, height, bounds) = match (&bbox, &shape, &transform) {
                (Some(b), Some(s), _) => (s[1] as i64, s[0] as i64, [b[0], b[1], b[2], b[3]]),
                (Some(b), None, Some(t)) => {
                    if b[0] != t[2] || b[3] != t[5] {
                        continue;
                    }
                    (((b[2] - b[0]) / t[0]) as i64, ((b[3] - b[1]) / -t[4]) as i64, [b[0], b[1], b[2], b[3]])
                }
                (None, Some(s), Some(t)) => {
                    let (w, h) = (s[1] as i64, s[0] as i64);
                    (w, h, [t[2], t[5] + h as f64 * t[4], t[2] + w as f64 * t[0], t[5]])
                }
                _ => continue,
            };
            if width <= 0 || height <= 0 {
                continue;
            }
            let entry = groups.entry(collection.clone()).or_default().entry(name.clone()).or_insert_with(|| AssetGroup {
                bands: if asset["bands"].is_array() { asset["bands"].clone() } else { asset["eo:bands"].clone() },
                by_crs: BTreeMap::new(),
            });
            let href = resolve_href(base, href);
            entry.by_crs.entry(crs).or_default().push(AssetItem {
                sign: needs_signing(&href, item, base, sign),
                href,
                datetime: props["datetime"].as_str().unwrap_or_default().to_string(),
                width: width as usize,
                height: height as usize,
                bounds,
            });
        }
    }
    groups
}

/// The subdatasets of grouped assets, named and described as GDAL's STACIT driver does.
fn stacit_subdatasets(source: &str, groups: &Groups, kept: &str) -> Vec<Subdataset> {
    let mut out = Vec::new();
    for (collection, assets) in groups {
        for (asset, group) in assets {
            let (arg, text) = if groups.len() > 1 {
                (format!("collection={collection},asset={asset}"), format!("Collection {collection}, Asset {asset}"))
            } else {
                (format!("asset={asset}"), format!("Asset {asset}"))
            };
            if group.by_crs.len() == 1 {
                out.push(Subdataset { name: format!("STACIT:\"{source}\":{arg}{kept}"), description: format!("{text} of {source}") });
            } else {
                for crs in group.by_crs.keys() {
                    out.push(Subdataset {
                        name: format!("STACIT:\"{source}\":{arg},crs={}{kept}", sanitize_crs(crs)),
                        description: format!("{text} of {source} in CRS {crs}"),
                    });
                }
            }
        }
    }
    out
}

/// The CRS of a STAC CRS string (`EPSG:32613`, WKT 2 or PROJJSON).
fn crs_of(s: &str) -> SpatialRef {
    if s.trim_start().starts_with('{')
        && let Ok(v) = serde_json::from_str::<Value>(s)
    {
        if let Some(code) = crate::drivers::vector::projjson::epsg(&v)
            && let Ok(r) = SpatialRef::from_epsg(code)
        {
            return r;
        }
        if let Some(w) = crate::drivers::vector::projjson::to_wkt1(&v) {
            return SpatialRef::from_wkt(&w);
        }
    }
    SpatialRef::parse(s).unwrap_or_default()
}

/// A mosaic of the files of a STACIT subdataset, read by window.
struct Mosaic {
    items: Vec<(AssetItem, Option<LazyRaster>)>,
    geo_transform: [f64; 6],
    data_type: DataType,
    nodata: Option<f64>,
}

impl WindowSource for Mosaic {
    fn overviews(&self) -> Vec<(usize, usize)> {
        Vec::new()
    }

    fn read(&mut self, _level: usize, w: Window, bands: &[usize]) -> Result<RasterData> {
        let (dw, dh) = (w.width, w.height);
        let gt = self.geo_transform;
        let (rx, ry) = (gt[1], -gt[5]);
        let mut out: Vec<Vec<f64>> = vec![vec![self.nodata.unwrap_or(0.0); dw * dh]; bands.len()];
        for (item, raster) in &mut self.items {
            let [xmin, ymin, xmax, ymax] = item.bounds;
            let (dxo, dxs) = ((xmin - gt[0]) / rx, (xmax - xmin) / rx);
            let (dyo, dys) = ((gt[3] - ymax) / ry, (ymax - ymin) / ry);
            let (x0, x1) = (dxo.max(w.x as f64), (dxo + dxs).min((w.x + dw) as f64));
            let (y0, y1) = (dyo.max(w.y as f64), (dyo + dys).min((w.y + dh) as f64));
            if x1 <= x0 || y1 <= y0 {
                continue;
            }
            let (scx, scy) = (item.width as f64 / dxs, item.height as f64 / dys);
            let clamp = |v: f64, max: usize| (v.max(0.0) as usize).min(max);
            let sx0 = clamp(((x0 - dxo) * scx).floor() - 1.0, item.width);
            let sx1 = clamp(((x1 - dxo) * scx).ceil() + 1.0, item.width);
            let sy0 = clamp(((y0 - dyo) * scy).floor() - 1.0, item.height);
            let sy1 = clamp(((y1 - dyo) * scy).ceil() + 1.0, item.height);
            if sx1 <= sx0 || sy1 <= sy0 {
                continue;
            }
            if raster.is_none() {
                *raster = Some(item.open()?);
            }
            let r = raster.as_mut().ok_or_else(|| bad("unreachable"))?;
            let (sw, sh) = (sx1 - sx0, sy1 - sy0);
            if sx1 > r.width() || sy1 > r.height() {
                return Err(bad(format!("{} is {}x{}, smaller than its STAC metadata says", item.href, r.width(), r.height())));
            }
            let (window, options) = (Window::new(sx0, sy0, sw, sh), WindowOptions { bands: bands.to_vec(), overview: None });
            let sub = match r.read_window(window, &options) {
                // A signed URL whose token expired since the file was opened: open it again,
                // signed anew, and read once more.
                Err(Error::Http(_)) if item.sign => {
                    *r = item.open()?;
                    r.read_window(window, &options)?
                }
                result => result?,
            };
            for (bi, buf) in out.iter_mut().enumerate() {
                let values = crate::ops::BandView::of(&sub.raster, bi);
                let valid: Option<Vec<bool>> = self.nodata.map(|nd| values.valid(nd));
                let req = SourceRead {
                    values,
                    valid: valid.as_deref(),
                    width: sw,
                    height: sh,
                    src_type: sub.raster.data_type,
                    src_rect: [-(sx0 as f64), -(sy0 as f64), item.width as f64, item.height as f64],
                    dst_rect: [dxo - w.x as f64, dyo - w.y as f64, dxs, dys],
                    resampling: crate::ops::Resampling::Nearest,
                    working_float: self.nodata.is_some(),
                    invalid_value: None,
                };
                req.read(dw, dh, buf, |v| v);
            }
        }
        Ok(RasterData::from_f64_vec(self.data_type, out.concat()))
    }
}

/// Opens the one mosaic of grouped assets (GDAL's `STACITDataset::SetupDataset()`).
fn open_mosaic(source: &str, groups: Groups, resolution: Option<&str>, kept: &str) -> Result<LazyRaster> {
    let group_count: usize = groups.values().flat_map(|a| a.values()).map(|g| g.by_crs.len()).sum();
    if group_count == 0 {
        return Err(bad(format!("no compatible asset found in '{source}'")));
    }
    if group_count > 1 {
        let subs = stacit_subdatasets(source, &groups, kept);
        let names: Vec<String> = subs.iter().take(8).map(|s| s.name.clone()).collect();
        return Err(Error::InvalidArgument(format!(
            "'{source}' holds {group_count} mosaics (assets or CRSs): choose one, such as {}{}",
            names.join(", "),
            if subs.len() > 8 { ", ..." } else { "" }
        )));
    }
    let (_, assets) = groups.into_iter().next().ok_or_else(|| bad("no collection"))?;
    let (_, group) = assets.into_iter().next().ok_or_else(|| bad("no asset"))?;
    let (crs, mut items) = group.by_crs.into_iter().next().ok_or_else(|| bad("no CRS"))?;
    let (mut xmin, mut ymin, mut xmax, mut ymax) = (f64::MAX, f64::MAX, -f64::MAX, -f64::MAX);
    let (mut rx, mut ry) = (0.0, 0.0);
    let strategy = resolution.unwrap_or("AVERAGE");
    for it in &items {
        xmin = xmin.min(it.bounds[0]);
        ymin = ymin.min(it.bounds[1]);
        xmax = xmax.max(it.bounds[2]);
        ymax = ymax.max(it.bounds[3]);
        let (tx, ty) = ((it.bounds[2] - it.bounds[0]) / it.width as f64, (it.bounds[3] - it.bounds[1]) / it.height as f64);
        if rx != 0.0 && strategy.eq_ignore_ascii_case("HIGHEST") {
            rx = f64::min(rx, tx);
            ry = f64::min(ry, ty);
        } else if rx != 0.0 && strategy.eq_ignore_ascii_case("LOWEST") {
            rx = f64::max(rx, tx);
            ry = f64::max(ry, ty);
        } else {
            rx += tx;
            ry += ty;
        }
    }
    if strategy.eq_ignore_ascii_case("AVERAGE") {
        rx /= items.len() as f64;
        ry /= items.len() as f64;
    }
    let (w, h) = (((xmax - xmin) / rx).round(), ((ymax - ymin) / ry).round());
    if !(rx > 0.0 && ry > 0.0 && w >= 1.0 && h >= 1.0 && w < i32::MAX as f64 && h < i32::MAX as f64) {
        return Err(bad("invalid computed dataset dimensions"));
    }
    let (w, h) = (w as usize, h as usize);
    // The first item tells the band count, data type and nodata value.
    let first_href = items[0].href.clone();
    let first = items[0].open()?;
    let (bands, data_type, nodata) = (first.band_count(), first.data_type(), first.nodata());
    // Ascending datetime: later items are drawn over earlier ones.
    items.sort_by(|a, b| a.datetime.cmp(&b.datetime));
    let srs = crs_of(&crs);
    let mut metadata = Vec::new();
    for (i, b) in group.bands.as_array().into_iter().flatten().enumerate().take(bands) {
        if let Some(name) = b["name"].as_str() {
            metadata.push((format!("BAND_{}_NAME", i + 1), name.to_string()));
        }
    }
    let raster = wbraster::Raster {
        cols: w,
        rows: h,
        bands,
        x_min: xmin,
        y_min: ymax - h as f64 * ry,
        cell_size_x: rx,
        cell_size_y: ry,
        nodata: nodata.unwrap_or(-9999.0),
        data_type,
        crs: srs.to_crs_info(),
        metadata,
        data: RasterData::new_filled(data_type, 0, 0.0),
    };
    let header = RasterDataset::with_driver(raster, RasterDriver::STACIT, nodata.is_some());
    let mut items: Vec<(AssetItem, Option<LazyRaster>)> = items.into_iter().map(|i| (i, None)).collect();
    if let Some(slot) = items.iter_mut().find(|(i, _)| i.href == first_href) {
        slot.1 = Some(first);
    }
    let mosaic = Mosaic { items, geo_transform: [xmin, rx, 0.0, ymax, 0.0, -ry], data_type, nodata };
    Ok(LazyRaster::from_source(header, Box::new(mosaic)))
}

// ---------------------------------------------------------------------------------------------
// Entry points

/// Whether a path may be a STAC document: a `.json` file or URL, or an API endpoint.
fn may_be_stac(path: &str) -> bool {
    let base = path.split(['?', '#']).next().unwrap_or(path).to_ascii_lowercase();
    if base.ends_with(".json") || base.ends_with(".geojson") {
        return true;
    }
    // Zarr stores (`.../collections/.../x.zarr/...`) are not STAC documents.
    if base.split('/').any(|s| s.ends_with(".zarr")) {
        return false;
    }
    is_url(path) && (base.contains("/search") || base.ends_with("/items") || base.contains("/collections/"))
}

/// A STAC document at `path`: `None` when it is not one (having read at most its first bytes
/// for local files). A remote JSON document that is not STAC is opened as GeoJSON.
enum Probe {
    Stac(Value),
    /// An item with tiled assets (see [`crate::stacta`]).
    Stacta(Box<crate::stacta::Item>),
    Other(Box<Dataset>),
}

fn probe(path: &str) -> Option<Result<Probe>> {
    if !may_be_stac(path) {
        return None;
    }
    let loc = crate::vfs::Location::parse(path);
    if let crate::vfs::Location::Local(p) = &loc {
        // Only the first bytes, for files that are not STAC.
        let mut head = vec![0u8; 65536];
        let n = std::fs::File::open(p).and_then(|mut f| std::io::Read::read(&mut f, &mut head)).ok()?;
        if !String::from_utf8_lossy(&head[..n]).contains("\"stac_version\"") {
            return None;
        }
    } else if !matches!(loc, crate::vfs::Location::Url(_) | crate::vfs::Location::S3 { .. }) {
        return None;
    }
    let bytes = match fetch(path) {
        Ok(b) => b,
        Err(e) => return Some(Err(e)),
    };
    match serde_json::from_slice::<Value>(&bytes) {
        Ok(v) if crate::stacta::is_stacta(&v) && kind(&v) == Some(Kind::Item) => {
            crate::stacta::Item::parse(&bytes, path).map(|item| Ok(Probe::Stacta(Box::new(item))))
        }
        Ok(v) if kind(&v).is_some() => Some(Ok(Probe::Stac(v))),
        _ if loc.is_local() => None,
        _ => Some(crate::memory::open_bytes(bytes, &loc.file_name()).map(|ds| Probe::Other(Box::new(ds)))),
    }
}

/// The footprints layer of a STAC document.
fn footprint_dataset(doc: Value, path: &str, max_items: usize) -> Result<(VectorDataset, Groups)> {
    let name = layer_name(&doc, path);
    let items = load_items(doc, path, max_items)?;
    let groups = group_assets(&items.items, &Filters { sign: Some(SignMode::None), ..Filters::default() });
    let layer = footprints(&items.items, &name)?;
    Ok((crate::dataset::finish_vector(vec![layer], path, VectorDriver::GeoJSON), groups))
}

/// Opens a STAC dataset name or document. `None` when `path` is neither.
pub(crate) fn open(path: &str) -> Option<Result<Dataset>> {
    if let Some(r) = crate::stacta::open_named_or_local(path) {
        return Some(r.and_then(|mut r| r.read()).map(Dataset::Raster));
    }
    match parse_name(path) {
        Some(Name::Asset { item, asset }) => return Some(asset_href(&item, &asset).and_then(|href| crate::open(&href))),
        Some(Name::Mosaic { .. }) => return Some(open_lazy(path)?.and_then(|mut r| r.read()).map(Dataset::Raster)),
        None => {}
    }
    Some(match probe(path)? {
        Ok(Probe::Stac(doc)) => footprint_dataset(doc, path, DEFAULT_MAX_ITEMS).map(|(v, _)| Dataset::Vector(v)),
        Ok(Probe::Stacta(item)) => crate::stacta::open_first(*item, None, None).and_then(|mut r| r.read()).map(Dataset::Raster),
        Ok(Probe::Other(ds)) => Ok(*ds),
        Err(e) => Err(e),
    })
}

/// The first request of a `STACIT:` source: a `GET`, or with `method=POST` a `POST` of its query.
fn search_request(source: &str, filters: &Filters) -> Request {
    if filters.post && is_url(source) { post_request(source) } else { Request::get(source) }
}

/// Opens a `STAC:` asset or `STACIT:` mosaic lazily. `None` when `path` is neither.
pub(crate) fn open_lazy(path: &str) -> Option<Result<LazyRaster>> {
    if let Some(r) = crate::stacta::open_lazy(path) {
        return Some(r);
    }
    Some(match parse_name(path)? {
        Name::Asset { item, asset } => asset_href(&item, &asset).and_then(|href| LazyRaster::open(&href)),
        Name::Mosaic { source, filters } => (|| {
            filters.check()?;
            let request = search_request(&source, &filters);
            let doc = request.fetch()?;
            let mut max = filters.max_items.unwrap_or(DEFAULT_MAX_ITEMS);
            if filters.max_items.is_none()
                && let Some(i) = source.to_ascii_lowercase().find("&limit=")
            {
                let digits: String = source[i + 7..].chars().take_while(char::is_ascii_digit).collect();
                max = max.max(digits.parse().unwrap_or(0));
            }
            let items = load_items_from(doc, &request, max)?;
            let groups = group_assets(&items.items, &filters);
            open_mosaic(&source, groups, filters.resolution.as_deref(), &filters.kept_options())
        })(),
    })
}

/// Summarizes a STAC dataset: the footprints of a document with its assets (an item's) or
/// mosaics (a collection's) as subdatasets, an asset, or a mosaic. `None` when `path` is not one.
pub(crate) fn info(path: &str, options: &InfoOptions) -> Option<Result<Info>> {
    if let Some(r) = crate::stacta::info(path, options) {
        return Some(r);
    }
    match parse_name(path) {
        Some(Name::Asset { item, asset }) => {
            return Some(asset_href(&item, &asset).and_then(|href| crate::info::info_path(&href, options)));
        }
        Some(Name::Mosaic { source, filters }) => {
            let several = (|| -> Result<Option<Info>> {
                filters.check()?;
                let request = search_request(&source, &filters);
                let doc = request.fetch()?;
                let max = filters.max_items.unwrap_or(DEFAULT_MAX_ITEMS);
                let name = layer_name(&doc, &source);
                let items = load_items_from(doc, &request, max)?;
                let groups = group_assets(&items.items, &Filters { sign: Some(SignMode::None), ..filters.clone() });
                let count: usize = groups.values().flat_map(|a| a.values()).map(|g| g.by_crs.len()).sum();
                if count <= 1 {
                    return Ok(None);
                }
                let layer = footprints(&items.items, &name)?;
                let ds = crate::dataset::finish_vector(vec![layer], &source, VectorDriver::GeoJSON);
                let mut v = crate::info::vector_info(&ds);
                v.subdatasets = stacit_subdatasets(&source, &groups, &filters.kept_options());
                Ok(Some(Info::Vector(v)))
            })();
            return Some(match several {
                Ok(Some(i)) => Ok(i),
                Ok(None) => {
                    open_lazy(path)?.and_then(|mut r| crate::info::lazy_raster_info(&mut r, options)).map(|r| Info::Raster(Box::new(r)))
                }
                Err(e) => Err(e),
            });
        }
        None => {}
    }
    let doc = match probe(path)? {
        Ok(Probe::Stac(doc)) => doc,
        Ok(Probe::Stacta(item)) => return Some(crate::stacta::info_item(*item, None, None, options)),
        Ok(Probe::Other(ds)) => return Some(Ok(crate::info::info(&ds, options))),
        Err(e) => return Some(Err(e)),
    };
    let is_item = kind(&doc) == Some(Kind::Item);
    let assets: Vec<(String, Value)> =
        if is_item { doc["assets"].as_object().into_iter().flatten().map(|(k, v)| (k.clone(), v.clone())).collect() } else { Vec::new() };
    Some(footprint_dataset(doc, path, DEFAULT_MAX_ITEMS).map(|(ds, groups)| {
        let mut v = crate::info::vector_info(&ds);
        v.subdatasets = if is_item {
            assets
                .iter()
                .filter(|(_, a)| is_data_asset(a))
                .map(|(k, a)| Subdataset {
                    name: format!("STAC:\"{path}\":{k}"),
                    description: match a["title"].as_str() {
                        Some(t) => format!("Asset {k}: {t}"),
                        None => format!("Asset {k}"),
                    },
                })
                .collect()
        } else {
            stacit_subdatasets(path, &groups, "")
        };
        Info::Vector(v)
    }))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_names() {
        assert_eq!(
            parse_name(r#"STACIT:"https://x.org/search?a=1&b=2":asset=red,crs=EPSG_32613"#),
            Some(Name::Mosaic {
                source: "https://x.org/search?a=1&b=2".into(),
                filters: Filters { asset: Some("red".into()), crs: Some("EPSG_32613".into()), ..Default::default() }
            })
        );
        assert_eq!(
            parse_name("STACIT:items.json:asset=red"),
            Some(Name::Mosaic { source: "items.json".into(), filters: Filters { asset: Some("red".into()), ..Default::default() } })
        );
        assert_eq!(
            parse_name("STACIT:https://x.org/items.json"),
            Some(Name::Mosaic { source: "https://x.org/items.json".into(), filters: Filters::default() })
        );
        assert_eq!(parse_name(r#"STAC:"C:\d\item.json":B04"#), Some(Name::Asset { item: r"C:\d\item.json".into(), asset: "B04".into() }));
        assert_eq!(
            parse_name("STAC:https://x.org/item.json:B04"),
            Some(Name::Asset { item: "https://x.org/item.json".into(), asset: "B04".into() })
        );
        assert_eq!(parse_name("dir/item.json!visual"), Some(Name::Asset { item: "dir/item.json".into(), asset: "visual".into() }));
        assert_eq!(parse_name("dir/item.json"), None);
        assert_eq!(parse_name("a.zip!b.json"), None);
    }

    #[test]
    fn resolves_hrefs() {
        assert_eq!(resolve_href("https://x.org/a/b/item.json?q=1", "./B04.tif"), "https://x.org/a/b/B04.tif");
        assert_eq!(resolve_href("https://x.org/a/b/item.json", "../c/B04.tif"), "https://x.org/a/c/B04.tif");
        assert_eq!(resolve_href("https://x.org/a/item.json", "/c/B04.tif"), "https://x.org/c/B04.tif");
        assert_eq!(resolve_href("dir/item.json", "B04.tif"), "dir/B04.tif");
        assert_eq!(resolve_href("item.json", "B04.tif"), "B04.tif");
        assert_eq!(resolve_href("/data/item.json", "s3://b/k.tif"), "s3://b/k.tif");
    }

    #[test]
    fn builds_post_searches_as_pystac_client() {
        let r = post_request(
            "https://x.org/v1/search?collections=a,b&bbox=-105.3,39.9,-105.1,40.1&limit=2&datetime=2023-07&sortby=-datetime&query=%7B%22eo%3Acloud_cover%22%3A%7B%22lt%22%3A10%7D%7D&filter=cloud%20%3C%2010",
        );
        assert_eq!(r.url, "https://x.org/v1/search");
        assert_eq!(
            r.body,
            Some(json!({
                "collections": ["a", "b"],
                "bbox": [-105.3, 39.9, -105.1, 40.1],
                "limit": 2,
                "datetime": "2023-07-01T00:00:00Z/2023-07-31T23:59:59Z",
                "sortby": [{"field": "datetime", "direction": "desc"}],
                "query": {"eo:cloud_cover": {"lt": 10}},
                "filter": "cloud < 10",
                "filter-lang": "cql2-text",
            }))
        );
        assert_eq!(rfc3339_interval("2024-02"), "2024-02-01T00:00:00Z/2024-02-29T23:59:59Z");
        assert_eq!(rfc3339_interval("2023-07-05"), "2023-07-05T00:00:00Z/2023-07-05T23:59:59Z");
        assert_eq!(rfc3339_interval("2023/.."), "2023-01-01T00:00:00Z/..");
        assert_eq!(rfc3339_interval("2023-07-05T10:00:00Z"), "2023-07-05T10:00:00Z");
    }

    #[test]
    fn follows_post_next_links() {
        let cur =
            Request { url: "https://x.org/search".into(), body: Some(json!({"limit": 2, "bbox": [0, 0, 1, 1]})), headers: Vec::new() };
        // Earth Search: the whole body again.
        let page = json!({"links": [{"rel": "next", "method": "POST", "href": "https://x.org/search", "merge": false, "body": {"limit": 2, "next": "t"}}]});
        assert_eq!(next_request(&page, &cur).and_then(|r| r.body), Some(json!({"limit": 2, "next": "t"})));
        // Merged into the previous body, with headers.
        let page = json!({"links": [{"rel": "next", "method": "POST", "href": "/search", "merge": true, "body": {"token": "t"}, "headers": {"X-A": "1"}}]});
        let next = next_request(&page, &cur).expect("a next page");
        assert_eq!(next.body, Some(json!({"limit": 2, "bbox": [0, 0, 1, 1], "token": "t"})));
        assert_eq!(next.headers, vec![("X-A".to_string(), "1".to_string())]);
        // A GET link is preferred.
        let page = json!({"links": [{"rel": "next", "method": "POST", "href": "/a"}, {"rel": "next", "href": "/b?page=2"}]});
        assert_eq!(next_request(&page, &cur).map(|r| (r.url, r.body)), Some(("https://x.org/b?page=2".into(), None)));
        assert_eq!(next_request(&json!({"links": []}), &cur), None);
        // Headers go on to pages of the same origin only.
        let keyed = Request { headers: vec![("X-Key".into(), "k".into())], ..cur.clone() };
        let page = json!({"links": [{"rel": "next", "href": "https://x.org/search?page=2"}]});
        assert_eq!(next_request(&page, &keyed).map(|r| r.headers), Some(keyed.headers.clone()));
        let page = json!({"links": [{"rel": "next", "href": "https://elsewhere.org/search?page=2"}]});
        assert_eq!(next_request(&page, &keyed).map(|r| r.headers), Some(Vec::new()));
        // A header of several values: one per value.
        let page = json!({"links": [{"rel": "next", "href": "/search?page=2", "headers": {"X-Cursor": ["a", "b"]}}]});
        assert_eq!(
            next_request(&page, &cur).map(|r| r.headers),
            Some(vec![("X-Cursor".to_string(), "a".to_string()), ("X-Cursor".to_string(), "b".to_string())])
        );
        // `merge`: the link's headers over the previous ones.
        let page =
            json!({"links": [{"rel": "next", "method": "POST", "href": "/search", "merge": true, "headers": {"Search-After": "t"}}]});
        assert_eq!(
            next_request(&page, &keyed).map(|r| r.headers),
            Some(vec![("X-Key".to_string(), "k".to_string()), ("Search-After".to_string(), "t".to_string())])
        );
    }

    #[test]
    fn signs_planetary_computer_assets_only() {
        let href = "https://sentinel2l2a01.blob.core.windows.net/sentinel2-l2/13/T/B04.tif";
        assert_eq!(blob_container(href), Some(("sentinel2l2a01", "sentinel2-l2")));
        assert_eq!(blob_container("https://example.com/a/b.tif"), None);
        assert_eq!(blob_container("https://evil.example#@a.blob.core.windows.net/c/x.tif"), None);
        assert_eq!(blob_container("https://u:p@a.blob.core.windows.net/c/x.tif"), None);
        let pc = "https://planetarycomputer.microsoft.com/api/stac/v1/search?collections=x";
        assert!(from_planetary_computer(&json!({}), pc));
        assert!(!from_planetary_computer(&json!({}), "https://earth-search.aws.element84.com/v1/search"));
        let linked =
            json!({"links": [{"rel": "self", "href": "https://planetarycomputer.microsoft.com/api/stac/v1/collections/x/items/y"}]});
        assert!(from_planetary_computer(&linked, "items.json"));
        // Not signed: not Planetary Computer's, not Azure, already signed, or signing off.
        let other = "https://earth-search.aws.element84.com/v1/search";
        assert_eq!(sign_href(href.into(), &json!({}), other, SignMode::Auto).ok().as_deref(), Some(href));
        assert_eq!(
            sign_href("https://e.com/a.tif".into(), &json!({}), pc, SignMode::PlanetaryComputer).ok().as_deref(),
            Some("https://e.com/a.tif")
        );
        let signed = format!("{href}?st=1&sig=abc");
        assert_eq!(sign_href(signed.clone(), &json!({}), pc, SignMode::Auto).ok(), Some(signed));
        assert_eq!(sign_href(href.into(), &json!({}), pc, SignMode::None).ok().as_deref(), Some(href));
        assert_eq!(SignMode::parse("planetary-computer"), Some(SignMode::PlanetaryComputer));
        assert_eq!(SignMode::parse("planetarycomputer"), Some(SignMode::PlanetaryComputer));
        let Some(Name::Mosaic { filters, .. }) = parse_name(r#"STACIT:"https://x.org/search":asset=a,sign=nope"#) else { panic!() };
        assert!(filters.check().is_err());
        let Some(Name::Mosaic { filters, .. }) = parse_name(r#"STACIT:"https://x.org/search":asset=a,method=POST,sign=none"#) else {
            panic!()
        };
        assert!(filters.check().is_ok());
        assert_eq!(filters.kept_options(), ",method=POST,sign=none");
    }

    #[cfg(remote)]
    #[test]
    fn counts_seconds_to_expiry() {
        let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs();
        // 2000-01-01T00:00:00Z is 946684800: long past.
        assert_eq!(seconds_until("2000-01-01T00:00:00Z"), None);
        // 2100-03-01T12:30:15Z is 4107587415 (a second of slack for the clock ticking).
        let s = seconds_until("2100-03-01T12:30:15Z").expect("a future time");
        assert!((4_107_587_415 - now).abs_diff(s) <= 1, "{s}");
        assert_eq!(seconds_until("not a time"), None);
    }

    #[test]
    fn sanitizes_crs_like_gdal() {
        assert_eq!(sanitize_crs("EPSG:32613"), "EPSG_32613");
        assert_eq!(sanitize_crs("{\"a\": 1}"), "_a_1");
    }
}