Skip to content

Commit 7541116

Browse files
committed
Added LLA2WebMercator conversion
1 parent 24e17fc commit 7541116

3 files changed

Lines changed: 154 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Geodesy\Conversion;
4+
5+
use Geodesy\Location\WebMercator;
6+
use Geodesy\Location\LatLong;
7+
use Geodesy\Datum\WGS84;
8+
9+
class LLA2WebMercator extends BaseConversion implements ConversionInterface
10+
{
11+
12+
protected $latlong;
13+
14+
protected $webmercator;
15+
16+
protected $datum;
17+
18+
public function __construct(LatLong $latlong)
19+
{
20+
parent::__construct();
21+
$this->webmercator = new WebMercator;
22+
$this->latlong = $latlong;
23+
$this->datum = new WGS84; // always set it to WGS84 when converting from LLA to WebMercator
24+
}
25+
26+
public function convert(): WebMercator
27+
{
28+
29+
$lat = deg2rad($this->latlong->getLatitude());
30+
31+
$long = deg2rad($this->latlong->getLongitude());
32+
33+
$f = $this->datum->getSemiMajorAxis();
34+
35+
$sinLat = sin($lat);
36+
37+
$this->webmercator->setX($this->getUnit()->convert(($f * 0.5) * log((1.0 + $sinLat) / (1.0 - $sinLat))));
38+
39+
$this->webmercator->setY($this->getUnit()->convert($f * $long));
40+
41+
return $this->webmercator;
42+
43+
}
44+
45+
46+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Geodesy\Conversion;
4+
5+
use Geodesy\Location\WebMercator;
6+
use Geodesy\Location\LatLong;
7+
use Geodesy\Datum\WGS84;
8+
9+
class WebMercator2LLA extends BaseConversion implements ConversionInterface
10+
{
11+
12+
protected $latlong;
13+
14+
protected $webmercator;
15+
16+
protected $datum;
17+
18+
public function __construct(WebMercator $webmercator)
19+
{
20+
parent::__construct();
21+
$this->webmercator = $webmercator;
22+
$this->latlong = new LatLong;
23+
$this->datum = $this->webmercator->getReference();
24+
}
25+
26+
public function convert(): LatLong
27+
{
28+
29+
$x = $this->webmercator->getX();
30+
$y = $this->webmercator->getY();
31+
32+
$f = $this->datum->getSemiMajorAxis();
33+
34+
$lat = $x / $f;
35+
36+
$long = (pi() / 2) - (2 * atan(exp(-1.0 * $y / $f)));
37+
38+
$this->latlong->setReference($this->datum);
39+
40+
$this->latlong->setLatitude(rad2deg($lat));
41+
42+
$this->latlong->setLongitude(rad2deg($long));
43+
44+
return $this->latlong;
45+
46+
}
47+
48+
49+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Geodesy\Location;
4+
5+
use Geodesy\Datum\WGS84;
6+
use Geodesy\Datum\DatumInterface;
7+
8+
class WebMercator
9+
{
10+
/**
11+
* Web Mercator, Google Web Mercator, Spherical Mercator, WGS 84 Web Mercator[1] or WGS 84/Pseudo-Mercator is a variant of the Mercator
12+
* projection and is the de facto standard for Web mapping applications.
13+
* X is easting and Y is northing.
14+
*/
15+
private $x;
16+
17+
private $y;
18+
19+
private $referenceDatum;
20+
21+
public function __construct()
22+
{
23+
$this->x = null;
24+
$this->y = null;
25+
$this->referenceDatum = new WGS84;
26+
}
27+
28+
public function getReference(): WGS84
29+
{
30+
return $this->referenceDatum;
31+
}
32+
33+
public function setX(float $x)
34+
{
35+
$this->x = $x;
36+
}
37+
38+
public function setY(float $y)
39+
{
40+
$this->y = $y;
41+
}
42+
43+
public function getX(): float
44+
{
45+
if($this->x === null) {
46+
throw new \Exception('X is not set.');
47+
}
48+
return $this->x;
49+
}
50+
51+
public function getY(): float
52+
{
53+
if($this->y === null) {
54+
throw new \Exception('Y is not set.');
55+
}
56+
return $this->y;
57+
}
58+
59+
}

0 commit comments

Comments
 (0)