堆栈:
OpenLayers 8.2.0、Typescript 5.3.3、GeoServer 2.22
目标:
使用 OpenLayers Extent ([minx, miny, maxx, maxy]) 和 getFeatureInfoUrl 对 Geoserver 运行 GetFeatureInfo 查询,并获取与该范围相交的所有要素的信息。我尝试在 CQL 中使用几何过滤器,但这不是强制性的。
代码:
async function getFeatureInfo(layerList: string | undefined, coords: Array<number>, bbox: number[] | undefined) {
// if no layers displayed, do nothing
if (!layerList) return;
const wmsSource = new TileWMS({
url: GEOSERVER_URL, serverType: "geoserver", params: { LAYERS: layerList, QUERY_LAYERS: layerList }
});
const resolution = map.getView().getResolution();
const projection = map.getView().getProjection();
let url: string | undefined;
// if bbox != undefined, it's a query with a bbox, otherwise it's a query with a point
if (bbox) {
url = wmsSource.getFeatureInfoUrl(
getCenter(bbox),
resolution,
projection,
{
INFO_FORMAT: "application/json",
FEATURE_COUNT: 100,
cql_filter: `BBOX(geom, ${bbox.join(',')} )`
});
} else {
url = wmsSource.getFeatureInfoUrl(coords, resolution, projection, { INFO_FORMAT: "application/json", FEATURE_COUNT: 100 });
}
if (url) {
return (await Axios.get(url)).data;
}
}
问题:
点选择效果很好,但 bbox 选择效果不佳。响应中没有任何数据,也没有任何错误消息,所以我不知道它是在 OpenLayers 端还是在 GeoServer 端。 我读过关于类似问题的旧帖子和其他更新的文章,但不明白出了什么问题......
编辑:我将源从 TileWMS 更改为 ImageWMS,但我的请求中仍然收到空响应和不连贯的 bbox:
QUERY_LAYERS: geoserver:grid
INFO_FORMAT: application/json
REQUEST: GetFeatureInfo
SERVICE: WMS
VERSION: 1.3.0
FORMAT: image/png
STYLES:
TRANSPARENT: true
LAYERS: geoserver:grid
FEATURE_COUNT: 100
cql_filter: BBOX(geom, 186301.81,5824496.35,221462.13,5858713.41)
I: 50
J: 50
WIDTH: 101
HEIGHT: 101
CRS: EPSG:3857
BBOX: -6702148.56,-1064425.65,7109912.51,12747635.42
BBOX
参数发出 WFS GetFeature 请求,而不是旨在立即获取信息的 WMS GetFeatureInfo 请求。参见
GeoServer WFS GetFeature 文档,搜索 BBOX。