version as used after the move to new hardware in september 2022
[osmrrze.git] / scripts / rename-tms-tiles.pl
CommitLineData
1af272b1
MM
1#!/usr/bin/perl -w
2
3use File::Copy qw/copy move/;
4use Digest::file qw/digest_file_hex/;
5
6$moveinsteadofcopy = 0;
7$noreplace = 0;
8$skipvoidtiles = 0;
9$verblev = 2;
10
11@voidtilesizes = ( 334, 854 );
12@voidtilehashes = (
13 '1cf52aed67c3ebf045f2fd2996ada35545517129e673dba00ef97c7d18275f144aa67b012556ec8a840fca158f6976e7cf30fcae030acd493e26e28fdff10ca3',
14 '1feba96da5f6fa6049d7b40dc72295058eabb7e771040dedc8a69527046aa34fdaaf98e676ed8d02e95770dc635b5d4bf292fdd19be3c381847d3883995d3f3f',
15 '609e45a0c39d961302ed560d9d8bffd798fdbcc912c3875c06ba1faa1fab83cce8d97f257d572138898d2d99fb5a7af80801080bb7be9688e029fce4a9c56f5f'
16 );
17
18# Par. 0: Level on which this gets printed
19# Par. 1: Text
20sub printlev($$) {
21 if ($verblev >= $_[0]) {
22 print($_[1]);
23 }
24}
25
26sub showhelpandexit() {
27 print("Syntax: $0 sourcedirectoy destinationdirectory\n");
28 exit(1);
29}
30
31sub handlexdir($$$) {
32 my $z = $_[2];
33 my $srcdir = $_[0];
34 my $dstdir = $_[1];
35 my $dh;
36 unless (opendir($dh, $srcdir)) {
37 print(STDERR "failed to read sourcedirectoy '$srcdir' for zoomlevel $z.\n");
38 exit(1);
39 }
40 unless (-d $dstdir) {
41 printlev(2, "MKDIR 'dstdir'\n");
42 unless (mkdir($dstdir)) {
43 print(STDERR "Destination directory '$dstdir' for zoomlevel $z does not exist and could not be created: $!\n");
44 return;
45 }
46 }
47 while (my $de = readdir($dh)) {
48 if ($de =~ m/^(\d+)\.png$/) { # just a number dot png => handle this.
49 my $ynr = $1;
50 my $correctedynr = ((2 ** $z) - 1) - $ynr;
51 my $srcname = $srcdir . '/' . $de;
52 my $dstname = sprintf("%s/%d.png", $dstdir, $correctedynr);
53 if ($noreplace) {
54 if (-e $dstname) {
55 printlev(2, "SKIP '$srcname' ('$dstname' already exists)\n");
56 next;
57 }
58 }
59 if ($skipvoidtiles) {
60 my $fsize = -s $srcname;
61 if ($fsize ~~ @voidtilesizes) { # One of the relevant filesizes, no
62 # point in even computing the hash otherwise.
63 my $sha512 = digest_file_hex($srcname, "SHA-512");
64 printlev(3, "HASH $srcname -> $sha512\n");
65 if ($sha512 ~~ @voidtilehashes) { # OK, one of the void hashes -> skip
66 printlev(2, "SKIP '$srcname' (it has the hash of a void file)\n");
67 next;
68 }
69 }
70 }
71 if ($moveinsteadofcopy) {
72 printlev(2, "MOVE '$srcname' '$dstname'\n");
73 move($srcname, $dstname);
74 } else {
75 printlev(2, "COPY '$srcname' '$dstname'\n");
76 copy($srcname, $dstname);
77 }
78 }
79 }
80 closedir($dh);
81}
82
83sub handlezoomdir($$$) {
84 my $z = $_[2];
85 my $srcdir = $_[0] . '/' . $z;
86 my $dstdir = $_[1] . '/' . $z;
87 unless (-d $srcdir) {
88 printlev(1, "sourcedirectoy '$srcdir' for zoomlevel $z does not exist, skipping.\n");
89 return;
90 }
91 my $dh;
92 unless (opendir($dh, $srcdir)) {
93 print(STDERR "failed to read sourcedirectoy '$srcdir' for zoomlevel $z.\n");
94 exit(1);
95 }
96 unless (-d $dstdir) {
97 printlev(2, "MKDIR '$dstdir'\n");
98 unless (mkdir($dstdir)) {
99 print(STDERR "Destination directory '$dstdir' for zoomlevel $z does not exist and could not be created: $!\n");
100 exit(1);
101 }
102 }
103 while (my $de = readdir($dh)) {
104 if ($de =~ m/^\d+$/) { # just a number -> handle this directory.
105 handlexdir($srcdir . '/' . $de, $dstdir . '/' . $de, $z);
106 }
107 }
108 closedir($dh);
109}
110
111my $srcdir = undef;
112my $dstdir = undef;
113foreach $a (@ARGV) {
114 if ($a eq '--move') {
115 $moveinsteadofcopy = 1;
116 } elsif ($a eq '--noreplace') {
117 $noreplace = 1;
118 } elsif ($a eq '--skipvoidtiles') {
119 $skipvoidtiles = 1;
120 } elsif ($a eq '--help') {
121 showhelpandexit();
122 } elsif ($a eq '-q') {
123 $verblev--;
124 } elsif ($a eq '-v') {
125 $verblev++;
126 } else { # This will be interpreted as a directory name
127 if (!defined($srcdir)) {
128 $srcdir = $a;
129 } elsif (!defined($dstdir)) {
130 $dstdir = $a;
131 } else { # More than 2 directory names? That is wrong.
132 showhelpandexit();
133 }
134 }
135}
136unless (-d $srcdir) {
137 print(STDERR "Source directory '$srcdir' does not seem to exist or is not a directory?\n");
138 exit(1);
139}
140unless (-d $dstdir) {
141 unless (mkdir($dstdir)) {
142 print(STDERR "Destination directory '$dstdir' does not exist and could not be created: $!\n");
143 exit(1);
144 }
145}
146for (my $z = 0; $z < 30; $z++) {
147 handlezoomdir($srcdir, $dstdir, $z);
148}
This page took 0.080388 seconds and 4 git commands to generate.