-
Notifications
You must be signed in to change notification settings - Fork 2
/
sparqlQueries.php
65 lines (54 loc) · 1.81 KB
/
sparqlQueries.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
<?php
/*
* This file allows to answer AJAX calls from
*/
include_once('lib/geoPHP/geoPHP.inc');
include_once('lib/sparqllib.php');
$uri = $_POST['uri'];
$query = 'SELECT DISTINCT ?geom WHERE {';
$query .= '<' . $uri . '>' . ' <http://www.opengis.net/ont/geosparql#hasGeometry> ?geomUri . ';
$query .= '?geomUri <http://www.opengis.net/ont/geosparql#hasSerialization> ?geom ';
$query .= '}';
//echo $query;
$queryResult = getQueryResults($query);
if ($queryResult->num_rows() == 0) echo 'no result';
else {
$return = '';
// To extract coordinates from the polygon string.
while( $row = $queryResult->fetch_array() ){
// Removing the first and last part of the string result depending on the length of the type name.
$temp = wkt_to_json($row["geom"]);
switch (substr($temp, 0, 13)) {
case '{"type":"Poly':
$return = substr($temp, 35, strlen($temp) - 39);
break;
case '{"type":"Poin':
$return = substr($temp, 31, strlen($temp) - 33);
break;
default:
$return = "Error - localization query: Query value type not recognized (" . substr($temp, 0, 12) . ").";
}
break;
}
}
echo $return;
function getQueryResults($query){
try {
$db = sparql_connect( "http://hxl.humanitarianresponse.info/sparql" );
if( !$db ) {
print $db->errno() . ": " . $db->error(). "\n"; exit;
}
$result = $db->query($query);
if( !$result ) {
print $db->errno() . ": " . $db->error(). "\n"; exit;
}
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
return $result;
}
function wkt_to_json($wkt) {
$geom = geoPHP::load($wkt,'wkt');
return $geom->out('json');
}
?>