Commit | Line | Data |
---|---|---|
0f4fc1d7 | 1 | #!/usr/bin/perl -w |
2 | ||
7792f331 UO |
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. | |
7a8d4e5b UO |
16 | # For rendering, the polytiles.pl script from |
17 | # https://github.com/Zverik/polytiles can be very helpful. | |
18 | # You might have to remove some non installed fonts from the XML. | |
19 | # After you are done rendering the source tiles, you can then use this | |
20 | # script to merge back+foreground. Voila, nicer looking tiles! | |
7792f331 UO |
21 | |
22 | # WARNING: Doing this for zoom level 0 in HD requires 90 GB of | |
7a8d4e5b | 23 | # space in $TMPDIR and/or RAM (depending on Graphics::Magick behaviour/config)! |
7792f331 | 24 | # That's why levels 0+1 are not generated by default (you need to change |
7a8d4e5b | 25 | # a line below, or generate them manually instead). |
0f4fc1d7 | 26 | |
7792f331 | 27 | # URL generally is: /zoom/X/Y.png within the dirs below. |
0f4fc1d7 | 28 | |
7792f331 UO |
29 | # The source for the foreground that you want to put over the |
30 | # background. The foreground needs to exist for all zoomlevels you | |
31 | # want to generate. | |
7a8d4e5b | 32 | $srcfgpath = '/mnt/tiles/statictiles/osm.org-lowzoom-fg/'; |
7792f331 UO |
33 | # The source for the background. Only one zoomlevel needs to exist, |
34 | # see ZOOMMAX below. | |
7a8d4e5b | 35 | $srcbgpath = '/mnt/tiles/statictiles/osm.org-lowzoom-bg/'; |
7792f331 | 36 | # Where the combined tiles (background+foreground) are written to. |
7a8d4e5b | 37 | $dstpath='/mnt/tiles/statictiles/osm.org-lowzoom-new/'; |
7792f331 UO |
38 | |
39 | # The name is a bit misleading: ZOOMMAX actually sets the zoomlevel | |
40 | # from which the backgrounds are taken (and then scaled as needed). | |
41 | # You obviously cannot merge tiles with a zoomlevel greater than | |
42 | # that. | |
43 | $ZOOMMAX = 8; | |
0f4fc1d7 | 44 | |
7792f331 | 45 | # Tilesize. Usually 256 for normal tiles or 512 for HD. |
7a8d4e5b | 46 | $TILESIZE = 256; |
7792f331 UO |
47 | |
48 | use Graphics::Magick; | |
0f4fc1d7 | 49 | |
50 | # Par. 0: Z | |
51 | # Par. 1: X | |
52 | # Par. 2: Y | |
53 | sub mergetile($$$) { | |
54 | my $ZS = $_[0]; | |
55 | my $XS = $_[1]; | |
56 | my $YS = $_[2]; | |
57 | my $N = 1; | |
58 | my $dstfilename = sprintf("%s/%d/%d/%d.png", $dstpath, $ZS, $XS, $YS); | |
7792f331 UO |
59 | my $olfilename = sprintf("%s/%d/%d/%d.png", $srcfgpath, $ZS, $XS, $YS); |
60 | if ($ZS > $ZOOMMAX) { | |
0f4fc1d7 | 61 | print("Sorry - can only merge Z <= $ZOOMMAX\n"); exit(1); |
62 | } | |
63 | my $z = $ZS; | |
64 | while ($ZS < $ZOOMMAX) { | |
65 | $N = $N * 2; # Twice as many tiles (per direction) | |
66 | $XS = $XS * 2; # Starting at 2*X | |
67 | $YS = $YS * 2; # and 2*Y. | |
68 | $ZS++; | |
69 | } | |
70 | printf("Combining %d tiles (%d x %d pixels)\n", $N * $N, $N * $TILESIZE, $N * $TILESIZE); | |
71 | # Now load them all and fill them into one big picture. | |
72 | my $dimage = Graphics::Magick->new('size' => sprintf("%dx%d", $N * $TILESIZE, $N * $TILESIZE)); # magick => 'png', | |
73 | $status = $dimage->Read("xc:white"); # This actually creates the image, the above does NOT - very intuitive! | |
74 | warn "$status" if "$status"; | |
75 | my $x; my $y; | |
76 | for ($x = 0; $x < $N; $x++) { | |
77 | for ($y = 0; $y < $N; $y++) { | |
7792f331 | 78 | my $fn = sprintf("%s/%d/%d/%d.png", $srcbgpath, $ZOOMMAX, $XS + $x, $YS + $y); |
0f4fc1d7 | 79 | #print("Loading: $fn\n"); |
80 | my $tmpimg = Graphics::Magick->new(); | |
81 | $status = $tmpimg->Read($fn); | |
82 | warn "$status" if "$status"; | |
83 | $dimage->Composite( 'image' => $tmpimg, 'compose' => 'over', | |
84 | 'x' => ($x * $TILESIZE), | |
85 | 'y' => ($y * $TILESIZE)); | |
86 | } | |
87 | } | |
88 | $dimage->Resize('geometry' => sprintf("%dx%d", $TILESIZE, $TILESIZE), 'filter' => 'Lanczos'); | |
89 | #print("Loading: $olfilename\n"); | |
90 | my $olimg = Graphics::Magick->new(); | |
91 | $status = $olimg->Read($olfilename); | |
92 | warn "$status" if "$status"; | |
93 | $dimage->Composite( 'image' => $olimg, 'compose' => 'over', 'x' => 0, 'y' => 0 ); | |
94 | print("Writing combined image to: $dstfilename\n"); | |
95 | $status = $dimage->Write($dstfilename); | |
96 | warn "$status" if "$status"; | |
97 | } | |
98 | ||
99 | # "main()" | |
100 | ||
101 | #mergetile(1, 1, 0); | |
102 | #mergetile(2, 2, 1); | |
103 | #mergetile(3, 4, 2); | |
104 | #mergetile(4, 8, 5); | |
105 | #mergetile(5, 16, 10); | |
106 | #mergetile(6, 33, 21); | |
107 | #mergetile(7, 67, 43); | |
108 | #mergetile(8, 135, 87); | |
109 | my $rz; my $rx; my $ry; | |
7792f331 | 110 | for ($rz = 2; $rz <= 8; $rz++) { |
0f4fc1d7 | 111 | my $ntilesperdir = 2 ** $rz; # Allgemein: 2 ^ Zoomlevel |
112 | for ($rx = 0; $rx < $ntilesperdir; $rx++) { | |
113 | my $targdir = sprintf("%s/%d/%d", $dstpath, $rz, $rx); | |
114 | if (! -e $targdir) { | |
115 | print("Trying to create $targdir..."); | |
116 | # FIXME: mkdir -p - the zoom level dir might not exist either! | |
117 | if (mkdir($targdir)) { | |
118 | print(" OK!\n"); | |
119 | } else { | |
120 | print(" FAILED!\n"); | |
121 | } | |
122 | } | |
123 | for ($ry = 0; $ry < $ntilesperdir; $ry++) { | |
124 | mergetile($rz, $rx, $ry); | |
125 | } | |
126 | } | |
0f4fc1d7 | 127 | } |
7792f331 | 128 |