Merge pull request #13893 from tschaub/grat

Omit minutes and seconds from HDMS formatting if zero
This commit is contained in:
Tim Schaub
2022-07-28 13:47:29 -06:00
committed by GitHub
4 changed files with 54 additions and 35 deletions

View File

@@ -65,23 +65,19 @@ describe('ol.layer.Graticule', function () {
};
graticule.drawLabels_(event);
expect(graticule.meridiansLabels_.length).to.be(13);
expect(graticule.meridiansLabels_[0].text).to.be('0° 00 00″');
expect(graticule.meridiansLabels_[0].text).to.be('0°');
expect(
graticule.meridiansLabels_[0].geom.getCoordinates()[0]
).to.roughlyEqual(0, 1e-9);
expect(graticule.parallelsLabels_.length).to.be(3);
expect(graticule.parallelsLabels_[0].text).to.be('0° 00 00″');
expect(graticule.parallelsLabels_[0].text).to.be('0°');
expect(
graticule.parallelsLabels_[0].geom.getCoordinates()[1]
).to.roughlyEqual(0, 1e-9);
feature.set('graticule_label', graticule.meridiansLabels_[0].text);
expect(graticule.lonLabelStyle_(feature).getText().getText()).to.be(
'0° 00 00″'
);
expect(graticule.lonLabelStyle_(feature).getText().getText()).to.be('0°');
feature.set('graticule_label', graticule.parallelsLabels_[0].text);
expect(graticule.latLabelStyle_(feature).getText().getText()).to.be(
'0° 00 00″'
);
expect(graticule.latLabelStyle_(feature).getText().getText()).to.be('0°');
});
it('creates a graticule with wrapped world labels', function () {
@@ -115,24 +111,20 @@ describe('ol.layer.Graticule', function () {
};
graticule.drawLabels_(event);
expect(graticule.meridiansLabels_.length).to.be(13);
expect(graticule.meridiansLabels_[0].text).to.be('0° 00 00″');
expect(graticule.meridiansLabels_[0].text).to.be('0°');
const coordinates = fromLonLat([360, 0]);
expect(
graticule.meridiansLabels_[0].geom.getCoordinates()[0]
).to.roughlyEqual(coordinates[0], 1e-9);
expect(graticule.parallelsLabels_.length).to.be(3);
expect(graticule.parallelsLabels_[0].text).to.be('0° 00 00″');
expect(graticule.parallelsLabels_[0].text).to.be('0°');
expect(
graticule.parallelsLabels_[0].geom.getCoordinates()[1]
).to.roughlyEqual(0, 1e-9);
feature.set('graticule_label', graticule.meridiansLabels_[0].text);
expect(graticule.lonLabelStyle_(feature).getText().getText()).to.be(
'0° 00 00″'
);
expect(graticule.lonLabelStyle_(feature).getText().getText()).to.be('0°');
feature.set('graticule_label', graticule.parallelsLabels_[0].text);
expect(graticule.latLabelStyle_(feature).getText().getText()).to.be(
'0° 00 00″'
);
expect(graticule.latLabelStyle_(feature).getText().getText()).to.be('0°');
});
it('has a default stroke style', function () {

View File

@@ -7,6 +7,7 @@ import {
closestOnSegment,
equals as coordinatesEqual,
createStringXY,
degreesToStringHDMS,
format as formatCoordinate,
rotate as rotateCoordinate,
scale as scaleCoordinate,
@@ -216,6 +217,26 @@ describe('ol/coordinate.js', function () {
});
});
describe('degreesToStringHDMS', () => {
it('includes minutes and seconds if non-zero', () => {
expect(degreesToStringHDMS('NS', 10 + 30 / 60 + 30 / 3600)).to.be(
'10° 30 30″ N'
);
});
it('omits minutes if zero', () => {
expect(degreesToStringHDMS('NS', 10)).to.be('10° N');
});
it('includes minutes if seconds are non-zero', () => {
expect(degreesToStringHDMS('NS', 10 + 30 / 3600)).to.be('10° 00 30″ N');
});
it('omits seconds if zero', () => {
expect(degreesToStringHDMS('NS', 10.5)).to.be('10° 30 N');
});
});
describe('#toStringHDMS', function () {
it('returns the empty string on undefined input', function () {
const got = toStringHDMS();
@@ -225,13 +246,16 @@ describe('ol/coordinate.js', function () {
it('formats with zero fractional digits as default', function () {
const coord = [7.85, 47.983333];
const got = toStringHDMS(coord);
const expected = '47° 59 00″ N 7° 51 00″ E';
const expected = '47° 59 N 7° 51 E';
expect(got).to.be(expected);
});
it('formats with given fractional digits, if passed', function () {
const coord = [7.85, 47.983333];
const coord = [
10 + 20 / 60 + 0.3456 / 3600,
20 + 30 / 60 + 0.4321 / 3600,
];
const got = toStringHDMS(coord, 3);
const expected = '47° 58 59.999″ N 7° 51 00.000″ E';
const expected = '20° 30 00.432″ N 10° 20 00.346″ E';
expect(got).to.be(expected);
});
});