原文地址:http://blog.thematicmapping.org/2012/07/terrain-mapping-with-mapnik.html
作者:作者:Bj?rnSandvik(Google Plus:https://plus.google.com/118196887774002693676)
在之前的三篇博文中,我們使用DEM資料分別建立了三個GroTiff的資料,分別是:山體陰影,坡度陰影和彩色地形圖,1所示。在這篇文章中,我們將把這三個資料合併為一個資料,即Jotunheimen的地形圖。
圖1 之前三篇博文中產生的三個資料
上面圖 1中的三個資料如下:
- 山體陰影:http://thematicmapping.org/playground/terrain/jotunheimen_hillshade.png
- 坡度陰影:http://thematicmapping.org/playground/terrain/jotunheimen_slopeshade.png
- 彩色地形圖:http://thematicmapping.org/playground/terrain/jotunheimen_color_relief.png
Mapnik是一個用於地圖渲染和繪製強大的開源工具包。接下來我們將使用RasterSymbolizer來對GeoTiff資料進行合并。樣式和圖層(影像資料)可以使用xml(jotunheimen_terrain.xml)檔案來進行配置,檔案內容如下:
<Map srs="+proj=utm +zone=32 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"> <Style name="color relief style"> <Rule> <RasterSymbolizer mode="normal" /> </Rule> </Style> <Style name="hillshade style"> <Rule> <RasterSymbolizer opacity="0.6" mode="multiply" /> </Rule> </Style> <Layer name="color relief"> <StyleName>color relief style</StyleName> <Datasource> <Parameter name="type">gdal</Parameter> <Parameter name="file">jotunheimen_colour_relief.tif</Parameter> </Datasource> </Layer> <Layer name="hillshade"> <StyleName>hillshade style</StyleName> <Datasource> <Parameter name="type">gdal</Parameter> <Parameter name="file">jotunheimen_hillshade.tif</Parameter> </Datasource> </Layer> </Map>
在這個xml檔案的第一行,定義了一個地圖投影(UTM 32N)。Mapnil使用PROJ.4庫,你可以在網站http://spatialreference.org/ref/epsg/32632/proj4/找到這個地圖投影對應的PROJ4的格式。Mapnik不能修改映像的投影,所以在使用Mapnik之間把資料都轉為所需的投影。
接下來,我定義了此地圖的兩個風格的RasterSymbolizer。山體陰影來和彩色地形圖檔案使用乘法混合模式進行混合。山體陰影的每個像素乘以對應的彩色地形圖的像素值。同時還設定了山體陰影的不透明度為0.6,減少其的貢獻量。接下來是地圖圖層的樣式的定義。Mapnik將按照這個順序進行渲染,首先從最頂層的圖層開始。
你可以使用一個簡單的python指令碼(jotunheimen_terrain.py)來渲染地圖映像(你也可以使用像TileCache和MapProxy工具)。
#!/usr/bin/env pythonimport mapnikmap = mapnik.Map(3134, 3134)mapnik.load_map(map, 'jotunheimen_relief.xml')map.zoom_all() mapnik.render_to_file(map,'jotunheimen_relief.png')
上面的指令碼建立一個PNG映像,使用方法是:
python jotunheimen_terrain.py
得到的結果如所示:
接下來,我們在上面的xml中添加坡度陰影資料(jotunheimen_terrain2.xml),改後的xml內容如下:
<Map srs="+proj=utm +zone=32 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"> <Style name="color relief style"> <Rule> <RasterSymbolizer mode="normal" /> </Rule> </Style> <Style name="slopeshade style"> <Rule> <RasterSymbolizer opacity="0.1" mode="multiply" /> </Rule> </Style> <Style name="hillshade style"> <Rule> <RasterSymbolizer opacity="0.4" mode="multiply" /> </Rule> </Style> <Layer name="color relief"> <StyleName>color relief style</StyleName> <Datasource> <Parameter name="type">gdal</Parameter> <Parameter name="file">jotunheimen_colour_relief.tif</Parameter> </Datasource> </Layer> <Layer name="slopeshade"> <StyleName>hillshade style</StyleName> <Datasource> <Parameter name="type">gdal</Parameter> <Parameter name="file">jotunheimen_slopeshade.tif</Parameter> </Datasource> </Layer> <Layer name="hillshade"> <StyleName>hillshade style</StyleName> <Datasource> <Parameter name="type">gdal</Parameter> <Parameter name="file">jotunheimen_hillshade.tif</Parameter> </Datasource> </Layer> </Map>
上面我調整了坡度陰影和山體陰影的透明度來產生新的映像。處理的結果如所示:
上面兩個映像那個好是完全是由個人的喜好決定的。第一個圖中,坡面比較亮,第二個圖中,陡峭的山比較明顯,而且坡面的細節資訊更豐富。
在這兩個圖中,我很想念Jotunheimen地區的兩個重要的地類——湖泊和冰川。在下一個博文中將討論怎麼添加土地覆蓋資料。
………………………………………………………分割線……………………………………………………………
關於Bjørn Sandvik系列的博文,暫時翻譯這四篇,剩下的不是我關心的,我就不翻譯了,有興趣的童鞋可以去作者的部落格看看。有空寫個程式,把上面的流程整理一下。