/** * Get closest items * * Note: If you set a max distance you may not always get the number of items specified in the limit. * * @param string $class MidCOM DBA class to query * @param array $position Center position * @param integer $limit How many results to return * @param integer $max_distance Maximum distance of returned objects in kilometers, or null if any * @param float $modifier * @return array array of MidCOM DBA objects sorted by proximity */ function get_closest($class, $center, $limit, $max_distance = null, $modifier = 0.15) { $classname = org_routamc_positioning_utils::get_positioning_class($class); $direct = false; if ($classname != $class) { $direct = false; } else { $direct = true; } $qb = midcom::get('dbfactory')->new_query_builder($classname); if (!$direct) { // We're querying a regular DBA object through a location object $qb->add_constraint('parentclass', '=', $class); } static $rounds = 0; $rounds++; // Limit to earth coordinates $from['latitude'] = $center['latitude'] + $modifier; if ($from['latitude'] > 90) { $from['latitude'] = 90; } $from['longitude'] = $center['longitude'] - $modifier; if ($from['longitude'] < -180) { $from['longitude'] = -180; } $to['latitude'] = $center['latitude'] - $modifier; if ($to['latitude'] < -90) { $to['latitude'] = -90; } $to['longitude'] = $center['longitude'] + $modifier; if ($to['longitude'] > 180) { $to['longitude'] = 180; } if (!isset($current_locale)) { $current_locale = setlocale(LC_NUMERIC, '0'); setlocale(LC_NUMERIC, 'C'); } $qb->begin_group('AND'); $qb->add_constraint('latitude', '<', (double) $from['latitude']); $qb->add_constraint('latitude', '>', (double) $to['latitude']); $qb->end_group(); $qb->begin_group('AND'); $qb->add_constraint('longitude', '>', (double) $from['longitude']); $qb->add_constraint('longitude', '<', (double) $to['longitude']); $qb->end_group(); $result_count = $qb->count(); if ($result_count == 0) { // Check that there are any in the DB before proceeding further $qb_check = midcom::get('dbfactory')->new_query_builder($classname); if ($qb_check->count_unchecked() == 0) { return array(); } } if ($result_count < $limit) { if ($from['latitude'] == 90 && $from['longitude'] == -180 && $to['latitude'] == -90 && $to['longitude'] == 180) { // We've queried the entire globe so we return whatever we got $results = $qb->execute(); $closest = array(); foreach ($results as $result) { $result_coordinates = array('latitude' => $result->latitude, 'longitude' => $result->longitude); $distance = sprintf("%05d", round(org_routamc_positioning_utils::get_distance($center, $result_coordinates))); if (!is_null($max_distance) && $distance > $max_distance) { // This entry is too far continue; } if (!$direct) { // Instantiate the real object as the result $located_object = new $class($result->parent); if (!$located_object->guid) { // This one has been deleted midcom::get('auth')->request_sudo('org.routamc.positioning'); $result->delete(); midcom::get('auth')->drop_sudo(); continue; } $result = $located_object; $result->latitude = $result_coordinates['latitude']; $result->longitude = $result_coordinates['longitude']; $result->distance = (int) $distance; } $closest[$distance . $result->guid] = $result; } ksort($closest); reset($closest); return $closest; } $modifier = $modifier * 1.05; setlocale(LC_NUMERIC, $current_locale); return org_routamc_positioning_utils::get_closest($class, $center, $limit, $max_distance, $modifier); } $results = $qb->execute(); $closest = array(); foreach ($results as $result) { $result_coordinates = array('latitude' => $result->latitude, 'longitude' => $result->longitude); $distance = sprintf("%05d", round(org_routamc_positioning_utils::get_distance($center, $result_coordinates))); if (!is_null($max_distance) && $distance > $max_distance) { // This entry is too far continue; } if (!$direct) { // Instantiate the real object as the result $located_object = new $class($result->parent); if (!$located_object->guid) { // This one has been deleted midcom::get('auth')->request_sudo('org.routamc.positioning'); $result->delete(); midcom::get('auth')->drop_sudo(); continue; } $result = $located_object; $result->latitude = $result_coordinates['latitude']; $result->longitude = $result_coordinates['longitude']; $result->distance = (int) $distance; } $closest[$distance . $result->guid] = $result; } ksort($closest); reset($closest); while (count($closest) > $limit) { array_pop($closest); } setlocale(LC_NUMERIC, $current_locale); return $closest; }
/** * Get closest items * * @param string $class MidCOM DBA class to query * @param org_routamc_positioning_spot $spot Center position * @param integer $limit How many results to return * @return Array Array of MidCOM DBA objects sorted by proximity */ static function get_closest($class, org_routamc_positioning_spot $spot, $limit, $modifier = 0.15) { $classname = org_routamc_positioning_utils::get_positioning_class($class); if ($classname != $class) { $direct = false; } else { $direct = true; } $qb = new midgard_query_builder($classname); if (!$direct) { // We're querying a regular DBA object through a location object $qb->add_constraint('parentclass', '=', $class); } static $rounds = 0; $rounds++; $from['latitude'] = $spot->latitude + $modifier; if ($from['latitude'] > 90) { $from['latitude'] = 90; } $from['longitude'] = $spot->longitude - $modifier; if ($from['longitude'] < -180) { $from['longitude'] = -180; } $to['latitude'] = $spot->latitude - $modifier; if ($to['latitude'] < -90) { $to['latitude'] = -90; } $to['longitude'] = $spot->longitude + $modifier; if ($to['longitude'] > 180) { $to['longitude'] = 180; } if (!isset($current_locale)) { $current_locale = setlocale(LC_NUMERIC, '0'); setlocale(LC_NUMERIC, 'C'); } $qb->begin_group('AND'); $qb->add_constraint('latitude', '<', (double) $from['latitude']); $qb->add_constraint('latitude', '>', (double) $to['latitude']); $qb->end_group(); $qb->begin_group('AND'); $qb->add_constraint('longitude', '>', (double) $from['longitude']); $qb->add_constraint('longitude', '<', (double) $to['longitude']); $qb->end_group(); $result_count = $qb->count(); //echo "<br />Round {$rounds}, lat1 {$from['latitude']} lon1 {$from['longitude']}, lat2 {$to['latitude']} lon2 {$to['longitude']}: {$result_count} results\n"; if ($result_count < $limit) { if ($from['latitude'] == 90 && $from['longitude'] == -180 && $to['latitude'] == -90 && $to['longitude'] == 180) { // We've queried the entire globe so we return whatever we got $results = $qb->execute(); $closest = array(); foreach ($results as $result) { $result_spot = new org_routamc_positioning_spot($result); $distance = sprintf("%05d", round(org_routamc_positioning_utils::get_distance($spot, $result_spot))); if (!$direct) { // Instantiate the real object as the result $result = new $class($result->parent); $result->spot = $result_spot; $result->latitude = $result_spot->latitude; $result->longitude = $result_spot->longitude; } $closest[$distance . $result->guid] = $result; } ksort($closest); reset($closest); return $closest; } $modifier = $modifier * 1.05; setlocale(LC_NUMERIC, $current_locale); return org_routamc_positioning_utils::get_closest($class, $spot, $limit, $modifier); } $results = $qb->execute(); $closest = array(); foreach ($results as $result) { $result_spot = new org_routamc_positioning_spot($result); $distance = sprintf("%05d", round(org_routamc_positioning_utils::get_distance($spot, $result_spot))); if (!$direct) { // Instantiate the real object as the result $result = new $class($result->parent); $result->spot = $result_spot; $result->latitude = $result_spot->latitude; $result->longitude = $result_spot->longitude; } $closest[$distance . $result->guid] = $result; } ksort($closest); reset($closest); while (count($closest) > $limit) { array_pop($closest); } setlocale(LC_NUMERIC, $current_locale); return $closest; }