|
| 1 | +define([ |
| 2 | + './Check', |
| 3 | + './Credit', |
| 4 | + './defaultValue', |
| 5 | + './defined', |
| 6 | + './Resource', |
| 7 | + './RuntimeError', |
| 8 | + './CesiumIonResource' |
| 9 | + ], function( |
| 10 | + Check, |
| 11 | + Credit, |
| 12 | + defaultValue, |
| 13 | + defined, |
| 14 | + Resource, |
| 15 | + RuntimeError, |
| 16 | + CesiumIonResource) { |
| 17 | + 'use strict'; |
| 18 | + |
| 19 | + /** |
| 20 | + * Utility object for working with the Cesium ion API. |
| 21 | + * |
| 22 | + * @see https://cesium.com |
| 23 | + * |
| 24 | + * @exports CesiumIon |
| 25 | + */ |
| 26 | + var CesiumIon = {}; |
| 27 | + |
| 28 | + /** |
| 29 | + * The default Cesium ion access token to use. |
| 30 | + * |
| 31 | + * @type {String} |
| 32 | + */ |
| 33 | + CesiumIon.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI0NDViM2NkNi0xYTE2LTRlZTUtODBlNy05M2Q4ODg4M2NmMTQiLCJpZCI6MjU5LCJpYXQiOjE1MTgxOTc4MDh9.sld5jPORDf_lWavMEsugh6vHPnjR6j3qd1aBkQTswNM'; |
| 34 | + |
| 35 | + /** |
| 36 | + * The default Cesium ion server to use. |
| 37 | + * |
| 38 | + * @type {String} |
| 39 | + * @default https://api.cesium.com |
| 40 | + */ |
| 41 | + CesiumIon.defaultServerUrl = 'https://api.cesium.com'; |
| 42 | + |
| 43 | + /** |
| 44 | + * Asynchronously creates a {@link Resource} representing a Cesium ion asset. |
| 45 | + * |
| 46 | + * @param {Number} assetId The Cesium ion asset id. |
| 47 | + * @param {Object} [options] An object with the following properties: |
| 48 | + * @param {String} [options.accessToken=CesiumIon.defaultAccessToken] The access token to use. |
| 49 | + * @param {String} [options.serverUrl=CesiumIon.defaultServerUrl] The url to the Cesium ion API server. |
| 50 | + * @returns {Promise.<Resource>} A Promise to a Resource representing the Cesium ion Asset. |
| 51 | + * |
| 52 | + * @example |
| 53 | + * //Load a Cesium3DTileset with asset ID of 124624234 |
| 54 | + * Cesium.CesiumIon.createResource(124624234) |
| 55 | + * .then(function (resource) { |
| 56 | + * viewer.scene.primitives.add(new Cesium.Cesium3DTileset({ url: resource })); |
| 57 | + * }); |
| 58 | + * |
| 59 | + * @example |
| 60 | + * //Load a CZML file with asset ID of 10890 |
| 61 | + * Cesium.CesiumIon.createResource(10890) |
| 62 | + * .then(function (resource) { |
| 63 | + * viewer.dataSources.add(Cesium.CzmlDataSource.load(resource)); |
| 64 | + * }); |
| 65 | + * |
| 66 | + * @example |
| 67 | + * //Load an ImageryProvider with asset ID of 2347923 |
| 68 | + * Cesium.CesiumIon.createResource(2347923) |
| 69 | + * .then(function (resource) { |
| 70 | + * viewer.imageryLayers.addProvider(Cesium.createTileMapServiceImageryProvider({url : resource })); |
| 71 | + * }); |
| 72 | + */ |
| 73 | + CesiumIon.createResource = function(assetId, options) { |
| 74 | + var endpointResource = CesiumIon._createEndpointResource(assetId, options); |
| 75 | + |
| 76 | + return CesiumIon._loadJson(endpointResource) |
| 77 | + .then(function (endpoint) { |
| 78 | + |
| 79 | + var externalType = endpoint.externalType; |
| 80 | + if (!defined(externalType)) { |
| 81 | + return CesiumIonResource.create(endpoint, endpointResource); |
| 82 | + } |
| 83 | + |
| 84 | + // 3D Tiles and STK Terrain Server external assets can still be represented as a resource |
| 85 | + // object, just not the CesiumIonResource object. |
| 86 | + if (externalType === '3DTILES' || externalType === 'STK_TERRAIN_SERVER') { |
| 87 | + var resource = new Resource({ url: endpoint.options.url }); |
| 88 | + |
| 89 | + resource.credits = endpoint.attributions.map(function(attribution) { |
| 90 | + return new Credit({ |
| 91 | + text: attribution.text, |
| 92 | + link: attribution.url, |
| 93 | + imageUrl: attribution.image, |
| 94 | + showOnScreen: defined(attribution.collapsible) && !attribution.collapsible |
| 95 | + }); |
| 96 | + }); |
| 97 | + resource.ionEndpoint = endpoint; |
| 98 | + var oldClone = resource.clone; |
| 99 | + resource.clone = function(result) { |
| 100 | + result = oldClone.apply(resource, result); |
| 101 | + result.ionEndpoint = this.ionEndpoint; |
| 102 | + result.credits = this.credits; |
| 103 | + return result; |
| 104 | + }; |
| 105 | + return resource; |
| 106 | + } |
| 107 | + |
| 108 | + //External imagery assets have additional configuration that can't be represented as a Resource |
| 109 | + throw new RuntimeError('CesiumIon.createResource does not support external imagery assets; use IonImageryProvider instead.'); |
| 110 | + }); |
| 111 | + }; |
| 112 | + |
| 113 | + /** |
| 114 | + * @private |
| 115 | + */ |
| 116 | + CesiumIon._createEndpointResource = function (assetId, options) { |
| 117 | + //>>includeStart('debug', pragmas.debug); |
| 118 | + Check.defined('assetId', assetId); |
| 119 | + //>>includeEnd('debug'); |
| 120 | + |
| 121 | + options = defaultValue(options, defaultValue.EMPTY_OBJECT); |
| 122 | + var serverUrl = defaultValue(options.serverUrl, CesiumIon.defaultServerUrl); |
| 123 | + var accessToken = defaultValue(options.accessToken, CesiumIon.defaultAccessToken); |
| 124 | + |
| 125 | + var resourceOptions = { |
| 126 | + url: serverUrl + '/v1/assets/' + assetId + '/endpoint' |
| 127 | + }; |
| 128 | + |
| 129 | + if (defined(accessToken)) { |
| 130 | + resourceOptions.queryParameters = { access_token: accessToken }; |
| 131 | + } |
| 132 | + |
| 133 | + return new Resource(resourceOptions); |
| 134 | + }; |
| 135 | + |
| 136 | + //Exposed for testing |
| 137 | + CesiumIon._loadJson = function(resource) { |
| 138 | + return resource.fetchJson(); |
| 139 | + }; |
| 140 | + |
| 141 | + return CesiumIon; |
| 142 | +}); |
0 commit comments