| 1 | #!/usr/bin/perl -w |
| 2 | |
| 3 | # WARNING: Doing this for zoom level 0 requires 90 GB of space in $TEMP! |
| 4 | # That's why layers 0-2 are not generated by default (you need to change |
| 5 | # a line below) |
| 6 | |
| 7 | # URL is: /zoom/X/Y.png |
| 8 | $srcpath = '/mnt/tiles/lowzoomtilegen/'; |
| 9 | $dstpath='/mnt/tiles/statictiles/mergedtiles/'; |
| 10 | |
| 11 | use Graphics::Magick; |
| 12 | |
| 13 | # No need to change this usually |
| 14 | $ZOOMMAX = 9; |
| 15 | $TILESIZE = 256; |
| 16 | |
| 17 | # Par. 0: Z |
| 18 | # Par. 1: X |
| 19 | # Par. 2: Y |
| 20 | sub mergetile($$$) { |
| 21 | my $ZS = $_[0]; |
| 22 | my $XS = $_[1]; |
| 23 | my $YS = $_[2]; |
| 24 | my $N = 1; |
| 25 | my $dstfilename = sprintf("%s/%d/%d/%d.png", $dstpath, $ZS, $XS, $YS); |
| 26 | my $olfilename = sprintf("%s/%d/%d/%d.png", $srcpath, $ZS, $XS, $YS); |
| 27 | if ($ZS >= $ZOOMMAX) { |
| 28 | print("Sorry - can only merge Z <= $ZOOMMAX\n"); exit(1); |
| 29 | } |
| 30 | my $z = $ZS; |
| 31 | while ($ZS < $ZOOMMAX) { |
| 32 | $N = $N * 2; # Twice as many tiles (per direction) |
| 33 | $XS = $XS * 2; # Starting at 2*X |
| 34 | $YS = $YS * 2; # and 2*Y. |
| 35 | $ZS++; |
| 36 | } |
| 37 | printf("Combining %d tiles (%d x %d pixels)\n", $N * $N, $N * $TILESIZE, $N * $TILESIZE); |
| 38 | # Now load them all and fill them into one big picture. |
| 39 | my $dimage = Graphics::Magick->new('size' => sprintf("%dx%d", $N * $TILESIZE, $N * $TILESIZE)); # magick => 'png', |
| 40 | $status = $dimage->Read("xc:white"); # This actually creates the image, the above does NOT - very intuitive! |
| 41 | warn "$status" if "$status"; |
| 42 | my $x; my $y; |
| 43 | for ($x = 0; $x < $N; $x++) { |
| 44 | for ($y = 0; $y < $N; $y++) { |
| 45 | my $fn = sprintf("%s/%d/%d/%d.png", $srcpath, $ZOOMMAX, $XS + $x, $YS + $y); |
| 46 | #print("Loading: $fn\n"); |
| 47 | my $tmpimg = Graphics::Magick->new(); |
| 48 | $status = $tmpimg->Read($fn); |
| 49 | warn "$status" if "$status"; |
| 50 | $dimage->Composite( 'image' => $tmpimg, 'compose' => 'over', |
| 51 | 'x' => ($x * $TILESIZE), |
| 52 | 'y' => ($y * $TILESIZE)); |
| 53 | } |
| 54 | } |
| 55 | $dimage->Resize('geometry' => sprintf("%dx%d", $TILESIZE, $TILESIZE), 'filter' => 'Lanczos'); |
| 56 | #print("Loading: $olfilename\n"); |
| 57 | my $olimg = Graphics::Magick->new(); |
| 58 | $status = $olimg->Read($olfilename); |
| 59 | warn "$status" if "$status"; |
| 60 | $dimage->Composite( 'image' => $olimg, 'compose' => 'over', 'x' => 0, 'y' => 0 ); |
| 61 | print("Writing combined image to: $dstfilename\n"); |
| 62 | $status = $dimage->Write($dstfilename); |
| 63 | warn "$status" if "$status"; |
| 64 | } |
| 65 | |
| 66 | # "main()" |
| 67 | |
| 68 | #mergetile(1, 1, 0); |
| 69 | #mergetile(2, 2, 1); |
| 70 | #mergetile(3, 4, 2); |
| 71 | #mergetile(4, 8, 5); |
| 72 | #mergetile(5, 16, 10); |
| 73 | #mergetile(6, 33, 21); |
| 74 | #mergetile(7, 67, 43); |
| 75 | #mergetile(8, 135, 87); |
| 76 | my $rz; my $rx; my $ry; |
| 77 | for ($rz = 3; $rz <= 8; $rz++) { |
| 78 | my $ntilesperdir = 2 ** $rz; # Allgemein: 2 ^ Zoomlevel |
| 79 | for ($rx = 0; $rx < $ntilesperdir; $rx++) { |
| 80 | my $targdir = sprintf("%s/%d/%d", $dstpath, $rz, $rx); |
| 81 | if (! -e $targdir) { |
| 82 | print("Trying to create $targdir..."); |
| 83 | # FIXME: mkdir -p - the zoom level dir might not exist either! |
| 84 | if (mkdir($targdir)) { |
| 85 | print(" OK!\n"); |
| 86 | } else { |
| 87 | print(" FAILED!\n"); |
| 88 | } |
| 89 | } |
| 90 | for ($ry = 0; $ry < $ntilesperdir; $ry++) { |
| 91 | mergetile($rz, $rx, $ry); |
| 92 | } |
| 93 | } |
| 94 | $ntilesperdir = $ntilesperdir * 2; |
| 95 | } |