I need to fix a raster that's been inappropriately resampled along the nodata edge. The bad data area is narrow, pretty much a single cell. The value range is too variable to use a simple "erase less than 20" kind of logic. How can I fix this?

I'm partial to a GDAL command line utilities or QGIS solution, but anything goes.

In the past I've used Gimp or Photoshop for this (magic wand select nodata with 0 tolerance, expand selection by 1 or 2px, delete, save, restore georeferencing), but I can't do that here. The image is 16bit (a DEM) which photo tools don't handle well (Gimp not at all) and at 4gb the image is too large to manage comfortably anyway.

In figure 1 black is the bad data to be removed, in figure 2 black is to be retained, but it's acceptable to lose 1 or 2 pixels.

Update: figure 3 close up showing cell values. A de-collaring tool like nearblack doesn't work because bad values are within valid value range, and often nowhere near black or white.

I've put samples from the dataset here: http://files.environmentyukon.ca/matt/gis-stack/expand-raster-nodata-area/

Update 2: remove Gdal/Qgis focus.

Figure 1, black is bad Figure 2, black is good Figure 3, showing cell values

  • No own experience on this but try to vectorize raster with gdal_polygonize, select nodata polygons, apply a buffer and use resulting polygons as cutline for gdalwarp. Or perhaps you need to take yes-data polygons and shrink them, I don't know.
    – user30184
    Commented Nov 13, 2015 at 1:00
  • This is essentially the same question: gis.stackexchange.com/questions/24933/…, however the accepted answer there didn't work for me. There's a lot of overhead translating the rasters in to Grass. I thought I managed to figure that out after a couple of hours, but r.grow and r.buffer had no observable result. Perhaps I didn't set mask options correctly(?). In any case, I failed, still looking for solution. Commented Nov 13, 2015 at 21:19
  • @Luke please flesh that out as an answer. It was a mistake to make the Q platform specific. Commented Nov 14, 2015 at 17:21
  • @Luke, unfortunately not. I'm sure it's possible but I ran out of time to research it. Commented Jul 31, 2018 at 16:41

2 Answers 2


In ArcGIS, I'd select out the NoData using the Con and IsNull functions, use the Expand function to buffer the NoData area by one cell then use the SetNull function with the expanded NoData as the condition. Not sure how to go about that with QGIS though.

Raster calculator expression using the sample data you posted:

SetNull(Expand(Con(IsNull("sample_2.tif"), 1, 0), 1, 1) == 1, "sample_2.tif")

Before: Sample 2 before

After: Sample 2 after

Pixel loss (blue pixels): enter image description here


You can use QGIS raster calculator or GDAL utility nearblack for this purpose.

  • 1
    Please expand your answer and explain how exactly you would solve the problem at hand with the raster calculator or the near black utility. With just one sentence your answer isn't providing much insight.
    – Kersten
    Commented Nov 13, 2015 at 9:38
  • Nearblack doesn't work because there are good values on edges that are "near black" and need to be kept, see second picture. Also there are bad values on edge whose numbers are in the valid data range. I'll add another image to help explain. Commented Nov 13, 2015 at 16:45
  • If you cannot give a threshold to separate "good" and "bad" values, neither raster calculator nor nearblack can be used.
    – Zoltan
    Commented Nov 15, 2015 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.