From 34ffddb76598340108ebb904b0adaa16328923c4 Mon Sep 17 00:00:00 2001 From: Fil Date: Fri, 20 Jun 2014 23:15:01 +0200 Subject: [PATCH] Autodetect minzoom, maxzoom and format for mbtiles * `format` is detected via JPEG's magic number `FFD8` * `minzoom` and `maxzoom` are `min(zoom_level)` and `max(zoom_level)` --- tileserver.php | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/tileserver.php b/tileserver.php index 35d455c..7f46112 100644 --- a/tileserver.php +++ b/tileserver.php @@ -170,6 +170,25 @@ class Server { $value = preg_replace('/(\\n)+/','',$r['value']); $metadata[$r['name']] = addslashes($value); } + if (!array_key_exists('minzoom', $metadata) + || !array_key_exists('maxzoom', $metadata) + ) { + // autodetect minzoom and maxzoom + $result = $this->db->query('select min(zoom_level) as min, max(zoom_level) as max from tiles'); + $resultdata = $result->fetchAll(); + if (!array_key_exists('minzoom', $metadata)) + $metadata['minzoom'] = $resultdata[0]['min']; + if (!array_key_exists('maxzoom', $metadata)) + $metadata['maxzoom'] = $resultdata[0]['max']; + } + // autodetect format using JPEG magic number FFD8 + if (!array_key_exists('format', $metadata)) { + $result = $this->db->query('select hex(substr(tile_data,1,2)) as magic from tiles limit 1'); + $resultdata = $result->fetchAll(); + $metadata['format'] = ($resultdata[0]['magic'] == 'FFD8') + ? 'jpg' + : 'png'; + } $metadata = $this->metadataValidation($metadata); $mbt = explode('.', $mbt); $metadata['basename'] = $mbt[0]; @@ -191,8 +210,8 @@ class Server { if (!array_key_exists('profile', $metadata)) { $metadata['profile'] = 'mercator'; } -// TODO: detect format, minzoom, maxzoom, thumb -// scandir() for directory / SQL for mbtiles +// TODO: detect format, minzoom, maxzoom, thumb / scandir() for directory +// TODO: detect thumb / SQL for mbtiles if (array_key_exists('minzoom', $metadata)) $metadata['minzoom'] = intval($metadata['minzoom']); else @@ -205,7 +224,7 @@ class Server { $metadata['format'] = 'png'; } /* - if (!array_key_exists('thumb', $metadata )) { + if (!array_key_exists('profile', $metadata )) { $metadata['profile'] = 'mercator'; } */