Openlayers: redraw vector layer without reloading data

I need a way to change the representation of a vector layer without reloading the data. I've defined a GLM feature layer and a build_style function to paint their geometry according to some peculiarity. I have an HTML form that calls the UpdateGlmLayer function, which is defined like this:

function UpdateGlmLayer(info_str) {
    var v = info_str.split("|");
    var filter_column = v[0];
    var values = [parseFloat(v[1]), parseFloat(v[2]), parseFloat(v[3])];
    glm.styleMap = build_style(filter_column, values);


the GLM layer is defined as follows:

gml_protocol = new OpenLayers.Protocol.HTTP({
    url: "http://localhost:8080/geoserver/ows?service=WFS&version=1.0.0&request=GetFeature&typeName="+info["layer_featurePrefix"]+":"+info["layer_featureType"],
    format: new OpenLayers.Format.GML()

glm = new OpenLayers.Layer.Vector(info["layer_name"], {
    strategies: [new OpenLayers.Strategy.BBOX({ratio: 3, resFactor: 1})],
    protocol: gml_protocol,
    styleMap: build_style(info["filter_property"], info["filter_values"]), 
    srsName: info["layer_srsName"],
    projection: new OpenLayers.Projection("EPSG:4326"),
    visibility: true


When you run UpdateGlmLayer, the colors seem to change immediately, but after that the system stops for about as long as it took to load the data on the initial page load. During this time nothing can be done. Something is wrong?


source to share

1 answer

The problem is you are setting resFactor. I created two demo maps, one loading some GML GeoServer vectors and restyling them without resFactor parameter 1, and the other with resFactor parameter 1, and the second one definitely sends multiple requests. If you set the resfactor to anything higher than 1, this will not happen.

No resFactor setting + pressing restyle button 3 times gives the following result:

3 Restyle Clicks, 1 data request

Only 1 data request.

However, the resFactor parameter 3 + click restyle 3 times gives the following result: 3 Restyle Clicks, 4 data requests

4 data requests.

This, I believe, is the behavior you are seeing. This looks like a bug to me, as the documentation says that everything you did is indeed. Looking at the code in the jos file of the BBOX strategy, the problem seems to be in the code:

var ratio = this.resolution /;
invalid = (ratio >= this.resFactor || ratio <= (1 / this.resFactor));


This is done in the .redraw () function to calculate the need for data reloading. Because redrawing the map will always be set to 1 (the resolution hasn't changed, so this.resolution === ()), then invalid will always be true, and so the layer is reloaded.


{Float} An optional factor used to determine if functions are invalid. If set, resFactor will be compared to the resolution of the previous request for the current resolution of the map. If resFactor> (old / new) and 1 / resFactor <(old / new). If you set resFactor to 1, the data will be requested every time the change permission is made. If you set resFactor to 3, data will be requested if the old resolution is 3 times the new one, or if the new one is 3 times the old one. If the old boundaries do not contain new boundaries, the new data will always be (with or without resFactor).

I am doing restyles like this:

var style1, style2;

style1 = new OpenLayers.Style({
                strokeColor: "yellow",
                strokeWidth: 10 });

style2 = new OpenLayers.Style({
                strokeColor: "blue",
                strokeWidth: 5 });

function restyle1()
    layer.styleMap = style1;


function restyle2()
    layer.styleMap = style2;





All Articles