1
1
import os
2
2
from typing import Any
3
- from typing import Callable
4
3
from typing import List
5
4
from typing import Tuple
6
5
7
6
import numpy as np
8
7
import rasterio
9
8
from affine import Affine
10
9
from loguru import logger
11
- from osgeo import gdal
12
- from osgeo import osr
13
10
from rasterio import warp
14
11
from rasterio .crs import CRS
15
12
from rasterio .enums import Resampling
@@ -85,7 +82,7 @@ def get_tile_pixel_coords(tiles: List[Tile], raster_file: str) -> List[Tuple[int
85
82
return list (zip (rows , cols ))
86
83
87
84
88
- def download_and_extract_tiles_window_COG (
85
+ def download_and_extract_tiles_window (
89
86
fs : Any ,
90
87
task : ExtractionTask ,
91
88
resolution : int ,
@@ -148,69 +145,8 @@ def download_and_extract_tiles_window_COG(
148
145
return outfiles
149
146
150
147
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
-
211
148
def task_mosaic_patches (
212
149
cloud_fs : Any ,
213
- download_f : Callable ,
214
150
task : ExtractionTask ,
215
151
method : str = "max" ,
216
152
resolution : int = 10 ,
@@ -219,7 +155,7 @@ def task_mosaic_patches(
219
155
"""Get tile patches from the mosaic of a given task
220
156
221
157
Args:
222
- download_f (Callable ): The function to download the task assets
158
+ cloud_fs (Any ): the cloud_fs to access the files
223
159
task (ExtractionTask): The task
224
160
method (str, optional): The method to use while merging the assets. Defaults to "max".
225
161
resolution (int, optional): The target resolution. Defaults to 10.
@@ -229,10 +165,7 @@ def task_mosaic_patches(
229
165
List[np.ndarray]: The tile patches as numpy arrays
230
166
"""
231
167
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 )
236
169
237
170
out_f = f"{ task .task_id } _{ dst_path } "
238
171
datasets = [rasterio .open (f ) for f in out_files ]
0 commit comments