@@ -15,6 +15,7 @@ use stac::{
1515use stac_io:: { Format , StacStore } ;
1616use stac_server:: Backend ;
1717use stac_validate:: Validate ;
18+ use std:: path:: Path ;
1819use std:: {
1920 collections:: { HashMap , VecDeque } ,
2021 io:: Write ,
@@ -299,6 +300,24 @@ pub enum Command {
299300 /// The shell to generate completion scripts for.
300301 shell : clap_complete:: Shell ,
301302 } ,
303+
304+ /// Generate a STAC collection from one or more items
305+ Collection {
306+ /// The input file.
307+ ///
308+ /// To read from standard input, pass `-` or don't provide an argument at all.
309+ infile : Option < String > ,
310+
311+ /// The output file.
312+ ///
313+ /// To write to standard output, pass `-` or don't provide an argument at all.
314+ outfile : Option < String > ,
315+
316+ /// The id of the output collection
317+ ///
318+ /// If not provided, will default to the file name without an extension.
319+ id : Option < String > ,
320+ } ,
302321}
303322
304323#[ derive( Debug ) ]
@@ -592,6 +611,44 @@ impl Rustac {
592611 clap_complete:: generate ( shell, & mut command, "rustac" , & mut std:: io:: stdout ( ) ) ;
593612 Ok ( ( ) )
594613 }
614+ Command :: Collection {
615+ ref infile,
616+ ref outfile,
617+ ref id,
618+ } => {
619+ let value = self . get ( infile. as_deref ( ) ) . await ?;
620+ let id = id. clone ( ) . unwrap_or_else ( || {
621+ infile
622+ . as_deref ( )
623+ . and_then ( |infile| {
624+ Path :: new ( infile)
625+ . file_stem ( )
626+ . map ( |s| s. to_string_lossy ( ) . into_owned ( ) )
627+ } )
628+ . unwrap_or ( "default-collection-id" . to_string ( ) )
629+ } ) ;
630+ let collection = match value {
631+ stac:: Value :: ItemCollection ( item_collection) => {
632+ Collection :: from_id_and_items ( id, & item_collection. items )
633+ }
634+ stac:: Value :: Item ( item) => Collection :: from_id_and_items ( id, & [ item] ) ,
635+ stac:: Value :: Collection ( collection) => collection,
636+ stac:: Value :: Catalog ( catalog) => {
637+ let mut json = serde_json:: to_value ( catalog) ?;
638+ let _ = json
639+ . as_object_mut ( )
640+ . unwrap ( )
641+ . insert ( "type" . to_string ( ) , "Collection" . into ( ) ) ;
642+ serde_json:: from_value ( json) ?
643+ }
644+ } ;
645+ self . put (
646+ outfile. as_deref ( ) ,
647+ Value :: Stac ( stac:: Value :: Collection ( collection) ) ,
648+ )
649+ . await ?;
650+ Ok ( ( ) )
651+ }
595652 }
596653 }
597654
0 commit comments