srtm_reader/
resolutions.rs

1/// this many rows and columns are there in a standard SRTM1 file
2const EXTENT: usize = 3600;
3
4/// the available resulutions of the SRTM data, in arc seconds
5#[derive(PartialEq, Eq, Clone, Copy, PartialOrd, Ord, Debug, Default)]
6pub enum Resolution {
7    SRTM05,
8    #[default]
9    SRTM1,
10    SRTM3,
11}
12
13impl Resolution {
14    /// the number of rows and columns in an SRTM data file of [`Resolution`]
15    pub const fn extent(&self) -> usize {
16        1 + match self {
17            Resolution::SRTM05 => EXTENT * 2,
18            Resolution::SRTM1 => EXTENT,
19            Resolution::SRTM3 => EXTENT / 3,
20        }
21    }
22    /// total file length in BigEndian, total file length in bytes is [`Resolution::total_len()`] * 2
23    pub const fn total_len(&self) -> usize {
24        self.extent().pow(2)
25    }
26}
27
28impl TryFrom<u64> for Resolution {
29    type Error = ();
30
31    fn try_from(len: u64) -> Result<Self, Self::Error> {
32        let len = usize::try_from(len).map_err(|_| ())?;
33        if len == Resolution::SRTM05.total_len() * 2 {
34            Ok(Resolution::SRTM05)
35        } else if len == Resolution::SRTM1.total_len() * 2 {
36            Ok(Resolution::SRTM1)
37        } else if len == Resolution::SRTM3.total_len() * 2 {
38            Ok(Resolution::SRTM3)
39        } else {
40            eprintln!("unknown filesize: {len}");
41            Err(())
42        }
43    }
44}