Menggabungkan banyak DEM menjadi Satu DEM


Berikut merupakan cara menggabungkan single DEM, seperti di data DEMNAS, dengan memperhatikan sebaran data DEMNAS.
yang pertama kali kita lakukan ialah tentukan dahulu DEM yang akan digunakan.
import os
from osgeo import gdal
from tqdm import tqdm
# Folder tempat file DEM tersimpan
dem_folder = "drive/MyDrive/demnas"
# Output file hasil gabungan
output_dem = "drive/MyDrive/demnas/merged_dem.tif"
# Ambil semua file .tif dari folder demnas
dem_files = [os.path.join(dem_folder, f) for f in os.listdir(dem_folder) if f.endswith(".tif")]
# Pastikan ada file yang akan digabungkan
if len(dem_files) == 0:
print("โ Tidak ada file DEM (.tif) di folder demnas.")
else:
print(f"๐ Menggabungkan {len(dem_files)} file DEM...")
# Progress bar untuk membaca file
with tqdm(total=len(dem_files), desc="๐ Memproses file DEM", unit="file") as bar:
vrt_file = "drive/MyDrive/document/demnas/temp.vrt"
gdal.BuildVRT(vrt_file, dem_files) # Buat VRT (Virtual Raster)
bar.update(len(dem_files)) # Langsung selesai karena BuildVRT satu tahap
# Progress bar untuk menyimpan hasil akhir
with tqdm(total=100, desc="๐พ Menyimpan file gabungan", unit="%", position=0, leave=True) as bar:
gdal.Translate(output_dem, vrt_file) # Simpan sebagai TIFF
bar.update(100) # Simulasi progres selesai
# Hapus file VRT sementara
os.remove(vrt_file)
print(f"โ
File DEM gabungan berhasil dibuat: {output_dem}")
lalu apabila sudah didownnload, tampilkan hasilnya dengan matplotlib sebagai berikut:
import rasterio
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
# Path ke file DEM
dem_path = "drive/MyDrive/demnas/merged_dem.tif"
# Buka file DEM dalam resolusi penuh
with rasterio.open(dem_path) as src:
dem_data = src.read(1).astype(float) # Pastikan tipe data float
extent = [src.bounds.left, src.bounds.right, src.bounds.bottom, src.bounds.top]
# Periksa nilai NoData dan ubah ke NaN
if src.nodata is not None:
dem_data[dem_data == src.nodata] = np.nan
# Pastikan tidak ada nilai NaN atau Inf
dem_data = np.nan_to_num(dem_data, nan=0.0, posinf=np.max(dem_data[dem_data != np.inf]), neginf=0.0)
# Buat colormap smooth dari hijau ke merah
colors = [
(0.0, "darkgreen"), # Elevasi rendah (hijau tua)
(0.1, "green"), # Elevasi sedikit lebih tinggi
(0.3, "yellowgreen"), # Peralihan ke elevasi sedang
(0.5, "yellow"), # Elevasi menengah
(0.7, "orange"), # Peralihan ke tinggi
(0.9, "red"), # Elevasi tinggi
(1.0, "darkred") # Elevasi sangat tinggi
]
cmap = LinearSegmentedColormap.from_list("topo_cmap", colors)
# Tampilkan DEM dengan colormap smooth
fig, ax = plt.subplots(figsize=(10, 8))
dem_plot = ax.imshow(dem_data, cmap=cmap, extent=extent)
# Tambahkan colorbar
cbar = plt.colorbar(dem_plot, ax=ax)
cbar.set_label("Elevation (m)")
# Tambahkan judul dan label
plt.title("Digital Elevation Model (DEM) - Smooth Topographic Colors")
plt.xlabel("Longitude")
plt.ylabel("Latitude")
# Tampilkan plot
plt.show()
Maka hasil yang didapatkan sebagai berikut:
Dari gambar diatas, perlu adanya batas administrasi atau batas garis pantau untuk memotong dem yang sudah digenerate.
Selain itu diharapkan kode ini bisa bermanfaat untuk menggabungkan data yang lebih teliti, dan ingat untuk mengecek kesesuaian batas antar dem agar tidak ada gap yang kosong maupun overlap.
Subscribe to my newsletter
Read articles from Hidayatullah directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Hidayatullah
Hidayatullah
Hi, my name is Hidayatullah. I am a GIS Engineer, Analyst, Specialist, and everything related to GIS. With over 5 years of experience, I am highly proficient in ArcGIS and QGIS. I specialize in spatial topology methods, least square adjustment measurement methods, PostGIS with PostgreSQL, RDBMS databases, spatial generalization by scale, WebGIS Geoserveer/Mapserver/Mapproxy, and more. If you're interested in my services, feel free to reach out via email at genhidayatullah@icloud.com.