Skip to content

Commit af8d59b

Browse files
authored
Merge pull request #1 from yageek/refactor
Refactor
2 parents 60bd4ea + 4dadd19 commit af8d59b

File tree

8 files changed

+286
-211
lines changed

8 files changed

+286
-211
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lambert"
3-
version = "0.1.0"
3+
version = "1.0.0"
44
authors = ["Heinrich, Yannick <[email protected]>"]
55

66
description = "lamber-rust is a crate helping to convert Lambert coordinates to WGS84"

Changelog.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# 1.0.0
2+
- Reformat library
3+
- Add documentation
4+
5+
# 0.1.0
6+
- Initial release

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ lambert-rust is a crate helping to convert Lambert coordinates to WGS84.
99
# Usage
1010

1111
```rust
12-
extern crate lambert;
13-
let mut loc= lambert::point::Point::new(668832.5384, 6950138.7285,lambert::zone::Zone::Lambert93);
14-
15-
println!("WGS84 Lat:{}, Lon:{}", loc.y, loc.x);
12+
let point = Point::new(369419.0, 1986498.0, 0.0)
13+
.wgs84_from_meter(Zone::Lambert93)
14+
.convert_unit(AngleUnit::Radian, AngleUnit::Degree);
15+
println!("WGS84 Lat:{}, Lon:{}", point.y, point.x);
1616
```

src/algo.rs

+126-91
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use ::point;
2-
use ::zone;
1+
use point::Point;
2+
use zone;
3+
use zone::Zone;
34
use std::f32;
45

5-
66
/// Convert latitude to latitude iso
7+
#[allow(dead_code)]
78
fn latitude_iso_from_latitude(lat: f32, e: f32) -> f32 {
89
return f32::log(f32::tan(f32::consts::FRAC_PI_4+lat/2.0)*f32::powf((1.0-e*f32::sin(lat))/(1.0+e*f32::sin(lat)),e/2.0), f32::consts::E);
910
}
@@ -28,22 +29,7 @@ fn latitude_from_latitude_iso(lat_iso: f32, e: f32, eps: f32) -> f32 {
2829
return phi_i
2930
}
3031

31-
#[test]
32-
fn test_latitude_from_latitude_iso(){
33-
let lat_iso: [f32; 3] = [1.00552653648,-0.30261690060 ,0.2000000000];
34-
let e: [f32; 3] = [0.08199188998,0.08199188998,0.08199188998];
35-
let eps: [f32; 3] = [1.0e-11,1.0e-11,1.0e-11];
36-
37-
let phi: [f32; 3] = [0.87266462600, -0.29999999997 ,0.19998903369];
38-
39-
for index in 0..3 {
40-
let result = latitude_from_latitude_iso(lat_iso[index], e[index], eps[index]);
41-
assert_delta!(result, phi[index], 1e-7);
42-
}
43-
}
44-
45-
46-
pub fn lambert_to_geographic(org: point::Point, zone: zone::Zone, lon_merid: f32, e: f32, eps: f32) -> point::Point {
32+
pub fn lambert_to_geographic(org: Point, zone: Zone, lon_merid: f32, e: f32, eps: f32) -> Point {
4733

4834
let n = zone::n(zone);
4935
let c = zone::c(zone);
@@ -63,42 +49,18 @@ pub fn lambert_to_geographic(org: point::Point, zone: zone::Zone, lon_merid: f32
6349

6450
let lat = latitude_from_latitude_iso(lat_iso, e, eps);
6551

66-
return point::Point { x: lon, y: lat, z: org.z, unit: point::AngleUnit::Radian};
67-
}
68-
69-
#[test]
70-
fn test_lambert_to_geographic(){
71-
let expected = point::Point::new(0.145512099,0.872664626, 0.0, point::AngleUnit::Radian);
72-
let org = point::Point::new(1029705.083, 272723.849, 0.0, point::AngleUnit::Radian);
73-
74-
let delta = 1e-7;
75-
let dest = lambert_to_geographic(org, zone::Zone::LambertI, ::consts::LON_MERID_GREENWICH, ::consts::E_CLARK_IGN, delta);
76-
assert_delta!(dest.x, expected.x, delta);
77-
assert_delta!(dest.y, expected.y, delta);
78-
assert_delta!(dest.z, expected.z, delta);
52+
return Point { x: lon, y: lat, z: org.z};
7953
}
8054

81-
8255
pub fn lambert_normal(lat: f32, a: f32, e: f32) -> f32 {
8356
return a/f32::sqrt(1.0-e*e*f32::sin(lat)*f32::sin(lat));
8457
}
8558

86-
#[test]
87-
fn test_lambert_normal(){
88-
let n = 6393174.9755;
89-
let lat = 0.97738438100;
90-
let a = 6378388.0000;
91-
let e = 0.081991890;
92-
93-
let calc = lambert_normal(lat,a,e);
94-
assert_eq!(n, calc);
95-
}
96-
97-
pub fn geographic_to_cartesian(lon: f32, lat: f32, he: f32, a: f32, e: f32) -> point::Point {
59+
pub fn geographic_to_cartesian(lon: f32, lat: f32, he: f32, a: f32, e: f32) -> Point {
9860

9961
let n = lambert_normal(lat, a, e);
10062

101-
let mut pt = point::Point::new(0.0,0.0,0.0, point::AngleUnit::Radian);
63+
let mut pt = Point::new(0.0, 0.0, 0.0);
10264
pt.x = (n+he)*f32::cos(lat)*f32::cos(lon);
10365

10466
pt.y = (n+he)*f32::cos(lat)*f32::sin(lon);
@@ -107,31 +69,7 @@ pub fn geographic_to_cartesian(lon: f32, lat: f32, he: f32, a: f32, e: f32) -> p
10769
return pt
10870
}
10971

110-
#[test]
111-
fn test_geographic_to_cartesian(){
112-
let lon:[f32; 3] = [0.01745329248 ,0.00290888212 ,0.00581776423];
113-
let lat:[f32; 3] = [0.02036217457,0.00000000000 ,-0.03199770300];
114-
let he:[f32; 3] = [100.0000,10.0000 ,2000.0000];
115-
let a:[f32; 3] = [6378249.2000 ,6378249.2000 ,6378249.2000];
116-
let e:[f32; 3] = [0.08248325679 ,0.08248325679 ,0.08248325679];
117-
118-
let points = vec![
119-
point::Point::new(6376064.6955,111294.6230,128984.7250, point::AngleUnit::Meter),
120-
point::Point::new(6378232.2149,18553.5780,0.0, point::AngleUnit::Meter),
121-
point::Point::new(6376897.5369,37099.7050,-202730.9070, point::AngleUnit::Meter)
122-
];
123-
124-
let delta = 1e-1;
125-
for i in 0..points.len() {
126-
let pt = geographic_to_cartesian(lon[i],lat[i],he[i],a[i],e[i]);
127-
128-
assert_delta!(pt.x,points[i].x, delta);
129-
assert_delta!(pt.y,points[i].y, delta);
130-
assert_delta!(pt.z,points[i].z, delta);
131-
}
132-
}
133-
134-
pub fn cartesian_to_geographic(point: point::Point, meridien: f32, a: f32, e: f32, eps: f32) -> point::Point{
72+
pub fn cartesian_to_geographic(point: Point, meridien: f32, a: f32, e: f32, eps: f32) -> Point{
13573

13674
let (x, y, z) = (point.x, point.y, point.z);
13775
let lon = meridien + f32::atan(y/x);
@@ -152,31 +90,128 @@ pub fn cartesian_to_geographic(point: point::Point, meridien: f32, a: f32, e: f3
15290
}
15391

15492
let he = module/f32::cos(phi_i) - a/f32::sqrt(1.0-e*e*f32::sin(phi_i)*f32::sin(phi_i));
155-
return point::Point { x:lon, y:phi_i, z: he, unit: point::AngleUnit::Radian};
93+
return Point { x:lon, y:phi_i, z: he};
15694
}
15795

158-
#[test]
159-
fn test_cartesian_to_geographic(){
96+
#[cfg(test)]
97+
mod tests {
98+
99+
macro_rules! assert_delta {
100+
( $left:expr, $right:expr, $d:expr ) => {
101+
{
102+
if $left > $right {
103+
if ($left - $right) > $d {
104+
panic!("left: {} | right: {} | delta: {}\n", $left, $right, ($left - $right));
105+
}
106+
} else {
107+
if ($right - $left) > $d {
108+
panic!("left: {} | right: {} | delta: {}\n", $left, $right, ($right - $left));
109+
}
110+
}
111+
}
112+
};
113+
}
114+
115+
use point::Point;
116+
use zone::Zone;
160117

161-
let a: [f32; 3] = [6378249.2000, 6378249.2000 ,6378249.2000];
162-
let e: [f32; 3] = [0.08248325679, 0.08248325679, 0.08248325679];
163-
let x: [f32; 3] = [6376064.6950, 6378232.2150, 6376897.5370];
164-
let y: [f32; 3] = [111294.6230, 18553.5780, 37099.7050];
165-
let z: [f32; 3] = [128984.7250, 0.0000, -202730.9070];
166-
let eps: [f32; 3] = [1e-11,1e-11,1e-11];
118+
use super::lambert_to_geographic;
167119

168-
let lon: [f32; 3] = [0.01745329248, 0.00290888212, 0.00581776423];
169-
let lat: [f32; 3] = [0.02036217457, 0.00000000000, -0.03199770301];
170-
let he: [f32; 3] = [99.9995, 10.0001, 2000.0001];
120+
#[test]
121+
fn test_lambert_to_geographic() {
122+
let expected = Point::new(0.145512099,0.872664626, 0.0);
123+
let org = Point::new(1029705.083, 272723.849, 0.0);
171124

172-
let delta = 1e-8;
173-
for i in 0..3 {
174-
let sample = point::Point::new(x[i],y[i],z[i], point::AngleUnit::Radian);
125+
let delta = 1e-7;
126+
let dest = lambert_to_geographic(org, Zone::LambertI, ::consts::LON_MERID_GREENWICH, ::consts::E_CLARK_IGN, delta);
127+
assert_delta!(dest.x, expected.x, delta);
128+
assert_delta!(dest.y, expected.y, delta);
129+
assert_delta!(dest.z, expected.z, delta);
130+
}
175131

176-
let val = cartesian_to_geographic(sample,::consts::LON_MERID_PARIS,a[i],e[i],eps[i]);
132+
133+
use super::lambert_normal;
134+
135+
#[test]
136+
fn test_lambert_normal() {
137+
let n = 6393174.9755;
138+
let lat = 0.97738438100;
139+
let a = 6378388.0000;
140+
let e = 0.081991890;
141+
142+
let calc = lambert_normal(lat,a,e);
143+
assert_eq!(n, calc);
144+
}
177145

178-
assert_delta!(val.x,lon[i],delta);
179-
assert_delta!(val.y,lat[i],delta);
180-
assert_delta!(val.z,he[i],1e-3);
146+
use super::geographic_to_cartesian;
147+
148+
#[test]
149+
fn test_geographic_to_cartesian() {
150+
let lon:[f32; 3] = [0.01745329248 ,0.00290888212 ,0.00581776423];
151+
let lat:[f32; 3] = [0.02036217457,0.00000000000 ,-0.03199770300];
152+
let he:[f32; 3] = [100.0000,10.0000 ,2000.0000];
153+
let a:[f32; 3] = [6378249.2000 ,6378249.2000 ,6378249.2000];
154+
let e:[f32; 3] = [0.08248325679 ,0.08248325679 ,0.08248325679];
155+
156+
let points = vec![
157+
Point::new(6376064.6955, 111294.6230, 128984.7250),
158+
Point::new(6378232.2149, 18553.5780, 0.0),
159+
Point::new(6376897.5369, 37099.7050, -202730.9070)
160+
];
161+
162+
let delta = 1e-1;
163+
for i in 0..points.len() {
164+
let pt = geographic_to_cartesian(lon[i],lat[i],he[i],a[i],e[i]);
165+
166+
assert_delta!(pt.x,points[i].x, delta);
167+
assert_delta!(pt.y,points[i].y, delta);
168+
assert_delta!(pt.z,points[i].z, delta);
169+
}
181170
}
182-
}
171+
172+
173+
use super::cartesian_to_geographic;
174+
175+
#[test]
176+
fn test_cartesian_to_geographic() {
177+
178+
let a: [f32; 3] = [6378249.2000, 6378249.2000 ,6378249.2000];
179+
let e: [f32; 3] = [0.08248325679, 0.08248325679, 0.08248325679];
180+
let x: [f32; 3] = [6376064.6950, 6378232.2150, 6376897.5370];
181+
let y: [f32; 3] = [111294.6230, 18553.5780, 37099.7050];
182+
let z: [f32; 3] = [128984.7250, 0.0000, -202730.9070];
183+
let eps: [f32; 3] = [1e-11,1e-11,1e-11];
184+
185+
let lon: [f32; 3] = [0.01745329248, 0.00290888212, 0.00581776423];
186+
let lat: [f32; 3] = [0.02036217457, 0.00000000000, -0.03199770301];
187+
let he: [f32; 3] = [99.9995, 10.0001, 2000.0001];
188+
189+
let delta = 1e-8;
190+
for i in 0..3 {
191+
let sample = Point::new(x[i],y[i],z[i]);
192+
193+
let val = cartesian_to_geographic(sample,::consts::LON_MERID_PARIS,a[i],e[i],eps[i]);
194+
195+
assert_delta!(val.x,lon[i],delta);
196+
assert_delta!(val.y,lat[i],delta);
197+
assert_delta!(val.z,he[i],1e-3);
198+
}
199+
}
200+
201+
202+
use super::latitude_from_latitude_iso;
203+
204+
#[test]
205+
fn test_latitude_from_latitude_iso() {
206+
let lat_iso: [f32; 3] = [1.00552653648,-0.30261690060 ,0.2000000000];
207+
let e: [f32; 3] = [0.08199188998,0.08199188998,0.08199188998];
208+
let eps: [f32; 3] = [1.0e-11,1.0e-11,1.0e-11];
209+
210+
let phi: [f32; 3] = [0.87266462600, -0.29999999997 ,0.19998903369];
211+
212+
for index in 0..3 {
213+
let result = latitude_from_latitude_iso(lat_iso[index], e[index], eps[index]);
214+
assert_delta!(result, phi[index], 1e-7);
215+
}
216+
}
217+
}

src/consts.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,19 @@ pub const A_WGS84: f32 = 6378137.0;
1010
pub const LON_MERID_PARIS: f32 = 0.0;
1111
pub const LON_MERID_GREENWICH: f32 = 0.04079234433;
1212
pub const LON_MERID_IERS: f32 = (3.0*std::f32::consts::PI/180.0);
13-
pub const AUTOCOMEIQUE_FIRST: f32 = 44.0*std::f32::consts::PI/180.0;
14-
pub const AUTOCOMEIQUE_SECOND: f32 = 49.0*std::f32::consts::PI/180.0;
15-
pub const LAT_ORIG: f32 = 46.5*std::f32::consts::PI/180.0;
16-
pub const CT_X0: f32 = 700000.0;
17-
pub const CT_Y0: f32 = 6600000.0;
13+
14+
// Future usage
15+
#[allow(dead_code)]
16+
const AUTOCOMEIQUE_FIRST: f32 = 44.0*std::f32::consts::PI/180.0;
17+
18+
#[allow(dead_code)]
19+
const AUTOCOMEIQUE_SECOND: f32 = 49.0*std::f32::consts::PI/180.0;
20+
21+
#[allow(dead_code)]
22+
const LAT_ORIG: f32 = 46.5*std::f32::consts::PI/180.0;
23+
24+
#[allow(dead_code)]
25+
const CT_X0: f32 = 700000.0;
26+
27+
#[allow(dead_code)]
28+
const CT_Y0: f32 = 6600000.0;

src/lib.rs

+31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
1+
//! Lambert to WGS84 projection conversion crate
2+
//!
3+
//! # Examples
4+
//!
5+
//! ```rust
6+
//! extern crate lambert;
7+
//! use lambert::{Point, AngleUnit, Zone};
8+
//!
9+
//! // Enter coordinates in point
10+
//! let point = Point::new(369419.0, 1986498.0, 0.0)
11+
//! .wgs84_from_meter(Zone::Lambert93)
12+
//! .convert_unit(AngleUnit::Radian, AngleUnit::Degree);
13+
//!
14+
//! println!("WGS84 Lat:{}, Lon:{}", point.y, point.x);
15+
//! ```
16+
117
#[macro_use]
218
mod point;
319
mod zone;
420
mod consts;
521
mod algo;
22+
23+
pub use point::{Point, AngleUnit};
24+
pub use zone::Zone;
25+
26+
#[cfg(test)]
27+
mod tests {
28+
use super::{Point, AngleUnit, Zone};
29+
#[test]
30+
fn test_usage() {
31+
let point = Point::new(369419.0, 1986498.0, 0.0)
32+
.wgs84_from_meter(Zone::Lambert93)
33+
.convert_unit(AngleUnit::Radian, AngleUnit::Degree);
34+
println!("WGS84 Lat:{}, Lon:{}", point.y, point.x);
35+
}
36+
}

0 commit comments

Comments
 (0)