summaryrefslogtreecommitdiff
path: root/scripts/convert_maps.1.sh
blob: 0c78c8fe3a65268ff774341d608636d1529cd31f (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
#!/bin/bash
# Convert a huge .png file into smaller chunks for LeafletJS.
#
# Copyright (C) 2020  Y. Wang <ywang@forksworld.de>
# 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/.

TILESIZE=256
MAPFILE="$1"
MAPNAME="${1/.png/}"

width=`file "$MAPFILE" | sed -n "s/.* \([0-9]\+\) x \([0-9]\+\).*/\1/p"`
crop=$TILESIZE
zoom=0

# Enabled because my VPS
IMFLAGS="-limit area 0"
#IMFLAGS=""

echo -n "Getting highest zoomlevel... ";
while [ $crop -lt $width ]; do
	crop=$(($crop * 2));
	zoom=$(($zoom + 1));
done;
echo "$zoom";

echo "Extending map for maptile generation..."
extended=$(mktemp)
echo "  The extended map is temporarily saved to $extended."
convert $MAPFILE -extent ${crop}x${crop} png:$extended

while [ $zoom -ge 0 ]; do
	out="$MAPNAME/$zoom"
	tempfile=$out/temp.png

	echo "Generating maps for zoomlevel $zoom to $out..."

	mkdir -p $out;

	echo "  Scaling image..."
	convert png:$extended -resize ${crop}x${crop} $tempfile

	echo "  Generating tiles..."
	convert $tempfile -crop ${TILESIZE}x${TILESIZE} +adjoin $out/%d.png

	rm $tempfile

	echo "  Creating symlinks..."
	n=$(($crop / $TILESIZE))
	for i in $(seq 0 $(($n-1))); do
		rm -rf $out/$i >/dev/null 2>/dev/null # Nobody cares if this fails ...
		mkdir $out/$i
		for j in $(seq 0 $(($n-1))); do
			ln -s ../$(($j*$n+$i)).png $out/$i/${j}.png
		done
	done

	crop=$(($crop / 2))
	zoom=$(($zoom - 1))
done;

rm $extended