summaryrefslogtreecommitdiff
path: root/scripts/convert_maps.sh
blob: 16bfe9f1576f54ed6aa22000570ff8dd5b4ae048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/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
		}
	}
}