-
Notifications
You must be signed in to change notification settings - Fork 3
/
GetRaster.php
54 lines (47 loc) · 1.47 KB
/
GetRaster.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
<?php
include_once('config.inc.php');
header('Content-Type: image/png');
// validate parameters
foreach (array('sql', 'spatial_type', 'bvals') as $k) {
if (!isset($_GET[$k])) $_GET[$k] = '';
$_GET[$k] = trim($_GET[$k]);
switch ($k) {
case 'sql':
if (!strlen($_GET[$k])) {
$_GET[$k] = "ST_Buffer('MULTIPOINT((1 2),(3 4),(5 6))'::geometry,1)";
$_GET['spatial_type'] = 'geometry';
}
break;
case 'bvals':
$_GET[$k] = explode(',', $_GET[$k]);
$fn = create_function('&$v', '$v = intval(trim($v));$v = ($v < 0 ? 0 : ($v > 255 ? 255 : $v));');
array_walk($_GET[$k], $fn);
break;
case 'spatial_type':
$_GET[$k] = strtolower($_GET[$k]);
if (!in_array($_GET[$k], array('raster', 'geometry')))
$_GET[$k] = 'geometry';
break;
}
}
// assemble connection string
$conn_str = array();
foreach ($conn as $k => $v) {
if (!strlen(trim($v))) continue;
$conn_str[] = $k . '=' . trim($v);
}
$conn_str = implode(' ', $conn_str);
$dbconn = pg_connect($conn_str);
if ($dbconn === false) return;
// do query
if ($_GET['spatial_type'] != 'raster')
$sql = "SELECT postgis_viewer_image('" . pg_escape_string($dbconn, $_GET['sql']) . "', 'geometry', ARRAY[" . implode(',', $_GET['bvals']) . "])";
else
$sql = "SELECT postgis_viewer_image('" . pg_escape_string($dbconn, $_GET['sql']) . "', 'raster')";
$result = pg_query($sql);
if ($result === false) return;
$row = pg_fetch_row($result);
pg_free_result($result);
if ($row === false) return;
echo pg_unescape_bytea($row[0]);
?>