-
Notifications
You must be signed in to change notification settings - Fork 5
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
3 changed files
with
154 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Geodesy\Conversion; | ||
|
||
use Geodesy\Location\WebMercator; | ||
use Geodesy\Location\LatLong; | ||
use Geodesy\Datum\WGS84; | ||
|
||
class LLA2WebMercator extends BaseConversion implements ConversionInterface | ||
{ | ||
|
||
protected $latlong; | ||
|
||
protected $webmercator; | ||
|
||
protected $datum; | ||
|
||
public function __construct(LatLong $latlong) | ||
{ | ||
parent::__construct(); | ||
$this->webmercator = new WebMercator; | ||
$this->latlong = $latlong; | ||
$this->datum = new WGS84; // always set it to WGS84 when converting from LLA to WebMercator | ||
} | ||
|
||
public function convert(): WebMercator | ||
{ | ||
|
||
$lat = deg2rad($this->latlong->getLatitude()); | ||
|
||
$long = deg2rad($this->latlong->getLongitude()); | ||
|
||
$f = $this->datum->getSemiMajorAxis(); | ||
|
||
$sinLat = sin($lat); | ||
|
||
$this->webmercator->setX($this->getUnit()->convert(($f * 0.5) * log((1.0 + $sinLat) / (1.0 - $sinLat)))); | ||
|
||
$this->webmercator->setY($this->getUnit()->convert($f * $long)); | ||
|
||
return $this->webmercator; | ||
|
||
} | ||
|
||
|
||
} |
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,49 @@ | ||
<?php | ||
|
||
namespace Geodesy\Conversion; | ||
|
||
use Geodesy\Location\WebMercator; | ||
use Geodesy\Location\LatLong; | ||
use Geodesy\Datum\WGS84; | ||
|
||
class WebMercator2LLA extends BaseConversion implements ConversionInterface | ||
{ | ||
|
||
protected $latlong; | ||
|
||
protected $webmercator; | ||
|
||
protected $datum; | ||
|
||
public function __construct(WebMercator $webmercator) | ||
{ | ||
parent::__construct(); | ||
$this->webmercator = $webmercator; | ||
$this->latlong = new LatLong; | ||
$this->datum = $this->webmercator->getReference(); | ||
} | ||
|
||
public function convert(): LatLong | ||
{ | ||
|
||
$x = $this->webmercator->getX(); | ||
$y = $this->webmercator->getY(); | ||
|
||
$f = $this->datum->getSemiMajorAxis(); | ||
|
||
$lat = $x / $f; | ||
|
||
$long = (pi() / 2) - (2 * atan(exp(-1.0 * $y / $f))); | ||
|
||
$this->latlong->setReference($this->datum); | ||
|
||
$this->latlong->setLatitude(rad2deg($lat)); | ||
|
||
$this->latlong->setLongitude(rad2deg($long)); | ||
|
||
return $this->latlong; | ||
|
||
} | ||
|
||
|
||
} |
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,59 @@ | ||
<?php | ||
|
||
namespace Geodesy\Location; | ||
|
||
use Geodesy\Datum\WGS84; | ||
use Geodesy\Datum\DatumInterface; | ||
|
||
class WebMercator | ||
{ | ||
/** | ||
* Web Mercator, Google Web Mercator, Spherical Mercator, WGS 84 Web Mercator[1] or WGS 84/Pseudo-Mercator is a variant of the Mercator | ||
* projection and is the de facto standard for Web mapping applications. | ||
* X is easting and Y is northing. | ||
*/ | ||
private $x; | ||
|
||
private $y; | ||
|
||
private $referenceDatum; | ||
|
||
public function __construct() | ||
{ | ||
$this->x = null; | ||
$this->y = null; | ||
$this->referenceDatum = new WGS84; | ||
} | ||
|
||
public function getReference(): WGS84 | ||
{ | ||
return $this->referenceDatum; | ||
} | ||
|
||
public function setX(float $x) | ||
{ | ||
$this->x = $x; | ||
} | ||
|
||
public function setY(float $y) | ||
{ | ||
$this->y = $y; | ||
} | ||
|
||
public function getX(): float | ||
{ | ||
if($this->x === null) { | ||
throw new \Exception('X is not set.'); | ||
} | ||
return $this->x; | ||
} | ||
|
||
public function getY(): float | ||
{ | ||
if($this->y === null) { | ||
throw new \Exception('Y is not set.'); | ||
} | ||
return $this->y; | ||
} | ||
|
||
} |