1

I'm trying to change raster layer with a selector.

I don't understand why the clear method don't work on tileWMS however it's on the API doc...

For a vector source, on an other app, it work :

var cqlFilter = 'date_debut BETWEEN 1000 AND 2000 OR date_fin BETWEEN 1000 AND 2000';
var urlTemplate = 'http://serveradress/geoserver/applications/wfs?service=WFS&' +
'version=1.0.0&request=GetFeature&' +
'typeNames=applications:patrimoine&' +
'CQL_FILTER=' + cqlFilter +
'&outputFormat=application/json&' +
'srsName=EPSG:4326';
console.log(urlTemplate);
var _vectorSource = new ol.source.Vector({
 format: new ol.format.GeoJSON(),
 url: function() {return urlTemplate;}
});
function changeDate(years) {
 var dateMin = years[0];
 var dateMax = years[1];
 var cqlNewFilter = '(date_debut BETWEEN '+ dateMin + ' AND ' + dateMax + ' OR date_fin BETWEEN ' + dateMin + ' AND ' + dateMax + ')';
 urlTemplate = 'http://serveradress/geoserver/applications/wfs?service=WFS&' +
 'version=1.0.0&request=GetFeature&' +
 'typeNames=applications:patrimoine&' +
 'CQL_FILTER=' + cqlNewFilter +
 '&outputFormat=application/json&' +
 'srsName=EPSG:4326';
 _vectorSource.clear(true);
 _vectorSource.refresh();
};
var _cluster = new ol.source.Cluster({
 distance: 50,
 source: _vectorSource
});
var _layer = new ol.layer.Vector({
 source: _cluster,
 style: _patrimoineStyle
});

But whith a TileWMS layer it don't work, it tells me that _rasterSource.clear is not a function :

var anneeCouche = "2016_20cm";
var _rasterSource = new ol.source.TileWMS({
 url: 'http://serveradress/geoserver/raster/wms?',
 params: {LAYERS: 'raster:ortho'+anneeCouche}
});
function changeDate(annee) {
 if (annee === '2013') {
 var anneeCouche = "2013_20cm";
 } else {
 var anneeCouche = "2016_20cm";
 };
 _rasterSource.clear(true);
 _rasterSource.refresh();
};
var _layer = new ol.layer.Tile({
 title: 'Ortho',
 source: _rasterSource
});
asked Apr 9, 2020 at 15:36
1
  • Which version of OL are you using? clear method was introduced for ol.source.TileWMS in OL6 (and it is without parameter). Commented Apr 9, 2020 at 18:10

1 Answer 1

1

Changing the value of anneeCouche won't update the value of params which have already been evaluated. Instead you should use updateParams (the source should be automatically refreshed)

function changeDate(annee) {
 if (annee === '2013') {
 var anneeCouche = "2013_20cm";
 } else {
 var anneeCouche = "2016_20cm";
 };
 _rasterSource.updateParams({LAYERS: 'raster:ortho'+anneeCouche});
};
answered Apr 9, 2020 at 19:47
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.