|
|
@@ -1,3 +1,27 @@
|
|
|
+const MAX_MERCATOR_LATITUDE = 85.0511287798066
|
|
|
+
|
|
|
+function tileX(longitude, level) {
|
|
|
+ const size = 2 ** level
|
|
|
+ return Math.floor(((longitude + 180) / 360) * size)
|
|
|
+}
|
|
|
+
|
|
|
+function tileY(latitude, level) {
|
|
|
+ const size = 2 ** level
|
|
|
+ const clamped = Math.max(-MAX_MERCATOR_LATITUDE, Math.min(MAX_MERCATOR_LATITUDE, latitude))
|
|
|
+ const radians = (clamped * Math.PI) / 180
|
|
|
+ return Math.floor(((1 - Math.log(Math.tan(radians) + 1 / Math.cos(radians)) / Math.PI) / 2) * size)
|
|
|
+}
|
|
|
+
|
|
|
+function tileInsideBounds(x, y, level, bounds) {
|
|
|
+ if (!Array.isArray(bounds) || bounds.length !== 4) return false
|
|
|
+ const x0 = tileX(bounds[0], level)
|
|
|
+ const x1 = tileX(bounds[2], level)
|
|
|
+ const y0 = tileY(bounds[1], level)
|
|
|
+ const y1 = tileY(bounds[3], level)
|
|
|
+ return x >= Math.min(x0, x1) && x <= Math.max(x0, x1)
|
|
|
+ && y >= Math.min(y0, y1) && y <= Math.max(y0, y1)
|
|
|
+}
|
|
|
+
|
|
|
/** 叠加影像:保留 Cesium 注入时序,URL 的 {reverseY} 表示 TMS 的 y。 */
|
|
|
export function createOverlayProvider(overlay, { UrlTemplateImageryProvider, Rectangle }) {
|
|
|
const options = { url: overlay.url }
|
|
|
@@ -7,18 +31,23 @@ export function createOverlayProvider(overlay, { UrlTemplateImageryProvider, Rec
|
|
|
if (overlay.credit) options.credit = overlay.credit
|
|
|
const provider = new UrlTemplateImageryProvider(options)
|
|
|
const requestImage = provider.requestImage
|
|
|
+ const excludeBounds = Array.isArray(overlay.excludeBounds) ? overlay.excludeBounds : []
|
|
|
let transparentImage
|
|
|
+ const emptyImage = () => {
|
|
|
+ if (!transparentImage) {
|
|
|
+ transparentImage = document.createElement('canvas')
|
|
|
+ transparentImage.width = 1
|
|
|
+ transparentImage.height = 1
|
|
|
+ }
|
|
|
+ return transparentImage
|
|
|
+ }
|
|
|
// Cesium 的失败回退会请求 minimumLevel 以下的父瓦片;声明级别外只透出底图。
|
|
|
- // 不捕获级别内的错误,不伪造缺失影像;同步保留 undefined 的请求节流语义。
|
|
|
+ // 环带资产按内圈整 tile 排除生成;命中排除矩形时返回透明图,避免向静态服务发必然 404。
|
|
|
provider.requestImage = function (...args) {
|
|
|
- const level = args[2]
|
|
|
- if (level < provider.minimumLevel || level > provider.maximumLevel) {
|
|
|
- if (!transparentImage) {
|
|
|
- transparentImage = document.createElement('canvas')
|
|
|
- transparentImage.width = 1
|
|
|
- transparentImage.height = 1
|
|
|
- }
|
|
|
- return Promise.resolve(transparentImage)
|
|
|
+ const [x, y, level] = args
|
|
|
+ if (level < provider.minimumLevel || level > provider.maximumLevel
|
|
|
+ || excludeBounds.some((bounds) => tileInsideBounds(x, y, level, bounds))) {
|
|
|
+ return Promise.resolve(emptyImage())
|
|
|
}
|
|
|
return requestImage.apply(this, args)
|
|
|
}
|