-
Notifications
You must be signed in to change notification settings - Fork 29
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
1 parent
b5f8820
commit 840bd2c
Showing
8 changed files
with
184 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Geo Class | ||
|
||
## Center calculation | ||
|
||
```php | ||
$geo = new \Geo\Geocoder\GeoCalculator(); | ||
$coordinates = [ | ||
new GeoCoordinate(48.1, 17.2), | ||
new GeoCoordinate(48.5, 17.1), | ||
new GeoCoordinate(48.8, 16.8), | ||
]; | ||
$centralCoordinate = $geo->getCentralGeoCoordinate(array $coordinates); | ||
``` |
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
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,48 @@ | ||
<?php | ||
|
||
namespace Geo\Geocoder; | ||
|
||
use RuntimeException; | ||
|
||
class GeoCalculator { | ||
|
||
/** | ||
* @param array<\Geo\Geocoder\GeoCoordinate> $geoCoordinates | ||
* | ||
* @return \Geo\Geocoder\GeoCoordinate | ||
*/ | ||
public static function getCentralGeoCoordinate(array $geoCoordinates): GeoCoordinate { | ||
if (!$geoCoordinates) { | ||
throw new RuntimeException('No geo coordinates provided'); | ||
} | ||
if (count($geoCoordinates) === 1) { | ||
return array_shift($geoCoordinates); | ||
} | ||
|
||
$x = 0; | ||
$y = 0; | ||
$z = 0; | ||
|
||
foreach ($geoCoordinates as $geoCoordinate) { | ||
$latitude = $geoCoordinate->getLatitude() * M_PI / 180; | ||
$longitude = $geoCoordinate->getLongitude() * M_PI / 180; | ||
|
||
$x += cos($latitude) * cos($longitude); | ||
$y += cos($latitude) * sin($longitude); | ||
$z += sin($latitude); | ||
} | ||
|
||
$total = count($geoCoordinates); | ||
|
||
$x /= $total; | ||
$y /= $total; | ||
$z /= $total; | ||
|
||
$centralLongitude = atan2($y, $x); | ||
$centralSquareRoot = sqrt($x * $x + $y * $y); | ||
$centralLatitude = atan2($z, $centralSquareRoot); | ||
|
||
return new GeoCoordinate($centralLatitude * 180 / M_PI, $centralLongitude * 180 / M_PI); | ||
} | ||
|
||
} |
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,65 @@ | ||
<?php | ||
|
||
namespace Geo\Geocoder; | ||
|
||
class GeoCoordinate implements \Stringable { | ||
|
||
protected float $latitude; | ||
|
||
protected float $longitude; | ||
|
||
/** | ||
* @param float $latitude | ||
* @param float $longitude | ||
*/ | ||
public function __construct(float $latitude, float $longitude) { | ||
$this->latitude = $latitude; | ||
$this->longitude = $longitude; | ||
} | ||
|
||
/** | ||
* @return float | ||
*/ | ||
public function getLatitude(): float { | ||
return $this->latitude; | ||
} | ||
|
||
/** | ||
* @return float | ||
*/ | ||
public function getLongitude(): float { | ||
return $this->longitude; | ||
} | ||
|
||
/** | ||
* @param bool $abbr | ||
* | ||
* @return array<string, float> | ||
*/ | ||
public function toArray(bool $abbr = false): array { | ||
return [ | ||
($abbr ? 'lat' : 'latitude') => $this->latitude, | ||
($abbr ? 'lng' : 'longitude') => $this->longitude, | ||
]; | ||
} | ||
|
||
/** | ||
* @param array<string, float> $data | ||
* | ||
* @return static | ||
*/ | ||
public static function fromArray(array $data): static { | ||
$lat = $data['latitude'] ?? $data['lat']; | ||
$lng = $data['longitude'] ?? $data['lng']; | ||
|
||
return new static($lat, $lng); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function __toString(): string { | ||
return $this->latitude . ',' . $this->longitude; | ||
} | ||
|
||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace Geo\Test\Geocoder; | ||
|
||
use Cake\TestSuite\TestCase; | ||
use Geo\Geocoder\GeoCalculator; | ||
use Geo\Geocoder\GeoCoordinate; | ||
|
||
class GeoCalculatorTest extends TestCase { | ||
|
||
/** | ||
* @var \Geocoder\Provider\Provider | ||
*/ | ||
protected $Geocoder; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testGetCentralGeoCoordinate() { | ||
$coordinates = [ | ||
new GeoCoordinate(48.1, 17.2), | ||
]; | ||
$result = GeoCalculator::getCentralGeoCoordinate($coordinates); | ||
$this->assertEquals($coordinates[0], $result); | ||
|
||
$coordinates = [ | ||
new GeoCoordinate(48.1, 17.2), | ||
new GeoCoordinate(48.5, 17.1), | ||
new GeoCoordinate(48.8, 16.8), | ||
]; | ||
$result = GeoCalculator::getCentralGeoCoordinate($coordinates); | ||
$this->assertWithinRange(48.46, $result->getLatitude(), 0.01); | ||
$this->assertWithinRange(17.03, $result->getLongitude(), 0.01); | ||
} | ||
|
||
} |