-
Notifications
You must be signed in to change notification settings - Fork 42
/
tile.php
148 lines (145 loc) · 5.54 KB
/
tile.php
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
<?
header('Access-Control-Allow-Origin: *');
if( isset($_GET['tileset']) && preg_match('/^[\w\d_-]+$/', $_GET['tileset']) ) {
$tileset = $_GET['tileset'];
$flip = true;
if( strlen($tileset) > 4 && substr($tileset,strlen($tileset)-4) == '-tms' ) {
$tileset = substr($tileset,0,strlen($tileset)-4);
$flip = false;
}
try {
$db = new PDO('sqlite:'.$tileset.'.mbtiles','','',array(PDO::ATTR_PERSISTENT => true));
if( !isset($db) ) {
header('Content-type: text/plain');
print 'Incorrect tileset name: '.$_GET['tileset'];
exit;
}
// http://c.tile.openstreetmap.org/12/2392/1190.png
$z = floatval($_GET['z']);
$y = floatval($_GET['y']);
$x = floatval($_GET['x']);
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( !isset($data) || $data === FALSE ) {
$png = imagecreatetruecolor(256, 256);
imagesavealpha($png, true);
$trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127);
imagefill($png, 0, 0, $trans_colour);
header('Content-type: image/png');
imagepng($png);
//header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
} else {
$result = $db->query('select value from metadata where name="format"');
$resultdata = $result->fetchColumn();
$format = isset($resultdata) && $resultdata !== FALSE ? $resultdata : 'png';
if( $format == 'jpg' )
$format = 'jpeg';
header('Content-type: image/'.$format);
print $data;
}
} catch( PDOException $e ) {
header('Content-type: text/plain');
print 'Error querying the database: '.$e->getMessage();
}
} elseif( isset($_GET['tmsinfo']) ) {
// get tms xml
header('Content-type: text/xml');
print '<?xml version="1.0" encoding="UTF-8" ?>';
$basetms = getbaseurl();
if( $_GET['tmsinfo'] == 'root' ) {
?><Services>
<TileMapService title="MBTiles PHP TMS" version="1.0.0" href="http://<?=$basetms ?>1.0.0/" />
</Services>
<?
} elseif( $_GET['tmsinfo'] == 'service' ) {
?> <TileMapService version="1.0.0" services="<?=$basetms ?>">
<Title>MBTiles PHP TMS</Title>
<Abstract />
<TileMaps>
<? if( $handle = opendir('.') ) {
while( ($file = readdir($handle)) !== false ) {
if( preg_match('/^[\w\d_-]+\.mbtiles$/', $file) && is_file($file) ) {
try {
$db = new PDO('sqlite:'.$file);
$params = readparams($db);
?> <TileMap title="<?=htmlspecialchars($params['name']) ?>" srs="OSGEO:41001" profile="global-mercator" href="<?=$basetms.'1.0.0/'.str_replace('.mbtiles','',$file) ?>" />
<? } catch( PDOException $e ) {}
}
}
}
?>
</TileMaps>
</TileMapService>
<?
} elseif( $_GET['tmsinfo'] == 'resource' && isset($_GET['tmslayer']) && preg_match('/^[\w\d_-]+$/', $_GET['tmslayer']) ) {
$layer = $_GET['tmslayer'];
try{
$db = new PDO('sqlite:'.$layer.'.mbtiles');
$params = readparams($db);
?><TileMap version="1.0.0" tilemapservice="<?=$basetms ?>1.0.0/">
<Title><?=htmlspecialchars($params['name']) ?></Title>
<Abstract><?=htmlspecialchars($params['description']) ?></Abstract>
<SRS>OSGEO:41001</SRS>
<BoundingBox minx="-180" miny="-90" maxx="180" maxy="90" />
<Origin x="0" y="0"/>
<TileFormat width="256" height="256" mime-type="image/<?=$params['format'] == 'jpg' ? 'jpeg' : 'png' ?>" extension="<?=$params['format']?>"/>
<TileSets profile="global-mercator">
<? foreach( readzooms($db) as $zoom ) { ?>
<TileSet href="<?=$basetms.'1.0.0/'.$layer.'/'.$zoom ?>" units-per-pixel="<?=78271.516 / pow(2, $zoom) ?>" order="<?=$zoom ?>" />
<? } ?>
</TileSets>
</TileMap>
<?
} catch( PDOException $e ) {}
}
} else {
// show list of all tilesets along with links
print '<h2>MBTiles PHP proxy</h2>';
if( $handle = opendir('.') ) {
$found = false;
while( ($file = readdir($handle)) !== false ) {
if( preg_match('/^[\w\d_-]+\.mbtiles$/', $file) && is_file($file) ) {
try {
$db = new PDO('sqlite:'.$file);
$params = readparams($db);
$zooms = readzooms($db);
$db = null;
print '<h3>'.htmlspecialchars($params['name']).'</h3>';
if( isset($params['description']) )
print '<p>'.htmlspecialchars($params['description']).'</p>';
print '<p>Type: '.$params['type'].', format: '.$params['format'].', version: '.$params['version'].'</p>';
if( isset($params['bounds']) )
print '<p>Bounds: '.str_replace(',', ', ',$params['bounds']).'</p>';
print '<p>Zoom levels: '.implode(', ', $zooms).'</p>';
print '<p>OpenLayers: <tt>new OpenLayers.Layer.OSM("'.htmlspecialchars($params['name']).'", "'.getbaseurl().preg_replace('/\.mbtiles/','',$file).'/${z}/${x}/${y}", {numZoomLevels: '.(end($zooms)+1).', isBaseLayer: '.($params['type']=='baselayer'?'true':'false').'});</tt></p>';
print '<p>TMS: <tt>http://'.$_SERVER['HTTP_HOST'].preg_replace('/\/[^\/]+$/','/',$_SERVER['REQUEST_URI']).'1.0.0/'.preg_replace('/\.mbtiles/','',$file).'</tt></p>';
} catch( PDOException $e ) {}
}
}
} else {
print 'Error opening script directory.';
}
}
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');
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$params[$row['name']] = $row['value'];
}
return $params;
}
function readzooms( $db ) {
$zooms = array();
$result = $db->query('select zoom_level from tiles group by zoom_level order by zoom_level');
while ($zoom = $result->fetchColumn()) {
$zooms[] = $zoom;
}
return $zooms;
}
?>