Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ascent and descent values too high #31

Open
microteq opened this issue Jan 19, 2024 · 2 comments
Open

Ascent and descent values too high #31

microteq opened this issue Jan 19, 2024 · 2 comments
Assignees
Labels

Comments

@microteq
Copy link

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;
	},
@morehawes
Copy link
Collaborator

@micrtoeq,

DGF are changes made by me, the rest is the original

Appreciated. I will look in to this.

Joe

@morehawes morehawes self-assigned this Jan 19, 2024
@morehawes
Copy link
Collaborator

morehawes commented Jan 19, 2024

Originally discussed here. My thoughts:

I’m not sure what the best way to deal with this will be honestly. The ascent/descent values in question were added in a recent Pull Request and were pulled over from the Leaflet Elevation plugin that Waymark uses… but is not using the most recent version (I believe because of some conflicts I was not able to resolve a long time ago… but can’t quite remember).

So this is quite messy already and any other edits may compound the problem. I agree though that these values should be accurate and that user control over the measurement “frequency” (there is probably a more accurate term) would be a good solution.

Introduced in PR by @MaximeChallon.

Input/help welcome! This may have been discussed/addressed in the source project already so that's worth checking.

@morehawes morehawes added the good first issue Good for newcomers label Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants