-
Notifications
You must be signed in to change notification settings - Fork 208
/
BingElevation.ts
143 lines (124 loc) · 5.9 KB
/
BingElevation.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Tiles
*/
import { request } from "../../request/Request";
import { IModelApp } from "../../IModelApp";
import { IModelConnection } from "../../IModelConnection";
import { Cartographic } from "@itwin/core-common";
import { Point3d, Range1d, Range2d } from "@itwin/core-geometry";
// cspell:ignore atae qdng uyzv auje sealevel
/** Provides an interface to the [Bing Maps elevation services](https://docs.microsoft.com/en-us/bingmaps/rest-services/elevations/).
* Use of these services requires an API key to be supplied via [[MapLayerOptions.BingMaps]] in the [[IModelAppOptions.mapLayerOptions]]
* passed to [[IModelApp.startup]].
* @public
* @extensions
*/
export class BingElevationProvider {
private _heightRangeRequestTemplate: string;
private _seaLevelOffsetRequestTemplate: string;
private _heightListRequestTemplate: string;
/** @public */
constructor() {
let bingKey = "";
if (IModelApp.mapLayerFormatRegistry.configOptions.BingMaps)
bingKey = IModelApp.mapLayerFormatRegistry.configOptions.BingMaps.value;
this._heightRangeRequestTemplate =
"https://dev.virtualearth.net/REST/v1/Elevation/Bounds?bounds={boundingBox}&rows=16&cols=16&heights=ellipsoid&key={BingMapsAPIKey}"
.replace("{BingMapsAPIKey}", bingKey);
this._seaLevelOffsetRequestTemplate =
"https://dev.virtualearth.net/REST/v1/Elevation/SeaLevel?points={points}&key={BingMapsAPIKey}"
.replace("{BingMapsAPIKey}", bingKey);
this._heightListRequestTemplate =
"https://dev.virtualearth.net/REST/v1/Elevation/List?points={points}&heights={heights}&key={BingMapsAPIKey}"
.replace("{BingMapsAPIKey}", bingKey);
}
/** Return the height (altitude) at a given cartographic location.
* If geodetic is true (the default) then height is returned in the Ellipsoidal WGS84 datum. If geodetic is false then the sea level height id returned using the Earth Gravitational Model 2008 (EGM2008 2.5’).
* @public
*/
public async getHeight(carto: Cartographic, geodetic = true) {
if (undefined === carto)
return 0.0;
const requestUrl =
this._heightListRequestTemplate
.replace("{points}", `${carto.latitudeDegrees},${carto.longitudeDegrees}`)
.replace("{heights}", geodetic ? "ellipsoid" : "sealevel");
try {
const tileResponseBody = await request(requestUrl, "json");
return tileResponseBody.resourceSets[0].resources[0].elevations[0];
} catch (error) {
return 0.0;
}
}
/** Returns 256 elevations in the specified range - 16 rows and 16 columns.
* The elevations are ordered starting with the southwest corner, then proceeding west to east and south to north.
* @beta
*/
public async getHeights(range: Range2d): Promise<number[] | undefined> {
const boundingBox = `${range.low.y},${range.low.x},${range.high.y},${range.high.x}`;
const requestUrl = this._heightRangeRequestTemplate.replace("{boundingBox}", boundingBox);
try {
const tileResponseBody = await request(requestUrl, "json");
return tileResponseBody.resourceSets[0].resources[0].elevations;
} catch (error) {
return undefined;
}
}
/** @internal */
public async getGeodeticToSeaLevelOffset(point: Point3d, iModel: IModelConnection): Promise<number> {
const carto = iModel.spatialToCartographicFromEcef(point);
if (carto === undefined)
return 0.0;
const requestUrl = this._seaLevelOffsetRequestTemplate.replace("{points}", `${carto.latitudeDegrees},${carto.longitudeDegrees}`);
try {
const tileResponseBody = await request(requestUrl, "json");
return tileResponseBody.resourceSets[0].resources[0].offsets[0];
} catch (error) {
return 0.0;
}
}
/** Get the height (altitude) at a given iModel coordinate. The height is geodetic (WGS84 ellipsoid)
* If geodetic is true (the default) then height is returned in the Ellipsoidal WGS84 datum. If geodetic is false then sea level height is returned using the Earth Gravitational Model 2008 (EGM2008 2.5’).
*
* @public
*/
public async getHeightValue(point: Point3d, iModel: IModelConnection, geodetic = true): Promise<number> {
return this.getHeight(iModel.spatialToCartographicFromEcef(point), geodetic);
}
/** Get the height (altitude) range for a given iModel project extents. The height values are geodetic (WGS84 ellipsoid).
* @public
*/
public async getHeightRange(iModel: IModelConnection) {
const latLongRange = Range2d.createNull();
const range = iModel.projectExtents.clone();
// Expand for project surroundings.
range.expandInPlace(1000);
for (const corner of range.corners()) {
const carto = iModel.spatialToCartographicFromEcef(corner);
latLongRange.extendXY(carto.longitudeDegrees, carto.latitudeDegrees);
}
const heights = await this.getHeights(latLongRange);
return heights ? Range1d.createArray(heights) : Range1d.createNull();
}
/** Get the average height (altitude) for a given iModel project extents. The height values are geodetic (WGS84 ellipsoid).
* @public
*/
public async getHeightAverage(iModel: IModelConnection) {
const latLongRange = Range2d.createNull();
for (const corner of iModel.projectExtents.corners()) {
const carto = iModel.spatialToCartographicFromEcef(corner);
latLongRange.extendXY(carto.longitudeDegrees, carto.latitudeDegrees);
}
const heights = await this.getHeights(latLongRange);
if (!heights || !heights.length)
return 0;
let total = 0.0;
for (const height of heights)
total += height;
return total / heights.length;
}
}