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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//! # proj5
//!
//! PROJ.5 is a Rust-based alternative to the established coordinate projection
//! library PROJ.4 (which is written in C). PROJ.5 aims to make coordinate transformations
//! more type-safe (instead of relying on projection strings) and multi-threaded
//! (using multiple threads as well as generated OpenCL code).
//!
//! While it is a large undertaking rewriting such a well-established library,
//! the speed benefits from multithreading, vectorization and batched transformation
//! are reason enough to at least try.
//!
//! I've written this library because I saw various GIS / projection libraries
//! floating around on github, but no centralized Rust-projection library.
//!
//! **Note:** This library is a work-in-progress and is by no means battle-tested.
//! It's just a collection of projection formulas from different authors,
//! ported to Rust with a type-safe interface.
//!
//! **Important:** Currently, there is no reprojection between ellipsoids yet.
//!
//! **Also important:** Coordinates are always horizonal, then vertical. (LonLat instead of LatLon)
//!
//! PROJ.5 defines the 24 standard ellipsoids (such as WGS84, Bessel, etc.),
//! but you can make your own ellipsoids.
//!
//! ## Usage
//!
//! ```rust
//! extern crate proj5;
//!
//! use proj5::prelude::*;
//!
//! fn main() {
//!
//! //! warning: PROJ.5 can currently not reproject between different ellipsoids!
//! //! using different ellipsoids will panic!
//! let ellipsoid = WGS_1984_ELLIPSOID;
//!
//! let source_coordinates = CoordinateSource::CoordinateBuf(Box::new(
//! CoordinateBuf {
//! data: vec![(377299.0, 1483035.0)],
//! crs: Box::new(UTMSystem {
//! utm_zone: 48,
//! }),
//! ellipsoid: ellipsoid,
//! }
//! ));
//!
//! let mut target_coordinates = CoordinateSource::CoordinateBuf(Box::new(
//! CoordinateBuf {
//! data: Vec::new(),
//! crs: Box::new(MercatorSystem),
//! ellipsoid: ellipsoid,
//! }
//! ));
//!
//! let mut strategy = MultithreadingStrategy::SingleCore;
//! source_coordinates.project(&mut target_coordinates, &mut strategy);
//!
//! println!("first batch of coordinates: {:#?}", target_coordinates.get_data_ref());
//! }
//!
//! ```
//!
//! ## Performance
//!
//! Performance is dependent on the chosen `MultithreadingStrategy`. Of course,
//! a multithreaded transform will always be faster than a single-threaded one.
//! When working with coordinates, X and Y are often calculated seperately,
//! which is why this library does not work with vectors (i.e. libraries such
//! as `nalgebra`. The transformations are not linear, which is why vectors
//! are in this case useless.
//!
//! PROJ.5 uses two virtual function calls per (batched) transformation. It
//! is highly, HIGHLY recommended to **batch** your coordinates, whenever
//! you can. PROJ.5 uses double precision for calculation.
//!
//!
//! ## Design
//!
//! Projecting from any projection and any ellipsoid into any other
//! projection and ellipsoid would result in
//! `(number of projections) ^ (number of ellipoids) ^ 2`
//! conversions. This is not realistically possible. Instead,
//! what PROJ.5 does is the following conversion:
//!
//! ```ignore
//! +-----------------+ +------------------+
//! |(1) | |(2) |
//! |Source CRS | |Longitude / Latit.|
//! |Source Ellipsoid +-v->+Source Ellipsoid |
//! |Source Units | |lon/lat (degrees) |
//! | | | |
//! +-----------------+ +--------+---------+
//! |
//! |
//! +-----------------+ +--------+---------+
//! |(4) | |(3) |
//! |Target CRS | |Longitude / Latit.|
//! |Target Ellipsoid +<-v-+Target Ellipsoid |
//! |Target Units | |lon/lat (degrees) |
//! | | | |
//! +-----------------+ +------------------+
//!
//! ```
//!
//! The arrows marked with `v` require a virtual function call,
//! in order to lookup the implementation of the given coordinate system.
//!
//! In order to implement your own CRS, you have to implement the `ToLatLon` and `FromLatLon` traits.
//! The required trait `Crs` is then automatically implemented for you.
//!
//! ```rust,ignore
//! impl ToLatLon for MyCoordinateSystem {
//! fn to_lon_lat(&self, mut data: Vec<(f64, f64)>, ellipsoid: Ellipsoid)
//! -> LonLatBuf
//! { ... }
//! }
//!
//! impl FromLatLon for MyCoordinateSystem {
//! fn from_lon_lat(&self, mut data: Vec<(f64, f64)>, ellipsoid: Ellipsoid)
//! -> CoordinateBuf
//! { ... }
//! }
//! ```
//!
//! This way, every coordinate system can talk to every other coordinate system.
//!
extern crate scoped_threadpool;
// #[cfg(target_arch = "wasm32")]
// mod math;
pub use ;
pub use Pool as ThreadPool;
pub use MultithreadingStrategy;
pub use *;
pub use LonLatBuf;
pub use CoordinateBuf;
// prelude for easy importing
pub use *;
pub use *;