1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use std::f64::consts;
use ParseError;
use coord::Coord;

/// 
/// Holds attributes for Universal Transverse Mercator (UTM) coordinate system
///
/// # Examples
/// ```
/// use std::error::Error;
///
/// extern crate geomorph;
///
/// fn try_main() -> Result<geomorph::utm::Utm, geomorph::ParseError> {
///     let lat: f64 = -23.0095839;
///     let lon: f64 = -43.4361816;
///
///     let coord = geomorph::coord::Coord::new(lat, lon)?;
///     geomorph::utm::Utm::new(coord)
/// }
///
/// fn main() {
///     let utm = try_main().unwrap();
/// }
/// ```
///
pub struct Utm {
    pub easting: f64,
    pub northing: f64,
    pub north: bool,
    pub zone: i32,
    pub band: char,
}

impl Utm {
    ///
    /// Return a new Utm instance.
    ///
    /// Inspired on the work of Rafael Palacios.
    ///
    pub fn new(coord: Coord) -> Result<Utm, ParseError> {
        let lat = coord.lat;
        let lon = coord.lon;

        let north: bool = lat >= 0.0;
        let band: char;
        if lat < -72.0 {band = 'C';}
        else if lat < -64.0 {band = 'D';}
        else if lat < -56.0 {band = 'E';}
        else if lat < -48.0 {band = 'F';}
        else if lat < -40.0 {band = 'G';}
        else if lat < -32.0 {band = 'H';}
        else if lat < -24.0 {band = 'J';}
        else if lat < -16.0 {band = 'K';}
        else if lat < -8.0 {band = 'L';}
        else if lat < 0.0 {band = 'M';}
        else if lat < 8.0 {band = 'N';}
        else if lat < 16.0 {band = 'P';}
        else if lat < 24.0 {band = 'Q';}
        else if lat < 32.0 {band = 'R';}
        else if lat < 40.0 {band = 'S';}
        else if lat < 48.0 {band = 'T';}
        else if lat < 56.0 {band = 'U';}
        else if lat < 64.0 {band = 'V';}
        else if lat < 72.0 {band = 'W';}
        else {band = 'X';}
        
        let sa: f64 = 6378137.0;
        let sb: f64 = 6356752.314245;
        let k_0: f64 = 0.9996;
        let e_0: f64 = 500000.0;
        let n_0: f64 = 10000000.0;
        
        let e2: f64 = (sa.powi(2) - sb.powi(2)).powf(0.5) / sb;
        let e2_2: f64 = e2.powi(2);
        let c: f64 = sa.powi(2) / sb;
        
        let rlat: f64 = lat.to_radians();
        let rlon: f64 = lon.to_radians();
        
        let mut zone: i32 = ((lon / 6.0).floor() as i32) + 31;
        
        //Treat zone exceptions
        {
            let fmod_lon = lon - 360.0 * (lon / 360.0).trunc();
            let floor_lon = fmod_lon.floor();
            let ilon: f64;
            if floor_lon >= 180.0 {ilon = floor_lon - 360.0;}
            else if floor_lon < -180.0 {ilon = floor_lon + 360.0;}
            else {ilon = floor_lon;}

            let except_band = 
                ((lat.floor() + 80.0) / 8.0 - 10.0)
                .min(9.0)
                .max(-10.0)
                .trunc();
            if except_band == 7.0 && zone == 31 && ilon >= 3.0 {
                zone = 32;    // Norway UTM exception
            } else if except_band == 9.0 && ilon >= 0.0 && ilon < 42.0 {
                // Svalbard UTM exception
                zone =
                    (2.0 * ((ilon + 183.0) / 12.0).trunc() + 1.0) as i32;
            }
        }

        let delta_s: f64 =
            rlon - consts::PI * ((zone * 6 - 183) as f64) / 180.0;
        
        let a: f64 = rlat.cos() * delta_s.sin();
        let epsilon: f64 = 0.5 * ((1.0 + a) / (1.0 - a)).ln();
        let nu: f64 = (rlat.tan() / delta_s.cos()).atan() - rlat;
        
        let v: f64 =
            c * k_0 / (1.0 + e2_2 * rlat.cos().powi(2)).sqrt();
        let ta: f64 =
            e2_2 * epsilon.powi(2) * rlat.cos().powi(2) / 2.0;
        let a1: f64 = (2.0 * rlat).sin();
        let a2: f64 = a1 * rlat.cos().powi(2);
        let j2: f64 = rlat + a1 / 2.0;
        let j4: f64 = (3.0 * j2 + a2) / 4.0;
        let j6: f64 = (5.0 * j4 + a2 * rlat.cos().powi(2)) / 3.0;
        let alfa: f64 = 3.0 * e2_2 / 4.0;
        let beta: f64 = 5.0 * alfa.powi(2) / 3.0;
        let gama: f64 = 35.0 * alfa.powi(3) / 27.0;
        let bm: f64 =
            k_0 * c * (rlat - alfa * j2 + beta * j4 - gama * j6);
        let easting: f64 = epsilon * v * (1.0 + ta / 3.0) + e_0;
        let northing: f64;
        if north {
            northing = nu * v * (1.0 + ta) + bm;
        } else {
            northing = nu * v * (1.0 + ta) + bm + n_0;
        }

        Ok(Utm {
            easting,
            northing,
            north,
            zone,
            band,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn utm_zone_south() {
        let coord = Coord {lat: -23.0095839, lon: -43.4361816};
        let utm = Utm::new(coord).unwrap();
        assert!((utm.easting - 660265.0944068021).abs() < 1.0);
        assert!((utm.northing - 7454564.243324452).abs() < 1.0);
        assert_eq!(utm.north, false);
        assert_eq!(utm.zone, 23);
        assert_eq!(utm.band, 'K');
    }

    #[test]
    fn utm_zone_north() {
        let coord = Coord {lat: 52.517153, lon: 13.412389};
        let utm = Utm::new(coord).unwrap();
        assert!((utm.easting - 392273.6051633584).abs() < 1.0);
        assert!((utm.northing - 5819744.4599129185).abs() < 1.0);
        assert_eq!(utm.north, true);
        assert_eq!(utm.zone, 33);
        assert_eq!(utm.band, 'U');
    }

    #[test]
    fn utm_norway_zone() {
        let coord = Coord {lat: 61.076521, lon: 4.680180};
        let utm = Utm::new(coord).unwrap();
        assert!((utm.easting - 267038.76).abs() < 1.0);
        assert!((utm.northing - 6779002.66).abs() < 1.0);
        assert_eq!(utm.north, true);
        assert_eq!(utm.zone, 32);
        assert_eq!(utm.band, 'V');
    }
    
    #[test]
    fn utm_svalbard_zone_1() {
        let coord = Coord {lat: 78.891608, lon: 10.457194};
        let utm = Utm::new(coord).unwrap();
        assert!((utm.easting - 402386.73).abs() < 1.0);
        assert!((utm.northing - 8761675.98).abs() < 1.0);
        assert_eq!(utm.north, true);
        assert_eq!(utm.zone, 33);
        assert_eq!(utm.band, 'X');
    }

    #[test]
    fn utm_svalbard_zone_2() {
        let coord = Coord {lat: 78.122200, lon: 20.349504};
        let utm = Utm::new(coord).unwrap();
        assert!((utm.easting - 622751.81).abs() < 1.0);
        assert!((utm.northing - 8677619.41).abs() < 1.0);
        assert_eq!(utm.north, true);
        assert_eq!(utm.zone, 33);
        assert_eq!(utm.band, 'X');
    }

    #[test]
    fn utm_svalbard_zone_3() {
        let coord = Coord {lat: 78.102575, lon: 21.013745};
        let utm = Utm::new(coord).unwrap();
        assert!((utm.easting - 362459.56).abs() < 1.0);
        assert!((utm.northing - 8676854.75).abs() < 1.0);
        assert_eq!(utm.north, true);
        assert_eq!(utm.zone, 35);
        assert_eq!(utm.band, 'X');
    }

    #[test]
    fn utm_svalbard_zone_4() {
        let coord = Coord {lat: 78.138264, lon: 30.194746};
        let utm = Utm::new(coord).unwrap();
        assert!((utm.easting - 573272.89).abs() < 1.0);
        assert!((utm.northing - 8675799.74).abs() < 1.0);
        assert_eq!(utm.north, true);
        assert_eq!(utm.zone, 35);
        assert_eq!(utm.band, 'X');
    }
}