|
3 | 3 | */
|
4 | 4 | import {get as getProjection} from 'ol/proj.js';
|
5 | 5 | import olcsUtil from '../util.js';
|
| 6 | +import {ENABLE_RASTER_REPROJECTION} from 'ol/reproj/common'; |
| 7 | +import olTileState from 'ol/TileState'; |
| 8 | +import {listen, unlistenByKey} from 'ol/events'; |
6 | 9 |
|
7 | 10 | class OLImageryProvider /* should not extend Cesium.ImageryProvider */ {
|
8 | 11 | /**
|
@@ -96,6 +99,9 @@ class OLImageryProvider /* should not extend Cesium.ImageryProvider */ {
|
96 | 99 | this.tilingScheme_ = new Cesium.GeographicTilingScheme();
|
97 | 100 | } else if (this.projection_ == getProjection('EPSG:3857')) {
|
98 | 101 | this.tilingScheme_ = new Cesium.WebMercatorTilingScheme();
|
| 102 | + } else if (ENABLE_RASTER_REPROJECTION) { |
| 103 | + this.tilingScheme_ = new Cesium.GeographicTilingScheme(); |
| 104 | + this.projection_ = getProjection('EPSG:4326'); |
99 | 105 | } else {
|
100 | 106 | return;
|
101 | 107 | }
|
@@ -138,24 +144,47 @@ class OLImageryProvider /* should not extend Cesium.ImageryProvider */ {
|
138 | 144 | * @override
|
139 | 145 | */
|
140 | 146 | requestImage(x, y, level) {
|
141 |
| - const tileUrlFunction = this.source_.getTileUrlFunction(); |
142 |
| - if (tileUrlFunction && this.projection_) { |
143 |
| - |
144 |
| - // Perform mapping of Cesium tile coordinates to OpenLayers tile coordinates: |
145 |
| - // 1) Cesium zoom level 0 is OpenLayers zoom level 1 for EPSG:4326 |
146 |
| - const z_ = this.tilingScheme_ instanceof Cesium.GeographicTilingScheme ? level + 1 : level; |
147 |
| - // 2) OpenLayers tile coordinates increase from bottom to top |
148 |
| - const y_ = -y - 1; |
149 |
| - |
150 |
| - let url = tileUrlFunction.call(this.source_, |
151 |
| - [z_, x, y_], 1, this.projection_); |
152 |
| - if (this.proxy_) { |
153 |
| - url = this.proxy_.getURL(url); |
154 |
| - } |
155 |
| - return url ? Cesium.ImageryProvider.loadImage(this, url) : this.emptyCanvas_; |
| 147 | + // Perform mapping of Cesium tile coordinates to ol3 tile coordinates: |
| 148 | + // 1) Cesium zoom level 0 is OpenLayers zoom level 1 for EPSG:4326 |
| 149 | + const z_ = this.tilingScheme_ instanceof Cesium.GeographicTilingScheme ? level + 1 : level; |
| 150 | + // 2) OpenLayers tile coordinates increase from bottom to top |
| 151 | + const y_ = -y - 1; |
| 152 | + |
| 153 | + const tilegrid = this.source_.getTileGridForProjection(this.projection_); |
| 154 | + if (z_ < tilegrid.getMinZoom() || z_ > tilegrid.getMaxZoom()) { |
| 155 | + return Promise.resolve(this.emptyCanvas_); // no data |
| 156 | + } |
| 157 | + |
| 158 | + const tile = this.source_.getTile(z_, x, y_, 1, this.projection_); |
| 159 | + |
| 160 | + tile.load(); |
| 161 | + |
| 162 | + // not yet loaded! |
| 163 | + // const image = tile.getImage(); |
| 164 | + // if (!image || !image.src) { |
| 165 | + // return this.emptyCanvas_; // no data |
| 166 | + // } |
| 167 | + |
| 168 | + |
| 169 | + const state = tile.getState(); |
| 170 | + if (state === olTileState.LOADED || state === olTileState.EMPTY) { |
| 171 | + return Promise.resolve(tile.getImage()) || undefined; |
| 172 | + } else if (state === olTileState.ERROR) { |
| 173 | + return undefined; // let Cesium continue retrieving later |
156 | 174 | } else {
|
157 |
| - // return empty canvas to stop Cesium from retrying later |
158 |
| - return this.emptyCanvas_; |
| 175 | + const promise = new Promise((resolve, reject) => { |
| 176 | + const unlisten = listen(tile, 'change', (evt) => { |
| 177 | + const state = tile.getState(); |
| 178 | + if (state === olTileState.LOADED || state === olTileState.EMPTY) { |
| 179 | + resolve(tile.getImage() || undefined); |
| 180 | + unlistenByKey(unlisten); |
| 181 | + } else if (state === olTileState.ERROR) { |
| 182 | + resolve(undefined); // let Cesium continue retrieving later |
| 183 | + unlistenByKey(unlisten); |
| 184 | + } |
| 185 | + }); |
| 186 | + }); |
| 187 | + return promise; |
159 | 188 | }
|
160 | 189 | }
|
161 | 190 | }
|
|
0 commit comments