1
1
"""Database ingestion script."""
2
2
import json
3
3
import os
4
+
4
5
import click
5
6
import requests
6
7
7
8
# Define the directory where your data files are located
8
9
DATA_DIR = os .path .join (os .path .dirname (__file__ ), "setup_data/" )
9
10
11
+
10
12
def load_data (filename ):
11
13
"""Load json data from a file."""
12
14
with open (os .path .join (DATA_DIR , filename )) as file :
13
15
return json .load (file )
14
16
17
+
15
18
def load_collection (base_url , collection_id ):
16
19
"""Load a STAC collection into the database."""
17
20
collection = load_data ("collection.json" )
@@ -27,6 +30,7 @@ def load_collection(base_url, collection_id):
27
30
except requests .ConnectionError :
28
31
click .secho ("Failed to connect" , fg = "red" )
29
32
33
+
30
34
def load_items (base_url , collection_id ):
31
35
"""Load STAC items into the database."""
32
36
feature_collection = load_data ("sentinel-s2-l2a-cogs_0_100.json" )
@@ -36,7 +40,9 @@ def load_items(base_url, collection_id):
36
40
for feature in feature_collection ["features" ]:
37
41
try :
38
42
feature ["collection" ] = collection
39
- resp = requests .post (f"{ base_url } /collections/{ collection } /items" , json = feature )
43
+ resp = requests .post (
44
+ f"{ base_url } /collections/{ collection } /items" , json = feature
45
+ )
40
46
if resp .status_code == 200 :
41
47
click .echo (f"Status code: { resp .status_code } " )
42
48
click .echo (f"Added item: { feature ['id' ]} " )
@@ -46,14 +52,18 @@ def load_items(base_url, collection_id):
46
52
except requests .ConnectionError :
47
53
click .secho ("Failed to connect" , fg = "red" )
48
54
55
+
49
56
@click .command ()
50
- @click .option ('--base-url' , required = True , help = 'Base URL of the STAC API' )
51
- @click .option ('--collection-id' , default = 'test-collection' , help = 'ID of the collection to which items are added' )
57
+ @click .option ("--base-url" , required = True , help = "Base URL of the STAC API" )
58
+ @click .option (
59
+ "--collection-id" ,
60
+ default = "test-collection" ,
61
+ help = "ID of the collection to which items are added" ,
62
+ )
52
63
def main (base_url , collection_id ):
53
- """
54
- Load STAC items into the database.
55
- """
64
+ """Load STAC items into the database."""
56
65
load_items (base_url , collection_id )
57
66
58
- if __name__ == '__main__' :
67
+
68
+ if __name__ == "__main__" :
59
69
main ()
0 commit comments