1+ """Data Loader CLI STAC_API Ingestion Tool."""
12import json
23import os
4+
35import click
46import requests
57
8+
69def load_data (data_dir , filename ):
710 """Load json data from a file within the specified data directory."""
811 filepath = os .path .join (data_dir , filename )
@@ -12,6 +15,7 @@ def load_data(data_dir, filename):
1215 with open (filepath ) as file :
1316 return json .load (file )
1417
18+
1519def load_collection (base_url , collection_id , data_dir ):
1620 """Load a STAC collection into the database."""
1721 collection = load_data (data_dir , "collection.json" )
@@ -27,14 +31,25 @@ def load_collection(base_url, collection_id, data_dir):
2731 except requests .ConnectionError :
2832 click .secho ("Failed to connect" , fg = "red" , err = True )
2933
34+
3035def load_items (base_url , collection_id , use_bulk , data_dir ):
3136 """Load STAC items into the database based on the method selected."""
3237 # Attempt to dynamically find a suitable feature collection file
33- feature_files = [file for file in os .listdir (data_dir ) if file .endswith ('.json' ) and file != "collection.json" ]
38+ feature_files = [
39+ file
40+ for file in os .listdir (data_dir )
41+ if file .endswith (".json" ) and file != "collection.json"
42+ ]
3443 if not feature_files :
35- click .secho ("No feature collection files found in the specified directory." , fg = "red" , err = True )
44+ click .secho (
45+ "No feature collection files found in the specified directory." ,
46+ fg = "red" ,
47+ err = True ,
48+ )
3649 raise click .Abort ()
37- feature_collection_file = feature_files [0 ] # Use the first found feature collection file
50+ feature_collection_file = feature_files [
51+ 0
52+ ] # Use the first found feature collection file
3853 feature_collection = load_data (data_dir , feature_collection_file )
3954
4055 load_collection (base_url , collection_id , data_dir )
@@ -43,12 +58,15 @@ def load_items(base_url, collection_id, use_bulk, data_dir):
4358 else :
4459 load_items_one_by_one (base_url , collection_id , feature_collection , data_dir )
4560
61+
4662def load_items_one_by_one (base_url , collection_id , feature_collection , data_dir ):
4763 """Load STAC items into the database one by one."""
4864 for feature in feature_collection ["features" ]:
4965 try :
5066 feature ["collection" ] = collection_id
51- resp = requests .post (f"{ base_url } /collections/{ collection_id } /items" , json = feature )
67+ resp = requests .post (
68+ f"{ base_url } /collections/{ collection_id } /items" , json = feature
69+ )
5270 if resp .status_code == 200 :
5371 click .echo (f"Status code: { resp .status_code } " )
5472 click .echo (f"Added item: { feature ['id' ]} " )
@@ -58,12 +76,15 @@ def load_items_one_by_one(base_url, collection_id, feature_collection, data_dir)
5876 except requests .ConnectionError :
5977 click .secho ("Failed to connect" , fg = "red" , err = True )
6078
79+
6180def load_items_bulk_insert (base_url , collection_id , feature_collection , data_dir ):
6281 """Load STAC items into the database via bulk insert."""
6382 try :
6483 for i , _ in enumerate (feature_collection ["features" ]):
6584 feature_collection ["features" ][i ]["collection" ] = collection_id
66- resp = requests .post (f"{ base_url } /collections/{ collection_id } /items" , json = feature_collection ) # Adjust this endpoint as necessary
85+ resp = requests .post (
86+ f"{ base_url } /collections/{ collection_id } /items" , json = feature_collection
87+ ) # Adjust this endpoint as necessary
6788 if resp .status_code == 200 :
6889 click .echo (f"Status code: { resp .status_code } " )
6990 click .echo ("Bulk inserted items successfully." )
@@ -76,14 +97,25 @@ def load_items_bulk_insert(base_url, collection_id, feature_collection, data_dir
7697 except requests .ConnectionError :
7798 click .secho ("Failed to connect" , fg = "red" , err = True )
7899
100+
79101@click .command ()
80102@click .option ("--base-url" , required = True , help = "Base URL of the STAC API" )
81- @click .option ("--collection-id" , default = "test-collection" , help = "ID of the collection to which items are added" )
103+ @click .option (
104+ "--collection-id" ,
105+ default = "test-collection" ,
106+ help = "ID of the collection to which items are added" ,
107+ )
82108@click .option ("--use-bulk" , is_flag = True , help = "Use bulk insert method for items" )
83- @click .option ("--data-dir" , type = click .Path (exists = True ), default = "sample_data/" , help = "Directory containing collection.json and feature collection file" )
109+ @click .option (
110+ "--data-dir" ,
111+ type = click .Path (exists = True ),
112+ default = "sample_data/" ,
113+ help = "Directory containing collection.json and feature collection file" ,
114+ )
84115def main (base_url , collection_id , use_bulk , data_dir ):
85116 """Load STAC items into the database."""
86117 load_items (base_url , collection_id , use_bulk , data_dir )
87118
119+
88120if __name__ == "__main__" :
89- main ()
121+ main ()
0 commit comments