<!DOCTYPE html><html><head><title></title><style type="text/css">p.MsoNormal,p.MsoNoSpacing{margin:0}</style></head><body><div style="font-family:Arial;">Hello,<br></div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">I've been looking over the docs and the code, but I'm still a little confused about how masking and/or no data are handled during resampling in GDAL. I'm interested mostly in the <span class="font" style="font-family:menlo, consolas, monospace, sans-serif;"><code style="border-top-color:rgb(204, 204, 204);border-top-style:solid;border-top-width:1px;border-right-color:rgb(204, 204, 204);border-right-style:solid;border-right-width:1px;border-bottom-color:rgb(204, 204, 204);border-bottom-style:solid;border-bottom-width:1px;border-left-color:rgb(204, 204, 204);border-left-style:solid;border-left-width:1px;border-image-outset:0;border-image-repeat:stretch;border-image-slice:100%;border-image-source:none;border-image-width:1;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(246, 246, 246);background-position-x:0%;background-position-y:0%;background-repeat:repeat;background-attachment:scroll;background-image:none;background-size:auto;background-origin:padding-box;background-clip:border-box;font-family:menlo, consolas, monospace;font-size:90%;padding-top:1px;padding-right:3px;padding-bottom:1px;padding-left:3px;">cubic</code></span> and <span class="font" style="font-family:menlo, consolas, monospace, sans-serif;"><code style="border-top-color:rgb(204, 204, 204);border-top-style:solid;border-top-width:1px;border-right-color:rgb(204, 204, 204);border-right-style:solid;border-right-width:1px;border-bottom-color:rgb(204, 204, 204);border-bottom-style:solid;border-bottom-width:1px;border-left-color:rgb(204, 204, 204);border-left-style:solid;border-left-width:1px;border-image-outset:0;border-image-repeat:stretch;border-image-slice:100%;border-image-source:none;border-image-width:1;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(246, 246, 246);background-position-x:0%;background-position-y:0%;background-repeat:repeat;background-attachment:scroll;background-image:none;background-size:auto;background-origin:padding-box;background-clip:border-box;font-family:menlo, consolas, monospace;font-size:90%;padding-top:1px;padding-right:3px;padding-bottom:1px;padding-left:3px;">average</code></span> resamples, but I assume the answers will apply to the other modes just as well.<br></div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">In my understanding:<br></div><ul><li style="font-family:Arial;"><span class="font" style="font-family:menlo, consolas, monospace, sans-serif;"><code style="border-top-color:rgb(204, 204, 204);border-top-style:solid;border-top-width:1px;border-right-color:rgb(204, 204, 204);border-right-style:solid;border-right-width:1px;border-bottom-color:rgb(204, 204, 204);border-bottom-style:solid;border-bottom-width:1px;border-left-color:rgb(204, 204, 204);border-left-style:solid;border-left-width:1px;border-image-outset:0;border-image-repeat:stretch;border-image-slice:100%;border-image-source:none;border-image-width:1;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(246, 246, 246);background-position-x:0%;background-position-y:0%;background-repeat:repeat;background-attachment:scroll;background-image:none;background-size:auto;background-origin:padding-box;background-clip:border-box;font-family:menlo, consolas, monospace;font-size:90%;padding-top:1px;padding-right:3px;padding-bottom:1px;padding-left:3px;">cubic</code></span> will happily include "no data" values in the computation, likely outputting not very useful results<br></li><li style="font-family:Arial;"><span class="font" style="font-family:menlo, consolas, monospace, sans-serif;"><code style="border-top-color:rgb(204, 204, 204);border-top-style:solid;border-top-width:1px;border-right-color:rgb(204, 204, 204);border-right-style:solid;border-right-width:1px;border-bottom-color:rgb(204, 204, 204);border-bottom-style:solid;border-bottom-width:1px;border-left-color:rgb(204, 204, 204);border-left-style:solid;border-left-width:1px;border-image-outset:0;border-image-repeat:stretch;border-image-slice:100%;border-image-source:none;border-image-width:1;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(246, 246, 246);background-position-x:0%;background-position-y:0%;background-repeat:repeat;background-attachment:scroll;background-image:none;background-size:auto;background-origin:padding-box;background-clip:border-box;font-family:menlo, consolas, monospace;font-size:90%;padding-top:1px;padding-right:3px;padding-bottom:1px;padding-left:3px;">average</code></span> will ignore no data input pixels; but how does <span class="font" style="font-family:menlo, consolas, monospace, sans-serif;">average</span> pick the weights? The <span class="font" style="font-family:menlo, consolas, monospace, sans-serif;"><code style="border-top-color:rgb(204, 204, 204);border-top-style:solid;border-top-width:1px;border-right-color:rgb(204, 204, 204);border-right-style:solid;border-right-width:1px;border-bottom-color:rgb(204, 204, 204);border-bottom-style:solid;border-bottom-width:1px;border-left-color:rgb(204, 204, 204);border-left-style:solid;border-left-width:1px;border-image-outset:0;border-image-repeat:stretch;border-image-slice:100%;border-image-source:none;border-image-width:1;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(246, 246, 246);background-position-x:0%;background-position-y:0%;background-repeat:repeat;background-attachment:scroll;background-image:none;background-size:auto;background-origin:padding-box;background-clip:border-box;font-family:menlo, consolas, monospace;font-size:90%;padding-top:1px;padding-right:3px;padding-bottom:1px;padding-left:3px;">gdal_translate</code></span> docs say "Starting with GDAL 3.1, this is a weighted average taking into account properly the weight of source pixels not contributing fully to the target pixel". I assume that's mostly unrelated.<br></li><li style="font-family:Arial;">the <span class="font" style="font-family:menlo, consolas, monospace, sans-serif;"><code style="border-top-color:rgb(204, 204, 204);border-top-style:solid;border-top-width:1px;border-right-color:rgb(204, 204, 204);border-right-style:solid;border-right-width:1px;border-bottom-color:rgb(204, 204, 204);border-bottom-style:solid;border-bottom-width:1px;border-left-color:rgb(204, 204, 204);border-left-style:solid;border-left-width:1px;border-image-outset:0;border-image-repeat:stretch;border-image-slice:100%;border-image-source:none;border-image-width:1;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(246, 246, 246);background-position-x:0%;background-position-y:0%;background-repeat:repeat;background-attachment:scroll;background-image:none;background-size:auto;background-origin:padding-box;background-clip:border-box;font-family:menlo, consolas, monospace;font-size:90%;padding-top:1px;padding-right:3px;padding-bottom:1px;padding-left:3px;">-srcnodata</code></span> docs and <span class="font" style="font-family:menlo, consolas, monospace, sans-serif;"><code style="border-top-color:rgb(204, 204, 204);border-top-style:solid;border-top-width:1px;border-right-color:rgb(204, 204, 204);border-right-style:solid;border-right-width:1px;border-bottom-color:rgb(204, 204, 204);border-bottom-style:solid;border-bottom-width:1px;border-left-color:rgb(204, 204, 204);border-left-style:solid;border-left-width:1px;border-image-outset:0;border-image-repeat:stretch;border-image-slice:100%;border-image-source:none;border-image-width:1;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(246, 246, 246);background-position-x:0%;background-position-y:0%;background-repeat:repeat;background-attachment:scroll;background-image:none;background-size:auto;background-origin:padding-box;background-clip:border-box;font-family:menlo, consolas, monospace;font-size:90%;padding-top:1px;padding-right:3px;padding-bottom:1px;padding-left:3px;">gdalwarpkernel.cpp</code></span> suggest that masked pixels are not included in the computation (unlike those of no data?)<br></li></ul><div style="font-family:Arial;">I know two ways of handling masked/no data pixels during resampling:<br></div><ul><li style="font-family:Arial;">ignore them and rescale the remaining weights of the input pixels (e.g. a 4x4 grid in the <span class="font" style="font-family:menlo, consolas, monospace, sans-serif;"><code style="border-top-color:rgb(204, 204, 204);border-top-style:solid;border-top-width:1px;border-right-color:rgb(204, 204, 204);border-right-style:solid;border-right-width:1px;border-bottom-color:rgb(204, 204, 204);border-bottom-style:solid;border-bottom-width:1px;border-left-color:rgb(204, 204, 204);border-left-style:solid;border-left-width:1px;border-image-outset:0;border-image-repeat:stretch;border-image-slice:100%;border-image-source:none;border-image-width:1;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(246, 246, 246);background-position-x:0%;background-position-y:0%;background-repeat:repeat;background-attachment:scroll;background-image:none;background-size:auto;background-origin:padding-box;background-clip:border-box;font-family:menlo, consolas, monospace;font-size:90%;padding-top:1px;padding-right:3px;padding-bottom:1px;padding-left:3px;">cubic</code></span> case), outputting no data if there's no valid pixel among the inputs -- this seems "ideal"<br></li><li style="font-family:Arial;">output no data if any of the input (e.g. 4x4 grid) pixels are no data<br></li></ul><div style="font-family:Arial;">The latter method can be simulated in a reasonably easy way by making a validity mask, resampling it with the same method, then thresholding it to get a new mask which only keeps the "pure" pixels. It might even be possible (but maybe not too easy) to implement do this with a VRT. But its disadvantage is that it tends to expand the invalid areas.<br></div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">So my main question if there's a supported way to handle this in GDAL (using band masks?), and what does it do (will I get the "ideal" result from above?).<br></div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">Regards,<br></div><div style="font-family:Arial;">Laurentiu Nicola<br></div></body></html>