Here are the steps needed to convert an image (like JPG or TIF) to MBTiles using GDAL:
-
Download and install GDAL (https://gdal.org/, https://anaconda.org/conda-forge/gdal):
conda install -c conda-forge gdal
Installing GDAL is kind of a pain in the ass, especially on Windows. I'm assuming you'll be able to work out whatever issues you run into here.
-
Download example
jpg
andtif
map images we can work with:- http://www.radcyberzine.com/xglobe/index.html#maps - See Super-hi-res maps section, download one of the
jpg
files. - https://www.naturalearthdata.com/downloads/10m-raster-data/10m-natural-earth-2/ - See Natural Earth II with Shaded Relief, Water, and Drainages section and download the large size. We are interested in the
tif
file contained within thezip
file downloaded.
- http://www.radcyberzine.com/xglobe/index.html#maps - See Super-hi-res maps section, download one of the
-
If you're starting with a
jpg
, first convert it totif
. To accomplish this run (replacingjpg
andtif
file names where necessary):gdal_translate -a_srs WGS84 -a_ullr -180 +90 +180 -90 map_1.jpg map_1.tif
-
To convert
tif
tombtiles
run (replacingtif
andmbtiles
file names where necessary):gdal_translate -co "ZLEVEL=9" -of mbtiles map_1.tif map_1.mbtiles gdaladdo -r nearest map_1.mbtiles
The first command lets GDAL figure out the max zoom it can generate based on the image resolution. The second command lets GDAL figure out and generate the lesser zoom levels based on the max zoom level that already exists. It's not uncommon for those two commands to take a while to complete.
-
You can open the generated
mbtiles
file using a SQLite GUI or programmatically connect to it via a SQLite driver to inpsect and verify the content is correct (the spec can be found https://github.com/mapbox/mbtiles-spec/blob/master/1.2/spec.md). -
Now you need to serve your tiled map data. Here is one way to accomplish this:
docker run -d -p 80:80 --name tileserver-php -v /d/mbtiles:/var/www/ klokantech/tileserver-php
The volume should map to the location where all your
mbtiles
files sit. If you wish, you can get better performance by extracting the.mbtiles
files out of the database onto the file system (if you have the disc space).You can now wire up a https://leafletjs.com/ client to your server. The url for the tile layer you feed into Leaflet would be
http://localhost:80/map_1/{z}/{x}/{y}.png
-
If you find tileserver-php 304's your requests when it shouldn't, just modify the
isModified
function intileserver.php
to always returnFALSE
(see maptiler/tileserver-php#148).