|
6 | 6 |
|
7 | 7 | import numpy as np |
8 | 8 | import rasterio |
9 | | -from affine import Affine |
10 | 9 | from loguru import logger |
11 | 10 | from osgeo import gdal |
12 | 11 | from osgeo import osr |
13 | | -from rasterio import warp |
14 | | -from rasterio.crs import CRS |
15 | | -from rasterio.enums import Resampling |
16 | 12 | from rasterio.merge import merge as riomerge |
17 | 13 | from satextractor.models import ExtractionTask |
18 | 14 | from satextractor.models import Tile |
19 | 15 |
|
20 | 16 |
|
21 | | -def get_window_union( |
22 | | - tiles: List[Tile], |
23 | | - ds: rasterio.io.DatasetReader, |
24 | | -) -> rasterio.windows.Window: |
25 | | - |
26 | | - """Get the window union to read all tiles from the geotiff. |
27 | | -
|
28 | | - Args: |
29 | | - tiles (List[Tile]): the tiles |
30 | | - ds (rasterio.io.DatasetReader): the rasterio dataset to read (for the transform) |
31 | | -
|
32 | | - Returns: |
33 | | - rasterio.windows.Window: The union of all tile windows. |
34 | | - """ |
35 | | - |
36 | | - windows = [] |
37 | | - |
38 | | - for tile in tiles: |
39 | | - |
40 | | - bounds_arr_tile_crs = tile.bbox |
41 | | - bounds_arr_rast_crs = warp.transform_bounds( |
42 | | - ds.crs, |
43 | | - CRS.from_epsg(tile.epsg), |
44 | | - *bounds_arr_tile_crs, |
45 | | - ) |
46 | | - |
47 | | - window = rasterio.windows.from_bounds(*bounds_arr_rast_crs, ds.transform) |
48 | | - |
49 | | - windows.append(window) |
50 | | - |
51 | | - return rasterio.windows.union(windows) |
52 | | - |
53 | | - |
54 | 17 | def get_proj_win(tiles: List[Tile]) -> Tuple[int, int, int, int]: |
55 | 18 | """Get the projection bounds window of the tiles. |
56 | 19 |
|
@@ -85,69 +48,6 @@ def get_tile_pixel_coords(tiles: List[Tile], raster_file: str) -> List[Tuple[int |
85 | 48 | return list(zip(rows, cols)) |
86 | 49 |
|
87 | 50 |
|
88 | | -def download_and_extract_tiles_window_COG( |
89 | | - fs: Any, |
90 | | - task: ExtractionTask, |
91 | | - resolution: int, |
92 | | -) -> List[str]: |
93 | | - """Download and extract from the task assets the data for the window from each asset. |
94 | | -
|
95 | | - Args: |
96 | | - task (ExtractionTask): The extraction task |
97 | | - resolution (int): The target resolution |
98 | | -
|
99 | | - Returns: |
100 | | - List[str]: A list of files that store the crops of the original assets |
101 | | - """ |
102 | | - |
103 | | - # task tiles all have same CRS, so get their max extents and crs |
104 | | - left, top, right, bottom = get_proj_win(task.tiles) |
105 | | - epsg = task.tiles[0].epsg |
106 | | - |
107 | | - # set the transforms for the output file |
108 | | - dst_transform = Affine(resolution, 0.0, left, 0.0, -resolution, top) |
109 | | - out_shp = (int((right - left) / resolution), int((top - bottom) / resolution)) |
110 | | - logger.debug(f"Affine with resolution: {dst_transform} and out_shp {out_shp} ") |
111 | | - |
112 | | - outfiles = [] |
113 | | - |
114 | | - band = task.band |
115 | | - urls = [item.assets[band].href for item in task.item_collection.items] |
116 | | - |
117 | | - for ii, url in enumerate(urls): |
118 | | - with fs.open(url) as f: |
119 | | - with rasterio.open(f) as ds: |
120 | | - window = get_window_union(task.tiles, ds) |
121 | | - |
122 | | - rst_arr = ds.read( |
123 | | - 1, |
124 | | - window=window, |
125 | | - out_shape=out_shp, |
126 | | - fill_value=0, |
127 | | - ) # boundless=True? |
128 | | - |
129 | | - out_f = f"{task.task_id}_{ii}.tif" |
130 | | - |
131 | | - with rasterio.open( |
132 | | - out_f, |
133 | | - "w", |
134 | | - driver="GTiff", |
135 | | - count=1, |
136 | | - width=out_shp[0], |
137 | | - height=out_shp[1], |
138 | | - transform=dst_transform, |
139 | | - crs=CRS.from_epsg(epsg), |
140 | | - dtype=rst_arr.dtype, |
141 | | - resampling=Resampling.bilinear, |
142 | | - ) as dst: |
143 | | - |
144 | | - dst.write(rst_arr, indexes=1) |
145 | | - |
146 | | - outfiles.append(out_f) |
147 | | - |
148 | | - return outfiles |
149 | | - |
150 | | - |
151 | 51 | def download_and_extract_tiles_window( |
152 | 52 | download_f: Callable, |
153 | 53 | task: ExtractionTask, |
@@ -228,11 +128,7 @@ def task_mosaic_patches( |
228 | 128 | Returns: |
229 | 129 | List[np.ndarray]: The tile patches as numpy arrays |
230 | 130 | """ |
231 | | - |
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) |
| 131 | + out_files = download_and_extract_tiles_window(download_f, task, resolution) |
236 | 132 |
|
237 | 133 | out_f = f"{task.task_id}_{dst_path}" |
238 | 134 | datasets = [rasterio.open(f) for f in out_files] |
|
0 commit comments