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
//! Pure Rust geographical projections library. Similar to `Proj` in
//! basic functionality but allows for a use in concurrent contexts.
//!
//! Projections' implementations closely follow algorithms and instructions provided in:
//! [Map projections: A working manual (John P. Snyder, 1987)](https://pubs.er.usgs.gov/publication/pp1395)
//!
//! **This crate in very early stages of development. If you are interested
//! in contributing do not hesitate to contact me on Github.**
//!
//! ## Usage example
//!
//! We can project the geographical coordinates to cartographic
//! coordinates on a map with sepcified projection as follows:
//!
//!```
//!# use mappers::{Ellipsoid, projections::LambertConformalConic, Projection, ProjectionError};
//!#
//!# fn main() -> Result<(), ProjectionError> {
//! // First, we define the projection
//!
//! // We use LCC with reference longitude centered on France
//! // parallels set for Europe and WGS84 ellipsoid
//! let lcc = LambertConformalConic::new(2.0, 0.0, 30.0, 60.0, Ellipsoid::WGS84)?;
//!
//! // Second, we define the coordinates of Mount Blanc
//! let (lon, lat) = (6.8651, 45.8326);
//!
//! // Project the coordinates
//! let (x, y) = lcc.project(lon, lat)?;
//!
//! // And print the result
//! println!("x: {}, y: {}", x, y); // x: 364836.4407792019, y: 5421073.726335758
//!# Ok(())
//!# }
//!```
//!
//! We can also inversly project the cartographic coordinates
//! to geographical coordinates:
//!
//!```
//!# use mappers::{Ellipsoid, projections::LambertConformalConic, Projection, ProjectionError};
//!#
//!# fn main() -> Result<(), ProjectionError> {
//! // We again start with defining the projection
//! let lcc = LambertConformalConic::new(2.0, 0.0, 30.0, 60.0, Ellipsoid::WGS84)?;
//!
//! // We take the previously projected coordinates
//! let (x, y) = (364836.4407792019, 5421073.726335758);
//!
//! // Inversly project the coordinates
//! let (lon, lat) = lcc.inverse_project(x, y)?;
//!
//! // And print the result
//! println!("lon: {}, lat: {}", lon, lat); // lon: 6.8651, lat: 45.83260000001716
//!# Ok(())
//!# }
//!```
//!
//! Some projections are mathematically exactly inversible, and technically
//! geographical coordinates projected and inverse projected should be identical.
//! However, in practice limitations of floating-point arithmetics will
//! introduce some errors along the way, as shown in the example above.
//!
//! ## ConversionPipe
//!
//! This crate also provides a struct [`ConversionPipe`] that allows for easy
//! conversion between two projections. It can be constructed directly from
//! [`Projection`] with [`pipe_to`](Projection::pipe_to) method or directly
//! with [`ConversionPipe::new()`].
//!
//! Before using it please read the documentation of [`ConversionPipe`].
//!
//! ### Example
//!
//!```
//!# use mappers::{Ellipsoid, Projection, ProjectionError, ConversionPipe};
//!# use mappers::projections::{LambertConformalConic, LongitudeLatitude};
//!# use float_cmp::assert_approx_eq;
//!#
//!# fn main() -> Result<(), ProjectionError> {
//! // We start by defining the source and target projections
//! // In this case we will use LCC and LongitudeLatitude
//! // to show how a normal projection can be done with ConversionPipe
//! let target_proj = LambertConformalConic::new(2.0, 0.0, 30.0, 60.0, Ellipsoid::WGS84)?;
//! let source_proj = LongitudeLatitude;
//!
//! let (lon, lat) = (6.8651, 45.8326);
//!
//! // Now we can convert to LCC and back to LongitudeLatitude
//! let (x, y) = source_proj.pipe_to(&target_proj).convert(lon, lat)?;
//! let (pipe_lon, pipe_lat) = target_proj.pipe_to(&source_proj).convert(x, y)?;
//!
//! // For simple cases the error remains small
//! // but it can quickly grow with more complex conversions
//! assert_approx_eq!(f64, lon, pipe_lon, epsilon = 1e-10);
//! assert_approx_eq!(f64, lat, pipe_lat, epsilon = 1e-10);
//!
//!# Ok(())
//!# }
//!```
use Debug;
pub use Ellipsoid;
pub use ProjectionError;
/// An interface for all projections included in the crate.
///
/// This trait is kept relatively simple and the most basic version of
/// projection functions are implemented. Alternative functions for more complex
/// types should be implemented by the user.
/// A struct that allows for easy conversion between two projections.
///
/// It can be constructed directly with the constructor or
/// from [`Projection`] with [`pipe_to`](Projection::pipe_to) method.
///
/// The implementation is very naive as it converts coordinates to longitude and latitude then projects them
/// to the target projection. Therefore projection and numerical errors are accumulated with every step and
/// long conversion chains are discouraged.
///
/// Main purpose of this struct is to allow creating generic conversion patterns independent of projections.
///
/// For usage see examples in [the main module](crate).