-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathconvert_meta.c
229 lines (186 loc) · 5.23 KB
/
convert_meta.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright (c) 2007 - 2023 by mod_tile contributors (see AUTHORS file)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; If not, see http://www.gnu.org/licenses/.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/un.h>
#include <poll.h>
#include <errno.h>
#include <math.h>
#include <getopt.h>
#include <time.h>
#include <sys/types.h>
#include <dirent.h>
#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include "render_config.h"
#include "dir_utils.h"
#include "store.h"
char *tile_dir = RENDERD_TILE_DIR;
#ifndef METATILE
#warning("convert_meta not implemented for non-metatile mode. Feel free to submit fix")
int main(int argc, char **argv)
{
fprintf(stderr, "convert_meta not implemented for non-metatile mode. Feel free to submit fix!\n");
return -1;
}
#else
static int minZoom = 0;
static int maxZoom = MAX_ZOOM;
static int verbose = 0;
static int num_render = 0, num_all = 0;
static struct timeval start, end;
static int unpack;
void display_rate(struct timeval start, struct timeval end, int num)
{
int d_s, d_us;
float sec;
d_s = end.tv_sec - start.tv_sec;
d_us = end.tv_usec - start.tv_usec;
sec = d_s + d_us / 1000000.0;
printf("Converted %d tiles in %.2f seconds (%.2f tiles/s)\n", num, sec, num / sec);
fflush(NULL);
}
static void descend(const char *search)
{
DIR *tiles = opendir(search);
struct dirent *entry;
char path[PATH_MAX];
if (!tiles) {
//fprintf(stderr, "Unable to open directory: %s\n", search);
return;
}
while ((entry = readdir(tiles))) {
struct stat b;
char *p;
//check_load();
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
continue;
}
snprintf(path, sizeof(path), "%s/%s", search, entry->d_name);
if (stat(path, &b)) {
continue;
}
if (S_ISDIR(b.st_mode)) {
descend(path);
continue;
}
p = strrchr(path, '.');
if (p) {
if (unpack) {
if (!strcmp(p, ".meta")) {
process_unpack(tile_dir, path);
}
} else {
if (!strcmp(p, ".png")) {
process_pack(tile_dir, path);
}
}
}
}
closedir(tiles);
}
int main(int argc, char **argv)
{
int z, c;
const char *map = "default";
while (1) {
int option_index = 0;
static struct option long_options[] = {
{"map", 1, 0, 'm'},
{"min-zoom", 1, 0, 'z'},
{"max-zoom", 1, 0, 'Z'},
{"unpack", 0, 0, 'u'},
{"tile-dir", 1, 0, 't'},
{"verbose", 0, 0, 'v'},
{"help", 0, 0, 'h'},
{0, 0, 0, 0}
};
c = getopt_long(argc, argv, "uhvz:Z:m:t:", long_options, &option_index);
if (c == -1) {
break;
}
switch (c) {
case 'z':
minZoom = atoi(optarg);
if (minZoom < 0 || minZoom > MAX_ZOOM) {
fprintf(stderr, "Invalid minimum zoom selected, must be between 0 and %d\n", MAX_ZOOM);
return 1;
}
break;
case 'Z':
maxZoom = atoi(optarg);
if (maxZoom < 0 || maxZoom > MAX_ZOOM) {
fprintf(stderr, "Invalid maximum zoom selected, must be between 0 and %d\n", MAX_ZOOM);
return 1;
}
break;
case 'm':
map = strdup(optarg);
break;
case 't':
tile_dir = strdup(optarg);
break;
case 'u':
unpack = 1;
break;
case 'v':
verbose = 1;
break;
case 'h':
fprintf(stderr, "Usage: convert_meta [OPTION] ...\n");
fprintf(stderr, "Convert the rendered PNGs into the more efficient .meta format\n");
fprintf(stderr, " -m, --map convert tiles in this map (default is 'default')\n");
fprintf(stderr, " -t, --tile-dir tile cache directory (default is '" RENDERD_TILE_DIR "')\n");
fprintf(stderr, " -u, --unpack unpack the .meta files back to PNGs\n");
fprintf(stderr, " -z, --min-zoom only process tiles greater or equal to this zoom level (default is 0)\n");
fprintf(stderr, " -Z, --max-zoom only process tiles less than or equal to this zoom level (default is %d)\n", MAX_ZOOM);
return -1;
default:
fprintf(stderr, "unhandled char '%c'\n", c);
break;
}
}
if (maxZoom < minZoom) {
fprintf(stderr, "Invalid zoom range, max zoom must be greater or equal to minimum zoom\n");
return 1;
}
fprintf(stderr, "Converting tiles in map %s\n", map);
gettimeofday(&start, NULL);
for (z = minZoom; z <= maxZoom; z++) {
char path[PATH_MAX];
snprintf(path, PATH_MAX, "%s/%s/%d", tile_dir, map, z);
descend(path);
}
gettimeofday(&end, NULL);
printf("\nTotal for all tiles converted\n");
printf("Meta tiles converted: ");
display_rate(start, end, num_render);
printf("Total tiles converted: ");
display_rate(start, end, num_render * METATILE * METATILE);
printf("Total tiles handled: ");
display_rate(start, end, num_all);
return 0;
}
#endif