【前言】
數值地形模型(DTM)的處理是空間資訊科學的基礎。幾乎所有與空間資訊相關的研究,都與 DTM 脫離不了關係。因此,在本系列課程中,我們將介紹 DTM 資料的基本處理方法,包含:
- 如何用 FORTRAN 讀取 DTM 資料檔案?(使用 Visual FORTRAN)
- 如何用 Visual Basic 6.0 讀取 DTM 資料檔案?
- 使用 VB.NET 進行二進位檔案的讀取與寫入。
- 如何用 VB.NET 讀取 DTM 資料檔案?
- 2026年技術評析
(1) 如何用 FORTRAN 讀取 DTM 資料檔案?
假設我們有一個 DTM 資料檔,格式為二進位(Binary)整數矩陣。雖然大部分的人會習慣使用 C/C++ 或 Matlab 來處理,但在科學運算領域,FORTRAN 依然是非常強大的工具。
資料規格假設:
- 檔案格式:二進位整數矩陣。
- 矩陣大小:477 (欄/Column) × 512 (列/Row)。
- 資料型態:2-byte 整數 (Integer*2)。
程式邏輯說明:
- 宣告陣列: 宣告一個
integer*2的二維陣列,大小為 (512, 477)。 - 開啟檔案: 使用
open指令,並將格式(form)宣告為binary。 - 讀取資料: 使用無格式(unformatted)的
read指令讀取高程值。 - 格式轉換: 將讀入的資料輸出為 GIS 軟體常見的標準格式:
- ArcView ASCII Grid (*.asc):純文字網格檔。
- ArcView Binary Raster (*.flt & *.hdr):二進位網格及其標頭檔。
【FORTRAN 範例原始碼:ReadDEM】
program ReadDEM
integer*2 dem(512, 477)
integer i, j
character*20 fmt
integer nCols, nRows
real*8 start_x, start_y
integer*2 cell_size
integer*2 NoData_Value
! 開啟原始二進位 DTM 檔案
open(10, file='SP25B.BIN', form='binary', status='old')
! 準備輸出 ASCII Grid 檔案
open(20, file='SP25B.ASC', status='unknown')
! 準備輸出 Binary Raster 標頭檔與資料檔
open(30, file='SP25B.hdr', status='unknown')
open(31, file='SP25B.flt', status='unknown', form='binary')
! 讀取二進位資料
do i = 1, 512
read(10) (dem(i, j), j = 1, 477)
end do
! 設定網格參數
nCols = 477
nRows = 512
start_x = 0.0
start_y = 0.0
cell_size = 40
NoData_value = -9999
! 動態建立格式字串
fmt = '( 512(i4, 1x))'
write(fmt(2:5), '(i4.4)') nCols
! 寫入 ASCII Grid 標頭資訊
write(20, 200) nCols
write(20, 201) nRows
write(20, 202) start_x
write(20, 203) start_y
write(20, 204) cell_size
write(20, 205) NoData_Value
! 寫入 ASCII Grid 資料本體
do i = 1, 512
write(20, fmt) (dem(i, j), j = 1, 512)
end do
200 format('ncols ', i3)
201 format('nrows ', i3)
202 format('xllcorner ', f10.2)
203 format('yllcorner ', f10.2)
204 format('cellsize ', i2)
205 format('NODATA_value ', i5)
! 寫入 Binary Raster 標頭檔
write(30, 200) nCols
write(30, 201) nRows
write(30, 202) start_x
write(30, 203) start_y
write(30, 204) cell_size
write(30, 205) NoData_Value
! 寫入 Binary Raster 資料檔 (.flt 需為浮點數)
do i = 1, 512
write(31) (float(dem(i, j)), j = 1, 512)
end do
stop
end
重點說明:
- 技術重點: 程式展現了如何處理
integer*2(16-bit) 資料,這是早期 DTM(如台灣全島 40m DTM)常見的格式。 - GIS 應用: 將資料轉存為
.asc和.flt,這使得 FORTRAN 運算的結果可以直接被 ArcGIS 或 ArcView 等 GIS 軟體讀取。

















