Stack:
OpenLayers 8.2.0, Typescript 5.3.3, GeoServer 2.22
Goal:
Use an OpenLayers Extent ([minx, miny, maxx, maxy]) with getFeatureInfoUrl to run a GetFeatureInfo query to Geoserver and get back the info of all the features intersected by the extent. I tried with Geometric filters in CQL but it's not mandatory.
Code:
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;
}
}
Issue:
The point selection is working great but not the bbox selection. Ain't got any data in the response neither any error message so I don't know if it's in the OpenLayers' side or in the GeoServer' side. I've read old posts and other more recent about similar issues but didn't understood what was wrong...
EDIT:
I changed the source from TileWMS to ImageWMS but I still get an empty response with a incoherent bbox in my request:
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
I suspect you want to make a WFS GetFeature request with a
BBOXparameter instead of a WMS GetFeatureInfo request which is intended to get info at point.See GeoServer WFS GetFeature docs, search for BBOX.