Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

WV-3450 Show Dams Layer When Disabling Classifications Fix (#5789)

christof-wittreich6a523d19a3a2
diff --git a/web/js/ol/lookupimagetile.js b/web/js/ol/lookupimagetile.js
index 5a865ba01..865ca5081 100644
--- a/web/js/ol/lookupimagetile.js
+++ b/web/js/ol/lookupimagetile.js
@@ -142,46 +142,76 @@ LookupImageTile.prototype.load = async function() {
const bufferSize = height * width * 4;
const arrBuffer = new Uint32Array(bufferSize);
- // Extract the colormap values. This is an array of integers representing rgba values.
- // Used in sets of 4 (i.e. colorMapArr[0] = r, colorMapArr[1] = b, etc.)
- // colorMapArr assumes a max of 256 colors
- const colorMapArr = getColormap(decodedPNG.tabs.PLTE);
-
// Extract the pixel data. This is an array of integers corresponding to the colormap
// i.e. if pixelData[0] == 5, this pixel is the color of the 5th entry in the colormap
const pixelData = decodedPNG.data;
- // iterate through the pixelData, drawing each pixel using the appropriate color
- for (let i = 0; i < pixelData.length; i += 1) {
- const arrBuffIndex = i * 4;
- const lookupIndex = pixelData[i] * 4;
-
- // Determine desired RGBA for this pixel
- const r = colorMapArr[lookupIndex];
- const g = colorMapArr[lookupIndex + 1];
- const b = colorMapArr[lookupIndex + 2];
- const a = 255;
- // Concatentate to 'r,g,b,a' string & check if that color is in the pixelsToDisplay array
- const rgbaStr = `${r},${g},${b},${a}`;
- const drawThisColor = pixelsToDisplay[rgbaStr];
-
- // If the intended color exists in pixelsToDisplay obj, draw that color, otherwise draw transparent
- if (drawThisColor !== undefined) {
- arrBuffer[arrBuffIndex + 0] = r;
- arrBuffer[arrBuffIndex + 1] = g;
- arrBuffer[arrBuffIndex + 2] = b;
- arrBuffer[arrBuffIndex + 3] = a;
- } else {
- // console.log('drawThisColor undefined, rgbaStr:', rgbaStr);
- arrBuffer[arrBuffIndex] = 0;
- arrBuffer[arrBuffIndex + 1] = 0;
- arrBuffer[arrBuffIndex + 2] = 0;
- arrBuffer[arrBuffIndex + 3] = 0;
+ let encodedBufferImage;
+
+ if (decodedPNG.tabs && decodedPNG.tabs.PLTE) {
+ // Extract the colormap values. This is an array of integers representing rgba values.
+ // Used in sets of 4 (i.e. colorMapArr[0] = r, colorMapArr[1] = b, etc.)
+ // colorMapArr assumes a max of 256 colors
+ const colorMapArr = getColormap(decodedPNG.tabs.PLTE);
+
+ // iterate through the pixelData, drawing each pixel using the appropriate color
+ for (let i = 0; i < pixelData.length; i += 1) {
+ const arrBuffIndex = i * 4;
+ const lookupIndex = pixelData[i] * 4;
+
+ // Determine desired RGBA for this pixel
+ const r = colorMapArr[lookupIndex];
+ const g = colorMapArr[lookupIndex + 1];
+ const b = colorMapArr[lookupIndex + 2];
+ const a = 255;
+ // Concatentate to 'r,g,b,a' string & check if that color is in the pixelsToDisplay array
+ const rgbaStr = `${r},${g},${b},${a}`;
+ const drawThisColor = pixelsToDisplay[rgbaStr];
+
+ // If the intended color exists in pixelsToDisplay obj, draw that color, otherwise draw transparent
+ if (drawThisColor !== undefined) {
+ arrBuffer[arrBuffIndex + 0] = r;
+ arrBuffer[arrBuffIndex + 1] = g;
+ arrBuffer[arrBuffIndex + 2] = b;
+ arrBuffer[arrBuffIndex + 3] = a;
+ } else {
+ // console.log('drawThisColor undefined, rgbaStr:', rgbaStr);
+ arrBuffer[arrBuffIndex] = 0;
+ arrBuffer[arrBuffIndex + 1] = 0;
+ arrBuffer[arrBuffIndex + 2] = 0;
+ arrBuffer[arrBuffIndex + 3] = 0;
+ }
+ }
+
+ // Encode the image, creating a new PNG file
+ encodedBufferImage = UPNG.encode([arrBuffer], decodedPNG.width, decodedPNG.height, decodedPNG.depth);
+ } else {
+ for (let i = 0; i < pixelData.length; i += 4) {
+ const r = pixelData[i];
+ const g = pixelData[i + 1];
+ const b = pixelData[i + 2];
+
+ // If the intended color exists in pixelsToDisplay obj, draw that color, otherwise draw transparent
+ let smallestDiff = 765; // Maximum difference
+ Object.keys(pixelsToDisplay).forEach((pix) => {
+ const pixSplit = pix.split(',');
+ const biggestDiff = Math.max(Math.abs(parseInt(r, 10) - parseInt(pixSplit[0], 10)), Math.abs(parseInt(g, 10) - parseInt(pixSplit[1], 10)), Math.abs(parseInt(b, 10) - parseInt(pixSplit[2], 10)));
+ if (smallestDiff > biggestDiff) {
+ smallestDiff = biggestDiff;
+ }
+ });
+ // Use difference to see how far away a color is from any pixelsToDisplay
+ // If difference is large enough, don't display the color
+ // This helps prevent edges of color dots from not being displayed accidentally
+ if (smallestDiff > 10) {
+ pixelData[i + 3] = 0;
+ }
}
+
+ // Encode the image, creating a new PNG file
+ encodedBufferImage = UPNG.encode([pixelData], decodedPNG.width, decodedPNG.height, 32);
}
- // Encode the image, creating a new PNG file
- const encodedBufferImage = UPNG.encode([arrBuffer], decodedPNG.width, decodedPNG.height, decodedPNG.depth);
const blob = new Blob([encodedBufferImage], { type: 'image/png' });
const dataURL = `${URL.createObjectURL(blob)}`;
this.image_.src = dataURL;