Web maps are usually served by smaller tiles which are loaded separately but gives an impression of a seamless image. These are the tiled based maps. Check out the earlier blog post on Converting Satellite imagery to Tile Map Service. Usually, tiles are small in size to get loaded. But things might get tricky if we want to use the offline maps in mobile version, or the tiles are huge which would make it slow and inefficient. One way of overcoming this problem is by using MBTiles.
MBTiles is a format given by MapBox, for storing the tilesets. It’s an open specification, for storing large vector or raster data, and is based on SQLite database. The tiles are compressed and compact, and as a result, makes it easier to transfer and carry around and used in the mobile.
In this post, we will be converting the raster tif image into the mbtiles format using the open source powerful GDAL library. Check the correct installation of the GDAL using gdalinfo.
- Get your raster data. I downloaded the landcover maps for Laos using the SERVIR-Mekong Regional Land Cover Monitoring System for year 2018. This is a single band image with unique values for each class. The class number and the assigned color looks like:
Class Name Class Value Class Color Unknown 0 6f6f6f Surface Water 1 aec3d4 Snow and Ice 2 b1f9ff Mangroves 3 111149 Flooded Forest 4 287463 Forest 5 152106 Orchard or Plantation Forest 6 c3aa69 Evergreen Broadleaf 7 7db087 Mixed Forest 8 387242 Urban and Built Up 9 cc0013 Cropland 10 8dc33b Rice 11 ffff00 Mining 12 cec2a5 Barren 13 674c06 Wetlands 14 3bc3b2 Grassland 15 f4a460 Shrubland 16 800080 Aquaculture 17 800080
- If you don’t have a color relief raster, you can create it. Unfortunately, the land cover map is not a color relief raster. Creating a color relief in the band is quite easy. We will be using gdaldem. Make a txt file called color and have the raster value and the required color (the color should be the name of the color as red, green, yellow or the actual RGB color like 111 111 111. The content of the color.txt file looks as:
0 111 111 111
1 174 195 212
2 177 249 255
3 17 17 73
4 40 116 99
5 21 33 6
6 195 170 105
7 125 176 135
8 56 114 66
9 204 0 19
10 141 195 59
11 255 255 0
12 206 194 165
13 103 76 6
14 59 195 178
15 244 164 96
16 128 0 128
17 81 118 142
- Next, generate the color relief map as follows:
where landcover.lc.tif is input file, color.txt is the value and color that needs to be applied and landcover-output.tif is the output file.
gdaldem color-relief landcover.lc.tif color.txt landcover-output.tif
- If you have a color table, you may want to expand your band to data with 3 band (RGB) or 4 bands (RGBA) as:
gdal_translate -expand rgba landcover-output.tif landcover.tif
- Since MBTiles works with Spherical Mercator projection, let’s reproject the raster using gdalwarp as:
where r is the resampling method and landcover.tif is the output file.
gdalwarp -t_srs EPSG:3857 -r cubicspline landcover-output.tif landcover.tif
- Create an MBTiles file from the result of step 5 using gdal_translate as:
gdal_translate -of mbtiles landcover.tif landcover.mbtiles
- The last step is to create overview images at different lower zoom levels using gdaladdo.
gdaladdo -r cubicspline landcover.mbtiles 2 4 8 16
Happy Geoinformatics 🙂