-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelevation_alt.ts
32 lines (24 loc) · 985 Bytes
/
elevation_alt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import * as gdal from 'gdal';
export const getElevation = (lat: number, lng: number): Promise<number> => {
const latCeil = Math.ceil(Math.abs(lat));
const lngCeil = Math.ceil(Math.abs(lng));
const elevFile = `./data/ca_elev_files/USGS_13_n${Math.abs(latCeil)}w${lngCeil}.tif`;
return new Promise((resolve, reject) => {
try {
const dataset = gdal.open(elevFile);
// get elev band
const band = dataset.bands.get(1);
const [originX, pixelWidth, , originY, , pixelHeight] = dataset.geoTransform;
// convert coords
const xPixel = Math.floor((lng - originX) / pixelWidth);
const yPixel = Math.floor((lat - originY) / pixelHeight);
// extract elev value
const buffer = band.pixels.read(xPixel, yPixel, 1, 1);
const elevation = buffer[0]; // Access the elevation value
resolve(elevation);
} catch (err) {
console.error(`Error reading file ${elevFile}:`, err);
reject(err);
}
});
};