Created
May 5, 2025 21:44
-
-
Save AdrianoPereira/41a6ae0e0d81a89cf0ee823e1d46785c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # mosaic | |
| import rasterio | |
| from rasterio.merge import merge | |
| from pathlib import Path | |
| import glob | |
| # Caminho para a pasta onde estão os GeoTIFFs baixados | |
| input_dir = Path("/home/adriano/Doutorado/landcover/EarthEngine_Exports/") # ajuste o nome da pasta se necessário | |
| # Lista todos os arquivos .tif | |
| tif_files = sorted(glob.glob(str(input_dir / "*.tif"))) | |
| # Abre todos os arquivos como objetos rasterio | |
| src_files_to_mosaic = [rasterio.open(fp) for fp in tif_files] | |
| # Realiza a fusão | |
| mosaic, out_transform = merge(src_files_to_mosaic) | |
| # Usa metadados do primeiro arquivo como base | |
| out_meta = src_files_to_mosaic[0].meta.copy() | |
| out_meta.update({ | |
| "driver": "GTiff", | |
| "height": mosaic.shape[1], | |
| "width": mosaic.shape[2], | |
| "transform": out_transform, | |
| "compress": "lzw" | |
| }) | |
| # Caminho para salvar o mosaico final | |
| output_file = "landcover_peru_merged.tif" | |
| # Salva o arquivo final | |
| with rasterio.open(output_file, "w", **out_meta) as dest: | |
| dest.write(mosaic) | |
| print(f"Mosaico salvo como {output_file}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment