cesium 修改源码获取wmts图层的bbox

修改 WebMapServiceImageryProvider.js 文件

增加以下代码

1
this._capabilitiesRes = getCapabilities(options.url, parameters.layers, parameters.crs);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
async function getCapabilities(url, layerName, crs) {
try {
let linkStr = "?";
const urlArr = url.split("?");
if (urlArr.length > 1 && urlArr[1].length) {
linkStr = "&";
}
const metaDataUrl = `${url}${linkStr}service=WMS&request=GetCapabilities`;
const capabilities = await Resource.fetchXML(metaDataUrl);
const layersDom = capabilities.querySelectorAll("Layer");
let bounds = [];
let layer = null;
if (layerName) {
const index = Array.from(layersDom).findIndex(layer => {
const key = layer.getElementsByTagName("Title")[0].textContent;
return key === layerName;
});
if (~index) {
layer = layersDom[index];
}
} else {
layer = layersDom[0];
}
if (!layer) {
return bounds;
}
const boundingBoxes = Array.from(layer.getElementsByTagName("BoundingBox"));
boundingBoxes.forEach(boundingBox => {
const boundingCrs = boundingBox.getAttribute("CRS");
const minx = parseFloat(boundingBox.getAttribute("minx"));
const miny = parseFloat(boundingBox.getAttribute("miny"));
const maxx = parseFloat(boundingBox.getAttribute("maxx"));
const maxy = parseFloat(boundingBox.getAttribute("maxy"));
if (crs === "CRS:84" && crs === boundingCrs) {
bounds = [minx, miny, maxx, maxy];
} else if (
crs === "EPSG:3857" &&
(boundingCrs === "EPSG:900913" || boundingCrs === "EPSG:3857")
) {
bounds = [minx, miny, maxx, maxy];
} else if (crs === "EPSG:4326" && crs === boundingCrs) {
bounds = [miny, minx, maxy, maxx];
}
});
return bounds;
} catch (error) {
console.log("error", error);
}
}
作者

wangyawei

发布于

2025-03-20

更新于

2025-03-21

许可协议