openlayers:集群与MVT VectorTileSource不可能?

问题描述 投票:1回答:2

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

如果我在群集选项中指示为source: MVT VectorTileSource,这似乎不起作用?

代码如下。没有Cluster就可以正常工作。

它不受支持吗?谢谢彼得

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 
  });
openlayers
2个回答
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);

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.