1
0
mirror of https://github.com/klokantech/tileserver-php.git synced 2025-08-04 13:47:49 +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 <?php
// Based on: https://github.com/Zverik/mbtiles-php // Based on: https://github.com/Zverik/mbtiles-php
// Read: https://github.com/klokantech/tileserver-php/issues/1 // Read: https://github.com/klokantech/tileserver-php/issues/1
// TODO: clean the code!!! // TODO: clean the code!!!
@@ -13,69 +14,112 @@ if (!is_file($_GET['tileset'])) {
// header("Content-type: image/png"); // 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"); //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']) ) { if (isset($_GET['tileset'])) {
$tileset = $_GET['tileset'];
$flip = true; $tileset = $_GET['tileset'];
try { $flip = true;
$db = new PDO('sqlite:'.$tileset,'','',array(PDO::ATTR_PERSISTENT => true)); try {
if( !isset($db) ) { $db = new PDO('sqlite:' . $tileset, '', '', array(PDO::ATTR_PERSISTENT => true));
header('Content-type: text/plain'); if (!isset($db)) {
print 'Incorrect tileset name: '.$_GET['tileset']; header('Content-type: text/plain');
exit; print 'Incorrect tileset name: ' . $_GET['tileset'];
} exit;
// http://c.tile.openstreetmap.org/12/2392/1190.png }
$z = floatval($_GET['z']); // http://c.tile.openstreetmap.org/12/2392/1190.png
$y = floatval($_GET['y']); $z = floatval($_GET['z']);
$x = floatval($_GET['x']); $y = floatval($_GET['y']);
if( $flip ) { $x = floatval($_GET['x']);
$y = pow(2, $z) - 1 - $y; if ($flip) {
} $y = pow(2, $z) - 1 - $y;
$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 ($_GET['ext'] != 'grid' && $_GET['ext'] != 'json') {
if( !isset($data) || $data === FALSE ) {
// TODO: Put here ready to use empty tile!!!
$png = imagecreatetruecolor(256, 256); $result = $db->query('select tile_data as t from tiles where zoom_level=' . $z . ' and tile_column=' . $x . ' and tile_row=' . $y);
imagesavealpha($png, true); $data = $result->fetchColumn();
$trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127); if (!isset($data) || $data === FALSE) {
imagefill($png, 0, 0, $trans_colour); // TODO: Put here ready to use empty tile!!!
header('Content-type: image/png'); $png = imagecreatetruecolor(256, 256);
imagepng($png); imagesavealpha($png, true);
//header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found'); $trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127);
} else { imagefill($png, 0, 0, $trans_colour);
$result = $db->query('select value from metadata where name="format"'); header('Content-type: image/png');
$resultdata = $result->fetchColumn(); imagepng($png);
$format = isset($resultdata) && $resultdata !== FALSE ? $resultdata : 'png'; //header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
if( $format == 'jpg' ) } else {
$format = 'jpeg'; $result = $db->query('select value from metadata where name="format"');
header('Content-type: image/'.$format); $resultdata = $result->fetchColumn();
print $data; $format = isset($resultdata) && $resultdata !== FALSE ? $resultdata : 'png';
} if ($format == 'jpg')
} catch( PDOException $e ) { $format = 'jpeg';
header('Content-type: text/plain'); header('Content-type: image/' . $format);
print 'Error querying the database: '.$e->getMessage(); 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();
}
} }
/* /*
function getbaseurl() { function getbaseurl() {
return 'http://'.$_SERVER['HTTP_HOST'].preg_replace('/\/(1.0.0\/)?[^\/]*$/','/',$_SERVER['REQUEST_URI']); return 'http://'.$_SERVER['HTTP_HOST'].preg_replace('/\/(1.0.0\/)?[^\/]*$/','/',$_SERVER['REQUEST_URI']);
} }
*/ */
function readparams( $db ) {
$params = array(); function readparams($db) {
$result = $db->query('select name, value from metadata'); $params = array();
while ($row = $result->fetch(PDO::FETCH_ASSOC)) { $result = $db->query('select name, value from metadata');
$params[$row['name']] = $row['value']; while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
} $params[$row['name']] = $row['value'];
return $params; }
return $params;
} }
function readzooms( $db ) { function readzooms($db) {
$zooms = array(); $zooms = array();
$result = $db->query('select zoom_level from tiles group by zoom_level order by zoom_level'); $result = $db->query('select zoom_level from tiles group by zoom_level order by zoom_level');
while ($zoom = $result->fetchColumn()) { while ($zoom = $result->fetchColumn()) {
$zooms[] = $zoom; $zooms[] = $zoom;
} }
return $zooms; return $zooms;
} }
?> ?>