Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

Round coordinate to meter precision before requesting profile #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/SwisstopoProfiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<ol.Coordinate>} */
(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())
Expand Down