Skip to content

Commit e242376

Browse files
authored
Merge pull request #26 from FrontierDevelopmentLab/bugfix/corrupted-bands
Remove COG downloader and use same as jp2k because it was buggy
2 parents 9201186 + a05e7ca commit e242376

1 file changed

Lines changed: 1 addition & 105 deletions

File tree

src/satextractor/extractor/extractor.py

Lines changed: 1 addition & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,14 @@
66

77
import numpy as np
88
import rasterio
9-
from affine import Affine
109
from loguru import logger
1110
from osgeo import gdal
1211
from osgeo import osr
13-
from rasterio import warp
14-
from rasterio.crs import CRS
15-
from rasterio.enums import Resampling
1612
from rasterio.merge import merge as riomerge
1713
from satextractor.models import ExtractionTask
1814
from satextractor.models import Tile
1915

2016

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-
5417
def get_proj_win(tiles: List[Tile]) -> Tuple[int, int, int, int]:
5518
"""Get the projection bounds window of the tiles.
5619
@@ -85,69 +48,6 @@ def get_tile_pixel_coords(tiles: List[Tile], raster_file: str) -> List[Tuple[int
8548
return list(zip(rows, cols))
8649

8750

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-
15151
def download_and_extract_tiles_window(
15252
download_f: Callable,
15353
task: ExtractionTask,
@@ -228,11 +128,7 @@ def task_mosaic_patches(
228128
Returns:
229129
List[np.ndarray]: The tile patches as numpy arrays
230130
"""
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)
236132

237133
out_f = f"{task.task_id}_{dst_path}"
238134
datasets = [rasterio.open(f) for f in out_files]

0 commit comments

Comments
 (0)