Skip to content

Commit

Permalink
Merge pull request #1169 from Batiste-Yole-SP/add-measure-length-prec…
Browse files Browse the repository at this point in the history
…ision

fix!: added getLength decimal precision for non geodesic map
  • Loading branch information
dnlkoch authored Oct 9, 2023
2 parents cea47be + 868cd7b commit 4420e36
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/MeasureUtil/MeasureUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,28 @@ class MeasureUtil {
* @param {OlMap} map An OlMap.
* @param {boolean} geodesic Is the measurement geodesic (default is true).
* @param {number} radius Sphere radius. By default, the radius of the earth
* is used (Clarke 1866 Authalic Sphere, 6371008.8).
* is used (Clarke 1866 Authalic Sphere, 6371008.8).
* @param {number} decimalPrecision Set the decimal precision on length value
* for non-geodesic map (default value 6)
*
* @return {number} The length of line in meters.
*/
static getLength(line: OlGeomLineString, map: OlMap, geodesic: boolean = true, radius: number = 6371008.8): number {
static getLength(
line: OlGeomLineString,
map: OlMap,
geodesic: boolean = true,
radius: number = 6371008.8,
decimalPrecision: number = 6
): number {
const decimalHelper = Math.pow(10, decimalPrecision);
if (geodesic) {
const opts = {
projection: map.getView().getProjection().getCode(),
radius
};
return getLength(line, opts);
} else {
return Math.round(line.getLength() * 100) / 100;
return Math.round(line.getLength() * decimalHelper) / decimalHelper;
}
}

Expand Down

0 comments on commit 4420e36

Please sign in to comment.