SRTM数据可以从Shuttle Radar Topography Mission的官方网站下载。
N32E020.hgt
的HGT格式文件。SRTM的HGT格式是一个由1201x1201个坐标点组成的二进制文件,采用大端序(Big endian)存储。
import sys
nrows
=ncols
=1201
xdim
=ydim
=0.000833
args
=sys
.argv
filename
=args
[1]
name, ext
=os
.path
.splitext
(filename
)
outname
=name
+'.csv'
longitude
=float(
name
[1:3])
latitude
=float(
name
[4:7])
print('longitude: '+
name
[0]+
name
[1:3])
print('latitude: '+
name
[3]+
name
[4:7])
ulxmap
=longitude
+(
xdim
/2.0)
ulymap
=(
latitude
+1.0)-
(
ydim
/2.0)
a
=array
.array
('h')
f
=open(
filename,
'rb')
a
.fromfile
(f, nrows
*ncols
)
f
.close
()
a
.byteswap
()
alist
=a
.tolist
()
out
=open(
outname,
'w')
count
=0
forrow
inrange(
nrows
):
for
col
inrange(
ncols
):
x
=ulxmap
+xdim
*col
y
=ulymap
-ydim
*row
z
=max(0.0, alist[
count
])
out
.write
(str(x
)+
','
+
str(
y
)+
','
+
str(
z
/10000.0)+
'\n')
count
+=1
if
count
%100000==
0:
print(str(int(100.0
*
count
/(nrows
*ncols
)))+
'%')
print('100%')
print('node number: '+
str(
count
))
out
.close
()
执行以下命令:
python hgtToCSV.py N32E020.hgt
将N32E020.hgt转换为N32E020.csv。注意:转换后的坐标可能不准确,实际使用时请确保转换为正确的坐标。
接下来,将CSV文件转换为ParaView可以读取的VTS格式(VTK的结构化网格数据格式)。这里也使用一个简单的Python脚本来完成转换。
importos
.path
import sys
fromparaview
.simple
import*
paraview
.simple
._DisableFirstRenderCameraReset
()
args
=sys
.argv
filename
=args
[1]
workingDir
='./'
fileName
=args
[1]
csvReader
=CSVReader
(FileName
=[workingDir
+fileName
+'.csv'])
csvReader
.DetectNumericColumns
=1
csvReader
.UseStringDelimiter
=1
csvReader
.HaveHeaders
=0
csvReader
.FieldDelimiterCharacters
=','
csvReader
.MergeConsecutiveDelimiters
=0
tableToStructuredGrid
=TableToStructuredGrid
(Input
=csvReader
)
tableToStructuredGrid
.WholeExtent
=[0, 1200, 0, 1200, 0, 0]
tableToStructuredGrid
.XColumn
='Field 0'
tableToStructuredGrid
.YColumn
='Field 1'
tableToStructuredGrid
.ZColumn
='Field 2'
calculator
=Calculator
(Input
=tableToStructuredGrid
)
calculator
.AttributeMode
='Point Data'
calculator
.CoordinateResults
=0
calculator
.ResultNormals
=0
calculator
.ResultTCoords
=0
calculator
.ResultArrayName
='z'
calculator
.Function
='coordsZ'
calculator
.ReplaceInvalidResults
=1
calculator
.ReplacementValue
=0.0
SaveData
(workingDir
+fileName
+'.vts', proxy=
calculator,
Writealltimestepsasfileseries
=0,
DataMode
='Appended',
EncodeAppendedData
=1,
CompressorType
='ZLib')
使用ParaView安装文件夹中的pvpython执行脚本:
pvpython csvToVts.py N32E020
转换完成后,将生成N32E020.vts文件。
将上述步骤生成的N32E020.vts文件加载到ParaView中,即可进行可视化。