Openlayers 6:在点聚类之前通过属性过滤点向量层



按照Openlayers.org上的示例,我创建了一个点矢量图层(带有叠加等)。这些点显示为集群点,如下例所示:

Openlayers集群示例

现在,我需要根据它们的属性过滤点,例如lab_data = 'yes', sampling_year>1990年。过滤会影响簇内点的数量。

到目前为止,我还没有找到一种方法来排除我的源或层对象的特征。我甚至无法访问功能和它们的属性。从那以后,我可以创建循环和条件。

有没有人知道这是怎么做的?


// Read GeoJson point locations
var pointLocations = new ol.source.Vector({
url: 'data/samplingLocations.json',
format: new ol.format.GeoJSON()
});
// create point cluster
var pointCluster = new ol.source.Cluster({
distance: 50,
source: pointLocations
});
// create simple style for single points and clusters
var getStyle = function(feature) {
var length = feature.get('features').length;
if (length > 1) {
style = new ol.style.Style({
image: new ol.style.Circle({
radius: 20,
fill: new ol.style.Fill({ color: "red" }),
}),
})
} else {
style = new ol.style.Style({
image: new ol.style.Circle({
radius: 10,
fill: new ol.style.Fill({ color: "black" }),
}),
})
}
return style;
};
// create a vector layer
var vectorLayer = new ol.layer.Vector({
id: "points",
source: pointCluster,
style:   getStyle
});
// create the map
var map = new ol.Map({
layers: [vectorLayer],
target: 'map',
view: new ol.View({
center: [895571,5911157],
zoom: 8,
})
});

到目前为止,我尝试访问一些不成功的属性:

console.log( pointLocations.getSource().getFeatures()[0].getProperties().values)
console.log( vectorLayer.getSource().getFeatures()[0].get('values') )

每一个帮助都是非常感谢的!

您可以使用几何函数进行过滤,例如:

var pointCluster = new ol.source.Cluster({
distance: 50,
source: pointLocations,
geometryFunction: function(feature) {
if (feature.get('lab_data') == 'yes' && feature.get('sampling_year') > 1990) {
return feature.getGeometry();
} else {
return null;
}
}
});