/** * * @param Array $coordinates Contains latitude and longitude values * @return Array containing geocoded information */ function reverse_geocode($coordinates, $options = array()) { $results = array(); $parameters = array('radius' => 10, 'maxRows' => 20, 'style' => 'FULL'); if (!empty($options)) { foreach ($options as $key => $value) { if (isset($parameters[$key])) { $parameters[$key] = $value; } } } if (!isset($coordinates['latitude']) && !isset($coordinates['longitude'])) { $this->error = 'POSITIONING_MISSING_ATTRIBUTES'; return null; } $params = array(); $params[] = 'lat=' . urlencode($coordinates['latitude']); $params[] = 'lng=' . urlencode($coordinates['longitude']); foreach ($parameters as $key => $value) { if (!is_null($value)) { $params[] = "{$key}=" . urlencode($value); } } $http_request = new org_openpsa_httplib(); $url = 'http://ws.geonames.org/findNearbyPlaceName?' . implode('&', $params); $response = $http_request->get($url); $simplexml = simplexml_load_string($response); if (!isset($simplexml->geoname) || count($simplexml->geoname) == 0) { $this->error = 'POSITIONING_DETAILS_NOT_FOUND'; if (isset($simplexml->status)) { $constant_name = strtoupper(str_replace(" ", "_", $simplexml->status)); $this->error = $constant_name; } return null; } for ($i = 0; $i < $parameters['maxRows']; $i++) { if (!isset($simplexml->geoname[$i])) { break; } $entry = $simplexml->geoname[$i]; $entry_coordinates = array('latitude' => (double) $entry->lat, 'longitude' => (double) $entry->lng); $meters = round(org_routamc_positioning_utils::get_distance($coordinates, $entry_coordinates) * 1000); $entry_meters = round((double) $entry->distance * 1000); if ($entry_meters < $meters) { $meters = $entry_meters; } $position = array(); $position['latitude'] = (double) $entry->lat; $position['longitude'] = (double) $entry->lng; $position['distance'] = array('meters' => $meters, 'bearing' => org_routamc_positioning_utils::get_bearing($coordinates, $entry_coordinates)); $position['city'] = (string) $entry->name; $position['region'] = (string) $entry->adminName2; $position['country'] = (string) $entry->countryCode; $position['postalcode'] = (string) $entry->postalcode; $position['alternate_names'] = (string) $entry->alternateNames; $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_GPS; $results[] = $position; } return $results; }
?> " alt="<?php echo $view['title']; ?> " border="0"></a> </div> <div class="taken location"> <?php $coordinates = null; if ($GLOBALS['midcom_config']['positioning_enable'] && class_exists('org_routamc_positioning_object') && $data['photo']->photographer) { $position_object = new org_routamc_positioning_object($data['photo']); $coordinates = $position_object->get_coordinates($data['photo']->photographer, $data['photo']->taken); } if ($coordinates && $coordinates['latitude'] && $coordinates['longitude']) { echo sprintf($data['l10n']->get('taken on %s in %s'), strftime('%d.%m.%Y %H:%M', $data['photo']->taken), org_routamc_positioning_utils::pretty_print_location($coordinates['latitude'], $coordinates['longitude'])); } else { echo sprintf('Otettu %s', strftime('%d.%m.%Y %H:%M', $data['photo']->taken)); } ?> </div> <?php if ($data['topic']->component == 'org.routamc.gallery') { if ($data['topic']->component == 'org.routamc.gallery') { $photostream_topic = midcom_helper_find_node_by_component('org.routamc.photostream'); if ($photostream_topic) { $photostream_prefix = $photostream_topic['18']; } } if (!isset($photostream_prefix)) { $photostream_prefix = str_replace($data['topic']->name . '/', '', $prefix);
<?php midcom::get('auth')->require_valid_user(); $user = midcom::get('auth')->user->get_storage(); $html = org_routamc_positioning_importer::create('html'); $coordinates = $html->get_icbm_location($user); if ($coordinates) { echo sprintf('According to ICBM URL your position is %s', org_routamc_positioning_utils::microformat_location($coordinates['latitude'], $coordinates['longitude'])); } else { echo "Failed to get position, last error is {$html->error}"; }
<?php midcom::get()->finish(); _midcom_stop_request(); } elseif (isset($_GET['f']) && $_GET['f'] == 'callback') { // the user has authorized us at FE, so now we can pick up our access token + secret if (!$session->exists('auth_state') || $session->get('auth_state') != 'start') { throw new midcom_error("Out of sequence."); } $fireeagle = new FireEagle($fireeagle_consumer_key, $fireeagle_consumer_secret, $session->get('request_token'), $session->get('request_secret')); $access_token = $fireeagle->getAccessToken(); if (!isset($access_token['oauth_token']) || !is_string($access_token['oauth_token']) || !isset($access_token['oauth_token_secret']) || !is_string($access_token['oauth_token_secret'])) { throw new midcom_error("Failed to get FireEagle access token\n"); } $user->set_parameter('net.yahoo.fireeagle', 'access_key', $access_token['oauth_token']); $user->set_parameter('net.yahoo.fireeagle', 'access_secret', $access_token['oauth_token_secret']); midcom::get()->relocate($_SERVER['SCRIPT_NAME']); // This will exit } ?> <p><a href="?f=start">Start Fire Eagle authentication</a></p> <?php midcom::get()->finish(); _midcom_stop_request(); } $fireeagle = org_routamc_positioning_importer::create('fireeagle'); $coordinates = $fireeagle->get_fireeagle_location($user); if ($coordinates) { echo sprintf('According to Fire Eagle your position since %s is %s', strftime('%x %X', $coordinates['date']), org_routamc_positioning_utils::microformat_location($coordinates['latitude'], $coordinates['longitude'])); } else { echo "Failed to get position, last error is {$fireeagle->error} {$fireeagle->error_string}"; }
if (array_key_exists('country', $_POST)) { $manual_position['country'] = $_POST['country']; } if (array_key_exists('latitude', $_POST) && !empty($_POST['latitude'])) { $manual_position['latitude'] = $_POST['latitude']; } if (array_key_exists('longitude', $_POST) && !empty($_POST['longitude'])) { $manual_position['longitude'] = $_POST['longitude']; } $import = $manual->import($manual_position); echo $manual->error . "<br />\n"; } $user_position = new org_routamc_positioning_person($user); $coordinates = $user_position->get_coordinates(); if ($coordinates) { echo "<p>" . sprintf('According to Midgard your position is now %s', org_routamc_positioning_utils::pretty_print_coordinates($coordinates['latitude'], $coordinates['longitude'])) . "</p>\n"; } ?> <form method="post"> <label>Street <input type="text" name="street" value="Valhallankatu" /></label>, <label>City <input type="text" name="city" value="Helsinki" /></label> and <label>Country <input type="text" name="country" value="FI" /></label><br /> <label> Geocoder <select name="geocoder"> <option value="city">Local city database</option> <option value="geonames">GeoNames</option> </select> </label> <p>OR</p> <label>Latitude <input type="text" name="latitude" /></label>
$user = midcom::get('auth')->user->get_storage(); $user_position = new org_routamc_positioning_person($user); $coordinates = $user_position->get_coordinates(); if (!$coordinates) { throw new midcom_error("Failed to get your current position."); } echo "<p>" . sprintf('Your position is %s', org_routamc_positioning_utils::pretty_print_coordinates($coordinates['latitude'], $coordinates['longitude'])) . "</p>\n"; $run_times = 1; $run = 0; $total_time = 0; while ($run < $run_times) { $run++; $start = microtime(); $closest = org_routamc_positioning_utils::get_closest('org_routamc_positioning_city_dba', $coordinates, 10); echo "<p>Closest places to you are:<br />"; echo "<ol>\n"; foreach ($closest as $city) { $city_coordinates = array('latitude' => $city->latitude, 'longitude' => $city->longitude); echo "<li>{$city->city}, {$city->country} is " . round(org_routamc_positioning_utils::get_distance($coordinates, $city_coordinates)) . " kilometers " . org_routamc_positioning_utils::get_bearing($coordinates, $city_coordinates) . " from you</li>"; } echo "</ol>\n"; $end = microtime(); $end_parts = explode(' ', $end); $end = $end_parts[1] + $end_parts[0]; $start_parts = explode(' ', $start); $start = $start_parts[1] + $start_parts[0]; $time_used = $end - $start; $total_time = $time_used + $total_time; } $average = $total_time / $run_times; echo "<p>Query took {$total_time} seconds (on average {$average} seconds per query).</p>";
/** * 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; }
/** * @param Array $coordinates Contains latitude and longitude values * @param Array $options * @return Array containing geocoded information */ function reverse_geocode($coordinates, $options = array()) { $results = array(); $parameters = array('maxRows' => 1); if (!empty($options)) { foreach ($options as $key => $value) { if (isset($parameters[$key])) { $parameters[$key] = $value; } } } if (!isset($coordinates['latitude']) && !isset($coordinates['longitude'])) { $this->error = 'POSITIONING_MISSING_ATTRIBUTES'; return null; } $closest = org_routamc_positioning_utils::get_closest('org_routamc_positioning_city_dba', $coordinates, $parameters['maxRows']); if (empty($closest)) { $this->error = 'POSITIONING_DETAILS_NOT_FOUND'; return null; } foreach ($closest as $city) { $city_coordinates = array('latitude' => $city->latitude, 'longitude' => $city->longitude); $position = array(); $position['latitude'] = $city->latitude; $position['longitude'] = $city->longitude; $position['distance'] = array('meters' => round(org_routamc_positioning_utils::get_distance($coordinates, $city_coordinates) * 1000), 'bearing' => org_routamc_positioning_utils::get_bearing($coordinates, $city_coordinates)); $position['city'] = $city->city; $position['region'] = $city->region; $position['country'] = $city->country; $position['alternate_names'] = $city->alternatenames; $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_GPS; $results[] = $position; } return $results; }
/** * 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; }
function get_city_string() { return org_routamc_positioning_utils::pretty_print_location($this->latitude, $this->longitude); }
function convert_to_html() { $result = ''; $adr_properties = array(); if ($this->location->description) { $adr_properties[] = "<span class=\"description\">{$this->location->description}</span>"; } if ($this->location->text) { $adr_properties[] = "<span class=\"text\">{$this->location->text}</span>"; } if ($this->location->room) { $adr_properties[] = "<span class=\"room\">{$this->location->room}</span>"; } if ($this->location->street) { $adr_properties[] = "<span class=\"street-address\">{$this->location->street}</span>"; } if ($this->location->postalcode) { $adr_properties[] = "<span class=\"postal-code\">{$this->location->postalcode}</span>"; } if ($this->location->city) { $city = new org_routamc_positioning_city_dba($this->location->city); $adr_properties[] = "<span class=\"locality\">{$city->city}</span>"; } if ($this->location->region) { $adr_properties[] = "<span class=\"region\">{$this->location->region}</span>"; } if ($this->location->country) { $adr_properties[] = "<span class=\"country-name\">{$this->location->country}</span>"; } if (count($adr_properties) > 0) { $result .= '<span class="adr">' . implode(', ', $adr_properties) . "</span>\n"; } $latitude_string = org_routamc_positioning_utils::pretty_print_coordinate($this->location->latitude); $latitude_string .= $this->location->latitude > 0 ? " N" : " S"; $longitude_string = org_routamc_positioning_utils::pretty_print_coordinate($this->location->longitude); $longitude_string .= $this->location->longitude > 0 ? " E" : " W"; $style = ''; if (!empty($result)) { $style = ' style="display: none;"'; } $result .= "<span class=\"geo\"{$style}>\n"; $result .= " <abbr class=\"latitude\" title=\"{$this->location->latitude}\">{$latitude_string}</abbr>\n"; $result .= " <abbr class=\"longitude\" title=\"{$this->location->longitude}\">{$longitude_string}</abbr>\n"; $result .= "</span>\n"; // TODO: Add Microformat for civic location return $result; }