Skip to content

Commit

Permalink
Merge pull request #476 from JeroenDeDauw/fixprecision
Browse files Browse the repository at this point in the history
 Use a sensible precision for float and DD coordinates
  • Loading branch information
JeroenDeDauw authored Oct 1, 2018
2 parents d20571a + ef76b4a commit 337e5f7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
3 changes: 2 additions & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ different releases and which versions of PHP and MediaWiki they support, see the

## Maps 6.0.2

Under development
Released on October 1st, 2018.

* Coordinates formatted as Decimal Degrees or Float are now rounded sensibly
* The `smgQPCoodDirectional` setting is no longer ignored

## Maps 6.0.1
Expand Down
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
28 changes: 28 additions & 0 deletions tests/Integration/Parser/CoordinatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,32 @@ public function testGivenInvalidFormat_defaultFormatGetsUsed() {
);
}

public function testRoundingWhenFormattingAsFloat() {
$this->assertContains(
'52.136945 N, 0.466722 W',
$this->parse( '{{#coordinates:52.136945,-0.466722|format=float}}' )
);
}

public function testRoundingWhenFormattingAsDMS() {
$this->assertContains(
'52° 8\' 13.00" N, 0° 28\' 0.20" W',
$this->parse( '{{#coordinates:52.136945,-0.466722|format=dms}}' )
);
}

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

public function testRoundingWhenFormattingAsDM() {
$this->assertContains(
'52° 8.2167\' N, 0° 28.0033\' W',
$this->parse( '{{#coordinates:52.136945,-0.466722|format=dm}}' )
);
}

}

0 comments on commit 337e5f7

Please sign in to comment.