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
});
  • Which version of OL are you using? clear method was introduced for ol.source.TileWMS in OL6 (and it is without parameter). – TomazicM Apr 9 at 18:10
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});
};
| improve this answer | |

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.