67 lines
1.7 KiB
Bash
Executable File
67 lines
1.7 KiB
Bash
Executable File
#!/bin/bash
|
|
# Convert a huge .png file into smaller chunks for LeafletJS.
|
|
#
|
|
# Copyright (C) 2020 Markus Koch <markus@notsyncing.net>
|
|
#
|
|
# 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="$1"
|
|
MAPFILE="$2"
|
|
|
|
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/%d.png
|
|
|
|
rm $tempfile
|
|
|
|
if [ $crop -ge $width ]; then
|
|
break;
|
|
fi
|
|
crop=$(($crop * 2))
|
|
zoom=$(($zoom + 1))
|
|
done;
|
|
|
|
echo "Renaming files..."
|
|
for (( z=0; z<=$zoom; z++ )) {
|
|
echo "Zoom level $z"
|
|
fac=$((2**$z))
|
|
for (( y=0; y<$fac; y++ )) {
|
|
outdir="$MAPNAME/$z/$y"
|
|
mkdir $outdir
|
|
for (( x=0; x<$fac; x++ )) {
|
|
mv $MAPNAME/$z/$(($fac * y + $x)).png $outdir/$x.png
|
|
}
|
|
}
|
|
}
|