简体   繁体   English

如何获取开放层3中向量层内的向量元素数

[英]How to get the number of vector elements inside a vector layer in open layers 3

Does anybody know how to get a count for the number of features present in an OL3 vector layer? 有人知道如何对OL3向量层中存在的要素数量进行计数吗?

My vector layer is defined as follows, I'd like to know how many elements it has, and ideally how many are currently being rendered: 我的矢量层的定义如下,我想知道它有多少个元素,理想情况下当前正在渲染多少个元素:

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];
}
});

Once the (GeoJSON) features are loaded you can call getFeatures on the vector source to get an array with references to the features included in the vector source. 加载(GeoJSON)功能后,您可以在向量源上调用getFeatures以获取包含对向量源中包含的功能的引用的数组。 So, to get the number of features you can use the following: 因此,要获得功能数量,可以使用以下命令:

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

As stated above the source should be loaded for this to work. 如上所述,应加载源以使其正常工作。 If you pass a url option to the source constructor the source will use an Ajax request to download features. 如果将url选项传递给源构造函数,则源将使用Ajax请求下载功能。 This occurs asynchronously, meaning that the source won't contain features after construction. 这是异步发生的,这意味着源在构造后将不包含要素。

You can register a change listener on the vector source to know when it's loaded: 您可以在矢量源上注册一个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
  }
});

EDIT: I edited this to change from change:state to change as the event name. 编辑:我编辑此更改从change:state change为事件名称。

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

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