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
//! Mercator projection
//!
//! Impelentation taken from:
//! 
//! http://wiki.openstreetmap.org/wiki/Mercator
//!
//! Copyright 2006 Christopher Schmidt

use prelude::*;

#[derive(Debug, Copy, Clone)]
pub struct MercatorSystem;

#[inline(always)]
fn pj_phi2(ts: f64, e: f64)
           -> f64
{    
    const HALFPI: f64 = ::std::f64::consts::PI / 2.0;
    const TOL: f64 = 0.0000000001;
    
    let eccnth = 0.5 * e;
    let mut phi = HALFPI - 2.0 * ts.atan();

    let mut dphi;
    let mut con;
    let mut iteration_cnt = 15;

    loop {
        con = e * phi.sin();
        dphi = HALFPI - 2.0 * (ts * ((1.0 - con) / (1.0 + con)).powf(eccnth)).atan() - phi;
        phi += dphi; 
        
        if dphi.abs() > TOL && (iteration_cnt - 1) > 0 { break; } else { iteration_cnt -= 1; }
    }
    
    return phi;
}

/// `temp = ellipsoid.b / ellipsoid.a`
#[inline(always)]
fn lat_to_mercator_y(mut lat: f64, ellipsoid_a: f64, temp: f64) -> f64 {

    if lat > 89.5  { lat = 89.5; }
    if lat < -89.5 { lat = -89.5; }

    let es = 1.0 - (temp * temp);
    let eccent = es.sqrt();
    let phi = lat.to_radians();
    let sinphi = phi.sin();
    let con = eccent * sinphi;
    let com = 0.5 * eccent;
    let con = (1.0 - con) / (1.0 + con).powf(com);
    let ts = (0.5 * (::std::f64::consts::PI * 0.5 - phi)).tan() / con;
    let y = 0.0 - ellipsoid_a * ts.ln();

    return y;
}

#[inline(always)]
fn merc_x_to_lon(x: f64, ellipsoid_a: f64)
                 -> f64
{
    (x / ellipsoid_a).to_degrees()
}

/// `temp = ellipsoid.a / ellipsoid.b`
/// `e = (1.0 - (temp * temp)).sqrt()`
#[inline(always)]
fn merc_y_to_lat(y: f64, ellipsoid_a: f64, e: f64)
                 -> f64
{
    pj_phi2((0.0 - (y / ellipsoid_a)).exp(), e).to_degrees()
}

impl ToLonLat for MercatorSystem {
    fn to_lon_lat(&self, mut data: Vec<(f64, f64)>, ellipsoid: &Ellipsoid, strategy: &mut MultithreadingStrategy)
                  -> LonLatBuf
    {
        let temp = ellipsoid.b / ellipsoid.a;
        let e = (1.0 - (temp * temp)).sqrt();
        
        match *strategy {
            SingleCore => {
                for &mut (ref mut x, ref mut y) in data.iter_mut() {
                    *x = merc_x_to_lon(*x, ellipsoid.a);
                    *y = merc_y_to_lat(*y, ellipsoid.b, e);
                }
            },

            MultiCore(ref mut thread_pool) => {
                thread_pool.scoped(|scoped| {
                    for &mut (ref mut x, ref mut y) in data.iter_mut() {
                        scoped.execute(move || {
                            *x = merc_x_to_lon(*x, ellipsoid.a);
                            *y = merc_y_to_lat(*y, ellipsoid.b, e);
                        });
                    }
                });
            },
            _ => unimplemented!("Multithreading methods other than SingleCore and MultiCore are not yet implemented!"),          
        }
        
        LonLatBuf {
            data: data,
            ellipsoid: *ellipsoid,
        }
    }
}

impl FromLonLat for MercatorSystem {

    fn from_lon_lat(&self, mut data: Vec<(f64, f64)>, ellipsoid: &Ellipsoid, strategy: &mut MultithreadingStrategy)
                    -> CoordinateBuf
    {        
        let temp = ellipsoid.b / ellipsoid.a;

        // TODO: copy-pasted! bad!
        match *strategy {
            SingleCore => {
                for &mut (ref mut lon, ref mut lat) in data.iter_mut() {
                    *lon = ellipsoid.a * lon.to_radians();
                    *lat = lat_to_mercator_y(*lat, ellipsoid.a, temp);
                }
            },

            MultiCore(ref mut thread_pool) => {
                thread_pool.scoped(|scoped| {
                    // Create references to each element in the vector ...
                    for &mut (ref mut lon, ref mut lat) in &mut data {
                        // ... and add 1 to it in a seperate thread
                        scoped.execute(move || {
                            *lon = ellipsoid.a * lon.to_radians();
                            *lat = lat_to_mercator_y(*lat, ellipsoid.a, temp);
                        });
                    }
                });
            },
            _ => unimplemented!("Multithreading methods other than SingleCore and MultiCore are not yet implemented!"),          
        }

        CoordinateBuf {
            data: data,
            crs: Box::new(MercatorSystem),
            ellipsoid: *ellipsoid,
        }
    }
}