DaBozUK
DaBozUK

Reputation: 610

Output degrees minutes from an OpenLayers coordinate, but without seconds?

The method ol.coordinate.toStringHDMS() will output in a format of degress, minutes and seconds with compass direction, e.g. 47° 59′ 59″ N 7° 50′ 59″ E

But is there a way to output degrees and minutes without seconds? Truncating the seconds is not appropriate as it will ignore rounding.

e.g. the above example should round to 48° 00′ N 7° 51′ E however truncating seconds would erronesouly give 47° 59′ N 7° 50′ E

Upvotes: 0

Views: 809

Answers (2)

Pigeo
Pigeo

Reputation: 83

Suggested derived functions:

function toStringHDM(coordinate) {
  if (coordinate) {
      return (degreesToStringHDM('NS', coordinate[1]) +
          ' ' +
          degreesToStringHDM('EW', coordinate[0]));
  }
  else {
      return '';
  }
}

and:

function degreesToStringHDM(hemispheres, degrees, opt_fractionDigits = 0) {
  var normalizedDegrees = modulo(degrees + 180, 360) - 180;
  var x = Math.abs(3600 * normalizedDegrees);
  var dflPrecision = opt_fractionDigits || 0;
  var precision = Math.pow(10, dflPrecision);
  var deg = Math.floor(x / 3600);
  var min = Math.floor((x - deg * 3600) / 60);
  var sec = x - deg * 3600 - min * 60;
  sec = Math.ceil(sec * precision) / precision;
  if (sec >= 60) {
      sec = 0;
      min += 1;
  }
  if (min >= 60) {
      min = 0;
      deg += 1;
  }
  return (deg +
      '\u00b0 ' +
      padNumber(min, 2) +
      '\u2032 ' +
      (normalizedDegrees == 0
          ? ''
          : ' ' + hemispheres.charAt(normalizedDegrees < 0 ? 1 : 0)));
}

Please note: assuming the compiler will do the job for me, I intentionally left a lot of dead code inside degreesToStringHDM() in order to keep it as close as possible to OpenLayers' original function degreesToStringHDMS(), should you need to compare both in the future (for example, when OpenLayers will be releasing a new version of this function, potentially fixing some bugs, you may be interested to perform a diff in order to quickly find what needs to be fixed here...)

Upvotes: 1

Thomas Gratier
Thomas Gratier

Reputation: 2371

You need to make your own function derivating it from both

Upvotes: 1

Related Questions