forked from jpatokal/openflights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kml.php
112 lines (93 loc) · 3.66 KB
/
kml.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
session_start();
header("Content-type: application/vnd.google-earth.kml+xml");
header("Content-disposition: attachment; filename=\"openflights-" . date("Y-m-d").".kml\"");
include 'greatcircle.php';
include 'helper.php';
include 'filter.php';
include 'db_pdo.php';
$METERSPERFOOT = 0.3048;
$uid = $_SESSION["uid"];
// Logged in?
if(!$uid or empty($uid)) {
$uid = 1;
}
$sql = "SELECT DISTINCT s.x AS sx,s.y AS sy,s.elevation AS sz,s.iata AS siata,s.icao AS sicao,d.x AS dx,d.y AS dy,d.elevation AS dz,d.iata AS diata,d.icao AS dicao,code,distance,mode FROM flights AS f, airports AS s, airports AS d WHERE f.src_apid=s.apid AND f.dst_apid=d.apid AND f.uid=:uid" . getFilterString($dbh, $_GET) . " GROUP BY f.fid,s.apid,d.apid";
$sth = $dbh->prepare($sql);
$sth->execute(compact('uid'));
readfile("../kml/header.kml");
print "<Folder>\n<name>Flights</name>\n";
// Plot flights on map
while($row = $sth->fetch()) {
$x1 = $row["sx"];
$y1 = $row["sy"];
$z1 = $row["sz"] * $METERSPERFOOT;
$x2 = $row["dx"];
$y2 = $row["dy"];
$z2 = $row["dz"] * $METERSPERFOOT;
$distance = $row["distance"];
// Skip flights where src==dest
if($x1 != $x2 && $y1 != $y2) {
$src_ap = format_apcode2($row["siata"], $row["sicao"]);
$dst_ap = format_apcode2($row["diata"], $row["dicao"]);
$code = $row["code"];
$mode = $row["mode"];
print "<Placemark>\n";
print " <name>$src_ap-$dst_ap</name>\n <description>$modes[$mode] $code</description>\n";
print " <styleUrl>#$mode</styleUrl>\n";
$points = gcPath(array("x" => $x1, "y" => $y1, "z" => $z1),
array("x" => $x2, "y" => $y2, "z" => $z2),
$distance, true);
if($row["mode"] == "F") {
$altitudeMode = "absolute";
} else {
$altitudeMode = "clampToGround";
}
print " <MultiGeometry>\n <LineString>\n <altitudeMode>$altitudeMode</altitudeMode><coordinates>\n";
foreach($points as $loc) {
if(! $loc) continue; // skip breaks
print $loc["x"] . "," . $loc["y"] . "," . $loc["z"] . "\n";
}
print " </coordinates>\n </LineString>\n";
print " </MultiGeometry>\n</Placemark>\n";
}
}
print "</Folder>\n";
print "<Folder><name>Airports</name>\n";
// Draw airports from largest to smallest
$airportColors = array ("black", "gray", "purple", "cyan", "cyan", "green");
$sql = "SELECT DISTINCT x,y,elevation,iata,icao,name,city,country,count(name) AS visits FROM flights AS f, airports AS a WHERE (f.src_apid=a.apid OR f.dst_apid=a.apid) AND f.uid=:uid" . getFilterString($dbh, $_GET) . " GROUP BY a.apid,name ORDER BY visits DESC";
$sth = $dbh->prepare($sql);
$sth->execute(compact('uid'));
$first = true;
while ($row = $sth->fetch()) {
$count = $row["visits"];
if($first) {
$maxFlights = $count;
$first = false;
}
$colorIndex = floor(($count / $maxFlights) * sizeof($airportColors)) + 1;
if($count <= 2 || $colorIndex < 0) {
$colorIndex = 0;
}
// More than two flights: at least 2nd smallest
if($count > 2) {
$colorIndex = max(1, $colorIndex);
}
// Max out at top color
if($colorIndex >= sizeof($airportColors)) {
$colorIndex = sizeof($airportColors) - 1;
}
print "<Placemark>\n";
print " <name>" . format_apcode($row) . "</name>\n";
printf(" <description>\n <![CDATA[\n<b>%s</b><br><i>Elevation</i>: %s ft<br><i>Flights</i>: %s\n]]>\n </description>\n",
format_airport($row), $row["elevation"], $count);
print " <Point>\n";
printf(" <coordinates>%s,%s,%s</coordinates>\n", $row["x"], $row["y"], $row["elevation"]);
print " </Point>\n";
print " <styleUrl>#" . $airportColors[$colorIndex] . "-pushpin</styleUrl>\n";
print "</Placemark>\n";
}
print "</Folder>\n";
readfile("../kml/footer.kml");
?>