[GIS] OpenLayers Vector Layers: adding and removing features VS. redraw/refresh

dynamicopenlayers-2vector

I am currently using addFeatures and removeAllFeatures in my code to get the desired results and this is working OK.

However, I have been asked to ensure that the vector layers are not being recreated each time. "They should be loading new data not creating new layers." I'm in no position to argue, and I really think the way I have it set up via removing/adding is pretty decent.

The type of data is airplane data in an AJAX call. The airplane moves along its flight path.

3 layers in question: the airplane layer, the projected flight path, and the travelled flight path.

Currently I have something like so:

if (vectors_airplane != "" || vectors_projected_path != "" || vectors_flown_path != "") {
  vectors_projected_path.removeAllFeatures([projected_path]);
  vectors_flown_path.removeAllFeatures([tail_path]);
  vectors_airplane.removeAllFeatures([airplane]);
}

and then for adding (within the same ajax call):

vectors_flown_path.addFeatures([tail_path]);
vectors_projected_path.addFeatures([projected_path]);
vectors_airplane.addFeatures([airplane]);

These are vector layers but there is no associated WFS. It seems to use the refresh strategy appropriately there must be a WFS Protocol (please correct me if I am wrong). I don't want to add a WFS protocol to each layer here, as it seems unnecessary.

The goal: how to change this logic to not use removeAllFeatures and addFeatures and instead use refresh/redraw?

Best Answer

Here is what really happens.

A Vector Layer is basically just a container for Graphics in the map. It can come from WFS, a GeoJSON file, GeoRSS, KML, an Ajax Request (like in your case), from a drawing control of the map, and many more.

A Vector Layer has an array of features. If a protocol has been set for the layer (i.e. You have told the layer where to get the data from), OpenLayers will use that to request for data, and have those features in the features array. How much data is requested, and when, is decided by the Strategy that is set on the Vector Layer.

Just using removeAllFeatures and addFeatures on the Layer will not 'add and remove the layer'. It is the same layer. the layer is not destroyed and recreated.

To prove this, you can see that the layer id remains the same. You could also listen to the layer's removed & added event, and see when they are fired. If you want even more proof, just read the source code.


If you still need to use the refresh method, this is what I would do.

Initialize the Vector layers, with the HTTP Protocol. Make sure it points to your service which returns data for each layer (currently you might be having one service for all three layers. You will have to break it down into 3 different services, or at least pass different data depending on a parameter). Use the Fixed Strategy. Set the Appropriate Format.

Here is an example which does something similar, with gpx data: Vector Behavior Example

And when you need to refresh the data, just call the following:

vectors_flown_path.refresh({force: true}); 
vectors_projected_path.refresh({force: true}); 
vectors_airplane.refresh({force: true});

You will see that a request will be made to the service, and new data will be returned.

Behind the scenes, this and your present procedure are almost same, so it's up to you whether you follow this procedure or your current procedure. Performance wise, there will be no difference.