Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active February 12, 2021 08:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/127c8b7b4781c77528ce6d4827f2dc1a to your computer and use it in GitHub Desktop.
Save ThomasG77/127c8b7b4781c77528ce6d4827f2dc1a to your computer and use it in GitHub Desktop.
Generate imagery to avoid dedicated mapping server

Recipe to serve raster imagery without a mapping server

It can be light server using a PHP script or simply a directory

Input data

wget https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/raster/HYP_HR_SR_OB_DR.zip
unzip HYP_HR_SR_OB_DR.zip # Or unzip manually with 7zip or a similar software

MBTiles approach

Generate MBTiles with GDAL approach

gdal_translate HYP_HR_SR_OB_DR/HYP_HR_SR_OB_DR.tif my_dataset.mbtiles -of MBTILES
gdaladdo -r average my_dataset.mbtiles 2 4 8 16
cd ..

Generate MBTiles with gdal2mbtiles approach

Install with

pip install gdal2mbtiles

Then

gdal2mbtiles --min-resolution 0 --max-resolution 12 HYP_HR_SR_OB_DR/HYP_HR_SR_OB_DR.tif HYP_HR_SR_OB_DR.mbtiles # broken on our device

Serve resulting MBTiles with PHP script

git clone https://github.com/maptiler/tileserver-php.git
cd mbtiles-php
mv ../my_dataset.mbtiles .
mv ../HYP_HR_SR_OB_DR.mbtiles .
php -S 0.0.0.0:8081 # To serve PHP code without using Apache (fine for dev only). You will need to push the PHP file on the server

Open http://localhost:8081/tileserver.php to access a preview and zoom in a bit (the tiles are not available on zoom 0)

It can work on any PHP hosting with PHP extension SQLite support

Image to tiles directory (with hierarchy)

# Create directory to receive generated tiles
mkdir output
# You will generate tiles and an associated viewer with Leaflet library
gdal2tiles.py --webviewer=leaflet --zoom=0-6 HYP_HR_SR_OB_DR.tif output # min zoom = 0 and max zoom = 6

You can now open leaflet.html file in your browser to see the result (PS: tick the layer the right panel and zoom)

Then, you can push the directory and its content to your hosting to deploy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment