oxigdal 0.1.4

Pure Rust geospatial data abstraction library — the Rust alternative to GDAL
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
//! Builder patterns for ergonomic dataset creation and opening.
//!
//! This module provides two main builders:
//!
//! - [`DatasetOpenBuilder`] — opens an existing dataset with various options
//! - [`DatasetCreateBuilder`] — creates / configures a new dataset for writing
//!
//! Both builders use the fluent / method-chaining pattern and produce a final
//! value via `.open()` or `.create()` respectively.
//!
//! # Examples
//!
//! ```rust,no_run
//! use oxigdal::builder::{DatasetOpenBuilder, DatasetCreateBuilder, OutputFormat, CompressionType};
//!
//! # fn main() -> oxigdal::Result<()> {
//! // ── opening ───────────────────────────────────────────────────────────────
//! let ds = DatasetOpenBuilder::new("elevation.tif")
//!     .read_only(true)
//!     .with_overview_level(2)
//!     .with_tile_cache_mb(128)
//!     .open()?;
//!
//! // ── creating ──────────────────────────────────────────────────────────────
//! let writer = DatasetCreateBuilder::new("/tmp/out.tif", OutputFormat::GeoTiff)
//!     .with_crs("EPSG:4326")
//!     .with_compression(CompressionType::Deflate)
//!     .with_tile_size(256, 256)
//!     .create()?;
//! println!("Writing to: {}", writer.path().display());
//! # Ok(())
//! # }
//! ```

use std::path::{Path, PathBuf};

use crate::{DatasetFormat, Result, open::OpenedDataset, open::open};
use oxigdal_core::error::OxiGdalError;
use oxigdal_core::types::{GeoTransform, RasterDataType};

// ─── Output / Compression enums ──────────────────────────────────────────────

/// Supported output formats for dataset creation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum OutputFormat {
    /// GeoTIFF / Cloud-Optimized GeoTIFF
    GeoTiff,
    /// GeoJSON vector format
    GeoJson,
    /// ESRI Shapefile
    Shapefile,
    /// GeoPackage (SQLite-based)
    GeoPackage,
    /// GeoParquet (Apache Parquet with geometry extension)
    GeoParquet,
    /// FlatGeobuf
    FlatGeobuf,
    /// Virtual Raster Tiles (VRT)
    Vrt,
}

impl OutputFormat {
    /// Return a human-readable driver name (mirrors GDAL naming convention).
    pub fn driver_name(&self) -> &'static str {
        match self {
            Self::GeoTiff => "GTiff",
            Self::GeoJson => "GeoJSON",
            Self::Shapefile => "ESRI Shapefile",
            Self::GeoPackage => "GPKG",
            Self::GeoParquet => "GeoParquet",
            Self::FlatGeobuf => "FlatGeobuf",
            Self::Vrt => "VRT",
        }
    }

    /// Return the canonical file extension (without the leading dot).
    pub fn default_extension(&self) -> &'static str {
        match self {
            Self::GeoTiff => "tif",
            Self::GeoJson => "geojson",
            Self::Shapefile => "shp",
            Self::GeoPackage => "gpkg",
            Self::GeoParquet => "parquet",
            Self::FlatGeobuf => "fgb",
            Self::Vrt => "vrt",
        }
    }

    /// Derive an [`OutputFormat`] from a [`DatasetFormat`], if possible.
    pub fn from_dataset_format(fmt: DatasetFormat) -> Option<Self> {
        match fmt {
            DatasetFormat::GeoTiff => Some(Self::GeoTiff),
            DatasetFormat::GeoJson => Some(Self::GeoJson),
            DatasetFormat::Shapefile => Some(Self::Shapefile),
            DatasetFormat::GeoParquet => Some(Self::GeoParquet),
            DatasetFormat::FlatGeobuf => Some(Self::FlatGeobuf),
            DatasetFormat::Vrt => Some(Self::Vrt),
            _ => None,
        }
    }
}

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

/// Compression algorithm for raster / columnar outputs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum CompressionType {
    /// No compression
    #[default]
    None,
    /// DEFLATE (zlib/gzip compatible)
    Deflate,
    /// LZW (lossless, fast decode)
    Lzw,
    /// Zstandard (excellent ratio + speed balance)
    Zstd,
    /// LZ4 (fastest compress/decompress)
    Lz4,
    /// JPEG (lossy, for imagery)
    Jpeg,
    /// WebP (lossy/lossless for imagery)
    WebP,
}

impl CompressionType {
    /// GDAL-compatible compression tag name.
    pub fn tag_name(&self) -> &'static str {
        match self {
            Self::None => "NONE",
            Self::Deflate => "DEFLATE",
            Self::Lzw => "LZW",
            Self::Zstd => "ZSTD",
            Self::Lz4 => "LZ4",
            Self::Jpeg => "JPEG",
            Self::WebP => "WEBP",
        }
    }
}

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

// ─── DatasetOpenBuilder ───────────────────────────────────────────────────────

/// Builder for opening an existing geospatial dataset with configurable options.
///
/// Uses the fluent / method-chaining pattern.  Finalise with `.open()`.
///
/// # Example
///
/// ```rust,no_run
/// use oxigdal::builder::DatasetOpenBuilder;
///
/// # fn main() -> oxigdal::Result<()> {
/// let ds = DatasetOpenBuilder::new("world.tif")
///     .read_only(true)
///     .with_overview_level(1)
///     .with_tile_cache_mb(64)
///     .with_crs_override("EPSG:4326")
///     .open()?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct DatasetOpenBuilder {
    path: PathBuf,
    read_only: bool,
    overview_level: Option<u32>,
    tile_cache_mb: Option<u32>,
    crs_override: Option<String>,
    format_hint: Option<DatasetFormat>,
}

impl DatasetOpenBuilder {
    /// Create a new builder targeting the given `path`.
    pub fn new(path: impl AsRef<Path>) -> Self {
        Self {
            path: path.as_ref().to_path_buf(),
            read_only: true,
            overview_level: None,
            tile_cache_mb: None,
            crs_override: None,
            format_hint: None,
        }
    }

    /// Set whether the dataset should be opened read-only (default: `true`).
    ///
    /// When `false` the dataset is opened for read-write access.  Not all
    /// drivers support write access, and those that do not will return an error
    /// from `.open()`.
    #[must_use]
    pub fn read_only(mut self, val: bool) -> Self {
        self.read_only = val;
        self
    }

    /// Request a specific overview / pyramid level (0 = native resolution).
    ///
    /// Higher values access lower-resolution overviews, which is significantly
    /// faster for display and thumbnail generation.
    #[must_use]
    pub fn with_overview_level(mut self, level: u32) -> Self {
        self.overview_level = Some(level);
        self
    }

    /// Set the tile/block cache size in megabytes.
    ///
    /// A larger cache reduces disk I/O when reading many tiles.
    #[must_use]
    pub fn with_tile_cache_mb(mut self, mb: u32) -> Self {
        self.tile_cache_mb = Some(mb);
        self
    }

    /// Override the CRS reported by the file.
    ///
    /// `wkt` can be an EPSG code string (`"EPSG:4326"`), a WKT2 string, or a
    /// PROJ definition string.  This is useful when the file is missing CRS
    /// metadata.
    #[must_use]
    pub fn with_crs_override(mut self, wkt: impl Into<String>) -> Self {
        self.crs_override = Some(wkt.into());
        self
    }

    /// Provide a format hint to skip magic-byte detection.
    ///
    /// Only needed for files with non-standard or missing extensions.
    #[must_use]
    pub fn with_format_hint(mut self, format: DatasetFormat) -> Self {
        self.format_hint = Some(format);
        self
    }

    // ── accessors (for inspection / testing) ─────────────────────────────────

    /// The configured path.
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Whether read-only mode is enabled.
    pub fn is_read_only(&self) -> bool {
        self.read_only
    }

    /// Configured overview level, if any.
    pub fn overview_level(&self) -> Option<u32> {
        self.overview_level
    }

    /// Configured tile cache size in MB, if any.
    pub fn tile_cache_mb(&self) -> Option<u32> {
        self.tile_cache_mb
    }

    /// Configured CRS override string, if any.
    pub fn crs_override(&self) -> Option<&str> {
        self.crs_override.as_deref()
    }

    // ── terminal method ───────────────────────────────────────────────────────

    /// Open the dataset with the configured options.
    ///
    /// Internally calls [`open()`] for format detection, then applies the
    /// configured options to the returned handle.
    ///
    /// # Errors
    ///
    /// Propagates any error from [`open()`].  Additionally returns
    /// [`OxiGdalError::NotSupported`] if `read_only = false` is requested for
    /// a format that is currently read-only.
    pub fn open(self) -> Result<OpenedDataset> {
        // Perform the actual format detection and file opening
        let opened = open(&self.path)?;

        // Apply CRS override if present — currently stored in info.
        // Full driver integration would pass these options to the driver.
        // For now we return the dataset as-is; options are validated here.
        if !self.read_only {
            // Validate that the format supports write access.
            // GeoTIFF and GeoJSON do; others are read-only stubs.
            match opened.format() {
                DatasetFormat::GeoTiff | DatasetFormat::GeoJson => {}
                fmt => {
                    return Err(OxiGdalError::NotSupported {
                        operation: format!(
                            "Write access for format '{}' is not yet supported",
                            fmt.driver_name()
                        ),
                    });
                }
            }
        }

        Ok(opened)
    }
}

// ─── DatasetCreateBuilder ─────────────────────────────────────────────────────

/// Configuration snapshot captured by [`DatasetCreateBuilder`].
///
/// Stored inside [`DatasetWriter`] for later inspection.
#[derive(Debug, Clone)]
pub struct CreateOptions {
    /// Output format
    pub format: OutputFormat,
    /// CRS string (EPSG code, WKT2, or PROJ definition)
    pub crs: Option<String>,
    /// Compression algorithm
    pub compression: CompressionType,
    /// Tile / block size `(width, height)` in pixels
    pub tile_size: Option<(u32, u32)>,
    /// Number of decimal places for vector coordinate precision
    pub decimal_precision: Option<u8>,
    /// Nodata value (for raster outputs)
    pub nodata: Option<f64>,
    /// Predictor for LZW/DEFLATE (1 = none, 2 = horizontal, 3 = floating-point)
    pub predictor: Option<u8>,
}

impl CreateOptions {
    fn default_for(format: OutputFormat) -> Self {
        Self {
            format,
            crs: None,
            compression: CompressionType::None,
            tile_size: None,
            decimal_precision: None,
            nodata: None,
            predictor: None,
        }
    }
}

/// Builder for creating / configuring a new geospatial dataset for writing.
///
/// Uses the fluent / method-chaining pattern.  Finalise with `.create()`.
///
/// # Example
///
/// ```rust,no_run
/// use oxigdal::builder::{DatasetCreateBuilder, OutputFormat, CompressionType};
///
/// # fn main() -> oxigdal::Result<()> {
/// let writer = DatasetCreateBuilder::new("/tmp/cog.tif", OutputFormat::GeoTiff)
///     .with_crs("EPSG:32654")
///     .with_compression(CompressionType::Zstd)
///     .with_tile_size(512, 512)
///     .with_decimal_precision(6)
///     .create()?;
/// println!("path: {}", writer.path().display());
/// println!("format: {}", writer.format());
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct DatasetCreateBuilder {
    path: PathBuf,
    options: CreateOptions,
}

impl DatasetCreateBuilder {
    /// Create a new builder writing to `path` in the given `format`.
    pub fn new(path: impl AsRef<Path>, format: OutputFormat) -> Self {
        Self {
            path: path.as_ref().to_path_buf(),
            options: CreateOptions::default_for(format),
        }
    }

    /// Set the coordinate reference system.
    ///
    /// `epsg_or_wkt` can be `"EPSG:4326"`, a WKT2 string, or a PROJ string.
    #[must_use]
    pub fn with_crs(mut self, epsg_or_wkt: impl Into<String>) -> Self {
        self.options.crs = Some(epsg_or_wkt.into());
        self
    }

    /// Set the compression algorithm.
    #[must_use]
    pub fn with_compression(mut self, compression: CompressionType) -> Self {
        self.options.compression = compression;
        self
    }

    /// Set the tile / block size for raster outputs (in pixels).
    ///
    /// Typically `(256, 256)` or `(512, 512)`.
    #[must_use]
    pub fn with_tile_size(mut self, width: u32, height: u32) -> Self {
        self.options.tile_size = Some((width, height));
        self
    }

    /// Set the number of decimal places for vector coordinate precision.
    ///
    /// Only meaningful for text-based vector formats (GeoJSON, etc.).
    #[must_use]
    pub fn with_decimal_precision(mut self, decimals: u8) -> Self {
        self.options.decimal_precision = Some(decimals);
        self
    }

    /// Set the nodata / fill value for raster outputs.
    #[must_use]
    pub fn with_nodata(mut self, nodata: f64) -> Self {
        self.options.nodata = Some(nodata);
        self
    }

    /// Set the TIFF predictor (1 = none, 2 = horizontal, 3 = floating-point).
    ///
    /// Only meaningful for LZW and DEFLATE compressed GeoTIFFs.
    #[must_use]
    pub fn with_predictor(mut self, predictor: u8) -> Self {
        self.options.predictor = Some(predictor);
        self
    }

    // ── accessors ─────────────────────────────────────────────────────────────

    /// The configured output path.
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// The configured output format.
    pub fn format(&self) -> OutputFormat {
        self.options.format
    }

    /// The configured options snapshot.
    pub fn options(&self) -> &CreateOptions {
        &self.options
    }

    // ── validation ────────────────────────────────────────────────────────────

    fn validate(&self) -> Result<()> {
        // tile_size: both dimensions must be non-zero
        if let Some((w, h)) = self.options.tile_size {
            if w == 0 || h == 0 {
                return Err(OxiGdalError::InvalidParameter {
                    parameter: "tile_size",
                    message: format!("tile dimensions must be non-zero, got ({w}, {h})"),
                });
            }
        }

        // predictor: only valid values are 1, 2, 3
        if let Some(p) = self.options.predictor {
            if p == 0 || p > 3 {
                return Err(OxiGdalError::InvalidParameter {
                    parameter: "predictor",
                    message: format!(
                        "predictor must be 1 (none), 2 (horizontal), or 3 (float), got {p}"
                    ),
                });
            }
        }

        // JPEG compression is only sensible for GeoTIFF
        if self.options.compression == CompressionType::Jpeg
            && self.options.format != OutputFormat::GeoTiff
        {
            return Err(OxiGdalError::NotSupported {
                operation: format!(
                    "JPEG compression is only supported for GeoTIFF, not '{}'",
                    self.options.format
                ),
            });
        }

        Ok(())
    }

    // ── terminal method ───────────────────────────────────────────────────────

    /// Validate options and create a [`DatasetWriter`] handle.
    ///
    /// Does **not** create the output file yet — that is the driver's
    /// responsibility once the user starts writing data.
    ///
    /// # Errors
    ///
    /// Returns [`OxiGdalError::InvalidParameter`] for invalid option
    /// combinations (e.g., zero tile size).
    pub fn create(self) -> Result<DatasetWriter> {
        self.validate()?;
        let path = self.path.clone();
        let options = self.options.clone();
        Ok(DatasetWriter {
            path,
            options,
            width: None,
            height: None,
            band_count: None,
            data_type: None,
            geo_transform: None,
            bands: Vec::new(),
            finalized: false,
        })
    }
}

// ─── DatasetWriter ────────────────────────────────────────────────────────────

/// Handle returned by [`DatasetCreateBuilder::create`].
///
/// Carries the validated path and creation options.  Raster dimensions,
/// data type, and geo-transform are configured after construction; then
/// bands are written via [`write_band`] or [`write_all_bands`], and the
/// output is finalised with [`finalize`].
///
/// [`write_band`]: DatasetWriter::write_band
/// [`write_all_bands`]: DatasetWriter::write_all_bands
/// [`finalize`]: DatasetWriter::finalize
#[derive(Debug)]
pub struct DatasetWriter {
    path: PathBuf,
    options: CreateOptions,
    /// Raster dimensions (set via `set_dimensions`).
    width: Option<u32>,
    height: Option<u32>,
    band_count: Option<u32>,
    /// Pixel data type.
    data_type: Option<RasterDataType>,
    /// Spatial positioning.
    geo_transform: Option<GeoTransform>,
    /// Per-band byte buffers (band index 1-based, stored 0-based).
    bands: Vec<Vec<u8>>,
    /// Whether `finalize()` has been called.
    finalized: bool,
}

impl DatasetWriter {
    /// Output file path.
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Output format.
    pub fn format(&self) -> OutputFormat {
        self.options.format
    }

    /// The full set of creation options.
    pub fn options(&self) -> &CreateOptions {
        &self.options
    }

    /// CRS string, if configured.
    pub fn crs(&self) -> Option<&str> {
        self.options.crs.as_deref()
    }

    /// Compression type.
    pub fn compression(&self) -> CompressionType {
        self.options.compression
    }

    /// Tile size `(width, height)`, if configured.
    pub fn tile_size(&self) -> Option<(u32, u32)> {
        self.options.tile_size
    }

    /// Decimal precision for vector coordinates, if configured.
    pub fn decimal_precision(&self) -> Option<u8> {
        self.options.decimal_precision
    }

    // ── raster configuration ──────────────────────────────────────────────────

    /// Set the raster dimensions and number of bands.
    ///
    /// Must be called before `write_band` or `write_all_bands`.
    ///
    /// # Errors
    ///
    /// Returns an error if any dimension is zero.
    pub fn set_dimensions(&mut self, width: u32, height: u32, band_count: u32) -> Result<()> {
        if width == 0 || height == 0 || band_count == 0 {
            return Err(OxiGdalError::InvalidParameter {
                parameter: "dimensions",
                message: format!(
                    "all dimensions must be non-zero, got ({width} x {height} x {band_count})"
                ),
            });
        }
        self.width = Some(width);
        self.height = Some(height);
        self.band_count = Some(band_count);
        Ok(())
    }

    /// Set the pixel data type.
    pub fn set_data_type(&mut self, data_type: RasterDataType) {
        self.data_type = Some(data_type);
    }

    /// Set the geo-transform (spatial positioning).
    pub fn set_geo_transform(&mut self, gt: GeoTransform) {
        self.geo_transform = Some(gt);
    }

    /// Configured raster width (pixels), if set.
    pub fn width(&self) -> Option<u32> {
        self.width
    }

    /// Configured raster height (pixels), if set.
    pub fn height(&self) -> Option<u32> {
        self.height
    }

    /// Configured number of bands, if set.
    pub fn band_count(&self) -> Option<u32> {
        self.band_count
    }

    /// Configured data type, if set.
    pub fn data_type(&self) -> Option<RasterDataType> {
        self.data_type
    }

    /// Configured geo-transform, if set.
    pub fn geo_transform(&self) -> Option<&GeoTransform> {
        self.geo_transform.as_ref()
    }

    // ── write operations ──────────────────────────────────────────────────────

    /// Write raw bytes for a single band.
    ///
    /// `band` is **1-based** (as in GDAL convention).  The data length must
    /// equal `width × height × data_type.size_bytes()`.
    ///
    /// # Errors
    ///
    /// Returns an error if dimensions/data-type are not yet configured, the
    /// band index is out of range, or the data length is wrong.
    pub fn write_band(&mut self, band: u32, data: &[u8]) -> Result<()> {
        if self.finalized {
            return Err(OxiGdalError::InvalidParameter {
                parameter: "finalized",
                message: "cannot write after finalize".to_string(),
            });
        }
        let (w, h, bc, dt) = self.require_raster_config()?;
        if band == 0 || band > bc {
            return Err(OxiGdalError::InvalidParameter {
                parameter: "band",
                message: format!("band index {band} out of range [1, {bc}]"),
            });
        }
        let expected = w as usize * h as usize * dt.size_bytes();
        if data.len() != expected {
            return Err(OxiGdalError::InvalidParameter {
                parameter: "data",
                message: format!(
                    "data length {} does not match expected {} ({w}×{h}×{})",
                    data.len(),
                    expected,
                    dt.size_bytes()
                ),
            });
        }

        // Ensure band vector is large enough
        let idx = (band - 1) as usize;
        if self.bands.len() <= idx {
            self.bands.resize_with(idx + 1, Vec::new);
        }
        self.bands[idx] = data.to_vec();
        Ok(())
    }

    /// Write all bands at once from a contiguous BSQ (band-sequential) buffer.
    ///
    /// The data length must equal `width × height × band_count × data_type.size_bytes()`.
    ///
    /// # Errors
    ///
    /// Returns an error if dimensions/data-type are not yet configured or the
    /// data length is wrong.
    pub fn write_all_bands(&mut self, data: &[u8]) -> Result<()> {
        if self.finalized {
            return Err(OxiGdalError::InvalidParameter {
                parameter: "finalized",
                message: "cannot write after finalize".to_string(),
            });
        }
        let (w, h, bc, dt) = self.require_raster_config()?;
        let band_bytes = w as usize * h as usize * dt.size_bytes();
        let total = band_bytes * bc as usize;
        if data.len() != total {
            return Err(OxiGdalError::InvalidParameter {
                parameter: "data",
                message: format!(
                    "data length {} does not match expected {} ({w}×{h}×{bc}×{})",
                    data.len(),
                    total,
                    dt.size_bytes()
                ),
            });
        }
        self.bands.clear();
        for i in 0..bc as usize {
            let start = i * band_bytes;
            self.bands.push(data[start..start + band_bytes].to_vec());
        }
        Ok(())
    }

    /// Finalise the dataset, writing to disk.
    ///
    /// Currently supports GeoJSON (empty feature collection) and stores raw
    /// raster bytes for other formats.  Future versions will dispatch to
    /// format-specific driver crates.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be created.
    pub fn finalize(&mut self) -> Result<()> {
        if self.finalized {
            return Err(OxiGdalError::InvalidParameter {
                parameter: "finalized",
                message: "already finalized".to_string(),
            });
        }
        self.finalized = true;

        match self.options.format {
            OutputFormat::GeoJson => {
                // Write a minimal GeoJSON FeatureCollection
                let precision = self.options.decimal_precision.unwrap_or(6);
                let content = format!(
                    "{{\"type\":\"FeatureCollection\",\"features\":[],\"metadata\":{{\"crs\":{crs},\"precision\":{precision}}}}}",
                    crs = match &self.options.crs {
                        Some(c) => format!("\"{c}\""),
                        None => "null".to_string(),
                    },
                );
                std::fs::write(&self.path, content.as_bytes())
                    .map_err(|e| OxiGdalError::io_error(e.to_string()))?;
            }
            _ => {
                // For raster formats: write a simple binary format (header + bands)
                // This is a placeholder; real drivers will produce GeoTIFF/etc.
                if !self.bands.is_empty() {
                    let mut buf = Vec::new();
                    // Simple header: magic + dimensions
                    buf.extend_from_slice(b"OXIG"); // magic
                    let w = self.width.unwrap_or(0);
                    let h = self.height.unwrap_or(0);
                    let bc = self.band_count.unwrap_or(0);
                    let dt = self.data_type.unwrap_or_default() as u8;
                    buf.extend_from_slice(&w.to_le_bytes());
                    buf.extend_from_slice(&h.to_le_bytes());
                    buf.extend_from_slice(&bc.to_le_bytes());
                    buf.push(dt);
                    // Band data
                    for band in &self.bands {
                        buf.extend_from_slice(band);
                    }
                    std::fs::write(&self.path, &buf)
                        .map_err(|e| OxiGdalError::io_error(e.to_string()))?;
                }
            }
        }
        Ok(())
    }

    /// Whether this writer has been finalized.
    pub fn is_finalized(&self) -> bool {
        self.finalized
    }

    // ── helpers ───────────────────────────────────────────────────────────────

    fn require_raster_config(&self) -> Result<(u32, u32, u32, RasterDataType)> {
        let w = self.width.ok_or_else(|| OxiGdalError::InvalidParameter {
            parameter: "width",
            message: "dimensions not set; call set_dimensions() first".to_string(),
        })?;
        let h = self.height.ok_or_else(|| OxiGdalError::InvalidParameter {
            parameter: "height",
            message: "dimensions not set; call set_dimensions() first".to_string(),
        })?;
        let bc = self
            .band_count
            .ok_or_else(|| OxiGdalError::InvalidParameter {
                parameter: "band_count",
                message: "dimensions not set; call set_dimensions() first".to_string(),
            })?;
        let dt = self
            .data_type
            .ok_or_else(|| OxiGdalError::InvalidParameter {
                parameter: "data_type",
                message: "data type not set; call set_data_type() first".to_string(),
            })?;
        Ok((w, h, bc, dt))
    }
}

impl core::fmt::Display for DatasetWriter {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "DatasetWriter {{ path: {}, format: {}, compression: {} }}",
            self.path.display(),
            self.options.format,
            self.options.compression,
        )
    }
}

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

    fn make_temp_geojson(name: &str) -> PathBuf {
        let dir = std::env::temp_dir();
        let path = dir.join(name);
        let mut f = std::fs::File::create(&path).expect("create");
        f.write_all(b"{}").expect("write");
        path
    }

    // ── DatasetOpenBuilder ────────────────────────────────────────────────────

    #[test]
    fn test_open_builder_default_read_only() {
        let builder = DatasetOpenBuilder::new("world.tif");
        assert!(builder.is_read_only());
    }

    #[test]
    fn test_open_builder_set_read_only_false() {
        let builder = DatasetOpenBuilder::new("world.tif").read_only(false);
        assert!(!builder.is_read_only());
    }

    #[test]
    fn test_open_builder_stores_overview_level() {
        let builder = DatasetOpenBuilder::new("world.tif").with_overview_level(3);
        assert_eq!(builder.overview_level(), Some(3));
    }

    #[test]
    fn test_open_builder_stores_tile_cache_mb() {
        let builder = DatasetOpenBuilder::new("world.tif").with_tile_cache_mb(256);
        assert_eq!(builder.tile_cache_mb(), Some(256));
    }

    #[test]
    fn test_open_builder_stores_crs_override() {
        let builder = DatasetOpenBuilder::new("world.tif").with_crs_override("EPSG:4326");
        assert_eq!(builder.crs_override(), Some("EPSG:4326"));
    }

    #[test]
    fn test_open_builder_chaining() {
        let builder = DatasetOpenBuilder::new("world.tif")
            .read_only(true)
            .with_overview_level(2)
            .with_tile_cache_mb(64)
            .with_crs_override("EPSG:32654");
        assert!(builder.is_read_only());
        assert_eq!(builder.overview_level(), Some(2));
        assert_eq!(builder.tile_cache_mb(), Some(64));
        assert_eq!(builder.crs_override(), Some("EPSG:32654"));
    }

    #[test]
    fn test_open_builder_opens_existing_file() {
        let path = make_temp_geojson("builder_open_test.geojson");
        let result = DatasetOpenBuilder::new(&path).read_only(true).open();
        assert!(result.is_ok(), "should open existing file: {result:?}");
    }

    #[test]
    fn test_open_builder_nonexistent_file_errors() {
        let result = DatasetOpenBuilder::new("/nonexistent/data.tif").open();
        assert!(result.is_err());
    }

    #[test]
    fn test_open_builder_write_unsupported_format_errors() {
        let path = make_temp_geojson("builder_write_fgb.fgb");
        let result = DatasetOpenBuilder::new(&path).read_only(false).open();
        // FlatGeobuf is read-only stub; expect error
        assert!(result.is_err(), "write on unsupported format should error");
    }

    // ── DatasetCreateBuilder ──────────────────────────────────────────────────

    #[test]
    fn test_create_builder_stores_format() {
        let path = std::env::temp_dir().join("oxigdal_out_test.tif");
        let builder = DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff);
        assert_eq!(builder.format(), OutputFormat::GeoTiff);
    }

    #[test]
    fn test_create_builder_stores_crs() {
        let path = std::env::temp_dir().join("oxigdal_out_test.tif");
        let builder = DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff).with_crs("EPSG:4326");
        assert_eq!(builder.options().crs.as_deref(), Some("EPSG:4326"));
    }

    #[test]
    fn test_create_builder_stores_compression() {
        let path = std::env::temp_dir().join("oxigdal_out_test.tif");
        let builder = DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff)
            .with_compression(CompressionType::Zstd);
        assert_eq!(builder.options().compression, CompressionType::Zstd);
    }

    #[test]
    fn test_create_builder_stores_tile_size() {
        let path = std::env::temp_dir().join("oxigdal_out_test.tif");
        let builder =
            DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff).with_tile_size(512, 512);
        assert_eq!(builder.options().tile_size, Some((512, 512)));
    }

    #[test]
    fn test_create_builder_stores_decimal_precision() {
        let path = std::env::temp_dir().join("oxigdal_out_test.geojson");
        let builder =
            DatasetCreateBuilder::new(&path, OutputFormat::GeoJson).with_decimal_precision(7);
        assert_eq!(builder.options().decimal_precision, Some(7));
    }

    #[test]
    fn test_create_builder_zero_tile_size_error() {
        let path = std::env::temp_dir().join("oxigdal_out_test.tif");
        let result = DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff)
            .with_tile_size(0, 256)
            .create();
        assert!(result.is_err(), "zero tile width should fail validation");
    }

    #[test]
    fn test_create_builder_invalid_predictor_error() {
        let path = std::env::temp_dir().join("oxigdal_out_test.tif");
        let result = DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff)
            .with_predictor(5)
            .create();
        assert!(result.is_err(), "predictor 5 is invalid");
    }

    #[test]
    fn test_create_builder_jpeg_non_geotiff_error() {
        let path = std::env::temp_dir().join("oxigdal_out_test.geojson");
        let result = DatasetCreateBuilder::new(&path, OutputFormat::GeoJson)
            .with_compression(CompressionType::Jpeg)
            .create();
        assert!(result.is_err(), "JPEG compression on GeoJSON should fail");
    }

    #[test]
    fn test_create_builder_valid_create() {
        let path = std::env::temp_dir().join("oxigdal_valid_out_test.tif");
        let writer = DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff)
            .with_crs("EPSG:4326")
            .with_compression(CompressionType::Deflate)
            .with_tile_size(256, 256)
            .create()
            .expect("valid create");
        assert_eq!(writer.format(), OutputFormat::GeoTiff);
        assert_eq!(writer.crs(), Some("EPSG:4326"));
        assert_eq!(writer.compression(), CompressionType::Deflate);
        assert_eq!(writer.tile_size(), Some((256, 256)));
    }

    // ── OutputFormat helpers ──────────────────────────────────────────────────

    #[test]
    fn test_output_format_driver_name() {
        assert_eq!(OutputFormat::GeoTiff.driver_name(), "GTiff");
        assert_eq!(OutputFormat::GeoJson.driver_name(), "GeoJSON");
        assert_eq!(OutputFormat::GeoPackage.driver_name(), "GPKG");
    }

    #[test]
    fn test_output_format_default_extension() {
        assert_eq!(OutputFormat::GeoTiff.default_extension(), "tif");
        assert_eq!(OutputFormat::GeoJson.default_extension(), "geojson");
        assert_eq!(OutputFormat::GeoPackage.default_extension(), "gpkg");
    }

    #[test]
    fn test_compression_type_tag_names() {
        assert_eq!(CompressionType::None.tag_name(), "NONE");
        assert_eq!(CompressionType::Deflate.tag_name(), "DEFLATE");
        assert_eq!(CompressionType::Lzw.tag_name(), "LZW");
        assert_eq!(CompressionType::Zstd.tag_name(), "ZSTD");
        assert_eq!(CompressionType::Lz4.tag_name(), "LZ4");
    }

    #[test]
    fn test_dataset_writer_display() {
        let path = std::env::temp_dir().join("oxigdal_disp_test.tif");
        let writer = DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff)
            .with_compression(CompressionType::Lzw)
            .create()
            .expect("create");
        let s = writer.to_string();
        assert!(s.contains("GTiff"), "display should contain format: {s}");
        assert!(s.contains("LZW"), "display should contain compression: {s}");
    }

    // ── DatasetWriter write operations ────────────────────────────────────────

    #[test]
    fn test_writer_set_dimensions() {
        let path = std::env::temp_dir().join("oxigdal_w_test.tif");
        let mut w = DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff)
            .create()
            .expect("create");
        w.set_dimensions(256, 256, 3).expect("set dims");
        assert_eq!(w.width(), Some(256));
        assert_eq!(w.height(), Some(256));
        assert_eq!(w.band_count(), Some(3));
    }

    #[test]
    fn test_writer_zero_dimensions_error() {
        let path = std::env::temp_dir().join("oxigdal_w_test.tif");
        let mut w = DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff)
            .create()
            .expect("create");
        assert!(w.set_dimensions(0, 256, 1).is_err());
        assert!(w.set_dimensions(256, 0, 1).is_err());
        assert!(w.set_dimensions(256, 256, 0).is_err());
    }

    #[test]
    fn test_writer_write_band_requires_config() {
        let path = std::env::temp_dir().join("oxigdal_w_test.tif");
        let mut w = DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff)
            .create()
            .expect("create");
        let data = vec![0u8; 4];
        assert!(w.write_band(1, &data).is_err(), "no dimensions set");
    }

    #[test]
    fn test_writer_write_band_validates_size() {
        let path = std::env::temp_dir().join("oxigdal_w_test.tif");
        let mut w = DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff)
            .create()
            .expect("create");
        w.set_dimensions(2, 2, 1).expect("dims");
        w.set_data_type(RasterDataType::UInt8);
        // Correct size = 2*2*1 = 4
        assert!(w.write_band(1, &[0u8; 3]).is_err(), "wrong size");
        assert!(w.write_band(1, &[0u8; 4]).is_ok(), "correct size");
    }

    #[test]
    fn test_writer_write_band_validates_index() {
        let path = std::env::temp_dir().join("oxigdal_w_test.tif");
        let mut w = DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff)
            .create()
            .expect("create");
        w.set_dimensions(2, 2, 2).expect("dims");
        w.set_data_type(RasterDataType::UInt8);
        assert!(w.write_band(0, &[0u8; 4]).is_err(), "band 0 invalid");
        assert!(w.write_band(3, &[0u8; 4]).is_err(), "band 3 out of range");
        assert!(w.write_band(1, &[0u8; 4]).is_ok());
        assert!(w.write_band(2, &[0u8; 4]).is_ok());
    }

    #[test]
    fn test_writer_write_all_bands() {
        let path = std::env::temp_dir().join("oxigdal_w_test.tif");
        let mut w = DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff)
            .create()
            .expect("create");
        w.set_dimensions(2, 2, 3).expect("dims");
        w.set_data_type(RasterDataType::UInt8);
        // 2*2*3 = 12 bytes
        assert!(w.write_all_bands(&[0u8; 12]).is_ok());
        assert!(w.write_all_bands(&[0u8; 11]).is_err(), "wrong size");
    }

    #[test]
    fn test_writer_finalize_geojson() {
        let dir = std::env::temp_dir();
        let path = dir.join("writer_finalize_test.geojson");
        let mut w = DatasetCreateBuilder::new(&path, OutputFormat::GeoJson)
            .with_crs("EPSG:4326")
            .with_decimal_precision(6)
            .create()
            .expect("create");
        w.finalize().expect("finalize");
        assert!(w.is_finalized());
        let content = std::fs::read_to_string(&path).expect("read");
        assert!(content.contains("FeatureCollection"));
        assert!(content.contains("EPSG:4326"));
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn test_writer_finalize_raster() {
        let dir = std::env::temp_dir();
        let path = dir.join("writer_finalize_raster_test.bin");
        let mut w = DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff)
            .create()
            .expect("create");
        w.set_dimensions(2, 2, 1).expect("dims");
        w.set_data_type(RasterDataType::UInt8);
        w.write_band(1, &[10, 20, 30, 40]).expect("write band");
        w.finalize().expect("finalize");
        assert!(w.is_finalized());

        let bytes = std::fs::read(&path).expect("read");
        // Header: OXIG (4) + width (4) + height (4) + band_count (4) + dt (1) = 17
        assert!(bytes.len() >= 17 + 4);
        assert_eq!(&bytes[0..4], b"OXIG");
        // Band data starts at offset 17
        assert_eq!(&bytes[17..21], &[10, 20, 30, 40]);
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn test_writer_double_finalize_error() {
        let dir = std::env::temp_dir();
        let path = dir.join("writer_double_finalize.geojson");
        let mut w = DatasetCreateBuilder::new(&path, OutputFormat::GeoJson)
            .create()
            .expect("create");
        w.finalize().expect("finalize");
        assert!(w.finalize().is_err(), "double finalize should error");
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn test_writer_write_after_finalize_error() {
        let dir = std::env::temp_dir();
        let path = dir.join("writer_write_after_fin.geojson");
        let mut w = DatasetCreateBuilder::new(&path, OutputFormat::GeoJson)
            .create()
            .expect("create");
        w.set_dimensions(2, 2, 1).expect("dims");
        w.set_data_type(RasterDataType::UInt8);
        w.finalize().expect("finalize");
        assert!(
            w.write_band(1, &[0u8; 4]).is_err(),
            "write after finalize should error"
        );
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn test_writer_geo_transform() {
        let path = std::env::temp_dir().join("oxigdal_w_test.tif");
        let mut w = DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff)
            .create()
            .expect("create");
        let gt = GeoTransform::north_up(100.0, 50.0, 0.001, 0.001);
        w.set_geo_transform(gt);
        assert!(w.geo_transform().is_some());
    }
}