diff --git a/TransverseMercator.php b/TransverseMercator.php index 6ff76f390..a6eb227e9 100644 --- a/TransverseMercator.php +++ b/TransverseMercator.php @@ -257,4 +257,25 @@ public function convertToLatitudeLongitude($N, $E, $N0, $E0, $phi0, $lambda0) return new LatLng(rad2deg($phi), rad2deg($lambda), 0, $refEll); } + + /** + * Calculate the surface distance between this object and the one + * passed in as a parameter. + * + * @param self $to object to measure the distance to + * @return float + */ + public function distance(self $to) { + + if ($this->refEll != $to->refEll) { + throw new \RuntimeException('Source and destination co-ordinates are not using the same ellipsoid'); + } + + //Because this is a 2D grid, we can use simple Pythagoras + $distanceX = $to->getX()-$this->getX(); + $distanceY = $to->getY()-$this->getY(); + + return pow(pow($distanceX, 2) + pow($distanceY, 2), 0.5); + + } } diff --git a/tests/OSRefTest.php b/tests/OSRefTest.php index 8a4bd44e9..dae8c519a 100644 --- a/tests/OSRefTest.php +++ b/tests/OSRefTest.php @@ -142,4 +142,15 @@ public function testGetters() { self::assertEquals(RefEll::airy1830(), $osRef->getRefEll()); } + public function testDistance() { + //old OS HQ + $from = new OSRef(438700, 114800, 0, RefEll::airy1830()); + + //Tower of London + $to = new OSRef(533600, 180500, 0, RefEll::airy1830()); + + $distance = round($from->distance($to)); + self::assertEquals(115423, $distance); + } + }