關閉      標題:桃園 dem 轉 tif 轉 wms 仿 moi
內容:
1. 下載 dem from :
https://data.gov.tw/dataset/35430
https://www.tgos.tw/MDE/MetaData/CRUD/ViewMetaData?GUID=74c6a7b7-1816-499c-bb60-89246065390f&VIEW_TYPE=only&SOURCE=1

2. 解壓縮,如桃園,將所有的 grd 合併成 csv
type *.grd > all.csv

3. 編輯 all.csv
第一行加入
X,Y,Z
取代所有的 空白 變成 ,
移除最後一行空白

4. 將 all.csv 變成 tif
建立 points.vrt
內容如下:
<OGRVRTDataSource>
    <OGRVRTLayer name="all">
        <SrcDataSource>all.csv</SrcDataSource>
        <GeometryType>wkbPoint</GeometryType>
        <LayerSRS>EPSG:3826</LayerSRS>
        <GeometryField encoding="PointFromColumns" x="X" y="Y" z="Z"/>
    </OGRVRTLayer>
</OGRVRTDataSource>


gdal_grid -of GTiff -a invdistnn:power=2.0:radius=100:max_points=12 -zfield Z -outsize 2660 3099 points.vrt dem_20m_fast.tif
如何得知 2660 3099
方法 1:從 真實範圍 + 目標解析度

如果你知道 DEM 的實際範圍(左上 X/Y、右下 X/Y)和你想要的解析度(像素大小):
ulx, uly = 左上座標
lrx, lry = 右下座標
dx = 像素大小 X(例如 20 m)
dy = 像素大小 Y(例如 20 m)

計算像素數量:
width  = ceil((lrx - ulx) / dx)
height = ceil((lry - uly) / dy)

例如你之前資料:
ulx = 246940, uly = 2718520
lrx = 298940, lry = 2779520
dx = dy = 20

計算:

width  = (298940 - 246940)/20 = 52000/20 = 2600
height = (2779520 - 2718520)/20 = 61000/20 = 3050

所以就可以寫:
-outsize 2600 3050

也可以用 ogrinfo 取得 extent
ogrinfo -al -so points.vrt
INFO: Open of `points.vrt'
      using driver `OGR_VRT' successful.
Layer name: all
Geometry: Point
Feature Count: 4052125
Extent: (246940.000000, 2718520.000000) - (298940.000000, 2779520.000000)

5. 將 tif 變成 坡度 percent 的格式
$CMD = "gdaldem slope dem_20m.tif slope_20m_percent.tif -p";
  `${CMD}`;

6. 建立坡度圖值  slope_colors.txt 
0	255  255 255
1    26  150  78
5   140  202  93
15  215  235 140
30  251  252 194
40  248  222 138
55  211   49  34
100 251    7   6


gdaldem color-relief slope_20m_percent.tif slope_colors.txt slope_20m_color.png -of PNG

7. 用 imagemagick 移除白色變成透明色
c:\bin\convert slope_20m_color_flipped.png -transparent "rgb(255,255,255)" slope_20m_color_flipped_alpha.png

7. 將 png 改回 geotiff
gdal_translate   -of GTiff   -a_srs EPSG:3826   -a_ullr 246940 2718520 298940 2779520   -co TILED=YES   -co COMPRESS=DEFLATE   -co ALPHA=YES   slope_20m_color_flipped_alpha.png slope_20m_color_flipped_alpha.tif

8. 將 geotiff 發佈成 wms 或 wmts

9. 成果
https://3wa.tw/demo/htm/test_javascript.php?id=397