SmartCane/python_app/experiments/omnicloud/check_tif.py
2026-01-06 14:17:37 +01:00

59 lines
2.1 KiB
Python

from osgeo import gdal
import numpy as np
from pathlib import Path
print("="*70)
print("CHECKING INDIVIDUAL TILES")
print("="*70)
# Check individual tiles
base = Path(r"C:\Users\timon\Resilience BV\4020 SCane ESA DEMO - Documenten\General\4020 SCDEMO Team\4020 TechnicalData\WP3\smartcane_v2\smartcane\laravel_app\storage\app\aura\cloud_test_single_images\2025-10-17")
tiles = [x for x in base.iterdir() if x.is_dir()]
print(f"\nTotal tiles: {len(tiles)}")
good_tiles = 0
empty_tiles = 0
for t in tiles:
tif = t / 'response.tiff'
if tif.exists():
ds = gdal.Open(str(tif))
r = ds.GetRasterBand(1).ReadAsArray()
pct = (r > 0).sum() / r.size * 100
mean_val = r[r > 0].mean() if (r > 0).sum() > 0 else 0
if pct > 10:
good_tiles += 1
print(f" ✓ Tile {t.name[:8]}... : {pct:5.1f}% non-zero, mean={mean_val:.3f}")
elif pct > 0:
print(f" ~ Tile {t.name[:8]}... : {pct:5.1f}% non-zero (sparse)")
else:
empty_tiles += 1
print(f"\nSummary: {good_tiles} good tiles, {empty_tiles} completely empty tiles")
print("\n" + "="*70)
print("CHECKING MERGED TIF")
print("="*70)
tif_path = r"C:\Users\timon\Resilience BV\4020 SCane ESA DEMO - Documenten\General\4020 SCDEMO Team\4020 TechnicalData\WP3\smartcane_v2\smartcane\laravel_app\storage\app\aura\cloud_test_merged_tif\2025-10-17.tif"
ds = gdal.Open(tif_path)
print(f"\nFile: 2025-10-17.tif")
print(f"Size: {ds.RasterXSize} x {ds.RasterYSize}")
print(f"Bands: {ds.RasterCount}")
red = ds.GetRasterBand(1).ReadAsArray()
print(f"\nRed band:")
print(f" Non-zero pixels: {(red > 0).sum() / red.size * 100:.2f}%")
print(f" Mean (all): {red.mean():.6f}")
print(f" Mean (non-zero): {red[red > 0].mean():.4f}")
print(f" Max: {red.max():.4f}")
print("\n" + "="*70)
print("DIAGNOSIS")
print("="*70)
print("\nThe problem: Most tiles are EMPTY (outside Planet imagery footprint)")
print("When merged, empty tiles dominate, making the image appear almost black.")
print("\nSolution: Use tighter bounding boxes or single bbox for the actual fields.")