【问题标题】:How to get the number of vector elements inside a vector layer in open layers 3如何在开放层3中获取矢量图层内的矢量元素数量
【发布时间】:2014-11-24 15:39:48
【问题描述】:

有人知道如何计算 OL3 矢量图层中存在的特征数量吗?

我的矢量图层定义如下,我想知道它有多少个元素,理想情况下当前正在渲染多少个:

var styleCache = {};
var WFS_layer_Traffic_Lights = new ol.layer.Vector({
source : new ol.source.GeoJSON({
    projection : 'EPSG:3857',
    url : "Vector_Data/Traffic_Lights_Bordeaux.geojson"
}),


style : function(feature, resolution) {

    var path;
    var x_anchor;
    var y_anchor;       

    if(resolution < 4){
    path = 'Icons/Traffic_Lights_Sign_Icon_Small.png';
    x_anchor = 23;
    y_anchor = 90;
    }
    if(resolution >= 4 && resolution < 10){
    path = 'Icons/Traffic_Lights_Sign_Small.png'; 
    x_anchor = 16;
    y_anchor = 16;
    }       
    if(resolution >= 10){
    path = 'Icons/Traffic_Lights_Sign_Tiny.png';        
    x_anchor = 10;
    y_anchor = 10;
    }


    if (!styleCache[path]) {
        styleCache[path] = [new ol.style.Style({
            fill : new ol.style.Fill({
                color : 'rgba(255, 255, 255, 0.1)'
            }),
            stroke : new ol.style.Stroke({
                color : '#319FD3',
                width : 1
            }),
            image: new ol.style.Icon(({
                    anchor: [x_anchor, y_anchor],
                    anchorXUnits: 'pixels',
                    anchorYUnits: 'pixels',
                    src: path
                })),
            text : new ol.style.Text({
                font : '12px Calibri,sans-serif',
                text : "",
                fill : new ol.style.Fill({
                    color : '#000'
                }),
                stroke : new ol.style.Stroke({
                    color : '#fff',
                    width : 4
                })
            }),
            zIndex : 1
        })];
    }
    return styleCache[path];
}
});

【问题讨论】:

    标签: javascript openlayers-3


    【解决方案1】:

    加载 (GeoJSON) 特征后,您可以在矢量源上调用 getFeatures 以获取一个数组,其中包含对矢量源中包含的特征的引用。因此,要获得功能数量,您可以使用以下方法:

    var featureCount = vectorLayer.getSource().getFeatures().length;
    

    如上所述,应加载源代码才能使其正常工作。如果您将url 选项传递给源构造函数,源将使用Ajax 请求来下载功能。这是异步发生的,这意味着构建后源将不包含特征。

    您可以在向量源上注册change 监听器以了解它何时加载:

    var vectorSource = vectorLayer.getSource();
    var listenerKey = vectorSource.on('change', function(e) {
      if (vectorSource.getState() == 'ready') {
        var featureCount = vectorSource.getFeatures().length;
        // ...
        ol.Observable.unByKey(listenerKey);
        // use vectorSource.unByKey(listenerKey) instead
        // if you do use the "master" branch of ol3
      }
    });
    

    编辑:我将其编辑为从 change:state 更改为 change 作为事件名称。

    【讨论】:

    • 谢谢,你也为我回答了两个问题,因为我想知道一旦我被告知如何获取 Ajax 请求线程的异步特性特征的数量。这么好的答案虽然我想知道......
    • 正如问题的最后一部分所述,我想知道@erilem 是否也可以获得渲染的元素数量?再次感谢。
    • 您可以使用view.calculateExtent(map.getSize) 获取当前视图范围,使用vectorSource.getFeaturesInExtent 获取该范围的要素。但这不是万无一失的,因为当视图旋转时它不会为您提供正确的结果,因为calculateExtent 中的问题,并且因为可能存在超出范围但在地图上仍然可见的特征(因为它们是例如用大符号表示)。所以请谨慎使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多