From 9015a27eb6178915c57fb416f93ab09aa94c8d13 Mon Sep 17 00:00:00 2001 From: Michael Kuenzli Date: Wed, 24 Apr 2019 13:57:15 +0200 Subject: [PATCH] Round coordinate to meter precision before requesting profile --- src/SwisstopoProfiler.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/SwisstopoProfiler.js b/src/SwisstopoProfiler.js index 7ae4314..128e42e 100644 --- a/src/SwisstopoProfiler.js +++ b/src/SwisstopoProfiler.js @@ -34,16 +34,22 @@ class SwisstopoProfiler { * @return {Promise} */ computeProfile(segment) { - // TODO: round to coordinate to meter precision + const geom = /** @type{ol.geom.LineString} */ (segment.getGeometry()); - const geom = this.geojsonFormat_.writeGeometry(segment.getGeometry()); + // Round coordinate to meter precision by rounding to the fifth decimal place which equals about 1.1 meters. + // see: https://gis.stackexchange.com/a/8674 + const lowerPrecisionCoords = /** @type{Array.} */ + (geom.getCoordinates().map(coord => coord.map(c => Number.parseFloat(c.toFixed(5))))); + geom.setCoordinates(lowerPrecisionCoords); + + const geojson = this.geojsonFormat_.writeGeometry(geom); const request = fetch(this.url_, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, - body: `geom=${geom}&sr=2056&offset=1&elevation_models=COMB` + body: `geom=${geojson}&sr=2056&offset=1&elevation_models=COMB` }); return request .then(response => response.json())