openlayers:使用MVT VectorTileSource进行群集不可能吗?

时间:2019-03-14 11:28:50

标签: openlayers

我是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 
  });

2 个答案:

答案 0 :(得分:0)

(信誉尚不足以发表评论) 根据文档,群集需要一个VectorSource。 VectorLayer和VectorTile源之间的OpenLayers有所不同,因为它们可以是非常不同的东西。

答案 1 :(得分:0)

可以预期将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);