Skip to content

Commit 15bf59d

Browse files
author
Daniel Kinzler
authored
Merge pull request #93 from DataValues/deltaFormatting
Round up in GeoCoordinateFormatter when close to a degree/minute
2 parents bcec7e8 + 545820f commit 15bf59d

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

src/Formatters/GeoCoordinateFormatter.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,14 @@ private function formatCoordinate( $degrees, $precision ) {
254254
return $this->getInFloatFormat( $degrees );
255255
}
256256

257+
if ( $format !== self::TYPE_DD ) {
258+
if ( $precision >= 1 - 1 / 60 && $precision < 1 ) {
259+
$precision = 1;
260+
} elseif ( $precision >= 1 / 60 - 1 / 3600 && $precision < 1 / 60 ) {
261+
$precision = 1 / 60;
262+
}
263+
}
264+
257265
if ( $format === self::TYPE_DD || $precision >= 1 ) {
258266
return $this->getInDecimalDegreeFormat( $degrees, $precision );
259267
}
@@ -321,9 +329,11 @@ private function getInDecimalDegreeFormat( $floatDegrees, $precision ) {
321329
private function getInDegreeMinuteSecondFormat( $floatDegrees, $precision ) {
322330
$isNegative = $floatDegrees < 0;
323331
$secondDigits = $this->getSignificantDigits( 3600, $precision );
332+
324333
$seconds = round( abs( $floatDegrees ) * 3600, max( 0, $secondDigits ) );
325334
$minutes = (int)( $seconds / 60 );
326335
$degrees = (int)( $minutes / 60 );
336+
327337
$seconds -= $minutes * 60;
328338
$minutes -= $degrees * 60;
329339

@@ -353,8 +363,10 @@ private function getInDegreeMinuteSecondFormat( $floatDegrees, $precision ) {
353363
private function getInDecimalMinuteFormat( $floatDegrees, $precision ) {
354364
$isNegative = $floatDegrees < 0;
355365
$minuteDigits = $this->getSignificantDigits( 60, $precision );
366+
356367
$minutes = round( abs( $floatDegrees ) * 60, max( 0, $minuteDigits ) );
357368
$degrees = (int)( $minutes / 60 );
369+
358370
$minutes -= $degrees * 60;
359371

360372
$space = $this->getSpacing( self::OPT_SPACE_COORDPARTS );

tests/unit/Formatters/GeoCoordinateFormatterTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,16 @@ public function decimalMinuteNotationProvider() {
275275
0.1 / 60,
276276
'-0° 0.1\', 0° 0.1\''
277277
),
278+
'round to degree when it does not make a difference' => array(
279+
new LatLongValue( 1.5, 2.5 ),
280+
1 - 1 / 60,
281+
'2°, 3°'
282+
),
283+
'round to minutes when it starts making a difference' => array(
284+
new LatLongValue( 1.5, 2.5 ),
285+
1 - 2 / 60,
286+
'1° 56\', 2° 54\''
287+
),
278288
'precision option must support strings' => array(
279289
new LatLongValue( -0.05, 0.05 ),
280290
'0.1',
@@ -396,6 +406,26 @@ public function decimalMinuteSecondNotationProvider() {
396406
0.1 / 3600,
397407
'-0° 0\' 0.1", 0° 0\' 0.1"'
398408
),
409+
'round to degree when it does not make a difference' => array(
410+
new LatLongValue( 1.5, 2.5 ),
411+
1 - 1 / 60,
412+
'2°, 3°'
413+
),
414+
'round to minutes when it starts making a difference' => array(
415+
new LatLongValue( 1.5, 2.5 ),
416+
1 - 2 / 60,
417+
'1° 56\', 2° 54\''
418+
),
419+
'round to minutes when it does not make a difference' => array(
420+
new LatLongValue( 1.926, 2.926 ),
421+
1 / 60 - 1 / 3600,
422+
'1° 56\', 2° 56\''
423+
),
424+
'round to seconds when it starts making a difference' => array(
425+
new LatLongValue( 1.926, 2.926 ),
426+
1 / 60 - 2 / 3600,
427+
'1° 56\' 0", 2° 55\' 56"'
428+
),
399429
'unexpected rounding to 36°, 36°' => array(
400430
new LatLongValue( 36.5867, 37.0458 ),
401431
1.1187604885913,

0 commit comments

Comments
 (0)