point-formats 0.1.0

Dependency-light LiDAR/point-cloud/mesh format conversion crate with explicit adapters for heavyweight formats.
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
use crate::error::{Error, Result};
use std::fmt;
use std::path::{Path, PathBuf};
use std::str::FromStr;

/// High-level family of a format. This is used to decide when conversion
/// requires semantic work such as meshing, rasterization, or decoding packets.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FormatFamily {
    PointCloud,
    Mesh,
    Raster,
    Vector,
    Database,
    RoboticsStream,
    SensorRaw,
    WebTiles,
    VendorProject,
}

/// Declares whether a format is implemented natively in this crate.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FormatSupport {
    NativeReadWrite,
    NativeReadOnly,
    NativeWriteOnly,
    AdapterRequired,
    MetadataOnly,
}

/// Formats from the supplied LiDAR / point-cloud list.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[allow(missing_docs)]
pub enum Format {
    Las,
    Laz,
    Copc,
    E57,
    Ply,
    Pcd,
    Xyz,
    Txt,
    Csv,
    Pts,
    Ptx,
    Rcp,
    Rcs,
    Potree,
    Ept,
    GeoTiff,
    Cog,
    AsciiGrid,
    NetCdf,
    Hdf5,
    Shapefile,
    GeoJson,
    Gpkg,
    Obj,
    Fbx,
    Gltf,
    Glb,
    Stl,
    Dxf,
    Dwg,
    Pcap,
    UdpPackets,
    VendorRaw,
    RosBag,
    Ros2Bag,
    PointCloud2,
}

impl Format {
    /// Infers a format from a path's extension and known compound extensions.
    pub fn from_path(path: impl AsRef<Path>) -> Result<Self> {
        let path = path.as_ref();
        Self::from_path_opt(path).ok_or_else(|| Error::UnknownFormat {
            path: PathBuf::from(path),
        })
    }

    /// Same as [`Format::from_path`], but returns `None` instead of an error.
    pub fn from_path_opt(path: impl AsRef<Path>) -> Option<Self> {
        let path = path.as_ref();
        let name = path.file_name()?.to_string_lossy().to_ascii_lowercase();
        if name.ends_with(".copc.laz") {
            return Some(Self::Copc);
        }
        if name.ends_with(".cog.tif") || name.ends_with(".cog.tiff") {
            return Some(Self::Cog);
        }
        if name.ends_with(".tar.gz") {
            return None;
        }

        let ext = path.extension()?.to_string_lossy().to_ascii_lowercase();
        match ext.as_str() {
            "las" => Some(Self::Las),
            "laz" => Some(Self::Laz),
            "copc" => Some(Self::Copc),
            "e57" => Some(Self::E57),
            "ply" => Some(Self::Ply),
            "pcd" => Some(Self::Pcd),
            "xyz" => Some(Self::Xyz),
            "txt" => Some(Self::Txt),
            "csv" => Some(Self::Csv),
            "pts" => Some(Self::Pts),
            "ptx" => Some(Self::Ptx),
            "rcp" => Some(Self::Rcp),
            "rcs" => Some(Self::Rcs),
            "potree" => Some(Self::Potree),
            "ept" => Some(Self::Ept),
            "tif" | "tiff" => Some(Self::GeoTiff),
            "asc" => Some(Self::AsciiGrid),
            "nc" | "cdf" | "netcdf" => Some(Self::NetCdf),
            "h5" | "hdf5" => Some(Self::Hdf5),
            "shp" => Some(Self::Shapefile),
            "geojson" | "json" => Some(Self::GeoJson),
            "gpkg" => Some(Self::Gpkg),
            "obj" => Some(Self::Obj),
            "fbx" => Some(Self::Fbx),
            "gltf" => Some(Self::Gltf),
            "glb" => Some(Self::Glb),
            "stl" => Some(Self::Stl),
            "dxf" => Some(Self::Dxf),
            "dwg" => Some(Self::Dwg),
            "pcap" | "pcapng" => Some(Self::Pcap),
            "udp" | "udppackets" => Some(Self::UdpPackets),
            "raw" | "vendorraw" => Some(Self::VendorRaw),
            "bag" => Some(Self::RosBag),
            "db3" => Some(Self::Ros2Bag),
            "pc2" | "pointcloud2" => Some(Self::PointCloud2),
            _ => None,
        }
    }

    /// Stable lowercase name used by the CLI and diagnostics.
    pub const fn name(self) -> &'static str {
        match self {
            Self::Las => "las",
            Self::Laz => "laz",
            Self::Copc => "copc",
            Self::E57 => "e57",
            Self::Ply => "ply",
            Self::Pcd => "pcd",
            Self::Xyz => "xyz",
            Self::Txt => "txt",
            Self::Csv => "csv",
            Self::Pts => "pts",
            Self::Ptx => "ptx",
            Self::Rcp => "rcp",
            Self::Rcs => "rcs",
            Self::Potree => "potree",
            Self::Ept => "ept",
            Self::GeoTiff => "geotiff",
            Self::Cog => "cog",
            Self::AsciiGrid => "ascii-grid",
            Self::NetCdf => "netcdf",
            Self::Hdf5 => "hdf5",
            Self::Shapefile => "shapefile",
            Self::GeoJson => "geojson",
            Self::Gpkg => "gpkg",
            Self::Obj => "obj",
            Self::Fbx => "fbx",
            Self::Gltf => "gltf",
            Self::Glb => "glb",
            Self::Stl => "stl",
            Self::Dxf => "dxf",
            Self::Dwg => "dwg",
            Self::Pcap => "pcap",
            Self::UdpPackets => "udp-packets",
            Self::VendorRaw => "vendor-raw",
            Self::RosBag => "ros-bag",
            Self::Ros2Bag => "ros2-bag",
            Self::PointCloud2 => "pointcloud2",
        }
    }

    /// Format family.
    pub const fn family(self) -> FormatFamily {
        match self {
            Self::Las
            | Self::Laz
            | Self::Copc
            | Self::E57
            | Self::Ply
            | Self::Pcd
            | Self::Xyz
            | Self::Txt
            | Self::Csv
            | Self::Pts
            | Self::Ptx => FormatFamily::PointCloud,
            Self::Obj | Self::Fbx | Self::Gltf | Self::Glb | Self::Stl | Self::Dxf | Self::Dwg => {
                FormatFamily::Mesh
            }
            Self::GeoTiff | Self::Cog | Self::AsciiGrid | Self::NetCdf | Self::Hdf5 => {
                FormatFamily::Raster
            }
            Self::Shapefile | Self::GeoJson => FormatFamily::Vector,
            Self::Gpkg => FormatFamily::Database,
            Self::RosBag | Self::Ros2Bag | Self::PointCloud2 => FormatFamily::RoboticsStream,
            Self::Pcap | Self::UdpPackets | Self::VendorRaw => FormatFamily::SensorRaw,
            Self::Potree | Self::Ept => FormatFamily::WebTiles,
            Self::Rcp | Self::Rcs => FormatFamily::VendorProject,
        }
    }

    /// Native support level in this crate.
    pub const fn support(self) -> FormatSupport {
        match self {
            Self::Ply
            | Self::Pcd
            | Self::Xyz
            | Self::Txt
            | Self::Csv
            | Self::Pts
            | Self::Ptx
            | Self::Obj
            | Self::Stl
            | Self::AsciiGrid => FormatSupport::NativeReadWrite,

            #[cfg(feature = "las")]
            Self::Las | Self::Laz => FormatSupport::NativeReadWrite,
            #[cfg(feature = "copc")]
            Self::Copc => FormatSupport::NativeReadOnly,
            #[cfg(feature = "e57")]
            Self::E57 => FormatSupport::NativeReadWrite,
            #[cfg(feature = "geospatial")]
            Self::GeoTiff | Self::Cog | Self::GeoJson => FormatSupport::NativeReadWrite,
            #[cfg(feature = "dxf")]
            Self::Dxf => FormatSupport::NativeReadWrite,
            #[cfg(feature = "shapefile")]
            Self::Shapefile => FormatSupport::NativeReadWrite,
            #[cfg(feature = "gltf")]
            Self::Gltf | Self::Glb => FormatSupport::NativeReadWrite,
            #[cfg(feature = "gpkg")]
            Self::Gpkg => FormatSupport::NativeReadWrite,
            #[cfg(feature = "robotics")]
            Self::RosBag | Self::Ros2Bag | Self::PointCloud2 => FormatSupport::NativeReadWrite,
            #[cfg(feature = "sensor")]
            Self::Pcap | Self::UdpPackets | Self::VendorRaw => FormatSupport::NativeReadWrite,

            #[cfg(not(feature = "las"))]
            Self::Las | Self::Laz => FormatSupport::AdapterRequired,
            #[cfg(not(feature = "copc"))]
            Self::Copc => FormatSupport::AdapterRequired,
            #[cfg(not(feature = "e57"))]
            Self::E57 => FormatSupport::AdapterRequired,
            #[cfg(not(feature = "geospatial"))]
            Self::GeoTiff | Self::Cog | Self::GeoJson => FormatSupport::AdapterRequired,
            #[cfg(not(feature = "dxf"))]
            Self::Dxf => FormatSupport::AdapterRequired,
            #[cfg(not(feature = "shapefile"))]
            Self::Shapefile => FormatSupport::AdapterRequired,
            #[cfg(not(feature = "gltf"))]
            Self::Gltf | Self::Glb => FormatSupport::AdapterRequired,
            #[cfg(not(feature = "gpkg"))]
            Self::Gpkg => FormatSupport::AdapterRequired,
            #[cfg(not(feature = "sensor"))]
            Self::Pcap | Self::UdpPackets | Self::VendorRaw => FormatSupport::AdapterRequired,

            Self::NetCdf
            | Self::Hdf5
            | Self::Fbx
            | Self::Dwg
            | Self::Potree
            | Self::Ept
            | Self::Rcp
            | Self::Rcs => FormatSupport::AdapterRequired,

            #[cfg(not(feature = "gpkg"))]
            Self::Gpkg => FormatSupport::AdapterRequired,

            #[cfg(not(feature = "gltf"))]
            Self::Gltf | Self::Glb => FormatSupport::AdapterRequired,

            #[cfg(not(feature = "robotics"))]
            Self::RosBag | Self::Ros2Bag | Self::PointCloud2 => FormatSupport::AdapterRequired,
        }
    }

    /// Returns true when the format can be read by the built-in codecs.
    pub const fn is_native_read(self) -> bool {
        matches!(
            self.support(),
            FormatSupport::NativeReadWrite | FormatSupport::NativeReadOnly
        )
    }

    /// Returns true when the format can be written by the built-in codecs.
    pub const fn is_native_write(self) -> bool {
        matches!(
            self.support(),
            FormatSupport::NativeReadWrite | FormatSupport::NativeWriteOnly
        )
    }

    /// Human-readable reason when this format needs an adapter.
    pub const fn adapter_hint(self) -> &'static str {
        match self {
            Self::Las | Self::Laz => "use an adapter built on the `las` crate; enable LAZ through its laz/laz-parallel features when writing compressed files",
            Self::Copc => "use an adapter built on `copc-rs` or a PDAL pipeline; COPC requires LAZ hierarchy/index handling",
            Self::E57 => "use an adapter built on the `e57` crate; E57 can contain multiple scans, poses, images, and vendor extensions",
            Self::GeoTiff | Self::Cog | Self::AsciiGrid => "raster products need an explicit gridding/rasterization policy and a GDAL/tiff adapter",
            Self::NetCdf | Self::Hdf5 => "scientific containers need dataset/schema selection and a netcdf/hdf5 adapter",
            Self::Shapefile | Self::GeoJson | Self::Gpkg => "vector/database products need an explicit feature extraction schema and GIS adapter",
            Self::Fbx | Self::Gltf | Self::Glb | Self::Dxf | Self::Dwg => "DCC/CAD formats need a mesh/CAD adapter and may not preserve point attributes",
            Self::Potree | Self::Ept => "web tile formats need tiling, indexing, and hierarchy generation",
            Self::Pcap | Self::UdpPackets | Self::VendorRaw => "raw sensor data must be decoded using vendor packet calibration before point-cloud export",
            Self::RosBag | Self::Ros2Bag | Self::PointCloud2 => "robotics streams need ROS message schemas, topic selection, frame transforms, and timestamp policy",
            Self::Rcp | Self::Rcs => "Autodesk project formats are proprietary/vendor-specific; use vendor/export tooling or an adapter",
            _ => "format is supported natively",
        }
    }

    /// All formats represented by this crate.
    pub const ALL: &'static [Self] = &[
        Self::Las,
        Self::Laz,
        Self::Copc,
        Self::E57,
        Self::Ply,
        Self::Pcd,
        Self::Xyz,
        Self::Txt,
        Self::Csv,
        Self::Pts,
        Self::Ptx,
        Self::Rcp,
        Self::Rcs,
        Self::Potree,
        Self::Ept,
        Self::GeoTiff,
        Self::Cog,
        Self::AsciiGrid,
        Self::NetCdf,
        Self::Hdf5,
        Self::Shapefile,
        Self::GeoJson,
        Self::Gpkg,
        Self::Obj,
        Self::Fbx,
        Self::Gltf,
        Self::Glb,
        Self::Stl,
        Self::Dxf,
        Self::Dwg,
        Self::Pcap,
        Self::UdpPackets,
        Self::VendorRaw,
        Self::RosBag,
        Self::Ros2Bag,
        Self::PointCloud2,
    ];
}

impl fmt::Display for Format {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.name())
    }
}

impl FromStr for Format {
    type Err = String;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        let normalized = s
            .trim()
            .to_ascii_lowercase()
            .replace('_', "-")
            .replace('.', "");
        match normalized.as_str() {
            "las" => Ok(Self::Las),
            "laz" => Ok(Self::Laz),
            "copc" | "copclaz" => Ok(Self::Copc),
            "e57" => Ok(Self::E57),
            "ply" => Ok(Self::Ply),
            "pcd" => Ok(Self::Pcd),
            "xyz" => Ok(Self::Xyz),
            "txt" => Ok(Self::Txt),
            "csv" => Ok(Self::Csv),
            "pts" => Ok(Self::Pts),
            "ptx" => Ok(Self::Ptx),
            "rcp" => Ok(Self::Rcp),
            "rcs" => Ok(Self::Rcs),
            "potree" => Ok(Self::Potree),
            "ept" => Ok(Self::Ept),
            "geotiff" | "tif" | "tiff" => Ok(Self::GeoTiff),
            "cog" => Ok(Self::Cog),
            "ascii-grid" | "asc" | "asciigrid" => Ok(Self::AsciiGrid),
            "netcdf" | "nc" => Ok(Self::NetCdf),
            "hdf5" | "h5" => Ok(Self::Hdf5),
            "shapefile" | "shp" => Ok(Self::Shapefile),
            "geojson" => Ok(Self::GeoJson),
            "gpkg" | "geopackage" => Ok(Self::Gpkg),
            "obj" => Ok(Self::Obj),
            "fbx" => Ok(Self::Fbx),
            "gltf" => Ok(Self::Gltf),
            "glb" => Ok(Self::Glb),
            "stl" => Ok(Self::Stl),
            "dxf" => Ok(Self::Dxf),
            "dwg" => Ok(Self::Dwg),
            "pcap" | "pcapng" => Ok(Self::Pcap),
            "udp" | "udp-packets" | "udppackets" => Ok(Self::UdpPackets),
            "vendor-raw" | "vendorraw" | "raw" => Ok(Self::VendorRaw),
            "ros-bag" | "rosbag" | "bag" => Ok(Self::RosBag),
            "ros2-bag" | "ros2bag" | "db3" => Ok(Self::Ros2Bag),
            "pointcloud2" | "point-cloud2" | "sensor-msgs-pointcloud2" => Ok(Self::PointCloud2),
            _ => Err(format!("unknown format '{s}'")),
        }
    }
}