简体   繁体   English

openlayers:重绘矢量图层而不再重新下载数据

[英]openlayers: redraw vector layer without downloading data again

What I need is a way to modify a vector layer representation without downloading the data again. 我需要的是一种修改矢量图层表示而无需再次下载数据的方法。 I have defined a GLM vector layer and a function called build_style to colorize their geometries according to some feature. 我已经定义了一个GLM矢量图层和一个名为build_style的函数,用于根据某些特征为其几何图形着色。 I have an HTML form that calls the function UpdateGlmLayer which is defined in this way: 我有一个HTML表单调用函数UpdateGlmLayer,它以这种方式定义:

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);
    glm.redraw();
};

the GLM layer is defined in this way: GLM层以这种方式定义:

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 UpdateGlmLayer is triggered the colors seem to change immediately but after that the system stops for approximately the same time it took to downloaded the data on the initial page load. 当触发UpdateGlmLayer时,颜色似乎立即发生变化,但之后系统停止的时间与初始页面加载下载数据的时间大致相同。 Nothing can be done during this time. 在此期间什么都做不了。 Is there something wrong? 有什么不对?

The problem is with you setting of the resFactor. 问题在于您设置resFactor。 I have created two demo maps, one loading some GeoServer GML vectors and restyling them without the resFactor 1 setting and another with the resFactor 1 setting and the second is definitely sending multiple requests. 我创建了两个演示映射,一个加载了一些GeoServer GML向量,并在不使用resFactor 1设置的情况下重新设置它们,另一个使用resFactor 1设置,第二个肯定是发送多个请求。 If you set the resfactor to anything above 1 this will not happen. 如果将resfactor设置为大于1的任何值,则不会发生这种情况。

No resFactor setting + clicking restyle 3 times gives this result: 没有resFactor设置+点击restyle 3次给出了这个结果:

3个Restyle Clicks,1个数据请求

Only 1 data request. 只有1个数据请求。

However, a resFactor setting of 3 + clicking restyle 3 times gives this result: 但是,resFactor设置3 +点击restyle 3次会得到以下结果: 3个Restyle点击,4个数据请求

4 data requests. 4个数据请求。

This I believe is the behavior you are seeing. 我相信这是你所看到的行为。 This looks like a bug to me as the documentation says what you have done is valid. 这看起来像是一个错误,因为文档说你所做的是有效的。 Looking at the code in the BBOX strategy js file the problem seems to be in the code: 看看BBOX策略js文件中的代码,问题似乎出现在代码中:

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

This is running on the .redraw() function to calculate whether it needs to reload the data. 这是在.redraw()函数上运行,以计算是否需要重新加载数据。 Because ratio will always be set to 1 in when you redraw the map (the resolution has not changed so this.resolution === this.layer.map.getResolution() ) then invalid will always be equal to true and therefore the layer reloads. 因为当您重绘地图时,比率将始终设置为1(分辨率未更改,因此this.resolution === this.layer.map.getResolution())则无效将始终等于true,因此图层重新加载。

resFactor resFactor

{Float} Optional factor used to determine when previously requested features are invalid. {Float}用于确定以前请求的功能何时无效的可选因子。 If set, the resFactor will be compared to the resolution of the previous request to the current map resolution. 如果设置,则将resFactor与先前请求的分辨率与当前地图分辨率进行比较。 If resFactor > (old / new) and 1/resFactor < (old / new). 如果resFactor>(旧/新)和1 / resFactor <(旧/新)。 If you set a resFactor of 1, data will be requested every time the resolution changes. 如果将resFactor设置为1,则每次分辨率更改时都会请求数据。 If you set a resFactor of 3, data will be requested if the old resolution is 3 times the new, or if the new is 3 times the old. 如果将resFactor设置为3,则如果旧分辨率是新分辨率的3倍,或者新分辨率是旧分辨率的3倍,则将请求数据。 If the old bounds do not contain the new bounds new data will always be requested (with or without considering resFactor). 如果旧边界不包含新边界,则将始终请求新数据(使用或不使用resFactor)。

I am doing the restyles in the following manner: 我正在以下列方式进行重新设计:

var style1, style2;


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


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

function restyle1()
{
    layer.styleMap = style1;
    layer.redraw();

}

function restyle2()
{
    layer.styleMap = style2;
    layer.redraw();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM