stac 0.16.5

Rust library for the SpatioTemporal Asset Catalog (STAC) specification
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
//! Read data from and write data in [stac-geoparquet](https://github.com/stac-utils/stac-geoparquet/blob/main/spec/stac-geoparquet-spec.md).

use crate::{
    Catalog, Collection, Error, Item, ItemCollection, Result, Value,
    geoarrow::{Encoder, Options},
};
use arrow_array::RecordBatch;
use bytes::Bytes;
use geoparquet::{
    reader::{GeoParquetReaderBuilder, GeoParquetRecordBatchReader},
    writer::{GeoParquetRecordBatchEncoder, GeoParquetWriterOptionsBuilder},
};
pub use parquet::basic::{BrotliLevel, Compression, GzipLevel, ZstdLevel};
use parquet::{
    arrow::{ArrowWriter, arrow_reader::ParquetRecordBatchReaderBuilder},
    file::{metadata::KeyValue, properties::WriterProperties, reader::ChunkReader},
};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, io::Write};

/// Default stac-geoparquet compression
pub fn default_compression() -> Compression {
    Compression::ZSTD(ZstdLevel::try_new(15).unwrap())
}

/// Default stac-geoparquet max row group size
pub const DEFAULT_STAC_MAX_ROW_GROUP_SIZE: usize = 150_000;

/// The stac-geoparquet metadata key.
pub const METADATA_KEY: &str = "stac-geoparquet";

/// The stac-geoparquet version.
pub const VERSION: &str = "1.0.0";

/// Options for writing stac-geoparquet files.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct WriterOptions {
    /// Parquet compression codec
    pub compression: Option<Compression>,

    /// Maximum number of rows in a row group
    pub max_row_group_size: usize,
}

/// An encoder for writing stac-geoparquet
#[allow(missing_debug_implementations)]
pub struct WriterEncoder {
    geoarrow_encoder: Encoder,
    encoder: GeoParquetRecordBatchEncoder,
}

impl WriterOptions {
    /// Creates a new WriterOptions with default values.
    ///
    /// # Examples
    ///
    /// ```
    /// use stac::geoparquet::WriterOptions;
    ///
    /// let options = WriterOptions::new();
    /// ```
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the compression codec.
    ///
    /// # Examples
    ///
    /// ```
    /// use stac::geoparquet::{WriterOptions, Compression};
    ///
    /// let options = WriterOptions::new().with_compression(Compression::SNAPPY);
    /// ```
    pub fn with_compression(mut self, compression: impl Into<Option<Compression>>) -> Self {
        self.compression = compression.into();
        self
    }

    /// Sets the maximum row group size.
    ///
    /// # Examples
    ///
    /// ```
    /// use stac::geoparquet::WriterOptions;
    ///
    /// let options = WriterOptions::new().with_max_row_group_size(50000);
    /// ```
    pub fn with_max_row_group_size(mut self, size: usize) -> Self {
        self.max_row_group_size = size;
        self
    }
}

impl Default for WriterOptions {
    fn default() -> Self {
        Self {
            compression: Some(default_compression()),
            max_row_group_size: DEFAULT_STAC_MAX_ROW_GROUP_SIZE,
        }
    }
}

/// Reads a [ItemCollection] from a [ChunkReader] as
/// [stac-geoparquet](https://github.com/stac-utils/stac-geoparquet).
///
/// # Examples
///
/// ```
/// use std::fs::File;
///
/// let file = File::open("data/extended-item.parquet").unwrap();
/// let item_collection = stac::geoparquet::from_reader(file).unwrap();
/// ```
pub fn from_reader<R>(reader: R) -> Result<ItemCollection>
where
    R: ChunkReader + 'static,
{
    let builder = ParquetRecordBatchReaderBuilder::try_new(reader)?;
    let geoparquet_metadata = builder
        .geoparquet_metadata()
        .transpose()?
        .ok_or(Error::MissingGeoparquetMetadata)?;
    let geoarrow_schema =
        builder.geoarrow_schema(&geoparquet_metadata, true, Default::default())?;
    let reader = builder.build()?;
    let reader = GeoParquetRecordBatchReader::try_new(reader, geoarrow_schema)?;
    crate::geoarrow::from_record_batch_reader(reader)
}

/// Returns an iterator that yields batches of [Item]s from a [ChunkReader].
///
/// Unlike [from_reader], this does not collect all items into memory at once.
/// Each iteration yields one record batch worth of items.
///
/// # Examples
///
/// ```
/// use std::fs::File;
///
/// let file = File::open("data/extended-item.parquet").unwrap();
/// let mut count = 0;
/// for result in stac::geoparquet::from_reader_iter(file).unwrap() {
///     let items = result.unwrap();
///     count += items.len();
/// }
/// assert!(count > 0);
/// ```
pub fn from_reader_iter<R>(reader: R) -> Result<impl Iterator<Item = Result<Vec<Item>>>>
where
    R: ChunkReader + 'static,
{
    let builder = ParquetRecordBatchReaderBuilder::try_new(reader)?;
    let geoparquet_metadata = builder
        .geoparquet_metadata()
        .transpose()?
        .ok_or(Error::MissingGeoparquetMetadata)?;
    let geoarrow_schema =
        builder.geoarrow_schema(&geoparquet_metadata, true, Default::default())?;
    let reader = builder.build()?;
    let reader = GeoParquetRecordBatchReader::try_new(reader, geoarrow_schema)?;
    Ok(reader.map(|result| {
        let record_batch = result?;
        crate::geoarrow::items_from_record_batch(record_batch)
    }))
}

/// Writes a [ItemCollection] to a [std::io::Write] as
/// [stac-geoparquet](https://github.com/stac-utils/stac-geoparquet).
///
/// Currently, will throw an error if the value is not an item or an item
/// collection.
///
/// # Examples
///
/// ```
/// use std::io::Cursor;
/// use stac::Item;
///
/// let item: Item = stac::read("examples/simple-item.json").unwrap();
/// let mut cursor = Cursor::new(Vec::new());
/// stac::geoparquet::into_writer(&mut cursor, vec![item]).unwrap();
/// ```
pub fn into_writer<W>(writer: W, item_collection: impl Into<ItemCollection>) -> Result<()>
where
    W: Write + Send,
{
    WriterBuilder::new(writer)
        .build(item_collection.into().items)?
        .finish()
}

/// Writes a [ItemCollection] to a [std::io::Write] as
/// [stac-geoparquet](https://github.com/stac-utils/stac-geoparquet) with the provided compression.
///
/// # Examples
///
/// ```
/// use std::io::Cursor;
/// use stac::{Item, geoparquet::Compression};
///
/// let item: Item = stac::read("examples/simple-item.json").unwrap();
/// let mut cursor = Cursor::new(Vec::new());
/// stac::geoparquet::into_writer_with_compression(&mut cursor, vec![item], Compression::SNAPPY).unwrap();
/// ```
pub fn into_writer_with_compression<W>(
    writer: W,
    // TODO should we switch to just take a vector of items in the signature?
    item_collection: impl Into<ItemCollection>,
    compression: Compression,
) -> Result<()>
where
    W: Write + Send,
{
    WriterBuilder::new(writer)
        .writer_options(WriterOptions::new().with_compression(compression))
        .build(item_collection.into().items)
        .and_then(|writer| writer.finish())
}

/// Builder for a stac-geoparquet writer.
#[derive(Debug)]
pub struct WriterBuilder<W: Write + Send> {
    writer: W,
    options: Options,
    writer_options: WriterOptions,
}

/// Write items to stac-geoparquet.
#[allow(missing_debug_implementations)]
pub struct Writer<W: Write + Send> {
    state: WriterState,
    arrow_writer: ArrowWriter<W>,
}

/// stac-geoparquet metadata
#[derive(Debug, Serialize, Deserialize)]
pub struct Metadata {
    /// The stac-geoparquet version.
    pub version: String,

    /// Any STAC collections stored alongside the items.
    #[serde(skip_serializing_if = "HashMap::is_empty")]
    pub collections: HashMap<String, Collection>,
}

impl<W: Write + Send> WriterBuilder<W> {
    /// Creates a new writer builder.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::io::Cursor;
    /// use stac::{Item, geoparquet::WriterBuilder};
    ///
    /// let item: Item = stac::read("examples/simple-item.json").unwrap();
    /// let cursor = Cursor::new(Vec::new());
    /// let writer = WriterBuilder::new(cursor).build(vec![item]).unwrap();
    /// ```
    pub fn new(writer: W) -> WriterBuilder<W> {
        WriterBuilder {
            writer,
            options: Options::default(),
            writer_options: WriterOptions::default(),
        }
    }

    /// Sets the writer options for parquet writing (compression, row group size, etc).
    ///
    /// # Examples
    ///
    /// ```
    /// use std::io::Cursor;
    /// use stac::{Item, geoparquet::{WriterBuilder, WriterOptions, Compression}};
    ///
    /// let item: Item = stac::read("examples/simple-item.json").unwrap();
    /// let cursor = Cursor::new(Vec::new());
    /// let options = WriterOptions::new()
    ///     .with_compression(Compression::SNAPPY)
    ///     .with_max_row_group_size(50000);
    /// let writer = WriterBuilder::new(cursor)
    ///     .writer_options(options)
    ///     .build(vec![item])
    ///     .unwrap();
    /// ```
    pub fn writer_options(mut self, writer_options: WriterOptions) -> WriterBuilder<W> {
        self.writer_options = writer_options;
        self
    }

    /// Sets the geoarrow encoding options
    ///
    /// # Examples
    ///
    /// ```
    /// use std::io::Cursor;
    /// use stac::{Item, geoarrow::Options, geoparquet::WriterBuilder};
    ///
    /// let item: Item = stac::read("examples/simple-item.json").unwrap();
    /// let cursor = Cursor::new(Vec::new());
    /// let options = Options::default();
    /// let writer = WriterBuilder::new(cursor)
    ///     .options(options)
    ///     .build(vec![item])
    ///     .unwrap();
    /// ```
    pub fn options(mut self, options: Options) -> WriterBuilder<W> {
        self.options = options;
        self
    }

    /// Builds the writer.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::io::Cursor;
    /// use stac::{Item, geoparquet::WriterBuilder};
    ///
    /// let item: Item = stac::read("examples/simple-item.json").unwrap();
    /// let cursor = Cursor::new(Vec::new());
    /// let mut writer = WriterBuilder::new(cursor).build(vec![item]).unwrap();
    /// writer.finish().unwrap();
    /// ```
    pub fn build(self, items: Vec<Item>) -> Result<Writer<W>> {
        Writer::new(self.writer, self.options, self.writer_options, items)
    }
}

impl WriterEncoder {
    /// Creates a new writer encoder.
    ///
    /// # Examples
    ///
    /// ```
    /// use stac::{Item, geoarrow::Options, geoparquet::WriterEncoder};
    ///
    /// let item: Item = stac::read("examples/simple-item.json").unwrap();
    /// let options = Options::default();
    /// let (encoder, record_batch) = WriterEncoder::new(options, vec![item]).unwrap();
    /// assert_eq!(record_batch.num_rows(), 1);
    /// ```
    pub fn new(options: Options, items: Vec<Item>) -> Result<(WriterEncoder, RecordBatch)> {
        let (geoarrow_encoder, record_batch) = Encoder::new(items, options)?;
        let options = GeoParquetWriterOptionsBuilder::default()
            .set_primary_column("geometry".to_string())
            .build();
        let mut encoder = GeoParquetRecordBatchEncoder::try_new(&record_batch.schema(), &options)?;
        let record_batch = encoder.encode_record_batch(&record_batch)?;
        Ok((
            WriterEncoder {
                geoarrow_encoder,
                encoder,
            },
            record_batch,
        ))
    }

    /// Encodes items into a record batch.
    ///
    /// # Examples
    ///
    /// ```
    /// use stac::{Item, geoarrow::Options, geoparquet::WriterEncoder};
    ///
    /// let item1: Item = stac::read("examples/simple-item.json").unwrap();
    /// let item2 = item1.clone();
    /// let options = Options::default();
    /// let (mut encoder, record_batch) = WriterEncoder::new(options, vec![item1]).unwrap();
    /// let record_batch = encoder.encode(vec![item2]).unwrap();
    /// assert_eq!(record_batch.num_rows(), 1);
    /// ```
    pub fn encode(&mut self, items: Vec<Item>) -> Result<RecordBatch> {
        let record_batch = self.geoarrow_encoder.encode(items)?;
        let record_batch = self.encoder.encode_record_batch(&record_batch)?;
        Ok(record_batch)
    }

    /// Consumes this encoder and returns the keys and values.
    ///
    /// # Examples
    ///
    /// ```
    /// use stac::{Item, geoarrow::Options, geoparquet::WriterEncoder};
    ///
    /// let item: Item = stac::read("examples/simple-item.json").unwrap();
    /// let options = Options::default();
    /// let (encoder, _) = WriterEncoder::new(options, vec![item]).unwrap();
    /// let key_value = encoder.into_keyvalue().unwrap();
    /// assert_eq!(key_value.key, "geo");
    /// ```
    pub fn into_keyvalue(self) -> Result<KeyValue> {
        let keyvalue = self.encoder.into_keyvalue()?;
        Ok(keyvalue)
    }
}

/// Shared state for STAC geoparquet writers (both sync and async).
///
/// This struct encapsulates the common logic for encoding items and
/// managing metadata across different writer implementations.
#[allow(missing_debug_implementations)]
pub struct WriterState {
    encoder: WriterEncoder,
    metadata: Metadata,
}

impl WriterState {
    /// Creates a new WriterState and returns the initial record batch.
    ///
    /// This should be called during writer construction to initialize
    /// the encoder and create the first record batch.
    ///
    /// # Examples
    ///
    /// ```
    /// use stac::{Item, geoarrow::Options, geoparquet::WriterState};
    ///
    /// let item: Item = stac::read("examples/simple-item.json").unwrap();
    /// let (state, record_batch) = WriterState::new(Options::default(), vec![item]).unwrap();
    /// assert_eq!(record_batch.num_rows(), 1);
    /// ```
    pub fn new(options: Options, items: Vec<Item>) -> Result<(WriterState, RecordBatch)> {
        let (encoder, record_batch) = WriterEncoder::new(options, items)?;
        Ok((
            WriterState {
                encoder,
                metadata: Metadata::default(),
            },
            record_batch,
        ))
    }

    /// Encodes items into a record batch.
    ///
    /// # Examples
    ///
    /// ```
    /// use stac::{Item, geoarrow::Options, geoparquet::WriterState};
    ///
    /// let item1: Item = stac::read("examples/simple-item.json").unwrap();
    /// let item2 = item1.clone();
    /// let (mut state, _) = WriterState::new(Options::default(), vec![item1]).unwrap();
    /// let record_batch = state.encode(vec![item2]).unwrap();
    /// assert_eq!(record_batch.num_rows(), 1);
    /// ```
    pub fn encode(&mut self, items: Vec<Item>) -> Result<RecordBatch> {
        self.encoder.encode(items)
    }

    /// Adds a collection to the metadata.
    ///
    /// Warns and overwrites if there's already a collection with the same id.
    ///
    /// # Examples
    ///
    /// ```
    /// use stac::{Item, Collection, geoarrow::Options, geoparquet::WriterState};
    ///
    /// let item: Item = stac::read("examples/simple-item.json").unwrap();
    /// let (mut state, _) = WriterState::new(Options::default(), vec![item]).unwrap();
    /// state.add_collection(Collection::new("test-id", "description"));
    /// ```
    pub fn add_collection(&mut self, collection: Collection) {
        if let Some(previous_collection) = self
            .metadata
            .collections
            .insert(collection.id.clone(), collection)
        {
            log::warn!(
                "Collection with id={} already existed in writer, overwriting",
                previous_collection.id
            )
        }
    }

    /// Consumes the state and returns the metadata key-value pairs.
    ///
    /// This returns both the geo metadata and the stac-geoparquet metadata
    /// that should be appended to the parquet file.
    ///
    /// # Examples
    ///
    /// ```
    /// use stac::{Item, geoarrow::Options, geoparquet::WriterState};
    ///
    /// let item: Item = stac::read("examples/simple-item.json").unwrap();
    /// let (state, _) = WriterState::new(Options::default(), vec![item]).unwrap();
    /// let metadata = state.into_metadata().unwrap();
    /// assert_eq!(metadata.len(), 2); // geo + stac-geoparquet metadata
    /// assert_eq!(metadata[0].key, "geo");
    /// assert_eq!(metadata[1].key, "stac-geoparquet");
    /// ```
    pub fn into_metadata(self) -> Result<Vec<KeyValue>> {
        let metadata = vec![
            self.encoder.into_keyvalue()?,
            KeyValue::new(
                METADATA_KEY.to_string(),
                serde_json::to_string(&self.metadata)?,
            ),
        ];
        Ok(metadata)
    }
}

impl<W: Write + Send> Writer<W> {
    fn new(
        writer: W,
        options: Options,
        writer_options: WriterOptions,
        items: Vec<Item>,
    ) -> Result<Self> {
        let (state, record_batch) = WriterState::new(options, items)?;
        let mut arrow_writer =
            ArrowWriter::try_new(writer, record_batch.schema(), Some(writer_options.into()))?;
        arrow_writer.write(&record_batch)?;
        Ok(Writer {
            state,
            arrow_writer,
        })
    }

    /// Writes more items to this writer.
    ///
    /// It's an error to write after `finish` has been called.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::io::Cursor;
    /// use stac::{Item, geoparquet::WriterBuilder};
    ///
    /// let item1: Item = stac::read("examples/simple-item.json").unwrap();
    /// let item2 = item1.clone();
    /// let cursor = Cursor::new(Vec::new());
    /// let mut writer = WriterBuilder::new(cursor).build(vec![item1]).unwrap();
    /// writer.write(vec![item2]).unwrap();
    /// writer.finish().unwrap();
    /// ```
    pub fn write(&mut self, items: Vec<Item>) -> Result<()> {
        let record_batch = self.state.encode(items)?;
        self.arrow_writer.write(&record_batch)?;
        Ok(())
    }

    /// Adds a collection to this writer's metadata.
    ///
    /// Warns and overwrites if there's already a collection with the same id.
    ///
    /// # Examples
    ///
    /// ```
    /// use stac::{Item, Collection, geoparquet::WriterBuilder};
    /// use std::io::Cursor;
    ///
    /// let item: Item = stac::read("examples/simple-item.json").unwrap();
    /// let cursor = Cursor::new(Vec::new());
    /// let writer = WriterBuilder::new(cursor)
    ///     .build(vec![item])
    ///     .unwrap()
    ///     .add_collection(Collection::new("an-id", "a description"))
    ///     .unwrap();
    /// writer.finish().unwrap();
    /// ```
    pub fn add_collection(mut self, collection: Collection) -> Result<Writer<W>> {
        self.state.add_collection(collection);
        Ok(self)
    }

    /// Finishes writing.
    ///
    /// It's an error to call finish twice.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::io::Cursor;
    /// use stac::{Item, geoparquet::WriterBuilder};
    ///
    /// let item: Item = stac::read("examples/simple-item.json").unwrap();
    /// let cursor = Cursor::new(Vec::new());
    /// let writer = WriterBuilder::new(cursor).build(vec![item]).unwrap();
    /// writer.finish().unwrap();
    /// ```
    pub fn finish(mut self) -> Result<()> {
        let metadata = self.state.into_metadata()?;
        for kv in metadata {
            self.arrow_writer.append_key_value_metadata(kv);
        }
        let _ = self.arrow_writer.finish()?;
        Ok(())
    }
}

/// Create a STAC object from geoparquet data.
pub trait FromGeoparquet: Sized {
    /// Creates a STAC object from geoparquet bytes.
    #[allow(unused_variables)]
    fn from_geoparquet_bytes(bytes: impl Into<Bytes>) -> Result<Self>;
}

/// Write a STAC object to geoparquet.
pub trait IntoGeoparquet: Sized {
    /// Writes a value to a writer as stac-geoparquet.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use stac::{IntoGeoparquet, ItemCollection, Item};
    /// use stac::geoparquet::WriterOptions;
    ///
    /// let item_collection: ItemCollection = vec![Item::new("a"), Item::new("b")].into();
    /// let mut buf = Vec::new();
    /// item_collection.into_geoparquet_writer(&mut buf, WriterOptions::default()).unwrap();
    /// ```
    fn into_geoparquet_writer(
        self,
        writer: impl Write + Send,
        writer_options: WriterOptions,
    ) -> Result<()>;

    /// Writes a value to a writer as stac-geoparquet to some bytes.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use stac::{IntoGeoparquet, ItemCollection, Item};
    /// use stac::geoparquet::WriterOptions;
    ///
    /// let item_collection: ItemCollection = vec![Item::new("a"), Item::new("b")].into();
    /// let bytes = item_collection.into_geoparquet_vec(WriterOptions::default()).unwrap();
    /// ```
    fn into_geoparquet_vec(self, writer_options: WriterOptions) -> Result<Vec<u8>> {
        let mut buf = Vec::new();
        self.into_geoparquet_writer(&mut buf, writer_options)?;
        Ok(buf)
    }
}

macro_rules! impl_from_geoparquet {
    ($object:ty) => {
        impl FromGeoparquet for $object {
            fn from_geoparquet_bytes(
                _: impl Into<Bytes>,
            ) -> std::result::Result<Self, crate::Error> {
                Err(crate::Error::UnsupportedGeoparquetType)
            }
        }
    };
}
macro_rules! impl_into_geoparquet {
    ($object:ty) => {
        impl IntoGeoparquet for $object {
            fn into_geoparquet_writer(
                self,
                _: impl Write + Send,
                _: WriterOptions,
            ) -> std::result::Result<(), crate::Error> {
                Err(crate::Error::UnsupportedGeoparquetType)
            }
        }
    };
}

impl_from_geoparquet!(Item);
impl_from_geoparquet!(Catalog);
impl_from_geoparquet!(Collection);
impl_into_geoparquet!(Catalog);
impl_into_geoparquet!(Collection);

impl FromGeoparquet for ItemCollection {
    fn from_geoparquet_bytes(bytes: impl Into<Bytes>) -> Result<Self> {
        let item_collection = from_reader(bytes.into())?;
        Ok(item_collection)
    }
}

impl FromGeoparquet for Value {
    fn from_geoparquet_bytes(bytes: impl Into<Bytes>) -> Result<Self> {
        Ok(Value::ItemCollection(
            ItemCollection::from_geoparquet_bytes(bytes)?,
        ))
    }
}

impl IntoGeoparquet for ItemCollection {
    fn into_geoparquet_writer(
        self,
        writer: impl Write + Send,
        writer_options: WriterOptions,
    ) -> Result<()> {
        WriterBuilder::new(writer)
            .writer_options(writer_options)
            .build(self.items)?
            .finish()
    }
}

impl IntoGeoparquet for Item {
    fn into_geoparquet_writer(
        self,
        writer: impl Write + Send,
        writer_options: WriterOptions,
    ) -> Result<()> {
        ItemCollection::from(vec![self]).into_geoparquet_writer(writer, writer_options)
    }
}

impl IntoGeoparquet for Value {
    fn into_geoparquet_writer(
        self,
        writer: impl Write + Send,
        writer_options: WriterOptions,
    ) -> Result<()> {
        ItemCollection::try_from(self)?.into_geoparquet_writer(writer, writer_options)
    }
}

impl IntoGeoparquet for serde_json::Value {
    fn into_geoparquet_writer(
        self,
        writer: impl Write + Send,
        writer_options: WriterOptions,
    ) -> Result<()> {
        let item_collection: ItemCollection = serde_json::from_value(self)?;
        item_collection.into_geoparquet_writer(writer, writer_options)
    }
}

impl From<WriterOptions> for WriterProperties {
    fn from(value: WriterOptions) -> Self {
        let mut builder = WriterProperties::builder();
        if let Some(compression) = value.compression {
            builder = builder.set_compression(compression);
        }
        builder = builder.set_max_row_group_size(value.max_row_group_size);
        builder.build()
    }
}

impl Default for Metadata {
    fn default() -> Self {
        Metadata {
            version: VERSION.to_string(),
            collections: HashMap::new(),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        Collection, FromGeoparquet, Item, ItemCollection, SelfHref, Value,
        geoparquet::{METADATA_KEY, Metadata, VERSION, WriterBuilder},
    };
    use bytes::Bytes;
    use parquet::file::reader::{FileReader, SerializedFileReader};
    use std::{
        fs::File,
        io::{Cursor, Read},
    };

    #[test]
    fn to_writer_item_collection() {
        let mut cursor = Cursor::new(Vec::new());
        let item = crate::read("examples/simple-item.json").unwrap();
        let item_collection = ItemCollection::from(vec![item]);
        super::into_writer(&mut cursor, item_collection).unwrap();
    }

    #[test]
    fn from_reader() {
        let file = File::open("data/extended-item.parquet").unwrap();
        let item_collection = super::from_reader(file).unwrap();
        assert_eq!(item_collection.items.len(), 1);
    }

    #[test]
    fn roundtrip() {
        let mut item: Item = crate::read("examples/simple-item.json").unwrap();
        item.clear_self_href();
        let mut cursor = Cursor::new(Vec::new());
        super::into_writer(&mut cursor, vec![item.clone()]).unwrap();
        let bytes = Bytes::from(cursor.into_inner());
        let item_collection = super::from_reader(bytes).unwrap();
        assert_eq!(item_collection.items[0], item);
    }

    #[test]
    fn roundtrip_proj_geometry() {
        let item_collection: ItemCollection = crate::read("data/multi-polygons.json").unwrap();
        let mut cursor = Cursor::new(Vec::new());
        super::into_writer(&mut cursor, item_collection).unwrap();
        let bytes = Bytes::from(cursor.into_inner());
        let item_collection = super::from_reader(bytes).unwrap();
        assert_eq!(item_collection.items.len(), 2);
    }

    #[test]
    fn geometry_primary_column() {
        // https://github.com/stac-utils/rustac/issues/755
        let item_collection: ItemCollection = crate::read("data/multi-polygons.json").unwrap();
        let mut cursor = Cursor::new(Vec::new());
        super::into_writer(&mut cursor, item_collection).unwrap();
        let bytes = Bytes::from(cursor.into_inner());
        let reader = SerializedFileReader::new(bytes).unwrap();
        let key_value = reader
            .metadata()
            .file_metadata()
            .key_value_metadata()
            .unwrap()
            .iter()
            .find(|key_value| key_value.key == "geo")
            .unwrap();
        let value: serde_json::Value =
            serde_json::from_str(key_value.value.as_deref().unwrap()).unwrap();
        assert_eq!(value["primary_column"], "geometry");
    }

    #[test]
    fn from_bytes() {
        let mut buf = Vec::new();
        let _ = File::open("data/extended-item.parquet")
            .unwrap()
            .read_to_end(&mut buf)
            .unwrap();
        let _ = ItemCollection::from_geoparquet_bytes(buf).unwrap();
    }

    #[test]
    fn value_from_bytes() {
        let mut buf = Vec::new();
        let _ = File::open("data/extended-item.parquet")
            .unwrap()
            .read_to_end(&mut buf)
            .unwrap();
        let _ = Value::from_geoparquet_bytes(buf).unwrap();
    }

    #[test]
    fn multipolygon() {
        let items: ItemCollection = stac::read("data/multi-polygons.json").unwrap();
        let cursor = Cursor::new(Vec::new());
        super::into_writer(cursor, items).unwrap();
    }

    #[test]
    fn no_proj_geometry_metadata() {
        let item: Item =
            crate::read("examples/extensions-collection/proj-example/proj-example.json").unwrap();
        let mut cursor = Cursor::new(Vec::new());
        super::into_writer(&mut cursor, vec![item]).unwrap();
        let bytes = Bytes::from(cursor.into_inner());
        let reader = SerializedFileReader::new(bytes).unwrap();
        let key_value = reader
            .metadata()
            .file_metadata()
            .key_value_metadata()
            .unwrap()
            .iter()
            .find(|key_value| key_value.key == "geo")
            .unwrap();
        let value: serde_json::Value =
            serde_json::from_str(key_value.value.as_deref().unwrap()).unwrap();
        assert!(
            !value["columns"]
                .as_object()
                .unwrap()
                .contains_key("proj:geometry")
        );
    }

    #[test]
    fn custom_max_row_group_size() {
        // Create multiple items to test row grouping
        let item: Item = crate::read("examples/simple-item.json").unwrap();
        let items: Vec<Item> = (0..100).map(|_| item.clone()).collect();

        let mut cursor = Cursor::new(Vec::new());
        let options = super::WriterOptions::new().with_max_row_group_size(25);
        WriterBuilder::new(&mut cursor)
            .writer_options(options)
            .build(items)
            .unwrap()
            .finish()
            .unwrap();

        let bytes = Bytes::from(cursor.into_inner());
        let reader = SerializedFileReader::new(bytes).unwrap();

        // Should have 4 row groups (100 items / 25 per group)
        assert_eq!(reader.metadata().num_row_groups(), 4);
    }

    #[test]
    fn default_max_row_group_size() {
        // Create multiple items to test row grouping
        let item: Item = crate::read("examples/simple-item.json").unwrap();
        let items: Vec<Item> = (0..1000).map(|_| item.clone()).collect();

        let mut cursor = Cursor::new(Vec::new());
        WriterBuilder::new(&mut cursor)
            .build(items)
            .unwrap()
            .finish()
            .unwrap();

        let bytes = Bytes::from(cursor.into_inner());
        let reader = SerializedFileReader::new(bytes).unwrap();

        // Should have a single row group
        assert_eq!(reader.metadata().num_row_groups(), 1);
    }

    #[test]
    fn no_assets() {
        let mut item: Item = crate::read("examples/simple-item.json").unwrap();
        item.assets = Default::default();
        let mut writer = Cursor::new(Vec::new());
        super::into_writer(&mut writer, vec![item]).unwrap();
        let item_collection = super::from_reader(Bytes::from(writer.into_inner())).unwrap();
        assert!(item_collection.items[0].assets.is_empty());
    }

    #[test]
    fn metadata() {
        let item: Item = crate::read("examples/simple-item.json").unwrap();
        let mut cursor = Cursor::new(Vec::new());
        WriterBuilder::new(&mut cursor)
            .build(vec![item])
            .unwrap()
            .add_collection(Collection::new("an-id", "a description"))
            .unwrap()
            .finish()
            .unwrap();
        let bytes = Bytes::from(cursor.into_inner());
        let reader = SerializedFileReader::new(bytes).unwrap();
        let metadata = reader
            .metadata()
            .file_metadata()
            .key_value_metadata()
            .unwrap()
            .iter()
            .find_map(|key_value| {
                if key_value.key == METADATA_KEY {
                    Some(
                        serde_json::from_str::<Metadata>(key_value.value.as_ref().unwrap())
                            .unwrap(),
                    )
                } else {
                    None
                }
            })
            .unwrap();
        assert_eq!(metadata.version, VERSION);
        assert_eq!(metadata.collections["an-id"].description, "a description");
    }

    #[test]
    fn links_as_integer_list() {
        // https://github.com/stac-utils/rustac/issues/959
        let file = File::open("data/opr-one.parquet").unwrap();
        let _: ItemCollection = super::from_reader(file).unwrap();
    }
}