wblidar 0.1.1

High-performance library for reading and writing LiDAR point cloud data (LAS, LAZ, COPC, PLY, E57)
Documentation
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! Point-cloud reprojection helpers powered by `wbprojection`.

use wbprojection::Crs as ProjCrs;

use crate::crs::Crs;
use crate::error::{Error, Result};
use crate::point::PointRecord;

/// Behavior when a point fails reprojection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransformFailurePolicy {
    /// Abort and return the first reprojection error.
    Error,
    /// Keep the point but set `x`/`y` to `NaN`.
    SetNaN,
    /// Drop the failed point from output.
    SkipPoint,
}

/// Options for LiDAR reprojection operations.
#[derive(Debug, Clone, Copy)]
pub struct LidarReprojectOptions {
    /// Policy applied when a point transformation fails.
    pub failure_policy: TransformFailurePolicy,
    /// When `true`, use 3D reprojection and update `z` values.
    pub use_3d_transform: bool,
}

impl Default for LidarReprojectOptions {
    fn default() -> Self {
        Self {
            failure_policy: TransformFailurePolicy::Error,
            use_3d_transform: false,
        }
    }
}

impl LidarReprojectOptions {
    /// Create default options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set transform failure policy.
    pub fn with_failure_policy(mut self, policy: TransformFailurePolicy) -> Self {
        self.failure_policy = policy;
        self
    }

    /// Enable/disable 3D reprojection (`x`, `y`, and `z`).
    ///
    /// Uses `wbprojection`'s preserve-horizontal 3D transform behavior.
    pub fn with_3d_transform(mut self, enabled: bool) -> Self {
        self.use_3d_transform = enabled;
        self
    }
}

/// Reproject points to a destination EPSG using source CRS metadata.
///
/// Source EPSG is read from `src_crs.epsg`.
pub fn points_to_epsg(points: &[PointRecord], src_crs: &Crs, dst_epsg: u32) -> Result<Vec<PointRecord>> {
    points_to_epsg_with_options(points, src_crs, dst_epsg, &LidarReprojectOptions::default())
}

/// Reproject points to a destination EPSG using source CRS metadata with options.
///
/// Source CRS is resolved from `src_crs.epsg` or `src_crs.wkt`.
pub fn points_to_epsg_with_options(
    points: &[PointRecord],
    src_crs: &Crs,
    dst_epsg: u32,
    options: &LidarReprojectOptions,
) -> Result<Vec<PointRecord>> {
    let src = source_proj_crs(src_crs, "points_to_epsg")?;
    let dst = ProjCrs::from_epsg(dst_epsg)
        .map_err(|e| Error::Projection(format!("invalid destination EPSG {dst_epsg}: {e}")))?;
    points_with_crs_options(points, &src, &dst, options)
}

/// Reproject points to a destination EPSG using source CRS metadata with
/// options and progress updates in the range [0, 1] as points are completed.
pub fn points_to_epsg_with_options_and_progress<F>(
    points: &[PointRecord],
    src_crs: &Crs,
    dst_epsg: u32,
    options: &LidarReprojectOptions,
    progress: F,
) -> Result<Vec<PointRecord>>
where
    F: Fn(f64) + Send + Sync,
{
    let src = source_proj_crs(src_crs, "points_to_epsg")?;
    let dst = ProjCrs::from_epsg(dst_epsg)
        .map_err(|e| Error::Projection(format!("invalid destination EPSG {dst_epsg}: {e}")))?;
    points_with_crs_options_internal(points, &src, &dst, options, Some(&progress))
}

/// Reproject points to a destination EPSG and return updated destination CRS metadata.
///
/// Source EPSG is read from `src_crs.epsg`.
pub fn points_to_epsg_with_output_crs(
    points: &[PointRecord],
    src_crs: &Crs,
    dst_epsg: u32,
) -> Result<(Vec<PointRecord>, Crs)> {
    points_to_epsg_with_output_crs_options(
        points,
        src_crs,
        dst_epsg,
        &LidarReprojectOptions::default(),
    )
}

/// Reproject points to a destination EPSG and return updated destination CRS metadata with options.
///
/// Source EPSG is read from `src_crs.epsg`.
pub fn points_to_epsg_with_output_crs_options(
    points: &[PointRecord],
    src_crs: &Crs,
    dst_epsg: u32,
    options: &LidarReprojectOptions,
) -> Result<(Vec<PointRecord>, Crs)> {
    let out = points_to_epsg_with_options(points, src_crs, dst_epsg, options)?;
    Ok((out, Crs::from_epsg(dst_epsg)))
}

/// Reproject points between explicit EPSG codes.
pub fn points_from_to_epsg(points: &[PointRecord], src_epsg: u32, dst_epsg: u32) -> Result<Vec<PointRecord>> {
    points_from_to_epsg_with_options(
        points,
        src_epsg,
        dst_epsg,
        &LidarReprojectOptions::default(),
    )
}

/// Reproject points between explicit EPSG codes with options.
pub fn points_from_to_epsg_with_options(
    points: &[PointRecord],
    src_epsg: u32,
    dst_epsg: u32,
    options: &LidarReprojectOptions,
) -> Result<Vec<PointRecord>> {
    let src = ProjCrs::from_epsg(src_epsg)
        .map_err(|e| Error::Projection(format!("invalid source EPSG {src_epsg}: {e}")))?;
    let dst = ProjCrs::from_epsg(dst_epsg)
        .map_err(|e| Error::Projection(format!("invalid destination EPSG {dst_epsg}: {e}")))?;
    points_with_crs_options(points, &src, &dst, options)
}

/// Reproject points between caller-supplied CRS objects.
pub fn points_with_crs(points: &[PointRecord], src: &ProjCrs, dst: &ProjCrs) -> Result<Vec<PointRecord>> {
    points_with_crs_options(points, src, dst, &LidarReprojectOptions::default())
}

/// Reproject points between caller-supplied CRS objects with options.
pub fn points_with_crs_options(
    points: &[PointRecord],
    src: &ProjCrs,
    dst: &ProjCrs,
    options: &LidarReprojectOptions,
) -> Result<Vec<PointRecord>> {
    points_with_crs_options_internal(points, src, dst, options, None)
}

/// Reproject points between caller-supplied CRS objects with options and
/// progress updates in the range [0, 1] as points are completed.
pub fn points_with_crs_options_and_progress<F>(
    points: &[PointRecord],
    src: &ProjCrs,
    dst: &ProjCrs,
    options: &LidarReprojectOptions,
    progress: F,
) -> Result<Vec<PointRecord>>
where
    F: Fn(f64) + Send + Sync,
{
    points_with_crs_options_internal(points, src, dst, options, Some(&progress))
}

fn points_with_crs_options_internal(
    points: &[PointRecord],
    src: &ProjCrs,
    dst: &ProjCrs,
    options: &LidarReprojectOptions,
    progress: Option<&(dyn Fn(f64) + Send + Sync)>,
) -> Result<Vec<PointRecord>> {
    let mut out = Vec::with_capacity(points.len());
    let total_points = points.len();

    for (index, p) in points.iter().enumerate() {
        let transformed = if options.use_3d_transform {
            src.transform_to_3d_preserve_horizontal(p.x, p.y, p.z, dst)
                .map(|(x, y, z)| (x, y, Some(z)))
        } else {
            src.transform_to(p.x, p.y, dst)
                .map(|(x, y)| (x, y, None))
        };

        match transformed {
            Ok((x, y, z_opt)) => {
                let mut q = *p;
                q.x = x;
                q.y = y;
                if let Some(z) = z_opt {
                    q.z = z;
                }
                out.push(q);
            }
            Err(err) => match options.failure_policy {
                TransformFailurePolicy::Error => {
                    return Err(Error::Projection(format!("point reprojection failed: {err}")));
                }
                TransformFailurePolicy::SetNaN => {
                    let mut q = *p;
                    q.x = f64::NAN;
                    q.y = f64::NAN;
                    if options.use_3d_transform {
                        q.z = f64::NAN;
                    }
                    out.push(q);
                }
                TransformFailurePolicy::SkipPoint => {}
            },
        }

        if let Some(progress_cb) = progress {
            progress_cb((index + 1) as f64 / total_points.max(1) as f64);
        }
    }

    if let Some(progress_cb) = progress {
        progress_cb(1.0);
    }

    Ok(out)
}

/// Reproject points in-place between explicit EPSG codes.
pub fn points_in_place_from_to_epsg(points: &mut [PointRecord], src_epsg: u32, dst_epsg: u32) -> Result<()> {
    let src = ProjCrs::from_epsg(src_epsg)
        .map_err(|e| Error::Projection(format!("invalid source EPSG {src_epsg}: {e}")))?;
    let dst = ProjCrs::from_epsg(dst_epsg)
        .map_err(|e| Error::Projection(format!("invalid destination EPSG {dst_epsg}: {e}")))?;

    for p in points.iter_mut() {
        let (x, y) = src
            .transform_to(p.x, p.y, &dst)
            .map_err(|e| Error::Projection(format!("point reprojection failed: {e}")))?;
        p.x = x;
        p.y = y;
    }
    Ok(())
}

/// Reproject points in-place to a destination EPSG using source CRS metadata,
/// then update that metadata to the destination CRS.
///
/// Source CRS is resolved from `src_crs.epsg` or `src_crs.wkt`.
pub fn points_in_place_to_epsg(
    points: &mut [PointRecord],
    src_crs: &mut Crs,
    dst_epsg: u32,
) -> Result<()> {
    points_in_place_to_epsg_with_options(
        points,
        src_crs,
        dst_epsg,
        &LidarReprojectOptions::default(),
    )
}

/// Reproject points in-place to a destination EPSG using source CRS metadata
/// and options, then update CRS metadata to the destination CRS.
pub fn points_in_place_to_epsg_with_options(
    points: &mut [PointRecord],
    src_crs: &mut Crs,
    dst_epsg: u32,
    options: &LidarReprojectOptions,
) -> Result<()> {
    let src = source_proj_crs(src_crs, "points_in_place_to_epsg")?;
    let dst = ProjCrs::from_epsg(dst_epsg)
        .map_err(|e| Error::Projection(format!("invalid destination EPSG {dst_epsg}: {e}")))?;

    for p in points.iter_mut() {
        let (x, y, z_opt) = if options.use_3d_transform {
            src.transform_to_3d_preserve_horizontal(p.x, p.y, p.z, &dst)
                .map(|(x, y, z)| (x, y, Some(z)))
                .map_err(|e| Error::Projection(format!("point reprojection failed: {e}")))?
        } else {
            src.transform_to(p.x, p.y, &dst)
                .map(|(x, y)| (x, y, None))
                .map_err(|e| Error::Projection(format!("point reprojection failed: {e}")))?
        };
        p.x = x;
        p.y = y;
        if let Some(z) = z_opt {
            p.z = z;
        }
    }

    *src_crs = Crs::from_epsg(dst_epsg);
    Ok(())
}

fn source_proj_crs(src_crs: &Crs, op_name: &str) -> Result<ProjCrs> {
    if let Some(src_epsg) = src_crs.epsg {
        return ProjCrs::from_epsg(src_epsg)
            .map_err(|e| Error::Projection(format!("invalid source EPSG {src_epsg}: {e}")));
    }

    if let Some(wkt) = src_crs.wkt.as_deref() {
        let trimmed = wkt.trim();
        if !trimmed.is_empty() {
            return wbprojection::from_wkt(trimmed)
                .map_err(|e| Error::Projection(format!("invalid source CRS WKT: {e}")));
        }
    }

    Err(Error::Projection(format!(
        "{op_name} requires source CRS metadata in src_crs (EPSG or WKT)"
    )))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Arc, Mutex};

    #[test]
    fn reprojects_4326_to_3857() {
        let points = vec![PointRecord { x: -2.0, y: -0.5, ..PointRecord::default() }];
        let out = points_from_to_epsg(&points, 4326, 3857).unwrap();
        assert_eq!(out.len(), 1);
        assert!(out[0].x.abs() > 1000.0);
        assert!(out[0].y.abs() > 100.0);
    }

    #[test]
    fn points_to_epsg_requires_source_epsg() {
        let points = vec![PointRecord::default()];
        let src = Crs::new();
        let err = points_to_epsg(&points, &src, 3857).unwrap_err();
        assert!(matches!(err, Error::Projection(_)));
        assert!(err
            .to_string()
            .contains("points_to_epsg requires source CRS metadata"));
    }

    #[test]
    fn points_to_epsg_accepts_source_wkt_without_epsg() {
        let points = vec![PointRecord { x: -2.0, y: -0.5, ..PointRecord::default() }];
        let src = Crs::new().with_wkt(
            "GEOGCRS[\"WGS 84\",DATUM[\"World Geodetic System 1984\",ELLIPSOID[\"WGS 84\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],CS[ellipsoidal,2],AXIS[\"Geodetic latitude (Lat)\",north],AXIS[\"Geodetic longitude (Lon)\",east],UNIT[\"degree\",0.0174532925199433],ID[\"EPSG\",4326]]"
        );
        let out = points_to_epsg(&points, &src, 3857).unwrap();
        assert_eq!(out.len(), 1);
        assert!(out[0].x.abs() > 1000.0);
        assert!(out[0].y.abs() > 100.0);
    }

    #[test]
    fn points_to_epsg_with_3d_option_preserves_z_path() {
        let points = vec![PointRecord { x: -2.0, y: -0.5, z: 123.4, ..PointRecord::default() }];
        let src = Crs::from_epsg(4326);
        let opts = LidarReprojectOptions::new().with_3d_transform(true);
        let out = points_to_epsg_with_options(&points, &src, 3857, &opts).unwrap();
        assert_eq!(out.len(), 1);
        assert!(out[0].x.abs() > 1000.0);
        assert!(out[0].y.abs() > 100.0);
        assert!(out[0].z.is_finite());
    }

    #[test]
    fn points_to_epsg_updates_coords() {
        let points = vec![PointRecord { x: -2.0, y: -0.5, ..PointRecord::default() }];
        let src = Crs::from_epsg(4326);
        let out = points_to_epsg(&points, &src, 3857).unwrap();
        assert_eq!(out.len(), 1);
        assert!(out[0].x.abs() > 1000.0);
        assert!(out[0].y.abs() > 100.0);
    }

    #[test]
    fn points_to_epsg_with_output_crs_returns_dst_crs() {
        let points = vec![PointRecord { x: -2.0, y: -0.5, ..PointRecord::default() }];
        let src = Crs::from_epsg(4326);
        let (out, dst_crs) = points_to_epsg_with_output_crs(&points, &src, 3857).unwrap();
        assert_eq!(out.len(), 1);
        assert_eq!(dst_crs.epsg, Some(3857));
    }

    #[test]
    fn points_in_place_to_epsg_updates_points_and_crs() {
        let mut points = vec![PointRecord { x: -2.0, y: -0.5, ..PointRecord::default() }];
        let mut crs = Crs::from_epsg(4326);
        points_in_place_to_epsg(&mut points, &mut crs, 3857).unwrap();
        assert_eq!(crs.epsg, Some(3857));
        assert!(points[0].x.abs() > 1000.0);
        assert!(points[0].y.abs() > 100.0);
    }

    #[test]
    fn points_in_place_to_epsg_requires_source_epsg() {
        let mut points = vec![PointRecord::default()];
        let mut crs = Crs::new();
        let err = points_in_place_to_epsg(&mut points, &mut crs, 3857).unwrap_err();
        assert!(matches!(err, Error::Projection(_)));
        assert!(err
            .to_string()
            .contains("points_in_place_to_epsg requires source CRS metadata"));
    }

    #[test]
    fn points_to_epsg_with_progress_emits_point_updates() {
        let points = vec![
            PointRecord { x: -2.0, y: -0.5, ..PointRecord::default() },
            PointRecord { x: -1.5, y: -0.4, ..PointRecord::default() },
            PointRecord { x: -1.0, y: -0.3, ..PointRecord::default() },
        ];
        let src = Crs::from_epsg(4326);

        let progress_values: Arc<Mutex<Vec<f64>>> = Arc::new(Mutex::new(Vec::new()));
        let sink = Arc::clone(&progress_values);

        let out = points_to_epsg_with_options_and_progress(
            &points,
            &src,
            3857,
            &LidarReprojectOptions::default(),
            move |pct| {
                sink.lock().unwrap().push(pct);
            },
        )
        .unwrap();

        let values = progress_values.lock().unwrap();
        assert_eq!(out.len(), points.len());
        assert!(!values.is_empty());
        assert_eq!(values.len(), points.len() + 1);
        assert!(values.iter().all(|v| v.is_finite() && *v >= 0.0 && *v <= 1.0));
        assert!((values.last().copied().unwrap() - 1.0).abs() < 1e-12);
    }
}