Skip to content

Commit

Permalink
Use a sensible precision for float and DD coordinates
Browse files Browse the repository at this point in the history
Fixes #474
  • Loading branch information
JeroenDeDauw committed Oct 1, 2018
1 parent e115d6c commit 68580fc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/CoordinateFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@
*/
class CoordinateFormatter {

private const PRECISION_MAP = [
'dms' => 1 / 360000,
'dm' => 1 / 600000,
'dd' => 1 / 1000000,
'float' => 1 / 1000000,
];

public function format( LatLongValue $latLong, string $format, bool $directional ) {
$formatter = new LatLongFormatter( new FormatterOptions(
[
LatLongFormatter::OPT_FORMAT => $format,
LatLongFormatter::OPT_DIRECTIONAL => $directional,
LatLongFormatter::OPT_PRECISION => 1 / 360000
LatLongFormatter::OPT_PRECISION => self::PRECISION_MAP[$format]
]
) );

Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Parser/CoordinatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testGivenInvalidFormat_defaultFormatGetsUsed() {

public function testRoundingWhenFormattingAsFloat() {
$this->assertContains(
'52.136944444444 N, 0.46672222222222 W',
'52.136945 N, 0.466722 W',
$this->parse( '{{#coordinates:52.136945,-0.466722|format=float}}' )
);
}
Expand All @@ -81,7 +81,7 @@ public function testRoundingWhenFormattingAsDMS() {

public function testRoundingWhenFormattingAsDD() {
$this->assertContains(
'52.136944° N, 0.466722° W',
'52.136945° N, 0.466722° W',
$this->parse( '{{#coordinates:52.136945,-0.466722|format=dd}}' )
);
}
Expand Down

0 comments on commit 68580fc

Please sign in to comment.