generated from igzard/php-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
241 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Igzard\Geohash\Domain; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class GeohashCalculator | ||
{ | ||
private const string BASE32 = '0123456789bcdefghjkmnpqrstuvwxyz'; | ||
|
||
private array $latitudeInterval = [-90.0, 90.0]; | ||
private array $longitudeInterval = [-180.0, 180.0]; | ||
private int $precision = 12; | ||
|
||
public function withLatitudeInterval(float $from, float $to): self | ||
{ | ||
$this->latitudeInterval = [$from, $to]; | ||
|
||
return $this; | ||
} | ||
|
||
public function withLongitudeInterval(float $from, float $to): self | ||
{ | ||
$this->longitudeInterval = [$from, $to]; | ||
|
||
return $this; | ||
} | ||
|
||
public function withPrecision(int $precision): self | ||
{ | ||
$this->precision = $precision; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Calculate geohash from latitude and longitude with precision. | ||
* | ||
* @param float $latitude Latitude | ||
* @param float $longitude Longitude | ||
* | ||
* @return string Geohash | ||
*/ | ||
public function calculate(float $latitude, float $longitude): string | ||
{ | ||
$result = ''; | ||
|
||
$isEven = true; | ||
$bit = 0; | ||
$ch = 0; | ||
|
||
while (\strlen($result) < $this->precision) { | ||
if ($isEven) { | ||
$this->refineInterval($this->longitudeInterval, $longitude, $ch, $bit); | ||
} else { | ||
$this->refineInterval($this->latitudeInterval, $latitude, $ch, $bit); | ||
} | ||
|
||
$isEven = !$isEven; | ||
|
||
if ($bit < 4) { | ||
++$bit; | ||
} else { | ||
$result .= self::BASE32[$ch]; | ||
$bit = 0; | ||
$ch = 0; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Refine interval. | ||
* | ||
* @param array $interval Interval | ||
* @param float $value Value | ||
* @param int $ch Ch | ||
* @param int $bit Bit | ||
*/ | ||
private function refineInterval(array &$interval, float $value, int &$ch, int $bit): void | ||
{ | ||
$mid = ($interval[0] + $interval[1]) / 2; | ||
|
||
if ($value > $mid) { | ||
$ch |= (1 << (4 - $bit)); | ||
$interval[0] = $mid; | ||
} else { | ||
$interval[1] = $mid; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Igzard\Geohash; | ||
|
||
use Igzard\Geohash\Domain\GeohashCalculator; | ||
|
||
final class Geohash | ||
{ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
protected function __clone() | ||
{ | ||
} | ||
|
||
/** | ||
* Generate geohash from latitude and longitude. | ||
* You can customize the precision, longitude and latitude intervals in config. | ||
* | ||
* @param float $latitude Latitude | ||
* @param float $longitude Longitude | ||
* @param array $config ['precision' => 12, 'longitude_interval' => [-180.0, 180.0], 'latitude_interval' => [-90.0, 90.0]] | ||
* | ||
* @return string Geohash | ||
*/ | ||
public static function generate(float $latitude, float $longitude, array $config = []): string | ||
{ | ||
$precision = $config['precision'] ?? 12; | ||
$longitudeInterval = $config['longitude_interval'] ?? [-180.0, 180.0]; | ||
$latitudeInterval = $config['latitude_interval'] ?? [-90.0, 90.0]; | ||
|
||
return (new GeohashCalculator()) | ||
->withLongitudeInterval($longitudeInterval[0], $longitudeInterval[1]) | ||
->withLatitudeInterval($latitudeInterval[0], $latitudeInterval[1]) | ||
->withPrecision($precision) | ||
->calculate($latitude, $longitude); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Igzard\Geohash\Geohash; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class GeohashTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider locationDataProvider | ||
*/ | ||
public function testAssertWithDefaultData(float $latitude, float $longitude, string $expected): void | ||
{ | ||
$this->assertEquals( | ||
$expected, | ||
Geohash::generate($latitude, $longitude) | ||
); | ||
} | ||
|
||
public function locationDataProvider(): array | ||
{ | ||
return [ | ||
'case 1# Budapest' => [ | ||
'latitude' => 47.497913, | ||
'longitude' => 19.040236, | ||
'expected' => 'u2mw1q8xmssz', | ||
], | ||
'case 2# New York' => [ | ||
'longitude' => 40.730610, | ||
'latitude' => -73.935242, | ||
'expected' => 'dr5rtwccpbpb', | ||
], | ||
'case 2# Moscow' => [ | ||
'longitude' => 55.751244, | ||
'latitude' => 37.618423, | ||
'expected' => 'ucfv0j0ysc1k', | ||
], | ||
]; | ||
} | ||
} |