2

I added a ol.source.Vector to my map which receives data from a wfs. Currently I am using ol.loadingstrategy.bbox because there is a lot of vector data and it would take too much time to load it all at once. The problem is that whenever I change the maps extent all the data which is in old and new extent is loaded and added to the source again. For example, If I just zoom out 3 times the data near to the map's center is added to the source 4 times. Obviously, after a while the performance is pretty bad because there is just too much data loaded.

Here is my code:

var wfsSource = new ol.source.Vector({ strategy: ol.loadingstrategy.bbox, format: new ol.format.GeoJSON(), url: getWFSURL }); function getWFSURL(extent){ var url = 'http://serveraddress/geoserver/ola/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=ola:V_GIS_VERLEGEBER_LN_PROD&outputFormat=application/json&srsname=EPSG:31467&bbox=' + extent.join(','); return url; } 

Is there a way to prevent OL3 from adding data multiple times?

5
  • You should perhaps keep track of the unique FIDs or gml:ids of the features, depending on the WFS version you are using, and add only those features which are new. Commented May 12, 2016 at 8:47
  • Technically this should be possible. But maybe it becomes time-consuming to check the ID of every existing feature everytime a new feature is beeing added. Commented May 12, 2016 at 9:17
  • If you keep on firing new requests with changing BBOX every time you pan or zoom that's about the only possibility you have if you want to avoid the trouble you have. I can imagine one workaround: build an aggregate polygon from all the BBOXes you have used. If the new view area after pan or zoom is withing that aggregate polygon there is no need to make a new request. I think that QGIS has an option to use this strategy. Perhaps you should have a look and study the source code if it could be used as an example. Commented May 12, 2016 at 9:28
  • 3
    OpenLayers does check for FIDs internally and will not add a previously added feature. So make sure that you get unique FIDs from GeoServer. If you have an incorrect configuration of your data source, you may end up getting random FIDs, and then OpenLayers cannot avoid the duplication on the client. Commented May 12, 2016 at 17:00
  • @ahocevar You are right. I checked that and figured out that the problem is indeed related to my data. I would accept that as the correct answer if I could. Commented May 18, 2016 at 12:00

2 Answers 2

3

OpenLayers does check for FIDs internally and will not add a previously added feature. So make sure that you get unique FIDs from GeoServer. If you have an incorrect configuration of your data source, you may end up getting random FIDs, and then OpenLayers cannot avoid the duplication on the client.

3
  • So I create a column called FID with an iterating unique number and then geoserver no longer loads duplicate geometries on map panning? Commented Aug 1, 2017 at 7:47
  • No. The FID is not a feature attribute. To get unique FIDs, you need to make sure that your data source has a unique, primary key field. Commented Aug 2, 2017 at 12:12
  • If Geoserver is not able to find your primary key, you can Control feature ID generation in spatial databases manually by creating a primary key metadata table. Commented Oct 2, 2017 at 23:46
3

I have had the same issue of the duplicating of features on map move when using mapserver and openlayers5 and WFS. The issue in my specific case was that mapserver puts the unique id field inside the properties collection, where as openlayers (according to what I read) expects it outside. The result is that the id_ field on each feature is not set by openlayers when readFeatures is run on the incoming WFS or GeoJSON data.

Response from mapserver contains an array of features like:

{type: "Feature", properties: {id: "24", name: ... 

Whereas other working online examples are in the form:

{type: "Feature", id: "24", properties: {name: ... 

The solution for me was to read the features from the WFS request into a variable, iterate through each of the features setting the feature id to the id value contained in the properties collection, and then read the features into the destination layer source. Openlayers no longer duplicates features once this is done.

 var url ... var field ... var vectorSource = new ol.source.Vector({ loader: function(extent) { $.ajax(url, { type: 'GET', data: { service: 'WFS', version: '1.0.0', request: 'GetFeature', typename: field, srsname: 'EPSG:2193', outputFormat: 'application/geojson', bbox: extent.join(',') + ',EPSG:2193' } }).done(function(response) { var tmp_feature=new ol.format.GeoJSON().readFeatures(response); var length=tmp_feature.length; for(var count=0; count<length; count++) { tmp_feature[count].id_=tmp_feature[count].get('id'); } vector.getSource().addFeatures(tmp_feature); }); }, strategy: ol.loadingstrategy.bbox, projection: 'EPSG:2193' }) 
3
  • You can set FORMATOPTION "USE_FEATUREID=true" to add the id to the features - see mapserver.org/output/ogr_output.html#outputformat-declarations Commented Aug 3, 2021 at 15:21
  • @madpom what is vector in vector.getSource().addFeatures(tmp_feature)? Commented Aug 17, 2022 at 9:13
  • sorry - should have made that clearer. that will be the layer, I believe. Commented Aug 18, 2022 at 10:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.