1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
5 * Copyright (C) 2014, Rob Norris <rw_norris@hotmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Mostly imported from https://github.com/openstreetmap/mod_tile/
31 #include <sys/types.h>
40 #define META_MAGIC "META"
41 #define META_MAGIC_COMPRESSED "METZ"
49 char magic[4]; // META_MAGIC or META_MAGIC_COMPRESSED
50 int count; // METATILE ^ 2
51 int x, y, z; // lowest x,y of this metatile, plus z
52 struct entry index[]; // count entries
53 // Followed by the tile data
54 // The index offsets are measured from the start of the file
57 // Use this to enable meta-tiles which will render NxN tiles at once
58 // Note: This should be a power of 2 (2, 4, 8, 16 ...)
63 * Based on function from mod_tile/src/store_file_utils.c
65 * Returns the path to the meta-tile and the offset within the meta-tile
67 int xyz_to_meta(char *path, size_t len, const char *dir, int x, int y, int z)
69 unsigned char i, hash[5], offset, mask;
71 // Each meta tile winds up in its own file, with several in each leaf directory
72 // the .meta tile name is based on the sub-tile at (0,0)
74 offset = (x & mask) * METATILE + (y & mask);
79 hash[i] = ((x & 0x0f) << 4) | (y & 0x0f);
84 snprintf(path, len, "%s/%d/%u/%u/%u/%u/%u.meta", dir, z, hash[4], hash[3], hash[2], hash[1], hash[0]);
90 * From function in mod_tile/src/store_file.c
91 * Slightly reworked to use simplified xyz_to_meta() above
93 * Reads into buf upto size specified by sz
95 * Returns whether the file is in a compressed format (possibly only gzip)
97 * Error messages returned in log_msg
99 int metatile_read(const char *dir, int x, int y, int z, char *buf, size_t sz, int * compressed, char * log_msg)
104 unsigned int header_len = sizeof(struct meta_layout) + METATILE*METATILE*sizeof(struct entry);
105 struct meta_layout *meta = (struct meta_layout *)malloc(header_len);
106 size_t file_offset, tile_size;
108 meta_offset = xyz_to_meta(path, sizeof(path), dir, x, y, z);
110 fd = open(path, O_RDONLY);
112 snprintf(log_msg,PATH_MAX - 1, "Could not open metatile %s. Reason: %s\n", path, strerror(errno));
118 while (pos < header_len) {
119 size_t len = header_len - pos;
120 int got = read(fd, ((unsigned char *) meta) + pos, len);
122 snprintf(log_msg,PATH_MAX - 1, "Failed to read complete header for metatile %s Reason: %s\n", path, strerror(errno));
126 } else if (got > 0) {
132 if (pos < header_len) {
133 snprintf(log_msg,PATH_MAX - 1, "Meta file %s too small to contain header\n", path);
138 if (memcmp(meta->magic, META_MAGIC, strlen(META_MAGIC))) {
139 if (memcmp(meta->magic, META_MAGIC_COMPRESSED, strlen(META_MAGIC_COMPRESSED))) {
140 snprintf(log_msg,PATH_MAX - 1, "Meta file %s header magic mismatch\n", path);
147 } else *compressed = 0;
149 // Currently this code only works with fixed metatile sizes (due to xyz_to_meta above)
150 if (meta->count != (METATILE * METATILE)) {
151 snprintf(log_msg, PATH_MAX - 1, "Meta file %s header bad count %d != %d\n", path, meta->count, METATILE * METATILE);
157 file_offset = meta->index[meta_offset].offset;
158 tile_size = meta->index[meta_offset].size;
162 if (tile_size > sz) {
163 snprintf(log_msg, PATH_MAX - 1, "Truncating tile %zd to fit buffer of %zd\n", tile_size, sz);
169 if (lseek(fd, file_offset, SEEK_SET) < 0) {
170 snprintf(log_msg, PATH_MAX - 1, "Meta file %s seek error: %s\n", path, strerror(errno));
176 while (pos < tile_size) {
177 size_t len = tile_size - pos;
178 int got = read(fd, buf + pos, len);
180 snprintf(log_msg, PATH_MAX - 1, "Failed to read data from file %s. Reason: %s\n", path, strerror(errno));
183 } else if (got > 0) {