Geohash::decode PHP Method

decode() public method

Decode a geohash and return an array with decimal lat,long in it
public decode ( $hash )
    public function decode($hash)
    {
        //decode hash into binary string
        $binary = "";
        $hl = strlen($hash);
        for ($i = 0; $i < $hl; $i++) {
            $binary .= $this->codingMap[substr($hash, $i, 1)];
        }
        //split the binary into lat and log binary strings
        $bl = strlen($binary);
        $blat = "";
        $blong = "";
        for ($i = 0; $i < $bl; $i++) {
            if ($i % 2) {
                $blat = $blat . substr($binary, $i, 1);
            } else {
                $blong = $blong . substr($binary, $i, 1);
            }
        }
        //now concert to decimal
        $lat = $this->binDecode($blat, -90, 90);
        $long = $this->binDecode($blong, -180, 180);
        //figure out how precise the bit count makes this calculation
        $latErr = $this->calcError(strlen($blat), -90, 90);
        $longErr = $this->calcError(strlen($blong), -180, 180);
        //how many decimal places should we use? There's a little art to
        //this to ensure I get the same roundings as geohash.org
        $latPlaces = max(1, -round(log10($latErr))) - 1;
        $longPlaces = max(1, -round(log10($longErr))) - 1;
        //round it
        $lat = round($lat, $latPlaces);
        $long = round($long, $longPlaces);
        return array($lat, $long);
    }

Usage Example

Ejemplo n.º 1
0
<?php

error_reporting(E_ALL);
echo "version:" . GEOHASH_VERSION . "\n";
echo microtime(true) . "\n";
$geohash = "wm3yr31d2524";
$coord = Geohash::decode($geohash);
print_r($coord);
var_dump($geohash, $coord);
$geohash = Geohash::encode($coord['latitude'], $coord['longitude']);
print_r(Geohash::encode(30.635780068114, 104.03160111979, 12));
var_dump($geohash);
echo microtime(true) . "\n";
All Usage Examples Of Geohash::decode