#!/bin/bash # Convert a huge .png file into smaller chunks for LeafletJS. # # Copyright (C) 2020 Markus Koch # # This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. # You can obtain a copy of the license at https://mozilla.org/MPL/2.0/. # TODO: The width of the input png has to be a multiple of TILESIZE, otherwise bad things will happen. # Workaround for now is to add white borders on the bottom and right: # `convert $MAPFILE -extent ${crop}x${crop} -gravity NorthWest $MAPFILE.scaled.png` TILESIZE=256 MAPNAME="world-2020-04-09" MAPFILE="../$MAPNAME.png" width=`file "$MAPFILE" | sed -n "s/.* \([0-9]\+\) x \([0-9]\+\).*/\1/p"` crop=$TILESIZE zoom=0 while true; do out="$MAPNAME/$zoom" tempfile=$out/temp.png echo "" echo "Generating maps for zoomlevel $zoom to $out..." mkdir -p $out; if [ ! -f "$tempfile" ]; then if [ $crop -ge $width ]; then echo " Reached max zoom at zoomlevel $zoom [using original zoom]" cp $MAPFILE $tempfile #convert $MAPFILE -extent ${crop}x${crop} -gravity NorthWest $tempfile else echo " Scaling map for zoomlevel $zoom" convert $MAPFILE -resize ${crop}x${crop} $tempfile fi else echo " Reusing existing scaled image" fi echo " Generating tiles..." convert $tempfile -crop ${TILESIZE}x${TILESIZE} +adjoin $out/%05d.png rm $tempfile if [ $crop -ge $width ]; then break; fi crop=$(($crop * 2)) zoom=$(($zoom + 1)) done;