carte continue: cropped images + continuous map + PDF download via jsPDF
This commit is contained in:
@ -780,6 +780,70 @@ def tif_to_png(tif_file, vis_dir, resolution, keep_tif=False, source_info=None,
|
||||
return None
|
||||
|
||||
|
||||
def tif_to_crop(tif_file, vis_dir, resolution, keep_tif=False, quality=98, output_format='avif'):
|
||||
"""Convert GeoTIFF to a cropped visualization image (no legend, no overlay).
|
||||
|
||||
Applies colormap and saves the image as a pure 1×1 km square.
|
||||
Used for grid/map display where images must tile seamlessly.
|
||||
|
||||
Args:
|
||||
tif_file: Path to input GeoTIFF.
|
||||
vis_dir: Output directory for the image file.
|
||||
resolution: Grid resolution in m/px.
|
||||
keep_tif: If True, keep the source TIFF after conversion.
|
||||
quality: Image quality (1-100). Use 100 for lossless.
|
||||
output_format: Output format ('webp' or 'avif').
|
||||
|
||||
Returns:
|
||||
Path to output image file, or None on failure.
|
||||
"""
|
||||
if not tif_file or not tif_file.exists():
|
||||
return None
|
||||
|
||||
ext = 'avif' if output_format == 'avif' else 'webp'
|
||||
output_file = vis_dir / f"{tif_file.stem}.{ext}"
|
||||
|
||||
try:
|
||||
with rasterio.open(tif_file) as src:
|
||||
is_rgb = src.count >= 3 and any(k in str(tif_file) for k in ('ortho', 'topo'))
|
||||
|
||||
if is_rgb:
|
||||
data = src.read([1, 2, 3])
|
||||
data = np.moveaxis(data, 0, -1)
|
||||
else:
|
||||
data = src.read(1)
|
||||
|
||||
# Apply colormap normalization
|
||||
data, cmap_name, title, legend_label, description, is_rgb_result = _apply_colormap(data, tif_file)
|
||||
|
||||
# Convert to RGB using colormap
|
||||
if is_rgb_result:
|
||||
# RGB images are already in RGB
|
||||
rgb_data = (data * 255).astype(np.uint8)
|
||||
else:
|
||||
# Normalize data to 0-1 range for colormap
|
||||
cmap = plt.get_cmap(cmap_name)
|
||||
rgb_float = cmap(data.clip(0, 1))
|
||||
rgb_data = (rgb_float[:, :, :3] * 255).astype(np.uint8)
|
||||
|
||||
# Save as AVIF/WebP
|
||||
img = PILImage.fromarray(rgb_data)
|
||||
pil_format = 'AVIF' if output_format == 'avif' else 'WEBP'
|
||||
if quality >= 100:
|
||||
img.save(str(output_file), format=pil_format, lossless=True)
|
||||
else:
|
||||
img.save(str(output_file), format=pil_format, quality=quality)
|
||||
|
||||
# Delete source TIFF (unless --keep-tif)
|
||||
if not keep_tif:
|
||||
tif_file.unlink(missing_ok=True)
|
||||
|
||||
return output_file
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f" Erreur conversion crop {ext.upper()}: {e}", exc_info=True)
|
||||
return None
|
||||
|
||||
|
||||
def generate_pdf_report(basename, vis_dir, pdf_dir, resolution):
|
||||
"""Generate A3 PDF report for a LiDAR file with all visualizations.
|
||||
|
||||
Reference in New Issue
Block a user