M. Jansen, terrestris & A. Hocevar, w3geo
FOSSGIS 2023 | 15. März 2023 | Berlin
ol@x.y.z
?const layer = new TileLayer({
declutter: true,
source: new OGCVectorTile({
url: './ogcapi/collections/countries/tiles/WebMercatorQuad',
format: new MVT()
})
});
import { applyStyle } from 'ol-mapbox-style';
applyStyle(layer, './ogcapi/collections/countries/styles/default.json');
import { apply } from 'ol-mapbox-style';
apply(
'map',
'https://sgx.geodatenzentrum.de/gdz_basemapde_vektor/styles/bm_web_top.json'
);
var center = [10, 51];
const mbMap = new mapboxgl.Map({
style: 'https://sgx.geodatenzentrum.de/gdz_basemapde_vektor/styles/bm_web_top.json',
center: center,
container: 'map',
});
const mbLayer = new Layer({
render(frameState) {
const canvas = mbMap.getCanvas();
const viewState = frameState.viewState;
mbMap.jumpTo({
center: toLonLat(viewState.center),
zoom: viewState.zoom - 1,
animate: false
});
return canvas;
}
});
const map = new Map({
layers: [mbLayer],
target: 'map',
view: new View({
center: fromLonLat(center),
zoom: 6
})
});
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
layer.setTileLoadFunction(async (tile, src) => {
const tileCoord = tile.getTileCoord();
const extent = tileGrid.getTileCoordExtent(tileCoord);
const resolution = tileGrid.getResolution(tileCoord[0])
const tileSize = tileGrid.getTileSize(tileCoord[0]);
canvas.width = tileSize;
canvas.height = tileSize;
const {gradientImageData} =
await getGradientData(extent, tileSize, tileSize, resolution);
context.putImageData(gradientImageData, 0, 0);
tile.getImage().src = canvas.toDataURL('image/png');
};
async function getGradientData(bbox, width, height, resolution) {
// Höhen aus dem geotiff laden
const dtm = (await geotiff.readRasters({ bbox, width, height }))[0];
const gradientData = new Uint8ClampedArray(width * height);
// befüllen mit den Hangneigungsklassen
// https://pro.arcgis.com/en/pro-app/2.9/tool-reference/spatial-analyst/how-slope-works.htm
const gradientImageData = context.createImageData(width, height);
// befüllen mit Farben für Hangneigungsklassen
return {gradientData, gradientImageData};
}
const target = document.createElement('div');
const headlessMap = new Map({
target,
pixelRatio: 1,
layers: [new VectorLayer({ source: selected, style: blackFill })],
view: new View({
resolution: 1,
center: getCenter(geomExtent)
})
});
headlessMap.setSize([getWidth(geomExtent), getHeight(geomExtent)]);
headlessMap.renderSync();
const imageURL = target.querySelector('canvas').toDataURL('image/png');
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const maskPixel = schlagImageData.slice(4 * (y * width + x), 4);
const alpha = maskPixel[3] / 255;
if (alpha > 0) {
const area = resolution * resolution * alpha;
classes[gradientData[y * width + x]] += area;
}
}
}
const drawInteraction = new Draw({
type: 'Polygon',
source: drawVector.getSource(),
trace: true,
traceSource: baseVector.getSource(),
style: {
'stroke-color': 'rgba(255, 255, 100, 0.5)',
'stroke-width': 1.5,
'fill-color': 'rgba(255, 255, 100, 0.25)',
'circle-radius': 6,
'circle-fill-color': 'rgba(255, 255, 100, 0.5)',
},
});
const classes = {
'10': 'forest',
'20': 'low vegetation',
'30': 'water',
'40': 'built-up',
'50': 'bare soil',
'60': 'agriculture'
}
// band 1 is 2020, band 2 is 2016
{
color: [
'case',
['==', ['band', 1], ['band', 2]],
[0, 0, 0, 0], // equal, make them transparent
['all', ['==', ['band', 1], 10], ['!=', ['band', 2], 10] ],
[30, 240, 0, 1], // wald in 2020, irgendwas nicht waldiges in 2016
['all', ['==', ['band', 2], 10], ['!=', ['band', 1], 10] ],
[240, 10, 0, 1], // wald in 2016, irgendwas nicht waldiges in 2020
['all', ['==', ['band', 1], 40], ['!=', ['band', 2], 40] ],
[255, 255, 255, 1], // built-up in 2020, irgendwas nicht built-up in 2016
[0, 0, 0, 0] // hide any other differences
]
}
Diese Folien sind unter CC BY-SA veröffentlicht.