Ascent and descent values too high

Open
#31 3 comments 1 reaction 1 assignee View on GitHub

@morehawes is already working on this.

Since Jan 19, 2024.

Assessment

This issue has not been assessed yet.

Description

bug good first issue

Hi

Ascent and descent values are too high, because they are calculated every 1 to 4 meters. They should be calculated in bigger walking distances. My suggestion is 10 meters.

I have adjusted the source code, which is pasted at the end of this message. Only what I have marked with DGF are changes made by me, the rest is the original. There are two functions in leaflet-elevation.js that are affected: _addData and _addPoint.

Best regards.
Daniel.


/*
* Parsing data either from GPX or GeoJSON and update the diagram data
*/
_addData: function(d) {
let geom = d && d.geometry;
let feat = d && d.type === "FeatureCollection";
let gpx = d && d._latlngs;

		if (geom) {
			switch (geom.type) {
				case 'LineString':
					this._addGeoJSONData(geom.coordinates);
					break;

				case 'MultiLineString':
					geom.coordinates.forEach(coords => this._addGeoJSONData(coords));
					break;

				default:
					console.warn('Unsopperted GeoJSON feature geometry type:' + geom.type);
			}
      //DGF if elevation difference is too small, make ascent and descent equal
      if (Math.abs(this._ascent-this._descent) <= 10)
      {
        this.track_info.descent = this.track_info.ascent;
        this._descent = this.track_info.descent;
      }
      //DGF end
		}

		if (feat) {
			d.features.forEach(feature => this._addData(feature));
		}

		if (gpx) {
			this._addGPXdata(d._latlngs);
		}
	},

/*
* Parse and push a single (x, y, z) point to current elevation profile.
*/
_addPoint: function(x, y, z) {
if (this.options.reverseCoords) {
[x, y] = [y, x];
}

		let data = this._data || [];
		let eleMax = this._maxElevation || -Infinity;
		let eleMin = this._minElevation || +Infinity;
		let dist = this._distance || 0;
    //DGF
    let elevDist = this._elevationMesurementDistance || 0;
    //DGF end
		let curr = new L.LatLng(x, y);
		let prev = data.length ? data[data.length - 1].latlng : curr;

		let delta = curr.distanceTo(prev) * this._distanceFactor;

		dist = dist + Math.round(delta / 1000 * 100000) / 100000;
    //DGF
    elevDist = elevDist + delta;
    //DGF end

    // check and fix missing elevation data on last added point
		if (!this.options.skipNullZCoords && data.length > 0) 
    {
			let prevZ = data[data.length - 1].z;
			if (isNaN(prevZ)) {
				let lastZ = this._lastValidZ;
				let currZ = z * this._heightFactor;
				if (!isNaN(lastZ) && !isNaN(currZ)) {
					prevZ = (lastZ + currZ) / 2;
				} else if (!isNaN(lastZ)) {
					prevZ = lastZ;
				} else if (!isNaN(currZ)) {
					prevZ = currZ;
				}
				if (!isNaN(prevZ)) data[data.length - 1].z = prevZ;
				else data.splice(data.length - 1, 1);
			}
		}

		z = z * this._heightFactor;

		// skip point if it has not elevation
		if (!isNaN(z)) 
    {
			eleMax = eleMax < z ? z : eleMax;
			eleMin = eleMin > z ? z : eleMin;

      // calculate new ascent or descent
      //DGF measure elevation only every 10 meters of walking distance
      if (elevDist >= 10 || isNaN(this._lastMeasuredZ))
      {
        elevDist = 0;
        let dz = z - this._lastMeasuredZ ;  //DGF
        if (dz > 0)
        {
          this.track_info.ascent  = (this.track_info.ascent || 0) + dz;  // Total Ascent
          this._ascent = this.track_info.ascent;
        }
        else if (dz < 0)
        {
          this.track_info.descent = (this.track_info.descent || 0) - dz; // Total Descent
          this._descent = this.track_info.descent;
        }
        this._lastMeasuredZ = z;  //DGF
      }
      this._elevationMesurementDistance = elevDist;
      //DGF end
      this._lastValidZ = z;
			// set up last valid z value	
		}

		data.push({
			dist: dist,
			x: x,
			y: y,
			z: z,
			latlng: curr
		});

		this._data = data;
		this._distance = dist;
		this._maxElevation = eleMax;
		this._minElevation = eleMin;
	},
Dominant language
PHP
Stars
29
Forks
12
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from OpenGIS/waymark-wp

All issues in OpenGIS/waymark-wp

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.