openlayers:无法使用 MVT VectorTileSource 进行集群?

openlayers: cluster with MVT VectorTileSource impossible?

我是 openlayers 的新手,我想对矢量数据使用 cluster 函数。

如果我在集群选项中指定 source: MVT VectorTileSource ?!

,这似乎不起作用

代码如下。它在集群中工作正常。

不支持吗? 谢谢 彼得

var vectorTileSource = new VectorTileSource({
     format: new MVT(),
     url: 
         'http://xxxx/geoserver/gwc/service/tms/1.0.0/' + 'airports:airports' +
         '@EPSG%3A'+'900913'+
         '@pbf/{z}/{x}/{-y}.pbf'
});

var clusterSource = new Cluster({
     distance: 30, 
     source: vectorTileSource
});


var clusterLayer = new VectorTileLayer({
    source: vectorTileSource, //----> this works   
    source: clusterSource, // ---> does NOT work 
    style: clusterStyle 
  });

(仍然没有足够的声望来发表评论) 根据文档,集群需要 VectorSource。 OpenLayers 在 Vector- 和 VectorTile-Sources 之间有所不同,因为它们可能是非常不同的东西。

可能预期将 MVT 切片加载到矢量源的代码不会产生任何特征,也不会产生任何错误。看 https://gis.stackexchange.com/questions/225615/how-to-use-mapbox-vector-tiles-as-a-vector-source-in-ol3-so-that-labelling-will

然而,可以从法向矢量源中的矢量切片层复制特征,该法向矢量源可用于聚类源(并在具有轮廓密度的测试中工作)

var vtLayer = new ol.layer.VectorTile({
    source: new ol.source.VectorTile({
        format new ol.format.MVT({
            featureClass: ol.Feature    // important
        }),
        ....
        ....
    }),
    style: []   // layer must be added to map to load features, use empty style to avoid display
});
map.addLayer(vtLayer);

var vSource = new ol.source.Vector();   // use this as the source for a cluster source
var featuresForZ = [];
var viewZ;

function vsRefresh() {
    vSource.clear();
    if (featuresForZ[viewZ]) {
        vSource.addFeatures(featuresForZ[viewZ]);
    }
}

vtLayer.getSource().on('tileloadend', function(evt) {
    var z = evt.tile.getTileCoord()[0];
    var features = evt.tile.getFeatures();
    features.forEach(function (feature) {
        // each vector tile has its own set of feature ids, but duplicates are not allowed in normal vector sources
        feature.setId(undefined);   
    });
    if (!Array.isArray(featuresForZ[z])) { featuresForZ[z] = []; }
    featuresForZ[z] = featuresForZ[z].concat(features);
    if (z === viewZ) {
        vsRefresh();
    }
});

map.getView().on('change:resolution', function() {
    // use VT features from the tile z level corresponding to view resolution
    var newZ = vtLayer.getSource().getTileGrid().getZForResolution(map.getView().getResolution());
    if (newZ !== viewZ) {
        viewZ = newZ;
        vsRefresh();
    }
});

// now create the cluster layer
var vLayer = new ol.layer.Vector({
    source: new ol.source.Cluster({
        source: vSource,
        geometryFunction: function(feature){
           // test data is linestrings
           return new ol.geom.Point(ol.extent.getCenter(feature.getGeometry().getExtent()));
        },
    }),
    style: function(feature) {
        return new ol.style.Style({
            image: new ol.style.Circle({
                radius: feature.get('features').length * 5,
                fill: new ol.style.Fill({ color: 'black' })
            })
       });
    }
});
map.addLayer(vLayer);