/** * Don't save aerodrome if another aerodrome is in same place or exists with same ICAO */ public function _on_creating() { if ($this->longitude && $this->latitude) { $qb = org_routamc_positioning_aerodrome_dba::new_query_builder(); $qb->add_constraint('longitude', '=', $this->longitude); $qb->add_constraint('latitude', '=', $this->latitude); $qb->set_limit(1); $matches = $qb->execute_unchecked(); if (count($matches) > 0) { // We don't need to save duplicate entries return false; } } if (!empty($this->icao)) { $qb = org_routamc_positioning_aerodrome_dba::new_query_builder(); $qb->add_constraint('icao', '=', $this->icao); $qb->set_limit(1); $matches = $qb->execute_unchecked(); if (count($matches) > 0) { // We don't need to save duplicate entries midcom_connection::set_error(MGD_ERR_DUPLICATE); return false; } } return true; }
/** * Import manually entered log entry. The entries are associative arrays containing * some or all of the following keys: * * - latitude * - longitude * - altitude * - city * - country * - aerodrome * - timestamp * * @param Array $log Log entry in Array format specific to importer * @return boolean Indicating success. */ function import($log) { $this->log = new org_routamc_positioning_log_dba(); $this->log->importer = 'manual'; // Set different person if required if (array_key_exists('person', $log)) { $this->log->person = $log['person']; } else { $this->log->person = midcom_connection::get_user(); } if (array_key_exists('timestamp', $log)) { $this->log->date = (int) $log['timestamp']; } else { $this->log->date = time(); } // Figure out which option we will use, starting from best option // Best option: we know coordinates if (array_key_exists('latitude', $log) && array_key_exists('longitude', $log)) { // Manually entered positions are assumed to be only semi-accurate $this->log->accuracy = ORG_ROUTAMC_POSITIONING_ACCURACY_MANUAL; // Normalize coordinates to decimal $coordinates = $this->normalize_coordinates($log['latitude'], $log['longitude']); $this->log->latitude = $coordinates['latitude']; $this->log->longitude = $coordinates['longitude']; } // Airport entered if (array_key_exists('aerodrome', $log)) { // Aerodrome position is not usually very accurate, except if we're at the airport of course $this->log->accuracy = ORG_ROUTAMC_POSITIONING_ACCURACY_CITY; // Normalize aerodrome name $aerodrome = strtoupper($log['aerodrome']); // Seek the aerodrome entry, first by accurate match $aerodrome_entry = null; $qb = org_routamc_positioning_aerodrome_dba::new_query_builder(); $qb->begin_group('OR'); // We will seek by both ICAO and IATA codes $qb->add_constraint('icao', '=', $aerodrome); $qb->add_constraint('iata', '=', $aerodrome); $qb->end_group(); $matches = $qb->execute(); if (count($matches) > 0) { $aerodrome_entry = $matches[0]; } if (is_null($aerodrome_entry)) { // Couldn't match the entered city to a location $this->error = 'POSITIONING_AERODROME_NOT_FOUND'; return false; } // Normalize coordinates $this->log->latitude = $aerodrome_entry->latitude; $this->log->longitude = $aerodrome_entry->longitude; $this->log->altitude = $aerodrome_entry->altitude; } // City and country entered if (array_key_exists('city', $log)) { if (!isset($log['geocoder'])) { $log['geocoder'] = 'city'; } $geocoder = org_routamc_positioning_geocoder::create($log['geocoder']); $position = $geocoder->geocode($log); if (!$position['latitude'] || !$position['longitude']) { // Couldn't match the entered city to a location $this->error = 'POSITIONING_CITY_NOT_FOUND'; return false; } foreach ($position as $key => $value) { $this->log->{$key} = $value; } } // Save altitude if provided if (array_key_exists('altitude', $log)) { $this->log->altitude = $log['altitude']; } // Try to create the entry $stat = $this->log->create(); $this->error = midcom_connection::get_error_string(); return $stat; }
<?php midcom::get('auth')->require_admin_user(); midcom::get()->disable_limits(); $http_request = new org_openpsa_httplib(); $csv = $http_request->get('http://weather.gladstonefamily.net/cgi-bin/location.pl/pjsg_all_location.csv?csv=1'); $csv = str_replace('"', '', $csv); $lines = explode("\n", $csv); foreach ($lines as $line) { $aerodromeinfo = explode(',', $line); // Skip the non-ICAO ones if (empty($aerodromeinfo[0]) || strlen($aerodromeinfo[0]) != 4) { continue; } // Skip non-WMO ones if (empty($aerodromeinfo[1])) { continue; } echo "<br />Importing {$aerodromeinfo[0]} {$aerodromeinfo[2]}...\n"; $aerodrome = new org_routamc_positioning_aerodrome_dba(); $aerodrome->icao = $aerodromeinfo[0]; $aerodrome->wmo = $aerodromeinfo[1]; $aerodrome->name = $aerodromeinfo[2]; $aerodrome->country = substr($aerodromeinfo[4], 0, 2); $aerodrome->latitude = (double) $aerodromeinfo[5]; $aerodrome->longitude = (double) $aerodromeinfo[6]; $aerodrome->altitude = (int) $aerodromeinfo[7]; $aerodrome->create(); echo midcom_connection::get_error_string(); flush(); }