-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetX3D.php
66 lines (60 loc) · 1.79 KB
/
GetX3D.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
<?php
include_once('config.inc.php');
header('Content-Type: image/png');
$vals = array();
// validate parameters
foreach (array('sql', 'spatial_type', 'bvals') as $k) {
if (!isset($_REQUEST[$k])) $_REQUEST[$k] = '';
$_REQUEST[$k] = trim($_REQUEST[$k]);
//print_r($_REQUEST);
switch ($k) {
case 'sql':
if (!strlen($_REQUEST[$k])) {
$vals[$k] = "ST_Buffer('MULTIPOINT((1 2),(3 4),(5 6))'::geometry,1)";
$vals['spatial_type'] = 'geometry';
}
else {
$vals[$k] = $_REQUEST[$k];
}
break;
case 'bvals':
$bvals = explode(',', $_REQUEST[$k]);
$cvals = array();
/**x3d colors are in rgb where intensity is on scale of 0 to 1 **/
function map_to_1($v) { $v = floatval(trim($v));$v = ($v < 0 ? 0 : ($v > 255 ? 1 : $v/255.0));};
for ($x = 0; $x <= 2; $x++) {
$v = floatval(trim($bvals[$x]));
$v = ($v < 0 ? 0 : ($v > 255 ? 1 : $v/255.0));
$cvals[$x] = $v;
}
$vals[$k] = $cvals;
break;
case 'spatial_type':
$vals[$k] = strtolower($_REQUEST[$k]);
if (!in_array($vals[$k], array('raw', 'geometry')))
$vals[$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 ($vals['spatial_type'] != 'raw')
$sql = "SELECT postgis_viewer_x3d('" . pg_escape_string($dbconn, $vals['sql']) . "', 'geometry', ARRAY[" . implode(',', $vals['bvals']) . "])";
else
$sql = "SELECT postgis_viewer_x3d('" . pg_escape_string($dbconn, $vals['sql']) . "', 'raw')";
//echo $sql;
$result = pg_query($sql);
if ($result === false) return;
$row = pg_fetch_row($result);
pg_free_result($result);
if ($row === false) return;
echo $row[0];
?>