forked from TerriaJS/terriajs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into workbench-type-item
- Loading branch information
Showing
91 changed files
with
3,266 additions
and
1,961 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 80 additions & 20 deletions
100
lib/Models/Catalog/CatalogItems/CesiumTerrainCatalogItem.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,103 @@ | ||
import { computed } from "mobx"; | ||
import { action, computed, observable, runInAction } from "mobx"; | ||
import CesiumTerrainProvider from "terriajs-cesium/Source/Core/CesiumTerrainProvider"; | ||
import IonResource from "terriajs-cesium/Source/Core/IonResource"; | ||
import MappableMixin from "../../../ModelMixins/MappableMixin"; | ||
import CatalogMemberMixin from "../../../ModelMixins/CatalogMemberMixin"; | ||
import MappableMixin from "../../../ModelMixins/MappableMixin"; | ||
import UrlMixin from "../../../ModelMixins/UrlMixin"; | ||
import CesiumTerrainCatalogItemTraits from "../../../Traits/TraitsClasses/CesiumTerrainCatalogItemTraits"; | ||
import CreateModel from "../../Definition/CreateModel"; | ||
import TerriaError from "../../../Core/TerriaError"; | ||
|
||
export default class CesiumTerrainCatalogItem extends UrlMixin( | ||
MappableMixin(CatalogMemberMixin(CreateModel(CesiumTerrainCatalogItemTraits))) | ||
) { | ||
static type = "cesium-terrain"; | ||
|
||
/** | ||
* An observable terrain provider instance set by forceLoadMapItems() | ||
*/ | ||
@observable | ||
private terrainProvider: CesiumTerrainProvider | undefined = undefined; | ||
|
||
get type() { | ||
return CesiumTerrainCatalogItem.type; | ||
} | ||
|
||
protected forceLoadMapItems(): Promise<void> { | ||
return Promise.resolve(); | ||
@computed | ||
get disableZoomTo() { | ||
return true; | ||
} | ||
|
||
@computed | ||
get mapItems() { | ||
let resource: string | Promise<IonResource> = this.url!; | ||
if (this.ionAssetId !== undefined) { | ||
resource = IonResource.fromAssetId(this.ionAssetId, { | ||
accessToken: | ||
this.ionAccessToken || | ||
this.terria.configParameters.cesiumIonAccessToken, | ||
server: this.ionServer | ||
}); | ||
// Deal with errors from this better | ||
private get isTerrainActive() { | ||
return this.terria.terrainProvider === this.terrainProvider; | ||
} | ||
|
||
@computed | ||
get shortReport() { | ||
if (super.shortReport === undefined) { | ||
const status = this.isTerrainActive ? "In use" : "Not in use"; | ||
return `Terrain status: ${status}`; | ||
} | ||
return super.shortReport; | ||
} | ||
|
||
return [ | ||
new CesiumTerrainProvider({ | ||
url: resource, | ||
credit: this.attribution | ||
}) | ||
]; | ||
/** | ||
* Returns a Promise to load the terrain provider | ||
*/ | ||
private async loadTerrainProvider(): Promise< | ||
CesiumTerrainProvider | undefined | ||
> { | ||
const resource = | ||
this.ionAssetId !== undefined | ||
? IonResource.fromAssetId(this.ionAssetId, { | ||
accessToken: | ||
this.ionAccessToken || | ||
this.terria.configParameters.cesiumIonAccessToken, | ||
server: this.ionServer | ||
}) | ||
: this.url; | ||
|
||
if (resource === undefined) { | ||
return undefined; | ||
} | ||
|
||
const terrainProvider = new CesiumTerrainProvider({ | ||
url: resource, | ||
credit: this.attribution | ||
}); | ||
|
||
// Some network errors are not rejected through readyPromise, so we have to | ||
// listen to them using the error event and dispose it later | ||
let networkErrorListener: (err: any) => void; | ||
const networkErrorPromise = new Promise((_resolve, reject) => { | ||
networkErrorListener = reject; | ||
terrainProvider.errorEvent.addEventListener(networkErrorListener); | ||
}); | ||
|
||
const isReady = await Promise.race([ | ||
networkErrorPromise, | ||
terrainProvider.readyPromise | ||
]) | ||
.catch(() => false) | ||
.finally(() => | ||
terrainProvider.errorEvent.removeEventListener(networkErrorListener) | ||
); | ||
|
||
return isReady | ||
? terrainProvider | ||
: Promise.reject(TerriaError.from("Failed to load terrain provider")); | ||
} | ||
|
||
protected async forceLoadMapItems(): Promise<void> { | ||
const terrainProvider = await this.loadTerrainProvider(); | ||
runInAction(() => { | ||
this.terrainProvider = terrainProvider; | ||
}); | ||
} | ||
|
||
@computed | ||
get mapItems() { | ||
return this.show && this.terrainProvider ? [this.terrainProvider] : []; | ||
} | ||
} |
Oops, something went wrong.