read and set features in custom tileLoadFunction

This commit is contained in:
mike-000
2020-04-17 00:01:28 +01:00
committed by GitHub
parent 7d2e367fd0
commit 71b866947f
+24 -26
View File
@@ -72,37 +72,35 @@ fetch(url)
extent: 4096, extent: 4096,
debug: 1, debug: 1,
}); });
const vectorSource = new VectorTileSource({ const format = new GeoJSON({
format: new GeoJSON({ // Data returned from geojson-vt is in tile pixel units
// Data returned from geojson-vt is in tile pixel units dataProjection: new Projection({
dataProjection: new Projection({ code: 'TILE_PIXELS',
code: 'TILE_PIXELS', units: 'tile-pixels',
units: 'tile-pixels', extent: [0, 0, 4096, 4096],
extent: [0, 0, 4096, 4096],
}),
}), }),
});
const vectorSource = new VectorTileSource({
tileUrlFunction: function (tileCoord) { tileUrlFunction: function (tileCoord) {
// Use the tile coordinate as a pseudo URL for caching purposes // Use the tile coordinate as a pseudo URL for caching purposes
return JSON.stringify(tileCoord); return JSON.stringify(tileCoord);
}, },
}); tileLoadFunction: function (tile, url) {
// For tile loading obtain the GeoJSON for the tile coordinate const tileCoord = JSON.parse(url);
// before calling the default tileLoadFunction const data = tileIndex.getTile(tileCoord[0], tileCoord[1], tileCoord[2]);
const defaultTileLoadFunction = vectorSource.getTileLoadFunction(); const geojson = JSON.stringify(
vectorSource.setTileLoadFunction(function (tile, url) { {
const tileCoord = JSON.parse(url); type: 'FeatureCollection',
const data = tileIndex.getTile(tileCoord[0], tileCoord[1], tileCoord[2]); features: data ? data.features : [],
const geojson = JSON.stringify( },
{ replacer
type: 'FeatureCollection', );
features: data ? data.features : [], const features = format.readFeatures(geojson, {
}, extent: vectorSource.getTileGrid().getTileCoordExtent(tileCoord),
replacer featureProjection: map.getView().getProjection()
); });
defaultTileLoadFunction( tile.setFeatures(features);
tile, },
'data:application/json;charset=UTF-8,' + geojson
);
}); });
const vectorLayer = new VectorTileLayer({ const vectorLayer = new VectorTileLayer({
source: vectorSource, source: vectorSource,