diff --git a/src/Geodesy/Conversion/LLA2WebMercator.php b/src/Geodesy/Conversion/LLA2WebMercator.php new file mode 100644 index 0000000..5408ee1 --- /dev/null +++ b/src/Geodesy/Conversion/LLA2WebMercator.php @@ -0,0 +1,46 @@ +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; + + } + + +} \ No newline at end of file diff --git a/src/Geodesy/Conversion/WebMercator2LLA.php b/src/Geodesy/Conversion/WebMercator2LLA.php new file mode 100644 index 0000000..6d48e54 --- /dev/null +++ b/src/Geodesy/Conversion/WebMercator2LLA.php @@ -0,0 +1,49 @@ +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; + + } + + +} \ No newline at end of file diff --git a/src/Geodesy/Location/WebMercator.php b/src/Geodesy/Location/WebMercator.php new file mode 100644 index 0000000..e50e6fb --- /dev/null +++ b/src/Geodesy/Location/WebMercator.php @@ -0,0 +1,59 @@ +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; + } + +} \ No newline at end of file