Skip to content

Commit 32131e6

Browse files
fix: Skip unknown GeoTag keys (#134)
* Add failing test for unknown tag * Lint * Add support for tag * Add bindings * Fix typo * Add docstring * Add tiny test * Ignore unknown GeoKeyTag ids * infer type in tests * fix python compile * fmt * restore whitespaces * Ensure geo and ome tiff tests are run * fmt --------- Co-authored-by: Max Jones <[email protected]>
1 parent bdfda31 commit 32131e6

File tree

9 files changed

+99
-29
lines changed

9 files changed

+99
-29
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ Download the following file for use in the tests.
2424

2525
```shell
2626
aws s3 cp s3://naip-visualization/ny/2022/60cm/rgb/40073/m_4007307_sw_18_060_20220803.tif ./ --request-payer
27+
aws s3 cp s3://prd-tnm/StagedProducts/Elevation/13/TIFF/current/s14w171/USGS_13_s14w171.tif ./ --no-sign-request --region us-west-2
2728
```

python/DEVELOP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
uv sync --no-install-package async-tiff
33
uv run --no-project maturin develop
44
uv run --no-project mkdocs serve
5+
uv run --no-project pytest --verbose
56
```

src/ifd.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,15 @@ impl ImageFileDirectory {
295295
.expect("There should be a chunk for each key.");
296296

297297
let key_id = chunk[0];
298-
let tag_name =
299-
GeoKeyTag::try_from_primitive(key_id).expect("Unknown GeoKeyTag id: {key_id}");
298+
let tag_name = if let Ok(tag_name) = GeoKeyTag::try_from_primitive(key_id) {
299+
tag_name
300+
} else {
301+
// Skip unknown GeoKeyTag ids. Some GeoTIFFs include keys that were proposed
302+
// but not included in the GeoTIFF spec. See
303+
// https://github.com/developmentseed/async-tiff/pull/131 and
304+
// https://github.com/virtual-zarr/virtual-tiff/issues/52
305+
continue;
306+
};
300307

301308
let tag_location = chunk[1];
302309
let count = chunk[2];

tests/geo.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use std::env;
2+
use std::sync::Arc;
3+
4+
use async_tiff::metadata::{PrefetchBuffer, TiffMetadataReader};
5+
use async_tiff::reader::{AsyncFileReader, ObjectReader};
6+
use object_store::local::LocalFileSystem;
7+
8+
#[tokio::test]
9+
async fn test_parse_file_with_unknown_geokey() {
10+
let folder = env::current_dir().unwrap();
11+
let path = object_store::path::Path::parse("tests/images/geogtowgs_subset_USGS_13_s14w171.tif")
12+
.unwrap();
13+
let store = Arc::new(LocalFileSystem::new_with_prefix(folder).unwrap());
14+
let reader = Arc::new(ObjectReader::new(store, path)) as Arc<dyn AsyncFileReader>;
15+
let prefetch_reader = PrefetchBuffer::new(reader.clone(), 32 * 1024)
16+
.await
17+
.unwrap();
18+
let mut metadata_reader = TiffMetadataReader::try_open(&prefetch_reader)
19+
.await
20+
.unwrap();
21+
let _ = metadata_reader
22+
.read_all_ifds(&prefetch_reader)
23+
.await
24+
.unwrap();
25+
}
317 Bytes
Binary file not shown.

tests/images/readme.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
`geogtowgs_subset_USGS_13_s14w171.tif` was created from "s3://prd-tnm/StagedProducts/Elevation/13/TIFF/current/s14w171/USGS_13_s14w171.tif" using these commands:
2+
3+
```bash
4+
gdal_translate USGS_13_s14w171.tif tiny.tif -srcwin 0 0 1 1 -co COMPRESS=DEFLATE
5+
listgeo USGS_13_s14w171.tif > metadata.txt # Then modify to remove information related to spatial extent
6+
cp tiny.tif geogtowgs_subset_USGS_13_s14w171.tif
7+
geotifcp -g metadata.txt tiny.tif geogtowgs_subset_USGS_13_s14w171.tif
8+
listgeo geogtowgs_subset_USGS_13_s14w171.tif
9+
```
10+
11+
and this workspace definition:
12+
13+
```toml
14+
[project]
15+
name = "gdal-workspace"
16+
version = "0.1.0"
17+
description = "workspace for using gdal via pixi"
18+
readme = "README.md"
19+
requires-python = ">=3.12"
20+
dependencies = []
21+
22+
[tool.pixi.workspace]
23+
channels = ["conda-forge"]
24+
platforms = ["osx-arm64"]
25+
26+
[tool.pixi.pypi-dependencies]
27+
gdal-workspace = { path = ".", editable = true }
28+
29+
[tool.pixi.tasks]
30+
31+
[tool.pixi.dependencies]
32+
gdal = ">=3.11.5,<4"
33+
libgdal = ">=3.11.5,<4"
34+
geotiff = ">=1.7.4,<2"
35+
```

tests/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
mod geo;
12
mod image_tiff;
3+
mod ome_tiff;

tests/ome_tiff.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
1-
/// Integration tests on OME-TIFF files.
1+
//! Integration tests on OME-TIFF files.
2+
3+
use std::sync::Arc;
4+
5+
use async_tiff::metadata::{PrefetchBuffer, TiffMetadataReader};
6+
use async_tiff::reader::{AsyncFileReader, ObjectReader};
27
use async_tiff::tiff::tags::PhotometricInterpretation;
8+
use async_tiff::TIFF;
9+
use reqwest::Url;
310

4-
mod util;
11+
async fn open_remote_tiff(url: &str) -> TIFF {
12+
let parsed_url = Url::parse(url).expect("failed parsing url");
13+
let (store, path) = object_store::parse_url(&parsed_url).unwrap();
14+
15+
let reader = Arc::new(ObjectReader::new(Arc::new(store), path)) as Arc<dyn AsyncFileReader>;
16+
let prefetch_reader = PrefetchBuffer::new(reader.clone(), 32 * 1024)
17+
.await
18+
.unwrap();
19+
let mut metadata_reader = TiffMetadataReader::try_open(&prefetch_reader)
20+
.await
21+
.unwrap();
22+
let ifds = metadata_reader
23+
.read_all_ifds(&prefetch_reader)
24+
.await
25+
.unwrap();
26+
TIFF::new(ifds)
27+
}
528

629
#[tokio::test]
730
async fn test_ome_tiff_single_channel() {
831
let tiff =
9-
util::open_remote_tiff("https://downloads.openmicroscopy.org/images/OME-TIFF/2016-06/bioformats-artificial/single-channel.ome.tif").await;
32+
open_remote_tiff("https://downloads.openmicroscopy.org/images/OME-TIFF/2016-06/bioformats-artificial/single-channel.ome.tif").await;
1033

1134
assert_eq!(tiff.ifds().len(), 1);
1235
let ifd = &tiff.ifds()[0];

tests/util/mod.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)