Skip to content

Commit

Permalink
Added LLA2WebMercator conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jtejido committed Apr 29, 2018
1 parent 24e17fc commit 7541116
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Geodesy/Conversion/LLA2WebMercator.php
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;

}


}
49 changes: 49 additions & 0 deletions src/Geodesy/Conversion/WebMercator2LLA.php
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;

}


}
59 changes: 59 additions & 0 deletions src/Geodesy/Location/WebMercator.php
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;
}

}

0 comments on commit 7541116

Please sign in to comment.