Clip tile layers by extent

This commit is contained in:
Tim Schaub
2018-11-16 12:34:09 +01:00
parent 73ffda10db
commit 038f122d11
6 changed files with 82 additions and 50 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

View File

@@ -1,47 +0,0 @@
import Map from '../../../src/ol/Map.js';
import View from '../../../src/ol/View.js';
import TileLayer from '../../../src/ol/layer/Tile.js';
import {fromLonLat} from '../../../src/ol/proj';
import XYZ from '../../../src/ol/source/XYZ';
import {getSize} from '../../../src/ol/extent';
const center = fromLonLat([8.6, 50.1]);
const map = new Map({
target: 'map',
view: new View({
center: center,
zoom: 3
})
});
const layerExtent = centerExtent();
map.addLayer(
new TileLayer({
source: new XYZ({
url: '/data/tiles/satellite/{z}/{x}/{y}.jpg'
}),
extent: layerExtent
}),
);
map.addLayer(
new TileLayer({
source: new XYZ({
url: '/data/tiles/stamen-labels/{z}/{x}/{y}.png'
}),
extent: layerExtent
})
);
render();
function centerExtent() {
const c = map.getView().calculateExtent([256, 256]);
const qw = getSize(c)[0] / 4;
const qh = getSize(c)[1] / 4;
return [c[0] + qw, c[1] + qh, c[2] - qw, c[3] - qh];
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

View File

@@ -0,0 +1,40 @@
/**
* Tile layers get clipped to their extent.
*/
import Map from '../../../src/ol/Map.js';
import View from '../../../src/ol/View.js';
import TileLayer from '../../../src/ol/layer/Tile.js';
import {fromLonLat} from '../../../src/ol/proj';
import {transformExtent} from '../../../src/ol/proj.js';
import XYZ from '../../../src/ol/source/XYZ';
const center = fromLonLat([7, 50]);
const extent = transformExtent([2, 47, 10, 53], 'EPSG:4326', 'EPSG:3857');
new Map({
target: 'map',
view: new View({
center: center,
zoom: 3
}),
layers: [
new TileLayer({
source: new XYZ({
url: '/data/tiles/satellite/{z}/{x}/{y}.jpg',
maxZoom: 3
}),
extent: extent
}),
new TileLayer({
source: new XYZ({
url: '/data/tiles/stamen-labels/{z}/{x}/{y}.png',
minZoom: 3,
maxZoom: 5
}),
extent: extent
})
]
});
render();