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

Fix Cesium ion external asset handling #6140

Merged
merged 4 commits into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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 .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Apps/HelloWorld.html
Apps/Sandcastle/ThirdParty/**
Build/**
Documentation/**
Instrumented/**
Source/Shaders/**
Source/ThirdParty/**
Source/Workers/cesiumWorkerBootstrapper.js
Expand Down
131 changes: 77 additions & 54 deletions Source/Scene/CesiumIon.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,34 @@ define([
* });
*/
CesiumIon.createResource = function(assetId, options) {
var endpointResource = CesiumIon._createEndpointResource(assetId, options);

return CesiumIon._loadJson(endpointResource)
.then(function (endpoint) {

var externalType = endpoint.externalType;
if (!defined(externalType)) {
return CesiumIonResource.create(endpoint, endpointResource);
}

// 3D Tiles and STK Terrain Server external assets can still be represented as a resource
// object, just not the CesiumIonResource object.
if (externalType === '3DTILES' || externalType === 'STK_TERRAIN_SERVER') {
return new Resource({ url: endpoint.options.url });
}

//External imagery assets have additional configuration that can't be represented as a Resource
throw new RuntimeError('CesiumIon.createResource does not support external imagery assets; use CesiumIon.createImageryProvider instead.');
});
};

/**
* @private
*/
CesiumIon._createEndpointResource = function (assetId, options) {
//>>includeStart('debug', pragmas.debug);
Check.defined('assetId', assetId);
//>>includeEnd('debug');

options = defaultValue(options, defaultValue.EMPTY_OBJECT);
var serverUrl = defaultValue(options.serverUrl, CesiumIon.defaultServerUrl);
Expand All @@ -105,11 +132,27 @@ define([
resourceOptions.queryParameters = { access_token: accessToken };
}

var endpointResource = new Resource(resourceOptions);
return CesiumIon._loadJson(endpointResource)
.then(function(endpoint) {
return CesiumIonResource.create(endpoint, endpointResource);
});
return new Resource(resourceOptions);
};

function createFactory(Type) {
return function(options) {
return new Type(options);
};
}

// These values are the unofficial list of supported external imagery
// assets in the Cesium ion beta. They are subject to change.
var ImageryProviderMapping = {
ARCGIS_MAPSERVER: createFactory(ArcGisMapServerImageryProvider),
BING: createFactory(BingMapsImageryProvider),
GOOGLE_EARTH: createFactory(GoogleEarthEnterpriseMapsProvider),
MAPBOX: createFactory(MapboxImageryProvider),
SINGLE_TILE: createFactory(SingleTileImageryProvider),
TMS: createTileMapServiceImageryProvider,
URL_TEMPLATE: createFactory(UrlTemplateImageryProvider),
WMS: createFactory(WebMapServiceImageryProvider),
WMTS: createFactory(WebMapTileServiceImageryProvider)
};

/**
Expand All @@ -130,16 +173,36 @@ define([
* });
*/
CesiumIon.createImageryProvider = function(assetId, options) {
return CesiumIon.createResource(assetId, options)
.then(function(resource) {
return resource.createImageryProvider();
var endpointResource = CesiumIon._createEndpointResource(assetId, options);

return CesiumIon._loadJson(endpointResource)
.then(function(endpoint) {

if (endpoint.type !== 'IMAGERY') {
throw new RuntimeError('Cesium ion asset ' + assetId + ' is not an imagery asset.');
}

var externalType = endpoint.externalType;
if (!defined(externalType)) {
return createTileMapServiceImageryProvider({
url: CesiumIonResource.create(endpoint, endpointResource)
});
}

var factory = ImageryProviderMapping[externalType];

if (!defined(factory)) {
throw new RuntimeError('Unrecognized Cesium ion imagery type: ' + externalType);
}

return factory(endpoint.options);
});
};

/**
* A {@link Resource} instance that encapsulates Cesium ion asset
* creation and automatic refresh token handling. This object
* should not be created directly, use CesiumIonResource.create
* creation and automatic refresh token handling. This object
* should not be created directly, use CesiumIonResource.create.
*
* @private
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should CesiumIonResource be in its own file? Usually in Cesium if we have a class with a few functions, we usually split it out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved it, my main reason for keeping it off is because it ends up on the Cesium object in the combined version, but that's par for the course for us anyway. This should be good to go.

Expand All @@ -159,14 +222,11 @@ define([
CesiumIonResource.create = function (endpoint, endpointResource) {
var options = {
url: endpoint.url,
retryCallback: createRetryCallback(endpoint, endpointResource),
retryAttempts: 1
retryAttempts: 1,
queryParameters: { access_token: endpoint.accessToken },
retryCallback: createRetryCallback(endpoint, endpointResource)
};

if (defined(endpoint.accessToken)) {
options.queryParameters = { access_token: endpoint.accessToken };
}

return new CesiumIonResource(options, endpoint, endpointResource);
};

Expand All @@ -176,17 +236,15 @@ define([
}

CesiumIonResource.prototype.clone = function(result) {
// We always want to use the root's information because it's the most up-to-date
var ionRoot = defaultValue(this.ionRoot, this);

if (!defined(result)) {
// We always want to use the root's information because it's the most up-to-date
result = new CesiumIonResource({ url: this._url }, ionRoot.ionEndpoint, ionRoot.ionEndpointResource);
}

result = Resource.prototype.clone.call(this, result);
result.ionRoot = ionRoot;

// Same comment as above, use the root's access_token
result.queryParameters.access_token = ionRoot.queryParameters.access_token;
return result;
};
Expand Down Expand Up @@ -239,41 +297,6 @@ define([
return retryCallback;
}

function createFactory(Type) {
return function(options) {
return new Type(options);
};
}

// These values are the unofficial list of supported external imagery
// assets in the Cesium ion beta. They are subject to change.
var ImageryProviderMapping = {
ARCGIS_MAPSERVER: createFactory(ArcGisMapServerImageryProvider),
BING: createFactory(BingMapsImageryProvider),
GOOGLE_EARTH: createFactory(GoogleEarthEnterpriseMapsProvider),
MAPBOX: createFactory(MapboxImageryProvider),
SINGLE_TILE: createFactory(SingleTileImageryProvider),
TMS: createTileMapServiceImageryProvider,
URL_TEMPLATE: createFactory(UrlTemplateImageryProvider),
WMS: createFactory(WebMapServiceImageryProvider),
WMTS: createFactory(WebMapTileServiceImageryProvider)
};

CesiumIonResource.prototype.createImageryProvider = function() {
var type = this.ionEndpoint.type;
if (type === 'IMAGERY') {
return createTileMapServiceImageryProvider({ url: this });
}

var factory = ImageryProviderMapping[type];

if (!defined(factory)) {
throw new RuntimeError('Unrecognized Cesium ion imagery type: ' + type);
}

return factory(this.ionEndpoint);
};

//Exposed for testing
CesiumIon._CesiumIonResource = CesiumIonResource;
CesiumIon._loadJson = loadJson;
Expand Down
Loading