1

I have a tile later with a WMS source coming from GeoServer. I want to add and change a filter to it when a users changes their selection. I can see some examples for Openlayers 3 and Vector layers but the OpenLayers.Filter class has changed and I can't get it to have any effect.

import OlTileLayer from 'ol/layer/Tile';
import OlOr from 'ol/format/filter/Or';
import OlEq from 'ol/format/filter/EqualTo';



this.structureLayer = new OlTileLayer({
  source: new TileWMS({
    url: 'http://test.com/fpat/wms', 
    params: {'LAYERS': 'fpat:fpat_surveys',
              'TILED': true},
    serverType: 'geoserver',
    transition: 0
  })
});


 this.structureLayer.filter = new OlOr(new OlEq('structure_types', 'Natural') , new OlEq('structure_types', 'Other'));

When the user changes their selection I'll need to rebuild the filter and make sure the layer updates. The documentation for a vector layer with a spatial filter suggests this, but I I get an error because refresh doesn't exist

this.structureLayer.refresh({force: true})
2

You need to refresh the params of the source of structureLayer. Don't try to use OpenLayers 2 syntax thinking it would work by luck on OpenLayers 3+: OpenLayers 3+ is a full rewrite (remark comes from your attempt to use this.structureLayer.refresh({force: true}))

It should be more

var params = this.structureLayer.geSource().getParams();
// I choose to use cql_filter instead of sld
params.cql_filter = "structure_types IN ('Natural', 'Other')";
// Uncomment line below and comment line above if you prefer using sld
// params.sld_body = "yourxmlfiltercontent"; 
this.structureLayer.geSource().updateParams(params);

You can use filter but you will need to create your own XML as the ol.format.filter are intended for WFS not WMS. That's why I've use cql_filter instead above (it does the same job with a more simple syntax)

You can see a demo to show "it works"

You should also consider using POST (default behaviour using GET and there are restrictions on length of HTTP GET call Url). You may need to look at this related post OpenLayers 3 WMS Styling Using SLD_BODY and POST request

PS: I'm using the "OpenLayers pre-ES6" syntax in the demo but the logic remains the same.

  • Thanks for that will try asap. Since the number of options the user can turn on may make a cql filter too long for a URL I might go for the XML filter. I can write it manually but I don't know how to apply that to a layer in OL. Could you add that to your answer? – Craig Aug 8 '18 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.