1
- """Data Loader CLI tool."""
2
1
import json
3
2
import os
4
-
5
3
import click
6
4
import requests
7
5
8
- # Define the directory where your data files are located
9
- DATA_DIR = os . path . join ( os . path . dirname ( __file__ ), "setup_data/" )
10
-
11
-
12
- def load_data ( filename ):
13
- """Load json data from a file."""
14
- with open (os . path . join ( DATA_DIR , filename ) ) as file :
6
+ def load_data ( data_dir , filename ):
7
+ """Load json data from a file within the specified data directory."""
8
+ filepath = os . path . join ( data_dir , filename )
9
+ if not os . path . exists ( filepath ):
10
+ click . secho ( f"File not found: { filepath } " , fg = "red" , err = True )
11
+ raise click . Abort ()
12
+ with open (filepath ) as file :
15
13
return json .load (file )
16
14
17
-
18
- def load_collection (base_url , collection_id ):
15
+ def load_collection (base_url , collection_id , data_dir ):
19
16
"""Load a STAC collection into the database."""
20
- collection = load_data ("collection.json" )
17
+ collection = load_data (data_dir , "collection.json" )
21
18
collection ["id" ] = collection_id
22
19
try :
23
20
resp = requests .post (f"{ base_url } /collections" , json = collection )
@@ -28,45 +25,45 @@ def load_collection(base_url, collection_id):
28
25
click .echo (f"Status code: { resp .status_code } " )
29
26
click .echo (f"Collection: { collection ['id' ]} already exists" )
30
27
except requests .ConnectionError :
31
- click .secho ("Failed to connect" , fg = "red" )
32
-
28
+ click .secho ("Failed to connect" , fg = "red" , err = True )
33
29
34
- def load_items (base_url , collection_id , use_bulk ):
30
+ def load_items (base_url , collection_id , use_bulk , data_dir ):
35
31
"""Load STAC items into the database based on the method selected."""
36
- feature_collection = load_data ("sentinel-s2-l2a-cogs_0_100.json" )
37
- load_collection (base_url , collection_id )
32
+ # 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" ]
34
+ if not feature_files :
35
+ click .secho ("No feature collection files found in the specified directory." , fg = "red" , err = True )
36
+ raise click .Abort ()
37
+ feature_collection_file = feature_files [0 ] # Use the first found feature collection file
38
+ feature_collection = load_data (data_dir , feature_collection_file )
39
+
40
+ load_collection (base_url , collection_id , data_dir )
38
41
if use_bulk :
39
- load_items_bulk_insert (base_url , collection_id , feature_collection )
42
+ load_items_bulk_insert (base_url , collection_id , feature_collection , data_dir )
40
43
else :
41
- load_items_one_by_one (base_url , collection_id , feature_collection )
44
+ load_items_one_by_one (base_url , collection_id , feature_collection , data_dir )
42
45
43
-
44
- def load_items_one_by_one (base_url , collection_id , feature_collection ):
46
+ def load_items_one_by_one (base_url , collection_id , feature_collection , data_dir ):
45
47
"""Load STAC items into the database one by one."""
46
48
for feature in feature_collection ["features" ]:
47
49
try :
48
50
feature ["collection" ] = collection_id
49
- resp = requests .post (
50
- f"{ base_url } /collections/{ collection_id } /items" , json = feature
51
- )
51
+ resp = requests .post (f"{ base_url } /collections/{ collection_id } /items" , json = feature )
52
52
if resp .status_code == 200 :
53
53
click .echo (f"Status code: { resp .status_code } " )
54
54
click .echo (f"Added item: { feature ['id' ]} " )
55
55
elif resp .status_code == 409 :
56
56
click .echo (f"Status code: { resp .status_code } " )
57
57
click .echo (f"Item: { feature ['id' ]} already exists" )
58
58
except requests .ConnectionError :
59
- click .secho ("Failed to connect" , fg = "red" )
60
-
59
+ click .secho ("Failed to connect" , fg = "red" , err = True )
61
60
62
- def load_items_bulk_insert (base_url , collection_id , feature_collection ):
61
+ def load_items_bulk_insert (base_url , collection_id , feature_collection , data_dir ):
63
62
"""Load STAC items into the database via bulk insert."""
64
63
try :
65
64
for i , _ in enumerate (feature_collection ["features" ]):
66
65
feature_collection ["features" ][i ]["collection" ] = collection_id
67
- resp = requests .post (
68
- f"{ base_url } /collections/{ collection_id } /items" , json = feature_collection
69
- ) # Adjust this endpoint as necessary
66
+ resp = requests .post (f"{ base_url } /collections/{ collection_id } /items" , json = feature_collection ) # Adjust this endpoint as necessary
70
67
if resp .status_code == 200 :
71
68
click .echo (f"Status code: { resp .status_code } " )
72
69
click .echo ("Bulk inserted items successfully." )
@@ -77,21 +74,16 @@ def load_items_bulk_insert(base_url, collection_id, feature_collection):
77
74
click .echo (f"Status code: { resp .status_code } " )
78
75
click .echo ("Conflict detected, some items might already exist." )
79
76
except requests .ConnectionError :
80
- click .secho ("Failed to connect" , fg = "red" )
81
-
77
+ click .secho ("Failed to connect" , fg = "red" , err = True )
82
78
83
79
@click .command ()
84
80
@click .option ("--base-url" , required = True , help = "Base URL of the STAC API" )
85
- @click .option (
86
- "--collection-id" ,
87
- default = "test-collection" ,
88
- help = "ID of the collection to which items are added" ,
89
- )
81
+ @click .option ("--collection-id" , default = "test-collection" , help = "ID of the collection to which items are added" )
90
82
@click .option ("--use-bulk" , is_flag = True , help = "Use bulk insert method for items" )
91
- def main (base_url , collection_id , use_bulk ):
83
+ @click .option ("--data-dir" , type = click .Path (exists = True ), default = "sample_data/" , help = "Directory containing collection.json and feature collection file" )
84
+ def main (base_url , collection_id , use_bulk , data_dir ):
92
85
"""Load STAC items into the database."""
93
- load_items (base_url , collection_id , use_bulk )
94
-
86
+ load_items (base_url , collection_id , use_bulk , data_dir )
95
87
96
88
if __name__ == "__main__" :
97
- main ()
89
+ main ()
0 commit comments