From 68580fc412547c80d5a65e9cd12af306e24735ab Mon Sep 17 00:00:00 2001 From: Jeroen De Dauw Date: Mon, 1 Oct 2018 08:55:45 +0200 Subject: [PATCH] Use a sensible precision for float and DD coordinates Fixes https://github.com/JeroenDeDauw/Maps/issues/474 --- src/CoordinateFormatter.php | 9 ++++++++- tests/Integration/Parser/CoordinatesTest.php | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/CoordinateFormatter.php b/src/CoordinateFormatter.php index 47794d5c4..5e72754b5 100644 --- a/src/CoordinateFormatter.php +++ b/src/CoordinateFormatter.php @@ -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] ] ) ); diff --git a/tests/Integration/Parser/CoordinatesTest.php b/tests/Integration/Parser/CoordinatesTest.php index 21522267c..4ec716303 100644 --- a/tests/Integration/Parser/CoordinatesTest.php +++ b/tests/Integration/Parser/CoordinatesTest.php @@ -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}}' ) ); } @@ -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}}' ) ); }