Stack Exchange
Get rid of duplicate polygons using OpenLayers
OpenLayers 6, Mapserver 7

I have defined a vector source like this :
const presc = new ol.source.Vector({
  format: new ol.format.GeoJSON(),
  loader: function(extent, resolution, projection, success, failure) {
     // let proj = projection.getCode();
     let url = 'http://xxxx/cgi-bin/mapserv.exe?MAP=c:/ms4w/Apache/site/mapfile/naasp_ol.map&service=WFS&version=1.1.0&request=GetFeature&typename=emprise_3857&outputFormat=geojson&SRSNAME=EPSG:3857&' + 
                'bbox=' + extent.join(',') + ',' + 'EPSG:3857'
     let xhr = new XMLHttpRequest();
     xhr.open('GET', url);
     let onError = function() {
       presc.removeLoadedExtent(extent);
       failure();
     }
     xhr.onerror = onError;
     xhr.onload = function() {
       if (xhr.status == 200) {
         let features = presc.getFormat().readFeatures(xhr.responseText);
         console.log(features)
         presc.addFeatures(features);
         
         success(features);
       } else {
         onError();
       }
     }
     xhr.send();
   },
   strategy: ol.loadingstrategy.bbox
 });

and the vector layer :
const prescLayer = new ol.layer.Vector({
    source: presc,
});

The WFS is based on a PostGis view having 'id' as a unique field.
console.log(features) returns an array with a correct number of features each time the extent changes. But features are duplicated in the map!
I did not find recent posts with this issue. I did not go anywhere with these old ones  OL3... and OpenLayers strategy.


Are you showing feature.getId() or feature.get('id')?

I guess you mean in the label? feature.get('id')

That is a property but not the unique id.  To set that try insertingfeatures.forEach(function(feature)[feature.setId(feature.get('id');}); before calling addFeatures

that's it. Thanks. To be sure  with this,I create a unique id different from the one used for the label?

The GeoJSON spec allows an id property directly in the feature object, which is independent of one with the same key inside the properties object, but in your case it should be given the same value.  It may also be possible to configure a server to set id in the feature object.

Thanks for using MS4W, great to see it in action here!  PS. please consider hiding the map= mapfile path from your WFS service.  See my related answer at https://stackoverflow.com/questions/73200841/mapserver-hide-the-map-parameter-map-path
