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
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
446
447
448
449
450
451
452
453
454
455
//! VRT builder API for fluent VRT creation

use crate::band::VrtBand;
use crate::dataset::VrtDataset;
use crate::error::{Result, VrtError};
use crate::source::{PixelRect, SourceWindow, VrtSource};
use crate::xml::VrtXmlWriter;
use oxigdal_core::types::{GeoTransform, RasterDataType};
use std::path::{Path, PathBuf};

/// VRT builder for creating VRT datasets
pub struct VrtBuilder {
    dataset: VrtDataset,
    auto_calculate_extent: bool,
    vrt_path: Option<PathBuf>,
}

impl VrtBuilder {
    /// Creates a new VRT builder
    pub fn new() -> Self {
        Self {
            dataset: VrtDataset::new(0, 0),
            auto_calculate_extent: true,
            vrt_path: None,
        }
    }

    /// Creates a VRT builder with specified dimensions
    pub fn with_size(width: u64, height: u64) -> Self {
        Self {
            dataset: VrtDataset::new(width, height),
            auto_calculate_extent: false,
            vrt_path: None,
        }
    }

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

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

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

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

    /// Adds a simple source to a band
    ///
    /// # Errors
    /// Returns an error if the source configuration is invalid
    pub fn add_source<P: AsRef<Path>>(
        mut self,
        path: P,
        band_num: usize,
        source_band: usize,
    ) -> Result<Self> {
        let source = VrtSource::simple(path.as_ref(), source_band);
        self.add_source_to_band(band_num, source)?;
        Ok(self)
    }

    /// Adds a source with a window to a band
    ///
    /// # Errors
    /// Returns an error if the source configuration is invalid
    pub fn add_source_with_window<P: AsRef<Path>>(
        mut self,
        path: P,
        band_num: usize,
        source_band: usize,
        src_rect: PixelRect,
        dst_rect: PixelRect,
    ) -> Result<Self> {
        let window = SourceWindow::new(src_rect, dst_rect);
        let source = VrtSource::simple(path.as_ref(), source_band).with_window(window);
        self.add_source_to_band(band_num, source)?;
        Ok(self)
    }

    /// Adds a mosaic tile at a specific position
    ///
    /// # Errors
    /// Returns an error if the tile configuration is invalid
    pub fn add_tile<P: AsRef<Path>>(
        mut self,
        path: P,
        x_off: u64,
        y_off: u64,
        width: u64,
        height: u64,
    ) -> Result<Self> {
        let src_rect = PixelRect::new(0, 0, width, height);
        let dst_rect = PixelRect::new(x_off, y_off, width, height);
        let window = SourceWindow::new(src_rect, dst_rect);

        let source = VrtSource::simple(path.as_ref(), 1).with_window(window);
        self.add_source_to_band(1, source)?;
        Ok(self)
    }

    /// Adds a mosaic tile grid (NxM tiles)
    ///
    /// # Errors
    /// Returns an error if the tile configuration is invalid
    pub fn add_tile_grid<P>(
        mut self,
        paths: &[P],
        tile_width: u64,
        tile_height: u64,
        cols: usize,
    ) -> Result<Self>
    where
        P: AsRef<Path>,
    {
        for (idx, path) in paths.iter().enumerate() {
            let col = idx % cols;
            let row = idx / cols;
            let x_off = col as u64 * tile_width;
            let y_off = row as u64 * tile_height;

            let src_rect = PixelRect::new(0, 0, tile_width, tile_height);
            let dst_rect = PixelRect::new(x_off, y_off, tile_width, tile_height);
            let window = SourceWindow::new(src_rect, dst_rect);

            let source = VrtSource::simple(path.as_ref(), 1).with_window(window);
            self.add_source_to_band(1, source)?;
        }

        Ok(self)
    }

    /// Adds a full band configuration
    ///
    /// # Errors
    /// Returns an error if the band is invalid
    pub fn add_band(mut self, band: VrtBand) -> Result<Self> {
        band.validate()?;
        self.dataset.add_band(band);
        Ok(self)
    }

    /// Sets the dataset dimensions explicitly
    pub fn set_dimensions(mut self, width: u64, height: u64) -> Self {
        self.dataset.raster_x_size = width;
        self.dataset.raster_y_size = height;
        self.auto_calculate_extent = false;
        self
    }

    /// Builds the VRT dataset
    ///
    /// # Errors
    /// Returns an error if the dataset configuration is invalid
    pub fn build(mut self) -> Result<VrtDataset> {
        // Auto-calculate extent if needed
        if self.auto_calculate_extent && self.dataset.raster_x_size == 0 {
            self.calculate_extent()?;
        }

        // Set VRT path if provided
        if let Some(path) = self.vrt_path {
            self.dataset.vrt_path = Some(path);
        }

        // Validate the dataset
        self.dataset.validate()?;

        Ok(self.dataset)
    }

    /// Builds and writes the VRT to a file
    ///
    /// # Errors
    /// Returns an error if building or writing fails
    pub fn build_file<P: AsRef<Path>>(mut self, path: P) -> Result<VrtDataset> {
        // Set VRT path for relative source resolution
        let path = path.as_ref();
        self.vrt_path = Some(path.to_path_buf());

        let dataset = self.build()?;
        VrtXmlWriter::write_file(&dataset, path)?;
        Ok(dataset)
    }

    /// Helper to add a source to a specific band
    fn add_source_to_band(&mut self, band_num: usize, source: VrtSource) -> Result<()> {
        source.validate()?;

        // Find or create the band
        let band_idx = band_num - 1;
        while self.dataset.bands.len() <= band_idx {
            let new_band_num = self.dataset.bands.len() + 1;
            let data_type = source
                .data_type
                .or_else(|| source.properties.as_ref().map(|p| p.data_type))
                .unwrap_or(RasterDataType::UInt8);
            self.dataset
                .bands
                .push(VrtBand::new(new_band_num, data_type));
        }

        if let Some(band) = self.dataset.get_band_mut(band_idx) {
            band.add_source(source);
        }

        Ok(())
    }

    /// Calculates extent from sources
    fn calculate_extent(&mut self) -> Result<()> {
        if self.dataset.bands.is_empty() {
            return Err(VrtError::invalid_structure(
                "No bands to calculate extent from",
            ));
        }

        let mut max_x = 0u64;
        let mut max_y = 0u64;

        for band in &self.dataset.bands {
            for source in &band.sources {
                if let Some(dst_rect) = source.dst_rect() {
                    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 {
                    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 from sources",
            ));
        }

        self.dataset.raster_x_size = max_x;
        self.dataset.raster_y_size = max_y;

        Ok(())
    }
}

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

/// Convenience builder for simple mosaic VRTs
pub struct MosaicBuilder {
    builder: VrtBuilder,
    tile_width: u64,
    tile_height: u64,
    current_x: u64,
    current_y: u64,
    max_x: u64,
    max_y: u64,
}

impl MosaicBuilder {
    /// Creates a new mosaic builder
    pub fn new(tile_width: u64, tile_height: u64) -> Self {
        Self {
            builder: VrtBuilder::new(),
            tile_width,
            tile_height,
            current_x: 0,
            current_y: 0,
            max_x: 0,
            max_y: 0,
        }
    }

    /// Adds a tile at the current position and advances
    ///
    /// # Errors
    /// Returns an error if the tile cannot be added
    pub fn add_tile<P: AsRef<Path>>(mut self, path: P) -> Result<Self> {
        let src_rect = PixelRect::new(0, 0, self.tile_width, self.tile_height);
        let dst_rect = PixelRect::new(
            self.current_x,
            self.current_y,
            self.tile_width,
            self.tile_height,
        );
        let window = SourceWindow::new(src_rect, dst_rect);

        let source = VrtSource::simple(path.as_ref(), 1).with_window(window);
        self.builder.add_source_to_band(1, source)?;

        // Update extent tracking
        self.max_x = self.max_x.max(self.current_x + self.tile_width);
        self.max_y = self.max_y.max(self.current_y + self.tile_height);

        Ok(self)
    }

    /// Adds a tile at a specific position
    ///
    /// # Errors
    /// Returns an error if the tile cannot be added
    pub fn add_tile_at<P: AsRef<Path>>(mut self, path: P, x: u64, y: u64) -> Result<Self> {
        let src_rect = PixelRect::new(0, 0, self.tile_width, self.tile_height);
        let dst_rect = PixelRect::new(x, y, self.tile_width, self.tile_height);
        let window = SourceWindow::new(src_rect, dst_rect);

        let source = VrtSource::simple(path.as_ref(), 1).with_window(window);
        self.builder.add_source_to_band(1, source)?;

        // Update extent tracking
        self.max_x = self.max_x.max(x + self.tile_width);
        self.max_y = self.max_y.max(y + self.tile_height);

        Ok(self)
    }

    /// Moves to the next column
    pub fn next_column(mut self) -> Self {
        self.current_x += self.tile_width;
        self
    }

    /// Moves to the next row
    pub fn next_row(mut self) -> Self {
        self.current_x = 0;
        self.current_y += self.tile_height;
        self
    }

    /// Sets the current position
    pub fn at(mut self, x: u64, y: u64) -> Self {
        self.current_x = x;
        self.current_y = y;
        self
    }

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

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

    /// Builds the mosaic VRT
    ///
    /// # Errors
    /// Returns an error if building fails
    pub fn build(mut self) -> Result<VrtDataset> {
        // Set dimensions based on extent
        self.builder = self.builder.set_dimensions(self.max_x, self.max_y);
        self.builder.build()
    }

    /// Builds and writes the mosaic VRT to a file
    ///
    /// # Errors
    /// Returns an error if building or writing fails
    pub fn build_file<P: AsRef<Path>>(mut self, path: P) -> Result<VrtDataset> {
        // Set dimensions based on extent
        self.builder = self.builder.set_dimensions(self.max_x, self.max_y);
        self.builder.build_file(path)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_vrt_builder() {
        let builder = VrtBuilder::with_size(512, 512);
        let result = builder.add_source("/test1.tif", 1, 1);

        assert!(result.is_ok());
        let builder = result.expect("Should add source");
        let dataset = builder.build();
        assert!(dataset.is_ok());
    }

    #[test]
    fn test_mosaic_builder() {
        let builder = MosaicBuilder::new(256, 256);
        let result = builder.add_tile("/tile1.tif");

        assert!(result.is_ok());
        let builder = result.expect("Should add tile");
        let result = builder.next_column().add_tile("/tile2.tif");
        assert!(result.is_ok());

        let builder = result.expect("Should add tile");
        let dataset = builder.build();
        assert!(dataset.is_ok());
        let ds = dataset.expect("Should build");
        assert_eq!(ds.band_count(), 1);
    }

    #[test]
    fn test_tile_grid() {
        let paths = vec!["/tile1.tif", "/tile2.tif", "/tile3.tif", "/tile4.tif"];
        let builder = VrtBuilder::new();
        let result = builder.add_tile_grid(&paths, 256, 256, 2);

        assert!(result.is_ok());
        let builder = result.expect("Should add tiles");
        let result = builder.set_dimensions(512, 512).build();
        assert!(result.is_ok());
    }

    #[test]
    fn test_geo_transform() {
        let gt = GeoTransform {
            origin_x: 0.0,
            pixel_width: 1.0,
            row_rotation: 0.0,
            origin_y: 0.0,
            col_rotation: 0.0,
            pixel_height: -1.0,
        };

        let builder = VrtBuilder::with_size(512, 512);
        let result = builder
            .with_geo_transform(gt)
            .with_srs("EPSG:4326")
            .add_source("/test.tif", 1, 1);

        assert!(result.is_ok());
        let builder = result.expect("Should configure");
        let dataset = builder.build();
        assert!(dataset.is_ok());
        let ds = dataset.expect("Should build");
        assert!(ds.geo_transform.is_some());
        assert!(ds.srs.is_some());
    }
}