From e77c78aede3fba154b24bae9c166f7cf09efb469 Mon Sep 17 00:00:00 2001
From: Raffaele Gaetano <raffaele.gaetano@cirad.fr>
Date: Thu, 1 Feb 2024 16:41:08 +0100
Subject: [PATCH] ENH: use api_key in all planetary downloads. Landsat added to
 interface.

---
 Common/demtools.py              |  4 +++-
 TimeSeries/landsat_planetary.py |  6 ++++--
 TimeSeries/s2planetary.py       |  4 +++-
 Workflows/operations.py         | 10 ++++++----
 4 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/Common/demtools.py b/Common/demtools.py
index 83b7312..3d51fae 100644
--- a/Common/demtools.py
+++ b/Common/demtools.py
@@ -9,7 +9,9 @@ import os
 from shapely.geometry import Polygon
 import urllib.request
 
-def fetch(shp, output_fld, product='cop-dem-glo-30'):
+def fetch(shp, output_fld, product='cop-dem-glo-30', auth=None):
+    if auth is not None:
+        os.environ['PC_SDK_SUBSCRIPTION_KEY'] = auth
     bbox, i_bbox, shp_srs = get_query_bbox(shp, return_all=True)
     api = Client.open('https://planetarycomputer.microsoft.com/api/stac/v1', modifier=PC.sign_inplace)
     res = api.search(collections=product, bbox=bbox)
diff --git a/TimeSeries/landsat_planetary.py b/TimeSeries/landsat_planetary.py
index 2945be0..aa2db6a 100644
--- a/TimeSeries/landsat_planetary.py
+++ b/TimeSeries/landsat_planetary.py
@@ -8,8 +8,10 @@ from shapely.geometry import Polygon
 import rasterio.mask
 import os
 
-def fetch(shp, dt, out_fld, band_list=None):
-
+def fetch(shp, dt, out_fld, auth=None, band_list=None):
+    
+    if auth is not None:
+        os.environ['PC_SDK_SUBSCRIPTION_KEY'] = auth
     bbox, i_bbox, shp_srs = get_query_bbox(shp, return_all=True)
     api = Client.open('https://planetarycomputer.microsoft.com/api/stac/v1', modifier=PC.sign_inplace)
     res = api.search(collections="landsat-c2-l2", bbox=bbox, datetime=dt)
diff --git a/TimeSeries/s2planetary.py b/TimeSeries/s2planetary.py
index 928ce53..efa8dd4 100644
--- a/TimeSeries/s2planetary.py
+++ b/TimeSeries/s2planetary.py
@@ -8,10 +8,12 @@ from pyproj import Transformer as T
 from shapely.geometry import Polygon
 import tqdm
 
-def fetch(shp, dt, output_fld, band_list=None):
+def fetch(shp, dt, output_fld, auth=None, band_list=None):
 
     bbox, i_bbox, shp_srs = get_query_bbox(shp, return_all=True)
 
+    if auth is not None:
+        os.environ['PC_SDK_SUBSCRIPTION_KEY'] = auth
     api = Client.open('https://planetarycomputer.microsoft.com/api/stac/v1', modifier=PC.sign_inplace)
     res = api.search(collections="sentinel-2-l2a", bbox=bbox, datetime=dt)
     lst = ['B02','B03','B04','B05','B06','B07','B08','B8A','B11','B12','SCL']
diff --git a/Workflows/operations.py b/Workflows/operations.py
index 5d7b805..3619f5f 100644
--- a/Workflows/operations.py
+++ b/Workflows/operations.py
@@ -1,7 +1,7 @@
 import os.path
 import OBIA.segmentation
 import VHR.vhrbase
-from TimeSeries import s2theia, s2planetary, s1base, s1planetary, planet_mosaics
+from TimeSeries import s2theia, s2planetary, s1base, s1planetary, planet_mosaics, landsat_planetary
 from Common import demtools
 
 def run_segmentation(img, threshold, cw, sw , out_seg,
@@ -84,7 +84,7 @@ def preprocess_s1(in_fld, roi, out_fld, dem_fld=None, geoid=None, direction=None
     return s1.write_outputs(out_fld)
 
 def fetch(imagery, shp, out_fld, dt=None, auth=None, only_tiles=None):
-    assert(imagery in ['s2theia', 's2planetary', 's1grd', 's1rtc', 'planetmosaics', 'cop-dem-glo-30', 'nasadem'])
+    assert(imagery in ['s2theia', 's2planetary', 's1grd', 's1rtc', 'planetmosaics', 'cop-dem-glo-30', 'nasadem', 'landsatplanetary'])
     if imagery not in ['s2planetary', 'cop-dem-glo-30', 'nasadem'] and auth is None:
         raise ValueError("Please provide authentication information.")
     if imagery not in ['cop-dem-glo-30', 'nasadem'] and dt is None:
@@ -93,7 +93,7 @@ def fetch(imagery, shp, out_fld, dt=None, auth=None, only_tiles=None):
         #temporarily switch to eodag since theia_picker is unusable
         s2theia.fetch_eodag(shp, dt, out_fld, auth, only_tiles.split(';'))
     elif imagery == 's2planetary':
-        s2planetary.fetch(shp, dt, out_fld)
+        s2planetary.fetch(shp, dt, out_fld, auth)
     elif imagery == 's1grd':
         s1base.fetch(shp, dt, out_fld, auth)
     elif imagery == 's1rtc':
@@ -101,5 +101,7 @@ def fetch(imagery, shp, out_fld, dt=None, auth=None, only_tiles=None):
     elif imagery == 'planetmosaics':
         planet_mosaics.fetch(shp, dt, out_fld, auth)
     elif imagery in ['cop-dem-glo-30', 'nasadem']:
-        demtools.fetch(shp, out_fld, product=imagery)
+        demtools.fetch(shp, out_fld, product=imagery, auth=auth)
+    elif imagery == 'landsatplanetary':
+        landsat_planetary.fetch(shp, dt, out_fld, auth)
     return
-- 
GitLab