View source
<?php
class GeoHash extends GeoAdapter {
private $table = "0123456789bcdefghjkmnpqrstuvwxyz";
private $neighbours = array(
'top' => array(
'even' => 'p0r21436x8zb9dcf5h7kjnmqesgutwvy',
'odd' => 'bc01fg45238967deuvhjyznpkmstqrwx',
),
'right' => array(
'even' => 'bc01fg45238967deuvhjyznpkmstqrwx',
'odd' => 'p0r21436x8zb9dcf5h7kjnmqesgutwvy',
),
'left' => array(
'even' => '238967debc01fg45kmstqrwxuvhjyznp',
'odd' => '14365h7k9dcfesgujnmqp0r2twvyx8zb',
),
'bottom' => array(
'even' => '14365h7k9dcfesgujnmqp0r2twvyx8zb',
'odd' => '238967debc01fg45kmstqrwxuvhjyznp',
),
);
private $borders = array(
'top' => array(
'even' => 'prxz',
'odd' => 'bcfguvyz',
),
'right' => array(
'even' => 'bcfguvyz',
'odd' => 'prxz',
),
'left' => array(
'even' => '0145hjnp',
'odd' => '028b',
),
'bottom' => array(
'even' => '028b',
'odd' => '0145hjnp',
),
);
public function read($hash, $as_grid = FALSE) {
$ll = $this
->decode($hash);
if (!$as_grid) {
return new Point($ll['medlon'], $ll['medlat']);
}
else {
return new Polygon(array(
new LineString(array(
new Point($ll['minlon'], $ll['maxlat']),
new Point($ll['maxlon'], $ll['maxlat']),
new Point($ll['maxlon'], $ll['minlat']),
new Point($ll['minlon'], $ll['minlat']),
new Point($ll['minlon'], $ll['maxlat']),
)),
));
}
}
public function write(Geometry $geometry, $precision = NULL) {
if ($geometry
->isEmpty()) {
return '';
}
if ($geometry
->geometryType() === 'Point') {
return $this
->encodePoint($geometry, $precision);
}
else {
$envelope = $geometry
->envelope();
$geohashes = array();
$geohash = '';
foreach ($envelope
->getPoints() as $point) {
$geohashes[] = $this
->encodePoint($point, 1.0E-7);
}
$i = 0;
while ($i < strlen($geohashes[0])) {
$char = $geohashes[0][$i];
foreach ($geohashes as $hash) {
if ($hash[$i] != $char) {
return $geohash;
}
}
$geohash .= $char;
$i++;
}
return $geohash;
}
}
private function encodePoint($point, $precision = NULL) {
if ($precision === NULL) {
$lap = strlen($point
->y()) - strpos($point
->y(), ".");
$lop = strlen($point
->x()) - strpos($point
->x(), ".");
$precision = pow(10, -max($lap - 1, $lop - 1, 0)) / 2;
}
$minlat = -90;
$maxlat = 90;
$minlon = -180;
$maxlon = 180;
$latE = 90;
$lonE = 180;
$i = 0;
$error = 180;
$hash = '';
while ($error >= $precision) {
$chr = 0;
for ($b = 4; $b >= 0; --$b) {
if ((1 & $b) == (1 & $i)) {
$next = ($minlon + $maxlon) / 2;
if ($point
->x() > $next) {
$chr |= pow(2, $b);
$minlon = $next;
}
else {
$maxlon = $next;
}
$lonE /= 2;
}
else {
$next = ($minlat + $maxlat) / 2;
if ($point
->y() > $next) {
$chr |= pow(2, $b);
$minlat = $next;
}
else {
$maxlat = $next;
}
$latE /= 2;
}
}
$hash .= $this->table[$chr];
$i++;
$error = min($latE, $lonE);
}
return $hash;
}
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;
}
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)];
}
}