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
#![doc(html_root_url = "https://urschrei.github.io/lonlat_bng/")]
use std;
use std::fmt;
use ostn15_phf::ostn15_lookup;
pub fn check<T>(to_check: T, bounds: (T, T)) -> Result<T, ()>
where T: std::cmp::PartialOrd + fmt::Display + Copy
{
match to_check {
to_check if bounds.0 <= to_check && to_check <= bounds.1 => Ok(to_check),
_ => Err(()),
}
}
pub trait ToMm {
fn round_to_mm(self) -> f64;
}
impl ToMm for f64 {
fn round_to_mm(self) -> f64 {
(self * 1000.).round() as f64 / 1000.
}
}
pub fn round_to_eight(x: f64, y: f64) -> (f64, f64) {
let new_x = (x * 100000000.).round() as f64 / 100000000.;
let new_y = (y * 100000000.).round() as f64 / 100000000.;
(new_x, new_y)
}
pub fn get_ostn_ref(x: &i32, y: &i32) -> Result<(f64, f64, f64), ()> {
let key = x + (y * 701) + 1;
let result = try!(ostn15_lookup(&key).ok_or(()));
Ok((result.0, result.1, result.2))
}
pub fn ostn15_shifts(x: &f64, y: &f64) -> Result<(f64, f64, f64), ()> {
let e_index = (*x / 1000.) as i32;
let n_index = (*y / 1000.) as i32;
let x0 = e_index as i32 * 1000;
let y0 = n_index as i32 * 1000;
let s0: (f64, f64, f64) = try!(get_ostn_ref(&(e_index), &(n_index)));
let s1: (f64, f64, f64) = try!(get_ostn_ref(&(e_index + 1), &(n_index + 0)));
let s2: (f64, f64, f64) = try!(get_ostn_ref(&(e_index + 0), &(n_index + 1)));
let s3: (f64, f64, f64) = try!(get_ostn_ref(&(e_index + 1), &(n_index + 1)));
let dx = x - (x0 as f64);
let dy = y - (y0 as f64);
let t = dx / 1000.;
let u = dy / 1000.;
let f0 = (1. - t) * (1. - u);
let f1 = t * (1. - u);
let f2 = (1. - t) * u;
let f3 = t * u;
let se = f0 * s0.0 + f1 * s1.0 + f2 * s2.0 + f3 * s3.0;
let sn = f0 * s0.1 + f1 * s1.1 + f2 * s2.1 + f3 * s3.1;
let sg = f0 * s0.2 + f1 * s1.2 + f2 * s2.2 + f3 * s3.2;
Ok((se.round_to_mm(), sn.round_to_mm(), sg.round_to_mm()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ostn_hashmap_retrieval() {
let eastings = 651;
let northings = 313;
let expected = (102.787, -78.242, 44.236);
assert_eq!(expected, get_ostn_ref(&eastings, &northings).unwrap());
}
#[test]
#[should_panic]
fn test_failed_ostn_hashmap_retrieval() {
let eastings = 999;
let northings = 999;
let expected = (1., 1., 1.);
assert_eq!(expected, get_ostn_ref(&eastings, &northings).unwrap());
}
#[test]
fn test_ostn15_shift_incorporation() {
let eastings = 651307.003;
let northings = 313255.686;
let expected = (102.801, -78.236, 44.228);
assert_eq!(expected, ostn15_shifts(&eastings, &northings).unwrap());
}
#[test]
#[should_panic]
fn test_min_lon_extents() {
let max_lon = 1.768960;
let min_lon = -6.379880;
check(&-6.379881, (&min_lon, &max_lon)).unwrap();
}
#[test]
#[should_panic]
fn test_min_lat_extents() {
let max_lat = 55.811741;
let min_lat = 49.871159;
check(&49.871158, (&min_lat, &max_lat)).unwrap();
}
#[test]
#[should_panic]
fn test_max_lon_extents() {
let max_lon = 1.768960;
let min_lon = -6.379880;
check(&1.768961, (&min_lon, &max_lon)).unwrap();
}
#[test]
#[should_panic]
fn test_max_lat_extents() {
let max_lat = 55.811741;
let min_lat = 49.871159;
check(&55.811742, (&min_lat, &max_lat)).unwrap();
}
}