I want users to be able to download the map as an Image But whenever I include the NDVI Layer on the OSM layer and click on the download button. I get an Uncaught DOMException: Failed to execute ‘toDataURL’ on ‘HTMLCanvasElement.
1st, I have 2 base maps here:
var base_maps = new ol.layer.Group({
title: 'Base maps',
layers: [
new ol.layer.Tile({
title: 'Satellite',
type: 'base',
visible: true,
source: new ol.source.XYZ({
attributions: [
'Powered by Esri',
'Source: Esri, DigitalGlobe, GeoEye, Earthstar Geographics, CNES/Airbus DS, USDA, USGS, AeroGRID, IGN, and the GIS User Community',
],
attributionsCollapsible: false,
url: 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
maxZoom: 23,
}),
}),
new ol.layer.Tile({
title: 'OSM',
type: 'base',
visible: true,
source: new ol.source.OSM(),
}),
],
});
var OSM = new ol.layer.Tile({
source: new ol.source.OSM(),
type: 'base',
title: 'OSM',
});
overlays = new ol.layer.Group({
title: 'Overlays',
layers: [],
});
And I also have an NDVI Image Layer here coming from the GeoServer
var ndviLayer = new ol.layer.Image({ title: 'ndvi_layer',
source: new ol.source.ImageWMS({
url: 'http://24.199.122.17:8080/geoserver/wms',
params: {
LAYERS: 'NDVI:ndvi22',
VERSION: '1.1.0',
TRANSPARENT: true,
},
ratio: 1,
serverType: 'geoserver',
}),
});
2nd, This is my code line to export Map
// Begining of Export Map
document.getElementById('export-png').addEventListener('click', function () {
map.once('rendercomplete', function () {
const mapCanvas = document.createElement('canvas');
const size = map.getSize();
mapCanvas.width = size[0];
mapCanvas.height = size[1];
const mapContext = mapCanvas.getContext('2d');
Array.prototype.forEach.call(
map.getViewport().querySelectorAll('.ol-layer canvas, canvas.ol-layer'),
function (canvas) {
if (canvas.width > 0) {
const opacity =
canvas.parentNode.style.opacity || canvas.style.opacity;
mapContext.globalAlpha = opacity === '' ? 1 : Number(opacity);
let matrix;
const transform = canvas.style.transform;
if (transform) {
// Get the transform parameters from the style's transform matrix
matrix = transform
.match(/^matrix(([^(]*))$/)[1]
.split(',')
.map(Number);
} else {
matrix = [
parseFloat(canvas.style.width) / canvas.width,
0,
0,
parseFloat(canvas.style.height) / canvas.height,
0,
0,
];
}
// Apply the transform to the export map context
CanvasRenderingContext2D.prototype.setTransform.apply(
mapContext,
matrix
);
const backgroundColor = canvas.parentNode.style.backgroundColor;
if (backgroundColor) {
mapContext.fillStyle = backgroundColor;
mapContext.fillRect(0, 0, canvas.width, canvas.height);
}
// Add crossOrigin attribute to image elements
const images = canvas.parentNode.getElementsByTagName('img');
Array.prototype.forEach.call(images, function (image) {
image.setAttribute('crossOrigin', 'anonymous');
});
mapContext.drawImage(canvas, 0, 0);
}
}
);
mapContext.globalAlpha = 1;
mapContext.setTransform(1, 0, 0, 1, 0, 0);
const link = document.getElementById('image-download');
link.href = mapCanvas.toDataURL();
link.click();
});
map.renderSync();
}); // End of Export Map
The above export Map code works for only the OSM BaseMap Download
BUT
doesn’t work when the NDVI Image Layer is added; It gives the error below
enter image description here