You are here

private function GeoHash::decode in geoPHP 7

Same name and namespace in other branches
  1. 8 geoPHP/lib/adapters/GeoHash.class.php \GeoHash::decode()

@author algorithm based on code by Alexander Songe <a@songe.me>

Parameters

string $hash a geohash:

See also

https://github.com/asonge/php-geohash/issues/1

1 call to GeoHash::decode()
GeoHash::read in geoPHP/lib/adapters/GeoHash.class.php
Convert the geohash to a Point. The point is 2-dimensional.

File

geoPHP/lib/adapters/GeoHash.class.php, line 186

Class

GeoHash
PHP Geometry GeoHash encoder/decoder.

Code

private function decode($hash) {
  $ll = array();
  $minlat = -90;
  $maxlat = 90;
  $minlon = -180;
  $maxlon = 180;
  $latE = 90;
  $lonE = 180;
  for ($i = 0, $c = strlen($hash); $i < $c; $i++) {
    $v = strpos($this->table, $hash[$i]);
    if (1 & $i) {
      if (16 & $v) {
        $minlat = ($minlat + $maxlat) / 2;
      }
      else {
        $maxlat = ($minlat + $maxlat) / 2;
      }
      if (8 & $v) {
        $minlon = ($minlon + $maxlon) / 2;
      }
      else {
        $maxlon = ($minlon + $maxlon) / 2;
      }
      if (4 & $v) {
        $minlat = ($minlat + $maxlat) / 2;
      }
      else {
        $maxlat = ($minlat + $maxlat) / 2;
      }
      if (2 & $v) {
        $minlon = ($minlon + $maxlon) / 2;
      }
      else {
        $maxlon = ($minlon + $maxlon) / 2;
      }
      if (1 & $v) {
        $minlat = ($minlat + $maxlat) / 2;
      }
      else {
        $maxlat = ($minlat + $maxlat) / 2;
      }
      $latE /= 8;
      $lonE /= 4;
    }
    else {
      if (16 & $v) {
        $minlon = ($minlon + $maxlon) / 2;
      }
      else {
        $maxlon = ($minlon + $maxlon) / 2;
      }
      if (8 & $v) {
        $minlat = ($minlat + $maxlat) / 2;
      }
      else {
        $maxlat = ($minlat + $maxlat) / 2;
      }
      if (4 & $v) {
        $minlon = ($minlon + $maxlon) / 2;
      }
      else {
        $maxlon = ($minlon + $maxlon) / 2;
      }
      if (2 & $v) {
        $minlat = ($minlat + $maxlat) / 2;
      }
      else {
        $maxlat = ($minlat + $maxlat) / 2;
      }
      if (1 & $v) {
        $minlon = ($minlon + $maxlon) / 2;
      }
      else {
        $maxlon = ($minlon + $maxlon) / 2;
      }
      $latE /= 4;
      $lonE /= 8;
    }
  }
  $ll['minlat'] = $minlat;
  $ll['minlon'] = $minlon;
  $ll['maxlat'] = $maxlat;
  $ll['maxlon'] = $maxlon;
  $ll['medlat'] = round(($minlat + $maxlat) / 2, max(1, -round(log10($latE))) - 1);
  $ll['medlon'] = round(($minlon + $maxlon) / 2, max(1, -round(log10($lonE))) - 1);
  return $ll;
}