forked from Ysurac/FlightAirMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairspace-geojson.php
67 lines (55 loc) · 1.57 KB
/
airspace-geojson.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
<?php
require('require/class.Connection.php');
require('require/class.Spotter.php');
include_once('require/libs/geoPHP/geoPHP.inc');
if (isset($_GET['download']))
{
header('Content-disposition: attachment; filename="airspace.geojson"');
}
header('Content-Type: text/javascript');
$Connection = new Connection();
if (!Connection::tableExists('airspace')) {
die;
}
if (isset($_GET['coord']))
{
$coords = explode(',',$_GET['coord']);
$query = "SELECT *, AsWKB(SHAPE) AS wkb FROM airspace WHERE ST_Intersects(SHAPE, envelope(linestring(point(:minlon,:minlat), point(:maxlon,:maxlat))))";
try {
$sth = Connection::$db->prepare($query);
$sth->execute(array(':minlon' => $coords[0],':minlat' => $coords[1],':maxlon' => $coords[2],':maxlat' => $coords[3]));
} catch(PDOException $e) {
return "error";
}
} else {
$query = "SELECT *, AsWKB(SHAPE) AS wkb FROM airspace";
try {
$sth = Connection::$db->prepare($query);
$sth->execute();
} catch(PDOException $e) {
return "error";
}
}
function wkb_to_json($wkb) {
$geom = geoPHP::load($wkb,'wkb');
return $geom->out('json');
}
$geojson = array(
'type' => 'FeatureCollection',
'features' => array()
);
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
{
date_default_timezone_set('UTC');
$properties = $row;
unset($properties['wkb']);
unset($properties['SHAPE']);
$feature = array(
'type' => 'Feature',
'geometry' => json_decode(wkb_to_json($row['wkb'])),
'properties' => $properties
);
array_push($geojson['features'], $feature);
}
print json_encode($geojson, JSON_NUMERIC_CHECK);
?>