1
0
mirror of https://github.com/klokantech/tileserver-php.git synced 2025-08-06 06:38:27 +02:00

Added basic support for UTFgrid for url {server}/{layer}.mbtiles/{z}/{x}/{y}.grid

This commit is contained in:
Dalibor Janák
2014-03-25 10:10:06 +01:00
parent 8fa1bfcbab
commit bd8c6b286f

View File

@@ -1,4 +1,5 @@
<?php
// Based on: https://github.com/Zverik/mbtiles-php
// Read: https://github.com/klokantech/tileserver-php/issues/1
// TODO: clean the code!!!
@@ -13,8 +14,10 @@ if (!is_file($_GET['tileset'])) {
// header("Content-type: image/png");
//print("\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDAT\x08\xd7c````\x00\x00\x00\x05\x00\x01^\xf3*:\x00\x00\x00\x00IEND\xaeB`\x82");
}
$tileset = $_GET['tileset'];
if (isset($_GET['tileset'])) {
$tileset = $_GET['tileset'];
$flip = true;
try {
@@ -31,6 +34,9 @@ if( isset($_GET['tileset']) ) {
if ($flip) {
$y = pow(2, $z) - 1 - $y;
}
if ($_GET['ext'] != 'grid' && $_GET['ext'] != 'json') {
$result = $db->query('select tile_data as t from tiles where zoom_level=' . $z . ' and tile_column=' . $x . ' and tile_row=' . $y);
$data = $result->fetchColumn();
if (!isset($data) || $data === FALSE) {
@@ -51,6 +57,42 @@ if( isset($_GET['tileset']) ) {
header('Content-type: image/' . $format);
print $data;
}
} elseif ($_GET['ext'] == 'grid' || $_GET['ext'] == 'json') {
//Get and return UTFgrid
$result = $db->query('SELECT grid FROM grids WHERE tile_column = ' . $x . ' AND tile_row = ' . $y . ' AND zoom_level = ' . $z);
$data = $result->fetchColumn();
if (!isset($data) || $data === FALSE) {
// if not exists grid data return empty json
header('Access-Control-Allow-Origin: *');
echo 'grid({});';
die;
} else {
$grid = gzuncompress($data);
$grid = substr(trim($grid), 0, -1);
//adds legend (data) to output
$grid .= ',"data":{';
$result = $db->query('SELECT key_name as key, key_json as json FROM grid_data WHERE zoom_level=' . $z . ' and tile_column=' . $x . ' and tile_row=' . $y);
while ($r = $result->fetch(PDO::FETCH_ASSOC)) {
$grid .= '"' . $r['key'] . '":' . $r['json'] . ',';
}
$grid = rtrim($grid, ',') . '}}';
// CORS header
header('Access-Control-Allow-Origin: *');
//TODO: Process callback and ext but first in htaccess or route
if (isset($_GET['callback'])) {
echo $_GET['callback'] . '(' . $grid . ');';
} elseif ($_GET['ext'] == 'jsonp') {
echo 'grid(' . $grid . ');';
} else {
echo $grid;
}
die;
}
}
} catch (PDOException $e) {
header('Content-type: text/plain');
print 'Error querying the database: ' . $e->getMessage();
@@ -61,6 +103,7 @@ function getbaseurl() {
return 'http://'.$_SERVER['HTTP_HOST'].preg_replace('/\/(1.0.0\/)?[^\/]*$/','/',$_SERVER['REQUEST_URI']);
}
*/
function readparams($db) {
$params = array();
$result = $db->query('select name, value from metadata');
@@ -78,4 +121,5 @@ function readzooms( $db ) {
}
return $zooms;
}
?>