From 0b2b56da8e76d88398c417d97769ea4602b67412 Mon Sep 17 00:00:00 2001
From: impact <pascal.mouquet@ird.fr>
Date: Mon, 4 Jul 2022 18:25:45 +0400
Subject: [PATCH] new class S2cEodag to interact with eodag library

---
 sen2chain/__init__.py                         |  2 +
 sen2chain/config.py                           |  1 +
 .../custom_locations.yml                      |  4 --
 sen2chain/download_and_process.py             |  2 +-
 sen2chain/download_eodag.py                   | 61 +++++++++++++++++++
 5 files changed, 65 insertions(+), 5 deletions(-)
 delete mode 100644 sen2chain/data/eodag_workspace_locations_tiles/custom_locations.yml

diff --git a/sen2chain/__init__.py b/sen2chain/__init__.py
index 83cd503..1651d4b 100644
--- a/sen2chain/__init__.py
+++ b/sen2chain/__init__.py
@@ -32,6 +32,7 @@ from .library import Library
 from .data_request import DataRequest
 from .indices import IndicesCollection
 from .download_and_process import DownloadAndProcess
+from .download_eodag import S2cEodag
 from .time_series import TimeSeries
 from .automatization import Automatization
 from .utils import (
@@ -56,6 +57,7 @@ from .multi_processing import (
 from .tileset import TileSet
 from .jobs import Jobs, Job
 
+
 __version__ = "0.7.0"
 __author__ = (
     "Jérémy Commins <jebins@laposte.net> & Impact <pascal.mouquet@ird.fr>"
diff --git a/sen2chain/config.py b/sen2chain/config.py
index d44d7c6..27b4768 100644
--- a/sen2chain/config.py
+++ b/sen2chain/config.py
@@ -22,6 +22,7 @@ SHARED_DATA = dict(
     peps_download=ROOT / "sen2chain" / "peps_download3.py",
     sen2chain_meta=ROOT / "sen2chain" / "data" / "sen2chain_info.xml",
     raw_job_cfg=ROOT / "sen2chain" / "data" / "job_ini.cfg",
+    eodag_centroids_shp=ROOT / "sen2chain" / "data" / "eodag_workspace_locations_tiles" / "sentinel2_tiling_grid_centroids.shp"
 )
 
 
diff --git a/sen2chain/data/eodag_workspace_locations_tiles/custom_locations.yml b/sen2chain/data/eodag_workspace_locations_tiles/custom_locations.yml
deleted file mode 100644
index 32a5299..0000000
--- a/sen2chain/data/eodag_workspace_locations_tiles/custom_locations.yml
+++ /dev/null
@@ -1,4 +0,0 @@
-shapefiles:
-  - name: s2_tile_centroid
-    path: /home/operateur/sen2chain/sen2chain/data/sentinel2_tiling_grid_centroids.shp
-    attr: tile_id
\ No newline at end of file
diff --git a/sen2chain/download_and_process.py b/sen2chain/download_and_process.py
index 99532dc..fa7aae9 100644
--- a/sen2chain/download_and_process.py
+++ b/sen2chain/download_and_process.py
@@ -13,7 +13,7 @@ from datetime import datetime
 from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
 from collections import defaultdict
 from sentinelsat import SentinelAPI
-from sentinelhub import AwsProductRequest
+# from sentinelhub import AwsProductRequest
 from pprint import pprint
 
 # type annotations
diff --git a/sen2chain/download_eodag.py b/sen2chain/download_eodag.py
index b81b490..3f2716d 100644
--- a/sen2chain/download_eodag.py
+++ b/sen2chain/download_eodag.py
@@ -6,7 +6,68 @@ Module for downloading Sentinel-2 images using EODAG
 https://www.github.com/CS-SI/EODAG
 
 """
+import logging
+import shapefile
+import os
+from pathlib import Path
+from eodag import EODataAccessGateway
+from .config import SHARED_DATA, Config
 
 
+logging.basicConfig(level=logging.INFO)
+logger = logging.getLogger(__name__)
 
+ROOT = Path(os.path.realpath(__file__)).parent.parent
 
+class S2cEodag:
+
+    def __init__(self, name: str):
+        self.name = name
+        eodag_centroids_shp = SHARED_DATA.get("eodag_centroids_shp")
+        
+        with shapefile.Reader(eodag_centroids_shp) as shp:
+            shaperecs = shp.shapeRecords()
+        
+        locations_yaml_content = """
+        shapefiles:
+          - name: s2_tile_centroid
+            path: {}
+            attr: tile_id
+        """.format(os.path.abspath(eodag_centroids_shp))
+        
+        eodag_custom_location_cfg = os.path.abspath(Config()._CONFIG_DIR / "eodag_custom_locations.yml")
+
+        with open(eodag_custom_location_cfg, "w") as f_yml:
+            f_yml.write(locations_yaml_content.strip())
+        
+        self.dag = EODataAccessGateway(locations_conf_path = eodag_custom_location_cfg)
+        self.dag.set_preferred_provider("peps")
+
+        targeted_tile = [
+            sr
+            for sr in shaperecs
+            if sr.record["tile_id"] == name
+        ][0]
+        
+    
+    def search(
+        self,
+        productType = "S2_MSI_L1C",
+        start: str = "2015-01-01",
+        end: str = "9999-01-01",
+    
+    ):
+        default_search_criteria = dict(
+            productType = productType,
+            start = start,
+            end = end
+        )
+        
+        products = self.dag.search_all(
+            locations=dict(s2_tile_centroid=self.name),
+            **default_search_criteria
+        )
+        logger.info("{} products were found given the above search criteria".format(len(products)))
+        logger.info(products)
+        logger.info([p.properties["storageStatus"] for p in products])
+        
-- 
GitLab