Merge pull request #456 from Struart88/patch-2
[FlightAirMap.git] / search-geojson.php
blob812267ccf240e3a96b7a3eeb60bbfde37de4c764
1 <?php
2 require_once('require/class.Connection.php');
3 require_once('require/class.Language.php');
4 require_once('require/class.Spotter.php');
5 require_once('require/class.SpotterArchive.php');
6 $Spotter=new Spotter();
7 if (isset($_GET['start_date'])) {
8 //for the date manipulation into the query
9 if($_GET['start_date'] != "" && $_GET['end_date'] != ""){
10 $start_date = date("Y-m-d", strtotime($_GET['start_date']))." 00:00:00";
11 $end_date = date("Y-m-d",strtotime($_GET['end_date']))." 00:00:00";
12 $sql_date = $start_date.",".$end_date;
13 } else if($_GET['start_date'] != ""){
14 $start_date = date("Y-m-d",strtotime($_GET['start_date']))." 00:00:00";
15 $sql_date = $start_date;
16 } else if($_GET['start_date'] == "" && $_GET['end_date'] != ""){
17 $end_date = date("Y-m-d H:i:s", strtotime("2014-04-12")).",".date("Y-m-d",strtotime($_GET['end_date']))." 00:00:00";
18 $sql_date = $end_date;
19 } else $sql_date = '';
20 } else $sql_date = '';
22 if (isset($_GET['highest_altitude'])) {
23 //for altitude manipulation
24 if($_GET['highest_altitude'] != "" && $_GET['lowest_altitude'] != ""){
25 $end_altitude = filter_input(INPUT_GET,'highest_altitude',FILTER_SANITIZE_NUMBER_INT);
26 $start_altitude = filter_input(INPUT_GET,'lowest_altitude',FILTER_SANITIZE_NUMBER_INT);
27 $sql_altitude = $start_altitude.",".$end_altitude;
28 } else if($_GET['highest_altitude'] != ""){
29 $end_altitude = filter_input(INPUT_GET,'highest_altitude',FILTER_SANITIZE_NUMBER_INT);
30 $sql_altitude = $end_altitude;
31 } else if($_GET['highest_altitude'] == "" && $_GET['lowest_altitude'] != ""){
32 $start_altitude = filter_input(INPUT_GET,'lowest_altitude',FILTER_SANITIZE_NUMBER_INT).",60000";
33 $sql_altitude = $start_altitude;
34 } else $sql_altitude = '';
35 } else $sql_altitude = '';
37 //calculuation for the pagination
38 if(!isset($_GET['limit']))
40 if (!isset($_GET['number_results']))
42 $limit_start = 0;
43 $limit_end = 25;
44 $absolute_difference = 25;
45 } else {
46 if ($_GET['number_results'] > 1000){
47 $_GET['number_results'] = 1000;
49 $limit_start = 0;
50 $limit_end = filter_input(INPUT_GET,'number_results',FILTER_SANITIZE_NUMBER_INT);
51 $absolute_difference = filter_input(INPUT_GET,'number_results',FILTER_SANITIZE_NUMBER_INT);
53 } else {
54 $limit_explode = explode(",", $_GET['limit']);
55 $limit_start = filter_var($limit_explode[0],FILTER_SANITIZE_NUMBER_INT);
56 $limit_end = filter_var($limit_explode[1],FILTER_SANITIZE_NUMBER_INT);
59 $absolute_difference = abs($limit_start - $limit_end);
60 $limit_next = $limit_end + $absolute_difference;
61 $limit_previous_1 = $limit_start - $absolute_difference;
62 $limit_previous_2 = $limit_end - $absolute_difference;
64 if ($_GET['download'] == "true")
66 header('Content-disposition: attachment; filename="flightairmap.geojson"');
69 header('Content-Type: application/json');
71 if (isset($_GET['sort'])) $sort = $_GET['sort'];
72 else $sort = '';
73 $q = filter_input(INPUT_GET,'q',FILTER_SANITIZE_STRING);
74 $id = filter_input(INPUT_GET,'id',FILTER_SANITIZE_NUMBER_INT);
75 $registration = filter_input(INPUT_GET,'registratrion',FILTER_SANITIZE_STRING);
76 $aircraft = filter_input(INPUT_GET,'aircraft',FILTER_SANITIZE_STRING);
77 $manufacturer = filter_input(INPUT_GET,'manufacturer',FILTER_SANITIZE_STRING);
78 $highlights = filter_input(INPUT_GET,'highlights',FILTER_SANITIZE_STRING);
79 $airline = filter_input(INPUT_GET,'airline',FILTER_SANITIZE_STRING);
80 $airline_country = filter_input(INPUT_GET,'airline_country',FILTER_SANITIZE_STRING);
81 $airline_type = filter_input(INPUT_GET,'airline_type',FILTER_SANITIZE_STRING);
82 $airport = filter_input(INPUT_GET,'airport',FILTER_SANITIZE_STRING);
83 $airport_country = filter_input(INPUT_GET,'airport_country',FILTER_SANITIZE_STRING);
84 $callsign = filter_input(INPUT_GET,'callsign',FILTER_SANITIZE_STRING);
85 $owner = filter_input(INPUT_GET,'owner',FILTER_SANITIZE_STRING);
86 $pilot_id = filter_input(INPUT_GET,'pilot_id',FILTER_SANITIZE_STRING);
87 $pilot_name = filter_input(INPUT_GET,'pilot_name',FILTER_SANITIZE_STRING);
88 $departure_airport_route = filter_input(INPUT_GET,'departure_airport_route',FILTER_SANITIZE_STRING);
89 $arrival_airport_route = filter_input(INPUT_GET,'arrival_airport_route',FILTER_SANITIZE_STRING);
90 if ($id != '') {
91 $spotter_array = $Spotter->getSpotterDataByID($id);
92 } else {
93 $spotter_array = $Spotter->searchSpotterData($q,$registration,$aircraft,strtolower(str_replace("-", " ", $manufacturer)),$highlights,$airline,$airline_country,$airline_type,$airport,$airport_country,$callsign,$departure_airport_route,$arrival_airport_route,$owner,$pilot_id,$pilot_name,$sql_altitude,$sql_date,$limit_start.",".$absolute_difference,$sort,'');
96 $output = '{';
97 $output .= '"type": "FeatureCollection",';
98 $output .= '"features": [';
99 if (!empty($spotter_array)) {
100 foreach($spotter_array as $spotter_item) {
101 //waypoint plotting
102 $output .= '{';
103 $output .= '"type": "Feature",';
104 $output .= '"properties": {';
105 $output .= '"id": "'.$spotter_item['spotter_id'].'",';
106 $output .= '"ident": "'.$spotter_item['ident'].'",';
107 $output .= '"registration": "'.$spotter_item['registration'].'",';
108 $output .= '"aircraft_icao": "'.$spotter_item['aircraft_type'].'",';
109 $output .= '"aircraft_name": "'.$spotter_item['aircraft_name'].'",';
110 $output .= '"aircraft_manufacturer": "'.$spotter_item['aircraft_manufacturer'].'",';
111 $output .= '"airline_name": "'.$spotter_item['airline_name'].'",';
112 $output .= '"airline_icao": "'.$spotter_item['airline_icao'].'",';
113 $output .= '"airline_iata": "'.$spotter_item['airline_iata'].'",';
114 $output .= '"airline_country": "'.$spotter_item['airline_country'].'",';
115 $output .= '"airline_callsign": "'.$spotter_item['airline_callsign'].'",';
116 $output .= '"airline_type": "'.$spotter_item['airline_type'].'",';
117 $output .= '"departure_airport_city": "'.$spotter_item['departure_airport_city'].'",';
118 $output .= '"departure_airport_country": "'.$spotter_item['departure_airport_country'].'",';
119 $output .= '"departure_airport_iata": "'.$spotter_item['departure_airport_iata'].'",';
120 $output .= '"departure_airport_icao": "'.$spotter_item['departure_airport_icao'].'",';
121 $output .= '"departure_airport_latitude": "'.$spotter_item['departure_airport_latitude'].'",';
122 $output .= '"departure_airport_longitude": "'.$spotter_item['departure_airport_longitude'].'",';
123 $output .= '"departure_airport_altitude": "'.$spotter_item['departure_airport_altitude'].'",';
124 $output .= '"arrival_airport_city": "'.$spotter_item['arrival_airport_city'].'",';
125 $output .= '"arrival_airport_country": "'.$spotter_item['arrival_airport_country'].'",';
126 $output .= '"arrival_airport_iata": "'.$spotter_item['arrival_airport_iata'].'",';
127 $output .= '"departure_airport_icao": "'.$spotter_item['arrival_airport_icao'].'",';
128 $output .= '"arrival_airport_latitude": "'.$spotter_item['arrival_airport_latitude'].'",';
129 $output .= '"arrival_airport_longitude": "'.$spotter_item['arrival_airport_longitude'].'",';
130 $output .= '"arrival_airport_altitude": "'.$spotter_item['arrival_airport_altitude'].'",';
131 if ($spotter_item['latitude'] != 0 && $spotter_item['latitude'] != 0) {
132 $output .= '"latitude": "'.$spotter_item['latitude'].'",';
133 $output .= '"longitude": "'.$spotter_item['longitude'].'",';
134 $output .= '"altitude": "'.$spotter_item['altitude'].'",';
135 $output .= '"ground_speed": "'.$spotter_item['ground_speed'].'",';
136 $output .= '"heading": "'.$spotter_item['heading'].'",';
137 $output .= '"heading_name": "'.$spotter_item['heading_name'].'",';
139 $output .= '"date": "'.date("c", strtotime($spotter_item['date_iso_8601'])).'"';
140 $output .= '}';
142 if ($spotter_item['waypoints'] != '') {
143 $waypoint_pieces = explode(' ', $spotter_item['waypoints']);
144 $waypoint_pieces = array_chunk($waypoint_pieces, 2);
145 $output .= ',"geometry": {';
146 $output .= '"type": "LineString",';
147 $output .= '"coordinates": [';
148 foreach ($waypoint_pieces as $waypoint_coordinate) {
149 $output .= '[';
150 $output .= $waypoint_coordinate[1].', ';
151 $output .= $waypoint_coordinate[0];
152 $output .= '],';
154 $output = substr($output, 0, -1);
155 $output .= ']';
156 $output .= '}';
159 $SpotterArchive = new SpotterArchive();
160 $archive_data = $SpotterArchive->getAllArchiveSpotterDataById($spotter_item['flightaware_id']);
161 if (!empty($archive_data)) {
162 $output .= ',"geometry": {';
163 $output .= '"type": "LineString",';
164 $output .= '"coordinates": [';
165 foreach ($archive_data as $coord_data) {
166 $output .= '[';
167 $output .= $coord_data['longitude'].', ';
168 $output .= $coord_data['latitude'];
169 $output .= '],';
171 $output = substr($output, 0, -1);
172 $output .= ']';
173 $output .= '}';
176 $output .= '},';
177 /* //location of aircraft
178 $output .= '{';
179 $output .= '"type": "Feature",';
180 $output .= '"properties": {';
181 $output .= '"id": "'.$spotter_item['spotter_id'].'",';
182 $output .= '"ident": "'.$spotter_item['ident'].'",';
183 $output .= '"registration": "'.$spotter_item['registration'].'",';
184 $output .= '"aircraft_icao": "'.$spotter_item['aircraft_type'].'",';
185 $output .= '"aircraft_name": "'.$spotter_item['aircraft_name'].'",';
186 $output .= '"aircraft_manufacturer": "'.$spotter_item['aircraft_manufacturer'].'",';
187 $output .= '"airline_name": "'.$spotter_item['airline_name'].'",';
188 $output .= '"airline_icao": "'.$spotter_item['airline_icao'].'",';
189 $output .= '"airline_iata": "'.$spotter_item['airline_iata'].'",';
190 $output .= '"airline_country": "'.$spotter_item['airline_country'].'",';
191 $output .= '"airline_callsign": "'.$spotter_item['airline_callsign'].'",';
192 $output .= '"airline_type": "'.$spotter_item['airline_type'].'",';
193 $output .= '"departure_airport_city": "'.$spotter_item['departure_airport_city'].'",';
194 $output .= '"departure_airport_country": "'.$spotter_item['departure_airport_country'].'",';
195 $output .= '"departure_airport_iata": "'.$spotter_item['departure_airport_iata'].'",';
196 $output .= '"departure_airport_icao": "'.$spotter_item['departure_airport_icao'].'",';
197 $output .= '"departure_airport_latitude": "'.$spotter_item['departure_airport_latitude'].'",';
198 $output .= '"departure_airport_longitude": "'.$spotter_item['departure_airport_longitude'].'",';
199 $output .= '"departure_airport_altitude": "'.$spotter_item['departure_airport_altitude'].'",';
200 $output .= '"arrival_airport_city": "'.$spotter_item['arrival_airport_city'].'",';
201 $output .= '"arrival_airport_country": "'.$spotter_item['arrival_airport_country'].'",';
202 $output .= '"arrival_airport_iata": "'.$spotter_item['arrival_airport_iata'].'",';
203 $output .= '"departure_airport_icao": "'.$spotter_item['arrival_airport_icao'].'",';
204 $output .= '"arrival_airport_latitude": "'.$spotter_item['arrival_airport_latitude'].'",';
205 $output .= '"arrival_airport_longitude": "'.$spotter_item['arrival_airport_longitude'].'",';
206 $output .= '"arrival_airport_altitude": "'.$spotter_item['arrival_airport_altitude'].'",';
207 $output .= '"latitude": "'.$spotter_item['latitude'].'",';
208 $output .= '"longitude": "'.$spotter_item['longitude'].'",';
209 $output .= '"altitude": "'.$spotter_item['altitude'].'",';
210 $output .= '"ground_speed": "'.$spotter_item['ground_speed'].'",';
211 $output .= '"heading": "'.$spotter_item['heading'].'",';
212 $output .= '"heading_name": "'.$spotter_item['heading_name'].'",';
213 $output .= '"date": "'.date("c", strtotime($spotter_item['date_iso_8601'])).'"';
214 $output .= '},';
215 $output .= '"geometry": {';
216 $output .= '"type": "Point",';
217 $output .= '"coordinates": [';
218 $output .= $spotter_item['longitude'].', ';
219 $output .= $spotter_item['latitude'];
220 $output .= ']';
221 $output .= '}';
222 $output .= '},';
226 $output = substr($output, 0, -1);
227 $output .= ']';
228 $output .= '}';
229 print $output;