Skip to content

Commit 87ba239

Browse files
committed
add get_extent method
1 parent 139337e commit 87ba239

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

martin/src/cog/errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,7 @@ pub enum CogError {
5151

5252
#[error("Get full resolution failed from file: {0}")]
5353
GetFullResolutionFailed(PathBuf),
54+
55+
#[error("Get extent failed from file: {0}")]
56+
GetExtentFailed(PathBuf),
5457
}

martin/src/cog/source.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,51 @@ fn get_meta(path: &PathBuf) -> Result<Meta, FileError> {
376376
})
377377
}
378378

379+
fn get_extent(
380+
transformation: Option<&[f64]>,
381+
origin: Option<&[f64]>,
382+
full_resolution: &[f64],
383+
(full_width_pixel, full_height_pixel): (u32, u32),
384+
path: &Path,
385+
) -> Result<[f64; 4], CogError> {
386+
if let Some(matrix) = transformation {
387+
let corners = [
388+
[0, 0],
389+
[0, full_height_pixel],
390+
[full_width_pixel, 0],
391+
[full_width_pixel, full_height_pixel],
392+
];
393+
let transed = corners.map(|pixel| {
394+
let i = f64::from(pixel[0]);
395+
let j = f64::from(pixel[1]);
396+
let x = matrix[3] + (matrix[0] * i) + (matrix[1] * j);
397+
let y = matrix[7] + (matrix[4] * i) + (matrix[5] * j);
398+
(x, y)
399+
});
400+
let mut min_x = transed[0].0;
401+
let mut min_y = transed[1].1;
402+
let mut max_x = transed[0].0;
403+
let mut max_y = transed[1].1;
404+
for (x, y) in transed {
405+
if x <= min_x {
406+
min_x = x;
407+
}
408+
if y <= min_y {
409+
min_y = y;
410+
}
411+
if x >= max_x {
412+
max_x = x;
413+
}
414+
if y >= max_y {
415+
max_y = y;
416+
}
417+
}
418+
return Ok([min_x, min_y, max_x, max_y]);
419+
} else {
420+
return Err(CogError::GetExtentFailed(path.to_path_buf()));
421+
}
422+
}
423+
379424
fn get_full_resolution(
380425
pixel_scale: Option<&[f64]>,
381426
transformation: Option<&[f64]>,

0 commit comments

Comments
 (0)