1

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?

  • 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. – user30184 May 12 '16 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. – smartwepa May 12 '16 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. – user30184 May 12 '16 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. – ahocevar May 12 '16 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. – smartwepa May 18 '16 at 12:00
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.

  • So I create a column called FID with an iterating unique number and then geoserver no longer loads duplicate geometries on map panning? – Revo Aug 1 '17 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. – ahocevar Aug 2 '17 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. – jgrocha Oct 2 '17 at 23:46

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.