0

I am trying to use the DrawExtent interaction in OpenLayers5 to define the BBOX parameter of a WMS/WCS request to Geoserver. Using this technique I am able to retrieve the image subset I need with a custom WMS url, but when I set the BBOX in the params property of ImageWMS, the response defaults to the entire image.

I have tried using a "custom" url:

    'http://localhost:8080/geoserver/wms'+
    '?request=GetMap'+
    '&service=WMS'+
    '&version=1.3.0'+
    '&layers='+name+
    '&styles=""'+
    '&crs=EPSG:3857'+
  ->'&bbox='+bounds+
    '&width=665'+
    '&height=551'+
    '&format=image/geotiff';

to request the same subset which does work, but I cannot seem to get OpenLayers to recognize the GeoTIFF response as a layer (using ol.source.Image() and ol.layer.Image()). Thus I default back to ol.source.ImageWMS() which provides a param property that I can set BBOX to a string of coordinates. The issue is that these are overwritten(?) and the request is sent with BBOX defined as the image extent.

DRAW BOX EXTENT INTERACTION

var extent = new ol.interaction.Extent({
    condition: ol.events.conditions
});
map.addInteraction(extent);
extent.setActive(false);
window.addEventListener('keydown', function(event) {
    if (event.keyCode == 16) {
    extent.setActive(true);
    }
});
window.addEventListener('keyup', function(event) {
    if (event.keyCode == 16) {
        extent.setActive(false);
    var bbox = extent.getExtent();
    bbox[0]=Math.round(bbox[0]);
    bbox[1]=Math.round(bbox[1]);
    bbox[2]=Math.round(bbox[2]);
    bbox[3]=Math.round(bbox[3]);
    var bboxString = bbox.toString();
    getWMS('nurc:Img_Sample',bboxString);
    };
});

WMS GET MAP

function getWMS(name,bounds) {
var wmsSource = new ol.source.ImageWMS({
        url: 'http://localhost:8080/geoserver/wms',
        params: {LAYERS:name, BBOX:bounds, CRS:'EPSG:3857'},
        serverType: 'geoserver'
        crossOrigin: 'anonymous'
    });
var wmsLayer = new ol.layer.Image({
        source: wmsSource
    });
map.addLayer(wmsLayer);
};

The WMS request should have the BBOX property set to the bounds passed by the extent interaction, but the actual url is overwritten with the bounds of nurc:img_Sample

  • That is described in the API. https://openlayers.org/en/latest/apidoc/module-ol_source_ImageWMS-ImageWMS.html `WIDTH, HEIGHT, BBOX and CRS (SRS for WMS version < 1.3.0) will be set dynamically`. Set `projection` in the source options instead of CRS in `params`, and `extent` in the layer options to restrict the BBOX. – Mike Jul 30 '19 at 18:45
  • @Mike Thank you for the reply, I have made these changes but in doing so the request now fails to send, and no request is showing up in the console – Travis-ty Jul 30 '19 at 18:57
  • I got it, I was passing a string to the extent param instead of a float. Thanks for your help @Mike – Travis-ty Jul 30 '19 at 19:08

0 Answers0