Skip to content

Commit

Permalink
Add TranverseMercator distance calculations to complement the existin…
Browse files Browse the repository at this point in the history
…g lat/long implementation
  • Loading branch information
dvdoug committed Sep 20, 2016
1 parent d695282 commit 45133c2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
21 changes: 21 additions & 0 deletions TransverseMercator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}
11 changes: 11 additions & 0 deletions tests/OSRefTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit 45133c2

Please sign in to comment.