Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to tiff crate #17

Merged
merged 2 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 17 additions & 97 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ authors = ["Dominik Bucher <[email protected]>"]
repository = "https://github.com/georust/geotiff"

[dependencies]
byteorder = "*"
enum_primitive = "*"
num = "*"
num-traits = "0.2"
tiff = "0.9"
56 changes: 0 additions & 56 deletions src/geotiff.rs

This file was deleted.

119 changes: 89 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,99 @@
extern crate byteorder;
#[macro_use]
extern crate enum_primitive;
extern crate num;
use std::any::type_name;
use std::io::{Read, Seek};

use std::fmt;
use std::io::Result;
use num_traits::FromPrimitive;
use tiff::decoder::{Decoder, DecodingResult};
use tiff::tags::Tag;
use tiff::TiffResult;

pub mod geotiff;
mod lowlevel;
mod reader;
use crate::raster_data::*;

pub use geotiff::TIFF;
use reader::*;
mod raster_data;

/// The GeoTIFF library reads `.tiff` files.
macro_rules! unwrap_primitive_type {
($result: expr, $actual: ty, $expected: ty) => {
$result
.ok_or_else(|| {
format!(
"Cannot represent {} as {}",
type_name::<$actual>(),
type_name::<$expected>()
)
})
.unwrap()
gschulze marked this conversation as resolved.
Show resolved Hide resolved
};
}

/// The basic GeoTIFF struct. This includes any metadata as well as the actual raster data.
///
/// It is primarily used within a routing application that needs to parse digital elevation models.
/// As such, other use cases are NOT tested (for now).
impl TIFF {
/// Opens a `.tiff` file at the location indicated by `filename`.
pub fn open(filename: &str) -> Result<Box<TIFF>> {
let tiff_reader = TIFFReader;
tiff_reader.load(filename)
}
/// The raster data has a size of raster_width * raster_height * num_samples
#[derive(Debug)]
pub struct GeoTiff {
pub raster_width: usize,
pub raster_height: usize,
pub num_samples: usize,
gschulze marked this conversation as resolved.
Show resolved Hide resolved
raster_data: RasterData,
}

impl GeoTiff {
pub fn read<R: Read + Seek>(reader: R) -> TiffResult<Self> {
let mut decoder = Decoder::new(reader)?;

/// Gets the value at a given coordinate (in pixels).
pub fn get_value_at(&self, lon: usize, lat: usize) -> usize {
self.image_data[lon][lat][0]
let (raster_width, raster_height) = decoder
.dimensions()
.map(|(width, height)| (width as usize, height as usize))?;
let num_samples = match decoder.find_tag(Tag::SamplesPerPixel)? {
None => 1,
Some(value) => value.into_u16()? as usize,
};
let raster_data = match decoder.read_image()? {
DecodingResult::U8(data) => RasterData::U8(data),
DecodingResult::U16(data) => RasterData::U16(data),
DecodingResult::U32(data) => RasterData::U32(data),
DecodingResult::U64(data) => RasterData::U64(data),
DecodingResult::F32(data) => RasterData::F32(data),
DecodingResult::F64(data) => RasterData::F64(data),
DecodingResult::I8(data) => RasterData::I8(data),
DecodingResult::I16(data) => RasterData::I16(data),
DecodingResult::I32(data) => RasterData::I32(data),
DecodingResult::I64(data) => RasterData::I64(data),
};

Ok(Self {
raster_width,
raster_height,
num_samples,
raster_data,
})
}
}

/// Overwrite default display function.
impl fmt::Display for TIFF {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TIFF(Image size: [{}, {}, {}], Tag data: {:?})",
self.image_data.len(), self.image_data[0].len(),
self.image_data[0][0].len(), self.ifds)
pub fn get_value_at<T: FromPrimitive + 'static>(&self, x: usize, y: usize, sample: usize) -> T {
let GeoTiff {
raster_width,
num_samples,
raster_data,
..
} = self;

if &sample >= num_samples {
panic!(
"sample out of bounds: the number of samples is {} but the sample is {}",
num_samples, sample
)
}

let index = (y * raster_width + x) * num_samples + sample;
match raster_data {
RasterData::U8(data) => unwrap_primitive_type!(T::from_u8(data[index]), u8, T),
RasterData::U16(data) => unwrap_primitive_type!(T::from_u16(data[index]), u16, T),
RasterData::U32(data) => unwrap_primitive_type!(T::from_u32(data[index]), u32, T),
RasterData::U64(data) => unwrap_primitive_type!(T::from_u64(data[index]), u64, T),
RasterData::F32(data) => unwrap_primitive_type!(T::from_f32(data[index]), f32, T),
RasterData::F64(data) => unwrap_primitive_type!(T::from_f64(data[index]), f64, T),
RasterData::I8(data) => unwrap_primitive_type!(T::from_i8(data[index]), i8, T),
RasterData::I16(data) => unwrap_primitive_type!(T::from_i16(data[index]), i16, T),
RasterData::I32(data) => unwrap_primitive_type!(T::from_i32(data[index]), i32, T),
RasterData::I64(data) => unwrap_primitive_type!(T::from_i64(data[index]), i64, T),
}
}
}
Loading