oxigdal-vrt 0.1.7

VRT (Virtual Raster) driver for OxiGDAL - Pure Rust GDAL reimplementation
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
//! VRT dataset definition

use crate::band::VrtBand;
use crate::error::{Result, VrtError};
use crate::source::PixelRect;
use oxigdal_core::types::{GeoTransform, RasterDataType};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// VRT dataset
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct VrtDataset {
    /// Raster width in pixels
    pub raster_x_size: u64,
    /// Raster height in pixels
    pub raster_y_size: u64,
    /// GeoTransform (affine transform for georeferencing)
    pub geo_transform: Option<GeoTransform>,
    /// Spatial reference system (WKT or PROJ.4 string)
    pub srs: Option<String>,
    /// Bands
    pub bands: Vec<VrtBand>,
    /// Block size (default tile dimensions)
    pub block_size: Option<(u32, u32)>,
    /// Subclass (for special VRT types)
    pub subclass: Option<VrtSubclass>,
    /// VRT file path (for resolving relative paths)
    pub vrt_path: Option<PathBuf>,
}

impl VrtDataset {
    /// Creates a new VRT dataset
    pub fn new(raster_x_size: u64, raster_y_size: u64) -> Self {
        Self {
            raster_x_size,
            raster_y_size,
            geo_transform: None,
            srs: None,
            bands: Vec::new(),
            block_size: None,
            subclass: None,
            vrt_path: None,
        }
    }

    /// Creates a new VRT dataset with extent from sources
    ///
    /// # Errors
    /// Returns an error if no bands are provided
    pub fn from_bands(bands: Vec<VrtBand>) -> Result<Self> {
        if bands.is_empty() {
            return Err(VrtError::invalid_structure(
                "Dataset must have at least one band",
            ));
        }

        // Calculate extent from first band's sources
        let (width, height) = Self::calculate_extent_from_band(&bands[0])?;

        Ok(Self {
            raster_x_size: width,
            raster_y_size: height,
            geo_transform: None,
            srs: None,
            bands,
            block_size: None,
            subclass: None,
            vrt_path: None,
        })
    }

    /// Adds a band to the dataset
    pub fn add_band(&mut self, band: VrtBand) {
        self.bands.push(band);
    }

    /// Sets the GeoTransform
    pub fn with_geo_transform(mut self, geo_transform: GeoTransform) -> Self {
        self.geo_transform = Some(geo_transform);
        self
    }

    /// Sets the spatial reference system
    pub fn with_srs<S: Into<String>>(mut self, srs: S) -> Self {
        self.srs = Some(srs.into());
        self
    }

    /// Sets the block size
    pub fn with_block_size(mut self, width: u32, height: u32) -> Self {
        self.block_size = Some((width, height));
        self
    }

    /// Sets the subclass
    pub fn with_subclass(mut self, subclass: VrtSubclass) -> Self {
        self.subclass = Some(subclass);
        self
    }

    /// Sets the VRT file path
    pub fn with_vrt_path<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.vrt_path = Some(path.into());
        self
    }

    /// Validates the dataset
    ///
    /// # Errors
    /// Returns an error if the dataset is invalid
    pub fn validate(&self) -> Result<()> {
        if self.raster_x_size == 0 || self.raster_y_size == 0 {
            return Err(VrtError::invalid_structure(
                "Dataset dimensions must be > 0",
            ));
        }

        if self.bands.is_empty() {
            return Err(VrtError::invalid_structure(
                "Dataset must have at least one band",
            ));
        }

        // Validate all bands
        for (idx, band) in self.bands.iter().enumerate() {
            band.validate().map_err(|e| {
                VrtError::invalid_structure(format!("Band {} validation failed: {}", idx + 1, e))
            })?;

            // Check that band numbers are sequential
            if band.band != idx + 1 {
                return Err(VrtError::invalid_structure(format!(
                    "Band number mismatch: expected {}, got {}",
                    idx + 1,
                    band.band
                )));
            }
        }

        // Validate block size if present
        if let Some((width, height)) = self.block_size
            && (width == 0 || height == 0)
        {
            return Err(VrtError::invalid_structure("Block size must be > 0"));
        }

        Ok(())
    }

    /// Gets the number of bands
    pub fn band_count(&self) -> usize {
        self.bands.len()
    }

    /// Gets a band by index (0-based)
    pub fn get_band(&self, index: usize) -> Option<&VrtBand> {
        self.bands.get(index)
    }

    /// Gets a mutable reference to a band by index (0-based)
    pub fn get_band_mut(&mut self, index: usize) -> Option<&mut VrtBand> {
        self.bands.get_mut(index)
    }

    /// Gets the extent as a pixel rectangle
    pub fn extent(&self) -> PixelRect {
        PixelRect::new(0, 0, self.raster_x_size, self.raster_y_size)
    }

    /// Gets the effective block size (uses dataset default or falls back to 256x256)
    pub fn effective_block_size(&self) -> (u32, u32) {
        self.block_size.unwrap_or((256, 256))
    }

    /// Calculates extent from a band's sources
    fn calculate_extent_from_band(band: &VrtBand) -> Result<(u64, u64)> {
        if band.sources.is_empty() {
            return Err(VrtError::invalid_structure(
                "Band has no sources to calculate extent from",
            ));
        }

        // Find the bounding box of all destination rectangles
        let mut min_x = u64::MAX;
        let mut min_y = u64::MAX;
        let mut max_x = 0u64;
        let mut max_y = 0u64;

        for source in &band.sources {
            if let Some(dst_rect) = source.dst_rect() {
                min_x = min_x.min(dst_rect.x_off);
                min_y = min_y.min(dst_rect.y_off);
                max_x = max_x.max(dst_rect.x_off + dst_rect.x_size);
                max_y = max_y.max(dst_rect.y_off + dst_rect.y_size);
            } else if let Some(ref props) = source.properties {
                // If no dst_rect, use source properties
                max_x = max_x.max(props.width);
                max_y = max_y.max(props.height);
            }
        }

        if max_x == 0 || max_y == 0 {
            return Err(VrtError::invalid_structure(
                "Cannot calculate extent: no valid source windows",
            ));
        }

        Ok((max_x - min_x, max_y - min_y))
    }

    /// Merges GeoTransforms from multiple sources to create a unified transform
    ///
    /// # Errors
    /// Returns an error if sources have incompatible GeoTransforms
    pub fn merge_geo_transforms(&mut self) -> Result<()> {
        if self.geo_transform.is_some() {
            return Ok(()); // Already set
        }

        let mut transforms = Vec::new();

        // Collect all GeoTransforms from band sources
        for band in &self.bands {
            for source in &band.sources {
                if let Some(ref props) = source.properties
                    && let Some(ref gt) = props.geo_transform
                {
                    transforms.push(*gt);
                }
            }
        }

        if transforms.is_empty() {
            return Ok(()); // No GeoTransforms to merge
        }

        // For simplicity, use the first transform
        // In a real implementation, we would validate that all transforms are compatible
        self.geo_transform = Some(transforms[0]);

        Ok(())
    }

    /// Gets the data type of the first band
    pub fn primary_data_type(&self) -> Option<RasterDataType> {
        self.bands.first().map(|b| b.data_type)
    }

    /// Checks if all bands have the same data type
    pub fn has_uniform_data_type(&self) -> bool {
        if self.bands.is_empty() {
            return true;
        }

        let first_type = self.bands[0].data_type;
        self.bands.iter().all(|b| b.data_type == first_type)
    }
}

/// VRT subclass types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum VrtSubclass {
    /// Standard VRT
    #[default]
    Standard,
    /// Warped VRT (for reprojection)
    Warped,
    /// Pansharpened VRT
    Pansharpened,
    /// Processed VRT (with pixel functions)
    Processed,
}

/// VRT metadata
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct VrtMetadata {
    /// Metadata domain
    pub domain: Option<String>,
    /// Metadata items (key-value pairs)
    pub items: Vec<(String, String)>,
}

impl VrtMetadata {
    /// Creates new VRT metadata
    pub fn new() -> Self {
        Self {
            domain: None,
            items: Vec::new(),
        }
    }

    /// Creates VRT metadata with a domain
    pub fn with_domain<S: Into<String>>(domain: S) -> Self {
        Self {
            domain: Some(domain.into()),
            items: Vec::new(),
        }
    }

    /// Adds a metadata item
    pub fn add_item<K: Into<String>, V: Into<String>>(&mut self, key: K, value: V) {
        self.items.push((key.into(), value.into()));
    }

    /// Gets a metadata value by key
    pub fn get(&self, key: &str) -> Option<&str> {
        self.items
            .iter()
            .find(|(k, _)| k == key)
            .map(|(_, v)| v.as_str())
    }
}

impl Default for VrtMetadata {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::band::VrtBand;
    use crate::source::{SourceFilename, VrtSource};
    use oxigdal_core::types::RasterDataType;

    #[test]
    fn test_vrt_dataset_creation() {
        let dataset = VrtDataset::new(512, 512);
        assert_eq!(dataset.raster_x_size, 512);
        assert_eq!(dataset.raster_y_size, 512);
        assert_eq!(dataset.band_count(), 0);
    }

    #[test]
    fn test_vrt_dataset_validation() {
        let mut dataset = VrtDataset::new(512, 512);
        let source = VrtSource::new(SourceFilename::absolute("/test.tif"), 1);
        let band = VrtBand::simple(1, RasterDataType::UInt8, source);
        dataset.add_band(band);

        assert!(dataset.validate().is_ok());

        let empty_dataset = VrtDataset::new(512, 512);
        assert!(empty_dataset.validate().is_err());

        let invalid_dataset = VrtDataset::new(0, 0);
        assert!(invalid_dataset.validate().is_err());
    }

    #[test]
    fn test_vrt_dataset_extent() {
        let dataset = VrtDataset::new(1024, 768);
        let extent = dataset.extent();
        assert_eq!(extent.x_size, 1024);
        assert_eq!(extent.y_size, 768);
    }

    #[test]
    fn test_vrt_dataset_band_access() {
        let mut dataset = VrtDataset::new(512, 512);
        let source = VrtSource::new(SourceFilename::absolute("/test.tif"), 1);
        let band = VrtBand::simple(1, RasterDataType::UInt8, source);
        dataset.add_band(band);

        assert_eq!(dataset.band_count(), 1);
        assert!(dataset.get_band(0).is_some());
        assert!(dataset.get_band(1).is_none());
    }

    #[test]
    fn test_effective_block_size() {
        let dataset = VrtDataset::new(512, 512);
        assert_eq!(dataset.effective_block_size(), (256, 256));

        let dataset_with_blocks = VrtDataset::new(512, 512).with_block_size(128, 128);
        assert_eq!(dataset_with_blocks.effective_block_size(), (128, 128));
    }

    #[test]
    fn test_vrt_metadata() {
        let mut metadata = VrtMetadata::new();
        metadata.add_item("author", "test");
        metadata.add_item("version", "1.0");

        assert_eq!(metadata.get("author"), Some("test"));
        assert_eq!(metadata.get("version"), Some("1.0"));
        assert_eq!(metadata.get("missing"), None);
    }

    #[test]
    fn test_uniform_data_type() {
        let mut dataset = VrtDataset::new(512, 512);

        let source1 = VrtSource::new(SourceFilename::absolute("/test1.tif"), 1);
        let band1 = VrtBand::simple(1, RasterDataType::UInt8, source1);
        dataset.add_band(band1);

        let source2 = VrtSource::new(SourceFilename::absolute("/test2.tif"), 1);
        let band2 = VrtBand::simple(2, RasterDataType::UInt8, source2);
        dataset.add_band(band2);

        assert!(dataset.has_uniform_data_type());

        let source3 = VrtSource::new(SourceFilename::absolute("/test3.tif"), 1);
        let band3 = VrtBand::simple(3, RasterDataType::Float32, source3);
        dataset.add_band(band3);

        assert!(!dataset.has_uniform_data_type());
    }
}