From 1dc8d16d4c4f1610a857914ebd89bf07520793d0 Mon Sep 17 00:00:00 2001 From: cuong nguyen Date: Thu, 16 Mar 2023 13:56:32 +0700 Subject: [PATCH 1/3] Add queryTerrainElevation --- src/ui/camera.test.ts | 44 +++++++++++++++++++++++++++++++++++++++++++ src/ui/camera.ts | 20 ++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/ui/camera.test.ts b/src/ui/camera.test.ts index 9f593cc60a..3ec36e0baa 100644 --- a/src/ui/camera.test.ts +++ b/src/ui/camera.test.ts @@ -6,6 +6,7 @@ import {fixedLngLat, fixedNum} from '../../test/unit/lib/fixed'; import {setMatchMedia} from '../util/test/util'; import {mercatorZfromAltitude} from '../geo/mercator_coordinate'; import Terrain from '../render/terrain'; +import {LngLatLike} from '../geo/lng_lat'; beforeEach(() => { setMatchMedia(); @@ -1997,3 +1998,46 @@ describe('#fitScreenCoordinates', () => { expect(camera.getBearing()).toBeCloseTo(0); }); }); + +describe('queryTerrainElevation', () => { + let camera: Camera; + + beforeEach(() => { + camera = createCamera(); + }); + + test('should return null if terrain is not set', () => { + camera.terrain = null; + const result = camera.queryTerrainElevation([0, 0]); + expect(result).toBeNull(); + }); + + test('should return the correct elevation', () => { + // Set up mock transform and terrain objects + const transform = new Transform(0, 22, 0, 60, true); + transform.getElevation = jest.fn().mockReturnValue(200); + transform.elevation = 50; + const terrain = {} as Terrain; + + // Set up camera with mock transform and terrain + camera.transform = transform; + camera.terrain = terrain; + + // Call queryTerrainElevation with mock lngLat + const lngLatLike: LngLatLike = [1, 2]; + const expectedElevation = 150; // 200 - 50 = 150 + const result = camera.queryTerrainElevation(lngLatLike); + + // Check that transform.getElevation was called with the correct arguments + expect(transform.getElevation).toHaveBeenCalledWith( + expect.objectContaining({ + lng: lngLatLike[0], + lat: lngLatLike[1], + }), + terrain + ); + + // Check that the correct elevation value was returned + expect(result).toEqual(expectedElevation); + }); +}); diff --git a/src/ui/camera.ts b/src/ui/camera.ts index c141d0e8a1..4bcefa3d86 100644 --- a/src/ui/camera.ts +++ b/src/ui/camera.ts @@ -1343,6 +1343,26 @@ abstract class Camera extends Evented { delta > 180 ? -360 : delta < -180 ? 360 : 0; } + + /** + * Query the current elevation of location. It return null if terrain is not enabled. the elevation is in meters relative to mean sea-level + * @memberof Map# + * @param lngLatLike [x,y] or LngLat coordinates of the location + * @returns {number} elevation in meters + */ + queryTerrainElevation(lngLatLike: LngLatLike): number | null { + if (!this.terrain) { + return null; + } + const elevation = this.transform.getElevation(LngLat.convert(lngLatLike), this.terrain); + /** + * Different zoomlevels with different terrain-tiles the elvation-values are not the same. + * map.transform.elevation variable with the center-altitude. + * In maplibre the proj-matrix is translated by this value in negative z-direction. + * So we need to add this value to the elevation to get the correct value. + */ + return elevation - this.transform.elevation; + } } // In debug builds, check that camera change events are fired in the correct order. From 1776119b19f8003f8bfc09829f61ac560eae7214 Mon Sep 17 00:00:00 2001 From: cuong nguyen Date: Thu, 16 Mar 2023 13:56:41 +0700 Subject: [PATCH 2/3] Add Change Log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c530aa8f60..dd76b9c244 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ ## main +- Add queryTerrainElevation allows getting terrain elevation in meters at specific point ([#2264](https://github.com/maplibre/maplibre-gl-js/pull/2264)) - Fix the type of the `features` property on `MapLayerMouseEvent` and `MapLayerTouchEvent` to be `MapGeoJSONFeature[]` in lieu of `GeoJSON.Feature[]` ([#2244](https://github.com/maplibre/maplibre-gl-js/pull/2244)) From 957279979158d15d5a848d1a6ee1559ac3954bdf Mon Sep 17 00:00:00 2001 From: cuong nguyen Date: Thu, 16 Mar 2023 14:09:07 +0700 Subject: [PATCH 3/3] Move change log item to features and improvements section --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd76b9c244..ebb0441020 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,10 @@ ## main -- Add queryTerrainElevation allows getting terrain elevation in meters at specific point ([#2264](https://github.com/maplibre/maplibre-gl-js/pull/2264)) - Fix the type of the `features` property on `MapLayerMouseEvent` and `MapLayerTouchEvent` to be `MapGeoJSONFeature[]` in lieu of `GeoJSON.Feature[]` ([#2244](https://github.com/maplibre/maplibre-gl-js/pull/2244)) ### ✨ Features and improvements +- Add queryTerrainElevation allows getting terrain elevation in meters at specific point ([#2264](https://github.com/maplibre/maplibre-gl-js/pull/2264)) - Improve performance by sending style layers to worker thread before processing it on main thread to allow parallel processing ([#2131](https://github.com/maplibre/maplibre-gl-js/pull/2131)) - [Breaking] Resize map when container element is resized. the resize related events now has different data associated with it ([#2157](https://github.com/maplibre/maplibre-gl-js/pull/2157))
Previously the originalEvent field was the reason of this change, for example it could be a resize event from the browser