11import os
22from typing import Any
3- from typing import Callable
43from typing import List
54from typing import Tuple
65
76import numpy as np
87import rasterio
98from affine import Affine
109from loguru import logger
11- from osgeo import gdal
12- from osgeo import osr
1310from rasterio import warp
1411from rasterio .crs import CRS
1512from rasterio .enums import Resampling
@@ -85,7 +82,7 @@ def get_tile_pixel_coords(tiles: List[Tile], raster_file: str) -> List[Tuple[int
8582 return list (zip (rows , cols ))
8683
8784
88- def download_and_extract_tiles_window_COG (
85+ def download_and_extract_tiles_window (
8986 fs : Any ,
9087 task : ExtractionTask ,
9188 resolution : int ,
@@ -148,69 +145,8 @@ def download_and_extract_tiles_window_COG(
148145 return outfiles
149146
150147
151- def download_and_extract_tiles_window (
152- download_f : Callable ,
153- task : ExtractionTask ,
154- resolution : int ,
155- ) -> List [str ]:
156- """Download and extract from the task assets the window bounding the tiles.
157- i.e a crop of the original assets will
158-
159- Args:
160- download_f (Callable): The download function to use. It should return a BytesIO
161- to read the content.
162- task (ExtractionTask): The extraction task
163- resolution (int): The target resolution
164-
165- Returns:
166- List[str]: A list of files that store the crops of the original assets
167- """
168- band = task .band
169- urls = [item .assets [band ].href for item in task .item_collection .items ]
170-
171- epsg = task .tiles [0 ].epsg
172- out_files = []
173- for i , url in enumerate (urls ):
174- content = download_f (url )
175-
176- gdal .FileFromMemBuffer (f"/vsimem/{ task .task_id } _content" , content .read ())
177- d = gdal .Open (f"/vsimem/{ task .task_id } _content" , gdal .GA_Update )
178-
179- proj = osr .SpatialReference (wkt = d .GetProjection ())
180- proj = proj .GetAttrValue ("AUTHORITY" , 1 )
181- d = None
182-
183- proj_win = get_proj_win (task .tiles )
184-
185- if int (proj ) != epsg :
186- file = gdal .Warp (
187- f"{ task .task_id } _warp.vrt" ,
188- f"/vsimem/{ task .task_id } _content" ,
189- dstSRS = f"EPSG:{ epsg } " ,
190- creationOptions = ["QUALITY=100" , "REVERSIBLE=YES" ],
191- )
192- else :
193- file = f"/vsimem/{ task .task_id } _content"
194-
195- out_f = f"{ task .task_id } _{ i } .jp2"
196- gdal .Translate (
197- out_f ,
198- file ,
199- projWin = proj_win ,
200- projWinSRS = f"EPSG:{ epsg } " ,
201- xRes = resolution ,
202- yRes = - resolution ,
203- resampleAlg = "bilinear" ,
204- creationOptions = ["QUALITY=100" , "REVERSIBLE=YES" ],
205- )
206- file = None
207- out_files .append (out_f )
208- return out_files
209-
210-
211148def task_mosaic_patches (
212149 cloud_fs : Any ,
213- download_f : Callable ,
214150 task : ExtractionTask ,
215151 method : str = "max" ,
216152 resolution : int = 10 ,
@@ -219,7 +155,7 @@ def task_mosaic_patches(
219155 """Get tile patches from the mosaic of a given task
220156
221157 Args:
222- download_f (Callable ): The function to download the task assets
158+ cloud_fs (Any ): the cloud_fs to access the files
223159 task (ExtractionTask): The task
224160 method (str, optional): The method to use while merging the assets. Defaults to "max".
225161 resolution (int, optional): The target resolution. Defaults to 10.
@@ -229,10 +165,7 @@ def task_mosaic_patches(
229165 List[np.ndarray]: The tile patches as numpy arrays
230166 """
231167
232- if task .constellation == "sentinel-2" :
233- out_files = download_and_extract_tiles_window (download_f , task , resolution )
234- else :
235- out_files = download_and_extract_tiles_window_COG (cloud_fs , task , resolution )
168+ out_files = download_and_extract_tiles_window (cloud_fs , task , resolution )
236169
237170 out_f = f"{ task .task_id } _{ dst_path } "
238171 datasets = [rasterio .open (f ) for f in out_files ]
0 commit comments