Skip to content

Commit

Permalink
Use atan2 rather than atan when calculating longitude after Helmert t…
Browse files Browse the repository at this point in the history
…ransform to ensure the co-ordinate ends up in the right quadrant. Fixes #4, fixes #5
  • Loading branch information
dvdoug committed Dec 31, 2015
1 parent 6ea75a2 commit 07daecd
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
2 changes: 1 addition & 1 deletion LatLng.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public function transformDatum(RefEll $aFromEllipsoid, RefEll $aToEllipsoid, $aT
$b = $aToEllipsoid->min;
$eSquared = $aToEllipsoid->ecc;

$lambdaB = rad2deg(atan($yB / $xB));
$lambdaB = rad2deg(atan2($yB, $xB));
$p = sqrt(($xB * $xB) + ($yB * $yB));
$phiN = atan($zB / ($p * (1 - $eSquared)));
for ($i = 1; $i < 10; $i++) {
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ Output of calculations have been changed from the original code in the following
* When calculating surface distances, a more accurate mean radius is now used rather than
that derived from historical definitions of a nautical mile
* Corrected calculation of OS 6-figure grid references (rounding instead of truncating meant the
grid square was sometimes off by 1)
grid square was sometimes off by 1)
* Corrected issue with Helmert transform where the resulting co-ordinate could be placed into
the wrong quadrant

Usage
-----
Expand All @@ -57,4 +59,4 @@ Requirements

License
-------
The original PHPcoord is GPL-licensed, and this version inherits that. Terms can be found at http://www.gnu.org/licenses/gpl.html
The original PHPcoord is GPL-licensed, and this version inherits that. Terms can be found at http://www.gnu.org/licenses/gpl.html
68 changes: 67 additions & 1 deletion tests/LatLngTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,70 @@ public function testDistanceDifferentEllipsoids() {
self::assertEquals($expected, $work->distance($charingCross));
}

}
public function testNoopTransformSanFrancisco()
{
$LatLng = new LatLng(37.783333, -122.416667);
$LatLngTrans=clone $LatLng;

$LatLngTrans->transformDatum(RefEll::GRS80(), RefEll::GRS80(), 0, 0, 0, 0, 0, 0, 0);

$this->assertEquals($LatLng->lat,$LatLngTrans->lat,'Latitude transform failed');
$this->assertEquals($LatLng->lng,$LatLngTrans->lng,'Longitude transform failed');
}

public function testNoopTransformSydney()
{
$LatLng = new LatLng(-33.859972, 151.211111);
$LatLngTrans=clone $LatLng;

$LatLngTrans->transformDatum(RefEll::GRS80(), RefEll::GRS80(), 0, 0, 0, 0, 0, 0, 0);

$this->assertEquals($LatLng->lat,$LatLngTrans->lat,'Latitude transform failed');
$this->assertEquals($LatLng->lng,$LatLngTrans->lng,'Longitude transform failed');
}

public function testNoopTransformLondon()
{
$LatLng = new LatLng(51.54105, -0.12319);
$LatLngTrans=clone $LatLng;

$LatLngTrans->transformDatum(RefEll::GRS80(), RefEll::GRS80(), 0, 0, 0, 0, 0, 0, 0);

$this->assertEquals($LatLng->lat,$LatLngTrans->lat,'Latitude transform failed');
$this->assertEquals($LatLng->lng,$LatLngTrans->lng,'Longitude transform failed');
}

public function testNoopTransformTokyo()
{
$LatLng = new LatLng(35.694668, 139.693122);
$LatLngTrans=clone $LatLng;

$LatLngTrans->transformDatum(RefEll::GRS80(), RefEll::GRS80(), 0, 0, 0, 0, 0, 0, 0);

$this->assertEquals($LatLng->lat,$LatLngTrans->lat,'Latitude transform failed');
$this->assertEquals($LatLng->lng,$LatLngTrans->lng,'Longitude transform failed');
}

public function testNoopTransformCapeTown()
{
$LatLng = new LatLng(-33.925278, 18.423889);
$LatLngTrans=clone $LatLng;

$LatLngTrans->transformDatum(RefEll::GRS80(), RefEll::GRS80(), 0, 0, 0, 0, 0, 0, 0);

$this->assertEquals($LatLng->lat,$LatLngTrans->lat,'Latitude transform failed');
$this->assertEquals($LatLng->lng,$LatLngTrans->lng,'Longitude transform failed');
}

public function testNoopTransformNewYork()
{
$LatLng = new LatLng(40.7127, -74.0059);
$LatLngTrans=clone $LatLng;

$LatLngTrans->transformDatum(RefEll::GRS80(), RefEll::GRS80(), 0, 0, 0, 0, 0, 0, 0);

$this->assertEquals($LatLng->lat,$LatLngTrans->lat,'Latitude transform failed');
$this->assertEquals($LatLng->lng,$LatLngTrans->lng,'Longitude transform failed');
}

}

0 comments on commit 07daecd

Please sign in to comment.