3 # This is a helper script for generating pimped up OSM tiles.
4 # The standard openstreetmap-carto-style as used on openstreetmap.org
5 # looks extremely dull in Zoomlevels 0-8. They look a lot nicer if
6 # "landuse" (forests, deserts) is shown. One way to do this is to
7 # generate two modifications of the osm-carto style, render tiles
8 # in each style, and then merge them:
9 # - For the background, essentially empty all .mss files except
10 # style.mss and landcover.mss - you need to throw out everything but
11 # the landuse. For the landuse, make sure to adapt the rules so that
12 # it is shown on zoomlevel 8. Render only zoomlevel 8.
13 # - For the foreground, change style.mss: background-color within
14 # Map { } needs to be "transparent" (without the quotes). #world in
15 # shapefiles.mss needs to be removed. Render zoomlevels 0-8.
16 # Then use this script to merge back+foreground. Voila, nicer looking
19 # WARNING: Doing this for zoom level 0 in HD requires 90 GB of
21 # That's why levels 0+1 are not generated by default (you need to change
24 # URL generally is: /zoom/X/Y.png within the dirs below.
26 # The source for the foreground that you want to put over the
27 # background. The foreground needs to exist for all zoomlevels you
29 $srcfgpath = '/mnt/tiles/statictiles/lowzoom-fg/';
30 # The source for the background. Only one zoomlevel needs to exist,
32 $srcbgpath = '/mnt/tiles/statictiles/lowzoom-bg/';
33 # Where the combined tiles (background+foreground) are written to.
34 $dstpath='/mnt/tiles/statictiles/mergedtileshd/';
36 # The name is a bit misleading: ZOOMMAX actually sets the zoomlevel
37 # from which the backgrounds are taken (and then scaled as needed).
38 # You obviously cannot merge tiles with a zoomlevel greater than
42 # Tilesize. Usually 256 for normal tiles or 512 for HD.
55 my $dstfilename = sprintf("%s/%d/%d/%d.png", $dstpath, $ZS, $XS, $YS);
56 my $olfilename = sprintf("%s/%d/%d/%d.png", $srcfgpath, $ZS, $XS, $YS);
58 print("Sorry - can only merge Z <= $ZOOMMAX\n"); exit(1);
61 while ($ZS < $ZOOMMAX) {
62 $N = $N * 2; # Twice as many tiles (per direction)
63 $XS = $XS * 2; # Starting at 2*X
64 $YS = $YS * 2; # and 2*Y.
67 printf("Combining %d tiles (%d x %d pixels)\n", $N * $N, $N * $TILESIZE, $N * $TILESIZE);
68 # Now load them all and fill them into one big picture.
69 my $dimage = Graphics::Magick->new('size' => sprintf("%dx%d", $N * $TILESIZE, $N * $TILESIZE)); # magick => 'png',
70 $status = $dimage->Read("xc:white"); # This actually creates the image, the above does NOT - very intuitive!
71 warn "$status" if "$status";
73 for ($x = 0; $x < $N; $x++) {
74 for ($y = 0; $y < $N; $y++) {
75 my $fn = sprintf("%s/%d/%d/%d.png", $srcbgpath, $ZOOMMAX, $XS + $x, $YS + $y);
76 #print("Loading: $fn\n");
77 my $tmpimg = Graphics::Magick->new();
78 $status = $tmpimg->Read($fn);
79 warn "$status" if "$status";
80 $dimage->Composite( 'image' => $tmpimg, 'compose' => 'over',
81 'x' => ($x * $TILESIZE),
82 'y' => ($y * $TILESIZE));
85 $dimage->Resize('geometry' => sprintf("%dx%d", $TILESIZE, $TILESIZE), 'filter' => 'Lanczos');
86 #print("Loading: $olfilename\n");
87 my $olimg = Graphics::Magick->new();
88 $status = $olimg->Read($olfilename);
89 warn "$status" if "$status";
90 $dimage->Composite( 'image' => $olimg, 'compose' => 'over', 'x' => 0, 'y' => 0 );
91 print("Writing combined image to: $dstfilename\n");
92 $status = $dimage->Write($dstfilename);
93 warn "$status" if "$status";
102 #mergetile(5, 16, 10);
103 #mergetile(6, 33, 21);
104 #mergetile(7, 67, 43);
105 #mergetile(8, 135, 87);
106 my $rz; my $rx; my $ry;
107 for ($rz = 2; $rz <= 8; $rz++) {
108 my $ntilesperdir = 2 ** $rz; # Allgemein: 2 ^ Zoomlevel
109 for ($rx = 0; $rx < $ntilesperdir; $rx++) {
110 my $targdir = sprintf("%s/%d/%d", $dstpath, $rz, $rx);
112 print("Trying to create $targdir...");
113 # FIXME: mkdir -p - the zoom level dir might not exist either!
114 if (mkdir($targdir)) {
120 for ($ry = 0; $ry < $ntilesperdir; $ry++) {
121 mergetile($rz, $rx, $ry);
124 $ntilesperdir = $ntilesperdir * 2;