documentation in the form of comments added.
[osmrrze.git] / scripts / osm-mergemaptiles.pl
1 #!/usr/bin/perl -w
2
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
17 # tiles!
18
19 # WARNING: Doing this for zoom level 0 in HD requires 90 GB of
20 # space in $TMPDIR!
21 # That's why levels 0+1 are not generated by default (you need to change
22 # a line below)
23
24 # URL generally is:   /zoom/X/Y.png  within the dirs below.
25
26 # The source for the foreground that you want to put over the
27 # background. The foreground needs to exist for all zoomlevels you
28 # want to generate.
29 $srcfgpath = '/mnt/tiles/statictiles/lowzoom-fg/';
30 # The source for the background. Only one zoomlevel needs to exist,
31 # see ZOOMMAX below.
32 $srcbgpath = '/mnt/tiles/statictiles/lowzoom-bg/';
33 # Where the combined tiles (background+foreground) are written to.
34 $dstpath='/mnt/tiles/statictiles/mergedtileshd/';
35
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
39 # that.
40 $ZOOMMAX = 8;
41
42 # Tilesize. Usually 256 for normal tiles or 512 for HD.
43 $TILESIZE = 512;
44
45 use Graphics::Magick;
46
47 # Par. 0:  Z
48 # Par. 1:  X
49 # Par. 2:  Y
50 sub mergetile($$$) {
51   my $ZS = $_[0];
52   my $XS = $_[1];
53   my $YS = $_[2];
54   my $N = 1;
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);
57   if ($ZS > $ZOOMMAX) {
58     print("Sorry - can only merge Z <= $ZOOMMAX\n"); exit(1);
59   }
60   my $z = $ZS;
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.
65     $ZS++;
66   }
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";
72   my $x; my $y;
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));
83     }
84   }
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";
94 }
95
96 # "main()"
97
98 #mergetile(1, 1, 0);
99 #mergetile(2, 2, 1);
100 #mergetile(3, 4, 2);
101 #mergetile(4, 8, 5);
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);
111     if (! -e $targdir) {
112       print("Trying to create $targdir...");
113       # FIXME: mkdir -p   - the zoom level dir might not exist either!
114       if (mkdir($targdir)) {
115         print(" OK!\n");
116       } else {
117         print(" FAILED!\n");
118       }
119     }
120     for ($ry = 0; $ry < $ntilesperdir; $ry++) {
121       mergetile($rz, $rx, $ry);
122     }
123   }
124   $ntilesperdir = $ntilesperdir * 2;
125 }
126
This page took 0.07287 seconds and 3 git commands to generate.