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

Add queryTerrainElevation #2279

Merged
merged 3 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
manhcuongincusar1 marked this conversation as resolved.
Show resolved Hide resolved

- 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))

Expand Down
44 changes: 44 additions & 0 deletions src/ui/camera.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
});
});
20 changes: 20 additions & 0 deletions src/ui/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down