You are here

public function GeoHash::adjacent in geoPHP 7

Calculates the adjacent geohash of the geohash in the specified direction. This algorithm is available in various ports that seem to point back to geohash-js by David Troy under MIT notice.

Parameters

string $hash the geohash (lowercase):

string $direction the direction of the neighbor (top, bottom, left or right):

Return value

string the geohash of the adjacent cell

See also

https://github.com/davetroy/geohash-js

https://github.com/lyokato/objc-geohash

https://github.com/lyokato/libgeohash

https://github.com/masuidrive/pr_geohash

https://github.com/sunng87/node-geohash

https://github.com/davidmoten/geo

File

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

Class

GeoHash
PHP Geometry GeoHash encoder/decoder.

Code

public function adjacent($hash, $direction) {
  $last = substr($hash, -1);
  $type = strlen($hash) % 2 ? 'odd' : 'even';
  $base = substr($hash, 0, strlen($hash) - 1);
  if (strpos($this->borders[$direction][$type], $last) !== false) {
    $base = $this
      ->adjacent($base, $direction);
  }
  return $base . $this->table[strpos($this->neighbours[$direction][$type], $last)];
}