![]() |
Credits: Wikipedia |
We can use a GDAL command, gdal_contour, to create vector contour lines from a DEM:
gdal_contour -a height jotunheimen.tif jotunheimen_contour_25m.shp -i 25.0
The resulting shapefile has an attribute named “height” (-a) containing the elevation of each contour line. The contour interval, the difference in elevation between successive contour lines, is 25 meters (-i).
Let's render the contour lines with Mapnik (jotunheimen_contours.xml):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Map srs="+proj=utm +zone=32 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"> | |
<Style name="contours 25m style"> | |
<Rule> | |
<LineSymbolizer stroke="#747b90" stroke-width="0.7" /> | |
</Rule> | |
</Style> | |
<Layer name="contours 25m"> | |
<StyleName>contours 25m style</StyleName> | |
<Datasource> | |
<Parameter name="type">shape</Parameter> | |
<Parameter name="file">jotunheimen_contour_25m.shp</Parameter> | |
</Datasource> | |
</Layer> | |
</Map> |
We can use Nik2Img to create an image from the Mapnik XML file:
nik2img.py jotunheimen_contours.xml jotunheimen_contours.png -d 4096 4096 --projected-extent 460000 6810000 470000 6820000
![]() |
Contour lines for Besseggen mountain ridge |
I want to render the contour lines differently for various zoom levels. The contours should be more visible at higher zoom levels, and we should add numbers showing the actual elevation for some of the contours. I also want to make the contour lines for every 100 meters thicker. To achieve this we need to know the map scale at different zoom levels. In the last blog post, we calculated the resolution (meters per pixel) for each zoom level. You get the map scale by dividing this number by 0.00028 (234.375 / 0.00028 = 837053.571).
Zoom
|
Resolution
|
Scale
|
0
|
234.375
|
837054
|
1
|
117.1875
|
418527
|
2
|
58.59375
|
209263
|
3
|
29.296875
|
104632
|
4
|
14.6484375
|
52316
|
For zoom level 4, the map scale is about 1:50,000. Read more about map scale on this excellent Mapnik page.
Let’s create a style rule that will display the elevation on a contour line on zoom level 3:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Style name="contours 500m text style"> | |
<Rule> | |
<MaxScaleDenominator>200000</MaxScaleDenominator> | |
<MinScaleDenominator>100000</MinScaleDenominator> | |
<TextSymbolizer face-name="DejaVu Sans Book" size="9" fill="#747b90" halo-radius="1" placement="line">[height]</TextSymbolizer> | |
</Rule> | |
</Style> |
Finally, I've used TileCache to render transparent tiles with contours, and added them as a separate layer to Leaflet map:
Fullscreen map