Contour Lines in Vector Tiles
Contour lines are a key element in topographic maps, as they allow us to visualize the elevation of an area. In this tutorial, we will show you how to create contour lines from a digital elevation model (DEM) and display them using vector tiles.
To follow this tutorial, you will need to have
gdal installed on your
system. If you don't have it already, you can install it by running the
sudo apt-get install gdal-bin
We will be using the ASTER (opens in a new tab) dataset for this example. The geotiff file provided in this tutorial is already in the desired projection (WebMercator), but in case you want to use a different geotiff file, you can use the gdalwarp command to reproject it. Here's an example of how to do it:
gdalwarp -rc \ -s_srs epsg:4326 -t_srs epsg:3857 \ -dstnodata 0 -of GTiff -co tiled=yes \ liecthenstein-aster-dem-v2.tif \ liecthenstein-aster-dem-v2-3857.tif
Now that we have the GeoTiff in the desired projection,
we can use the gdal_contour command to generate the contour lines.
-a argument specifies the column that contains the elevation data,
-i argument specifies the interval at which contours
should be generated (in this case, we are using 10 meters),
-nln argument names the table that will contain the data.
gdal_contour \ -a elevation -nln aster_dem -i 10 \ -f PostgreSQL \ liecthenstein-aster-dem-v2-3857.tif "PG:host=localhost user=baremaps password=baremaps dbname=baremaps"
To improve the performance of the contour lines,
we can add some indexes to the database.
Here's an example of how to create a
DROP INDEX IF EXISTS aster_dem_gix; CREATE INDEX IF NOT EXISTS aster_dem_gix ON aster_dem USING SPGIST(wkb_geometry);
Finally, we can use Baremaps to display the contour lines in a web browser. To do this, run the following command:
baremaps map dev \ --tileset 'tileset.json' \ --style 'style.json'
This will start the tile server and automatically reload the configuration files when you make changes. You can then preview the map in your web browser.
If you want to improve the rendering of the contour lines, you can use a smoothing function like ST_ChaikinSmoothing. This function takes a geometry as input and returns a smoothed version of that geometry. Here's an example of how you could use it:
UPDATE aster_dem SET wkb_geometry = ST_ChaikinSmoothing(wkb_geometry, 0.1)
Keep in mind that this step is optional and might not be necessary depending on the quality of your input data. You can experiment with different smoothing parameters to find the best balance between smoothness and preservation of original features.
That's it! You now know how to generate and display contour lines from a digital elevation model using vector tiles. Happy mapping!