Skip to main content

oxigeo_drivers_advanced/
lib.rs

1//! OxiGeo Advanced Format Drivers
2//!
3//! This crate provides advanced geospatial format drivers for OxiGeo:
4//! - JPEG2000 (JP2) - Pure Rust JPEG2000 codec with GeoJP2 support
5//! - GeoPackage (GPKG) - SQLite-based vector and raster storage
6//! - KML/KMZ - Keyhole Markup Language for Google Earth
7//! - GML - Geography Markup Language (OGC standard)
8//!
9//! # Features
10//!
11//! - `jpeg2000` - JPEG2000 format support (enabled by default)
12//! - `geopackage` - GeoPackage format support (optional)
13//! - `kml` - KML/KMZ format support (enabled by default)
14//! - `gml` - GML format support (enabled by default)
15//! - `async` - Async I/O support (optional)
16//!
17//! # Examples
18//!
19//! ## Reading JPEG2000
20//!
21//! ```no_run
22//! use oxigeo_drivers_advanced::jp2;
23//! use std::fs::File;
24//!
25//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
26//! let file = File::open("image.jp2")?;
27//! let image = jp2::read_jp2(file)?;
28//! println!("Dimensions: {}x{}", image.width, image.height);
29//! # Ok(())
30//! # }
31//! ```
32//!
33//! ## Reading GeoPackage
34//!
35//! ```no_run
36//! # #[cfg(feature = "geopackage")]
37//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
38//! use oxigeo_drivers_advanced::gpkg::GeoPackage;
39//!
40//! let gpkg = GeoPackage::open("data.gpkg")?;
41//! let tables = gpkg.feature_tables()?;
42//! println!("Feature tables: {:?}", tables);
43//! # Ok(())
44//! # }
45//! ```
46//!
47//! ## Reading KML
48//!
49//! ```no_run
50//! use oxigeo_drivers_advanced::kml;
51//! use std::io::BufReader;
52//! use std::fs::File;
53//!
54//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
55//! let file = BufReader::new(File::open("placemarks.kml")?);
56//! let doc = kml::read_kml(file)?;
57//! println!("Placemarks: {}", doc.placemark_count());
58//! # Ok(())
59//! # }
60//! ```
61//!
62//! ## Reading GML
63//!
64//! ```no_run
65//! use oxigeo_drivers_advanced::gml;
66//! use std::io::BufReader;
67//! use std::fs::File;
68//!
69//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
70//! let file = BufReader::new(File::open("features.gml")?);
71//! let collection = gml::read_gml(file)?;
72//! println!("Features: {}", collection.len());
73//! # Ok(())
74//! # }
75//! ```
76
77#![deny(missing_docs)]
78#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::panic))]
79
80pub mod error;
81
82#[cfg(feature = "jpeg2000")]
83pub mod jp2;
84
85#[cfg(feature = "geopackage")]
86pub mod gpkg;
87
88#[cfg(feature = "kml")]
89pub mod kml;
90
91#[cfg(feature = "kml")]
92pub mod kmz;
93
94#[cfg(feature = "gml")]
95pub mod gml;
96
97pub use error::{Error, Result};
98
99/// Library version.
100pub const VERSION: &str = env!("CARGO_PKG_VERSION");
101
102/// Check if a format is supported based on file extension.
103pub fn is_supported(extension: &str) -> bool {
104    match extension.to_lowercase().as_str() {
105        #[cfg(feature = "jpeg2000")]
106        "jp2" | "j2k" | "jpf" | "jpx" => true,
107        #[cfg(feature = "geopackage")]
108        "gpkg" => true,
109        #[cfg(feature = "kml")]
110        "kml" | "kmz" => true,
111        #[cfg(feature = "gml")]
112        "gml" | "xml" => true,
113        _ => false,
114    }
115}
116
117/// Get list of supported format extensions.
118pub fn supported_extensions() -> Vec<&'static str> {
119    let mut extensions = Vec::new();
120
121    #[cfg(feature = "jpeg2000")]
122    {
123        extensions.extend_from_slice(&["jp2", "j2k", "jpf", "jpx"]);
124    }
125
126    #[cfg(feature = "geopackage")]
127    {
128        extensions.push("gpkg");
129    }
130
131    #[cfg(feature = "kml")]
132    {
133        extensions.extend_from_slice(&["kml", "kmz"]);
134    }
135
136    #[cfg(feature = "gml")]
137    {
138        extensions.extend_from_slice(&["gml", "xml"]);
139    }
140
141    extensions
142}
143
144#[cfg(test)]
145mod tests {
146    use super::*;
147
148    #[test]
149    fn test_version() {
150        assert!(!VERSION.is_empty());
151    }
152
153    #[test]
154    fn test_supported_extensions() {
155        let extensions = supported_extensions();
156        assert!(!extensions.is_empty());
157
158        #[cfg(feature = "jpeg2000")]
159        assert!(extensions.contains(&"jp2"));
160
161        #[cfg(feature = "geopackage")]
162        assert!(extensions.contains(&"gpkg"));
163
164        #[cfg(feature = "kml")]
165        assert!(extensions.contains(&"kml"));
166
167        #[cfg(feature = "gml")]
168        assert!(extensions.contains(&"gml"));
169    }
170
171    #[test]
172    fn test_is_supported() {
173        #[cfg(feature = "jpeg2000")]
174        {
175            assert!(is_supported("jp2"));
176            assert!(is_supported("JP2"));
177            assert!(is_supported("j2k"));
178        }
179
180        #[cfg(feature = "geopackage")]
181        assert!(is_supported("gpkg"));
182
183        #[cfg(feature = "kml")]
184        {
185            assert!(is_supported("kml"));
186            assert!(is_supported("kmz"));
187        }
188
189        #[cfg(feature = "gml")]
190        assert!(is_supported("gml"));
191
192        assert!(!is_supported("txt"));
193        assert!(!is_supported("unknown"));
194    }
195}