use std::fs::File;
use std::path::Path;
use tiff::decoder::Decoder;
use tiff::tags::Tag;
use crate::tiles::cog::CogError;
#[derive(Clone, Debug)]
pub struct ModelInfo {
pub pixel_scale: Option<Vec<f64>>,
pub tie_points: Option<Vec<f64>>,
pub transformation: Option<Vec<f64>>,
pub projected_crs: Option<u16>,
}
impl ModelInfo {
pub fn decode(decoder: &mut Decoder<File>, path: &Path) -> Self {
let pixel_scale = decoder
.get_tag_f64_vec(Tag::ModelPixelScaleTag)
.map_err(|e| {
CogError::TagsNotFound(
e,
vec![Tag::ModelPixelScaleTag.to_u16()],
0,
path.to_path_buf(),
)
})
.ok();
let tie_points = decoder
.get_tag_f64_vec(Tag::ModelTiepointTag)
.map_err(|e| {
CogError::TagsNotFound(
e,
vec![Tag::ModelTiepointTag.to_u16()],
0,
path.to_path_buf(),
)
})
.ok();
let transformation = decoder
.get_tag_f64_vec(Tag::ModelTransformationTag)
.map_err(|e| {
CogError::TagsNotFound(
e,
vec![Tag::ModelTransformationTag.to_u16()],
0,
path.to_path_buf(),
)
})
.ok();
let projected_crs = decoder
.get_tag_u16_vec(Tag::GeoKeyDirectoryTag)
.ok()
.and_then(|geokeys| {
let mut chunks = geokeys.chunks_exact(4);
match chunks.next()? {
[1, 1, 0, n_keys] if *n_keys > 0 => {}
_ => return None,
}
chunks.find_map(|chunk| match chunk {
[3072, _, _, value] => Some(*value),
_ => None,
})
});
Self {
pixel_scale,
tie_points,
transformation,
projected_crs,
}
}
}