File size: 11,936 Bytes
fab54be 7db73fd fab54be 7db73fd fab54be 7db73fd fab54be 7db73fd fab54be 7db73fd fab54be | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | """
Google Earth Engine data fetching services.
Handles optical (Sentinel-2), SAR (Sentinel-1), and Dynamic World baselines.
Production hardened to eliminate sub-tile grid cutting edge artifacts via spatial padding.
"""
import os
import math
import numpy as np
import tifffile as tiff
import requests
import ee
from src import config
import cv2
def download_gee_tile(image, n, s, e, w, prefix, r, c, scale=10):
"""
Download a partitioned tile slice from Earth Engine as a localized GeoTIFF.
"""
region = ee.Geometry.BBox(w, s, e, n)
url = image.getDownloadURL({
'crs': 'EPSG:4326',
'region': region,
'scale': scale,
'format': 'GEO_TIFF'
})
try:
resp = requests.get(url, timeout=config.GEE_REQUEST_TIMEOUT)
if resp.status_code != 200:
config.logger.warning(f"β Tile {r}-{c} failed with status {resp.status_code}.")
return None
path = os.path.join(config.DOWNLOADS_DIR, f"{prefix}_tile_{r}_{c}.tiff")
with open(path, 'wb') as f:
f.write(resp.content)
return path
except Exception as e:
config.logger.error(f"β Tile download failed {r}-{c}: {e}")
return None
def stitch_tiles_safely(grid, is_3d=False):
"""
Stitch a 2D list of image tiles using zero-padding instead of truncation.
Eliminates geometric square-notch boundary artifacts completely.
"""
# Step 1: Uniformly stitch columns inside each individual row block
stitched_rows = []
for r_idx, row_list in enumerate(grid):
max_h = max(tile.shape[0] for tile in row_list)
padded_row_tiles = []
for tile in row_list:
h, w = tile.shape[:2]
if h < max_h:
# Pad the bottom margin with zeros to maintain resolution grid alignment
pad_bottom = max_h - h
border_shape = (pad_bottom, w, tile.shape[2]) if is_3d else (pad_bottom, w)
padding = np.zeros(border_shape, dtype=tile.dtype)
tile = np.concatenate([tile, padding], axis=0)
padded_row_tiles.append(tile)
stitched_rows.append(np.concatenate(padded_row_tiles, axis=1))
# Step 2: Uniformly vertical-stitch all rows together into the final matrix
max_w = max(row.shape[1] for row in stitched_rows)
padded_rows = []
for row in stitched_rows:
h, w = row.shape[:2]
if w < max_w:
# Pad the right margin with zeros to seamlessly join row transitions
pad_right = max_w - w
border_shape = (h, pad_right, row.shape[2]) if is_3d else (h, pad_right)
padding = np.zeros(border_shape, dtype=row.dtype)
row = np.concatenate([row, padding], axis=1)
padded_rows.append(row)
return np.concatenate(padded_rows, axis=0)
def fetch_optical_composite(coords, time_interval, filename_prefix, scale=10):
"""
Fetch Sentinel-2 optical composite with cloud masking.
Automatically segments massive regions dynamically using lossless padding alignment.
"""
try:
region = ee.Geometry.BBox(coords['west'], coords['south'], coords['east'], coords['north'])
start_date, end_date = time_interval
col = (ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
.filterBounds(region)
.filterDate(start_date, end_date)
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', config.S2_CLOUD_THRESHOLD)))
if col.size().getInfo() == 0:
col = (ee.ImageCollection('COPERNICUS/S2_HARMONIZED')
.filterBounds(region)
.filterDate(start_date, end_date)
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', config.S2_CLOUD_THRESHOLD)))
if col.size().getInfo() == 0:
return None, None, None, 100.0
def maskS2clouds(image):
qa = image.select('QA60')
cloudBitMask = 1 << 10
cirrusBitMask = 1 << 11
mask = qa.bitwiseAnd(cloudBitMask).eq(0).And(qa.bitwiseAnd(cirrusBitMask).eq(0))
return image.updateMask(mask)
mosaic = col.map(maskS2clouds).median()
final_image = mosaic.select(config.S2_BANDS).toInt16().unmask(0)
valid_mask = final_image.select('B4').gt(0)
stats = valid_mask.reduceRegion(
reducer=ee.Reducer.mean(),
geometry=region,
scale=config.GEE_SCALE_OPTICAL,
maxPixels=1e9
)
clear_fraction = stats.get('B4').getInfo()
if clear_fraction is None:
clear_fraction = 0
cloud_percent = (1.0 - clear_fraction) * 100.0
if cloud_percent > config.OPTICAL_CLEAR_THRESHOLD:
return None, None, None, cloud_percent
actual_date = end_date
source_type = "Sentinel-2 Optical (Cloud-Masked Composite)"
n, s, e, w = coords['north'], coords['south'], coords['east'], coords['west']
if scale == config.SCALE_XLARGE:
max_deg_step = config.MAX_DEG_STEP_XLARGE
elif scale == config.SCALE_LARGE:
max_deg_step = config.MAX_DEG_STEP_LARGE
else:
max_deg_step = config.MAX_DEG_STEP_SMALL
rows = math.ceil((n - s) / max_deg_step)
cols = math.ceil((e - w) / max_deg_step)
lat_steps = np.linspace(n, s, rows + 1)
lon_steps = np.linspace(w, e, cols + 1)
grid_data = []
for r in range(rows):
row_list = []
for c in range(cols):
tile_n, tile_s = lat_steps[r], lat_steps[r+1]
tile_w, tile_e = lon_steps[c], lon_steps[c+1]
tile_path = download_gee_tile(final_image, tile_n, tile_s, tile_e, tile_w,
filename_prefix, r, c, scale=scale)
if not tile_path:
return None, None, None, 100.0
img = tiff.imread(tile_path)
row_list.append(img)
os.remove(tile_path)
grid_data.append(row_list)
# Safe Stitching Execution
final_stitched = stitch_tiles_safely(grid_data, is_3d=True)
save_path = os.path.join(config.DOWNLOADS_DIR, f"{filename_prefix}.tiff")
tiff.imwrite(save_path, final_stitched)
return save_path, actual_date, source_type, cloud_percent
except Exception as e:
config.logger.error(f"β GEE Optical Error {filename_prefix}: {e}")
return None, None, None, 100.0
def fetch_sar_data(coords, time_interval, filename_prefix, is_baseline=False, scale=10):
"""
Fetch Sentinel-1 SAR imagery matrix (VV backscatter polarization).
"""
try:
region = ee.Geometry.BBox(coords['west'], coords['south'], coords['east'], coords['north'])
start_date, end_date = time_interval
s1_col = (ee.ImageCollection('COPERNICUS/S1_GRD')
.filterBounds(region)
.filterDate(start_date, end_date)
.filter(ee.Filter.listContains('transmitterReceiverPolarisation', config.S1_POLARIZATION))
.filter(ee.Filter.eq('instrumentMode', config.S1_INSTRUMENT_MODE)))
if s1_col.size().getInfo() == 0:
return None, None, None
if is_baseline:
s1_image = s1_col.median().select(config.S1_POLARIZATION).unmask(-99)
else:
s1_image = s1_col.mosaic().select(config.S1_POLARIZATION).unmask(-99)
final_image = s1_image.toFloat()
n, s, e, w = coords['north'], coords['south'], coords['east'], coords['west']
if scale == config.SCALE_XLARGE:
max_deg_step = config.MAX_DEG_STEP_XLARGE
elif scale == config.SCALE_LARGE:
max_deg_step = config.MAX_DEG_STEP_LARGE
else:
max_deg_step = config.MAX_DEG_STEP_SMALL
rows = math.ceil((n - s) / max_deg_step)
cols = math.ceil((e - w) / max_deg_step)
lat_steps = np.linspace(n, s, rows + 1)
lon_steps = np.linspace(w, e, cols + 1)
grid_data = []
for r in range(rows):
row_list = []
for c in range(cols):
tile_n, tile_s = lat_steps[r], lat_steps[r+1]
tile_w, tile_e = lon_steps[c], lon_steps[c+1]
tile_path = download_gee_tile(final_image, tile_n, tile_s, tile_e, tile_w,
filename_prefix, r, c, scale=scale)
if not tile_path:
return None, None, None
img = tiff.imread(tile_path)
row_list.append(img)
os.remove(tile_path)
grid_data.append(row_list)
final_stitched = stitch_tiles_safely(grid_data, is_3d=False)
actual_date = end_date
source_type = "Sentinel-1 SAR (VV Backscatter)" if not is_baseline else "Sentinel-1 SAR (Dry-Season Baseline)"
save_path = os.path.join(config.DOWNLOADS_DIR, f"{filename_prefix}_sar.tiff")
tiff.imwrite(save_path, final_stitched)
return save_path, actual_date, source_type
except Exception as e:
config.logger.error(f"β SAR Error {filename_prefix}: {e}")
return None, None, None
def fetch_dynamic_world_baseline(coords, time_interval, filename_prefix, scale=10):
"""
Fetch Dynamic World land cover discrete classification mapping for historical baselines.
"""
try:
region = ee.Geometry.BBox(coords['west'], coords['south'], coords['east'], coords['north'])
start_date, end_date = time_interval
dw_col = (ee.ImageCollection('GOOGLE/DYNAMICWORLD/V1')
.filterBounds(region)
.filterDate(start_date, end_date))
if dw_col.size().getInfo() == 0:
return None, None
final_image = dw_col.select('label').mode().eq(config.DW_WATER_CLASS).unmask(0).multiply(255).toByte()
n, s, e, w = coords['north'], coords['south'], coords['east'], coords['west']
if scale == config.SCALE_XLARGE:
max_deg_step = config.MAX_DEG_STEP_XLARGE
elif scale == config.SCALE_LARGE:
max_deg_step = config.MAX_DEG_STEP_LARGE
else:
max_deg_step = config.MAX_DEG_STEP_SMALL
rows = math.ceil((n - s) / max_deg_step)
cols = math.ceil((e - w) / max_deg_step)
lat_steps = np.linspace(n, s, rows + 1)
lon_steps = np.linspace(w, e, cols + 1)
grid_data = []
for r in range(rows):
row_list = []
for c in range(cols):
tile_n, tile_s = lat_steps[r], lat_steps[r+1]
tile_w, tile_e = lon_steps[c], lon_steps[c+1]
tile_path = download_gee_tile(final_image, tile_n, tile_s, tile_e, tile_w,
filename_prefix, r, c, scale=scale)
if not tile_path:
return None, None
img = tiff.imread(tile_path)
row_list.append(img)
os.remove(tile_path)
grid_data.append(row_list)
final_stitched = stitch_tiles_safely(grid_data, is_3d=False)
save_path = os.path.join(config.DOWNLOADS_DIR, f"{filename_prefix}.tiff")
tiff.imwrite(save_path, final_stitched)
return save_path, "Dynamic Dry-Season Anchor (April-May)"
except Exception as e:
config.logger.error(f"β Dynamic World Error {filename_prefix}: {e}")
return None, None |