summaryrefslogtreecommitdiff
path: root/scripts/convert_maps.sh
blob: 32ef029c578daf5df90800f427f938b7cb4154a0 (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
#!/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="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;