-
Notifications
You must be signed in to change notification settings - Fork 7
/
getairspace.php
69 lines (66 loc) · 2.18 KB
/
getairspace.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
68
69
<?php
function getpolygons($whereclause) {
global $mysqli;
$polygons=array();
$polylist = $mysqli->query("SELECT base,AsText(outline) FROM geopoly WHERE $whereclause");
while($polygon=$polylist->fetch_row())
{
$reduced=str_replace(")","",str_replace("LINESTRING(","",str_replace(")LINESTRING(",",",$polygon[1])));
$pointlist=explode(",",$reduced);
unset($coordlist);
foreach($pointlist as $point) {
$splitpoint= explode(" ",$point);
$coords['lat']=$splitpoint[0];
$coords['lng']=$splitpoint[1];
$coordlist[]=$coords;
}
$polygons[]=( object)array("base"=>$polygon[0],"coords"=>$coordlist);
}
$polylist->close();
return $polygons;
}
function getcircles($whereclause) {
global $mysqli;
$circlelist = $mysqli->query("SELECT base,AsText(centre),radius FROM geocircle WHERE $whereclause");
$circles=array();
while($circle=$circlelist->fetch_row()) {
$reduced=str_replace(")","",str_replace("POINT(","",$circle[1]));
$splitpoint=explode(" ",$reduced);
$centre['lat']=$splitpoint[0];
$centre['lng']=$splitpoint[1];
$circles[]=( object)array("base"=>$circle[0],"centre"=>$centre,"radius"=>$circle[2]);
}
return $circles;
}
$degdist = 111; // km- circumference of the earth divided by 360
require_once("../db_inc.php");
$mysqli=new mysqli($dbserver,$username,$password,$database);
$bounds=json_decode($_POST['bounds']);
$latmargin=$_POST['margin']/$degdist;
if(abs($bounds->north) > abs($bounds->south)) {
$latref=$bounds->south;
}
else {
$latref= $bounds->north;
}
$lngmargin=$latmargin/cos(deg2rad($latref));
$north=$bounds->north + $latmargin;
$south= $bounds->south - $latmargin;;
$east= $bounds->east + $lngmargin;
if($east > 180) {
$east= 360-$east;
}
$west=$bounds->west - $lngmargin;
if($west < -180) {
$west= 360 + $west;
}
$box="POLYGON(($north $west,$north $east,$south $east,$south $west,$north $west))";
$sql="SET @bbox=GeomFromText('".$box."')";
$mysqli->query($sql);
$wherepolygons="INTERSECTS(outline,@bbox)";
$wherecircles="INTERSECTS(mbr,@bbox)";
$retval['polygons']=getpolygons($wherepolygons);
$retval['circles']=getcircles($wherecircles);
echo json_encode($retval,JSON_NUMERIC_CHECK);
$mysqli->close();
?>