Skip to content

Commit

Permalink
Merge pull request #875 from DieuAnh/update-format-length
Browse files Browse the repository at this point in the history
#503 add mm to formatLength and formatArea
  • Loading branch information
dnlkoch committed Jan 4, 2023
2 parents be7ce8d + a08f857 commit 3249a86
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/MeasureUtil/MeasureUtil.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,21 @@ describe('MeasureUtil', () => {
const start = [0, 0];
const end = [0, 100];
const end2 = [0, 100550];
const end3 = [0, 0.1];

const shortLine = new OlGeomLineString([start, end]);
const longLine = new OlGeomLineString([start, end2]);
const veryShortLine = new OlGeomLineString([start, end3]);

map = TestUtil.createMap();

const expectedShortLength = MeasureUtil.formatLength(shortLine, map, 2);
const expectedLongLength = MeasureUtil.formatLength(longLine, map, 2);
const expectedVeryShortLength = MeasureUtil.formatLength(veryShortLine, map, 2);

expect(expectedShortLength).toBe('99.89 m');
expect(expectedLongLength).toBe('100.43 km');
expect(expectedVeryShortLength).toBe('99.89 mm');

TestUtil.removeMap(map);
});
Expand Down Expand Up @@ -104,17 +108,21 @@ describe('MeasureUtil', () => {
});
it('formats the area of a polygon as expected', () => {
const bigPolyCoords = smallPolyCoords.map(coord => [coord[0] * 100, coord[1] * 100]);
const verySmallPolyCoords = smallPolyCoords.map(coord => [coord[0] / 100, coord[1] / 100]);

const smallPoly = new OlGeomPolygon([smallPolyCoords]);
const bigPoly = new OlGeomPolygon([bigPolyCoords]);
const verySmallPoly = new OlGeomPolygon([verySmallPolyCoords]);

map = TestUtil.createMap();

const expectedSmallArea = MeasureUtil.formatArea(smallPoly, map, 2);
const expectedBigArea = MeasureUtil.formatArea(bigPoly, map, 2);
const expectedVerySmallArea = MeasureUtil.formatArea(verySmallPoly, map, 2);

expect(expectedSmallArea).toBe('99.78 m<sup>2</sup>');
expect(expectedBigArea).toBe('1 km<sup>2</sup>');
expect(expectedVerySmallArea).toBe('9977.66 mm<sup>2</sup>');

TestUtil.removeMap(map);
});
Expand Down
12 changes: 9 additions & 3 deletions src/MeasureUtil/MeasureUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class MeasureUtil {
* allowed for the measure tooltips
* @param {boolean} geodesic Is the measurement geodesic (default is true).
*
* @return {string} The formatted length of the line.
* @return {string} The formatted length of the line (units: km, m or mm).
*/
static formatLength(
line: OlGeomLineString, map: OlMap, decimalPlacesInToolTips: number, geodesic = true
Expand All @@ -55,9 +55,12 @@ class MeasureUtil {
if (length > 1000) {
output = (Math.round(length / 1000 * decimalHelper) /
decimalHelper) + ' km';
} else {
} else if (length > 1) {
output = (Math.round(length * decimalHelper) / decimalHelper) +
' m';
} else {
output = (Math.round(length * 1000 * decimalHelper) / decimalHelper) +
' mm';
}
return output;
}
Expand Down Expand Up @@ -107,9 +110,12 @@ class MeasureUtil {
if (area > 10000) {
output = (Math.round(area / 1000000 * decimalHelper) /
decimalHelper) + ' km<sup>2</sup>';
} else {
} else if (area > 0.01) {
output = (Math.round(area * decimalHelper) / decimalHelper) +
' m<sup>2</sup>';
} else {
output = (Math.round(area * 1000000 * decimalHelper) / decimalHelper) +
' mm<sup>2</sup>';
}
return output;
}
Expand Down

0 comments on commit 3249a86

Please sign in to comment.