-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet17-spatialapi.php
50 lines (43 loc) · 1.53 KB
/
leaflet17-spatialapi.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
<?php
// Get the BBOX from the URL
if (!isSet ($_GET['bbox'])) {return false;}
if (!preg_match ('/^([0-9.]+),([0-9.]+),([0-9.]+),([0-9.]+)$/', $_GET['bbox'], $matches)) {return false;}
// Get the data from the database
// Construct the query and assemble the parameters, using a spatial query
$query = '
SELECT *
FROM locations
WHERE MBRCONTAINS(ST_LINESTRINGFROMTEXT(:linestring), lonLat)
;';
$preparedStatementValues = array (
'linestring' => "LINESTRING({$matches[1]} {$matches[2]}, {$matches[3]} {$matches[4]})", // LINESTRING(W S, E N)
);
// Get the data
require_once ('database.php');
$database = new database ('localhost', $username = 'leaflet', $password = 'leaflet123', $database = 'leaflet');
$data = $database->getData ($query, false, true, $preparedStatementValues);
//var_dump ($database->error ());
// Assemble the features
$features = array ();
foreach ($data as $record) {
$features[] = array (
"type" => "Point",
"properties" => array (
'id' => $record['id'],
'caption' => $record['caption'],
'categoryId' => $record['categoryId'],
'iconUrl' => $record['iconUrl'],
'username' => $record['username'],
),
"coordinates" => array ($record['longitude'], $record['latitude']),
);
}
// Assemble the GeoJSON
$geojson = array (
"type" => "FeatureCollection",
"features" => $features,
);
// Serve as GeoJSON
header('Content-Type: application/json');
echo json_encode ($geojson, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
?>