I have a raster with no data values, I want to give them values according to values around them. If a no data pixel is surrounded by 8 pixels whose values are 1 then the no data pixel should adopt this value or if at least it has 5 or more pixels with that value.

How can I do it?


In the ArcGIS raster calculator you can use the Con, IsNull and FocalStatistics functions:

Con(IsNull("YourRaster") & (FocalStatistics("YourRaster", NbrRectangle(3, 3, "CELL"), "VARIETY", "") < 5), FocalStatistics("YourRaster", NbrRectangle(3, 3, "CELL"), "MAJORITY", ""), "YourRaster")

Reformatted for reading:

    IsNull("YourRaster") & (FocalStatistics("YourRaster", NbrRectangle(3, 3, "CELL"), "VARIETY", "") < 5), 
    FocalStatistics("YourRaster", NbrRectangle(3, 3, "CELL"), "MAJORITY", ""), 

What this does:

  • Con is an if/then/else function
    • IsNull("YourRaster") & (FocalStatistics("YourRaster", NbrRectangle(3, 3, "CELL"), "VARIETY", "") < 5) the "if" component of the Con function.
      • IsNull tests if a pixel is NoData
      • (FocalStatistics("YourRaster", NbrRectangle(3, 3, "CELL"), "VARIETY", "") < 5) tests if there are less than 5 distinct values surrounding the pixels. Because you said you only want to fill a NoData pixel if there are at least 5 identical values surrounding it, there can only be at most three more different distinct values around it (the 5 identical values count as 1 distinct value). For example, say you had 1,1,1,2,3,4,5,6 surrounding your NoData pixel you would have 6 distinct values, but 1,1,1,1,1,4,5,6 is 4 distinct values. Note the expression is surrounded with brackets, this is important, otherwise the bitwise & "and" won't work.
    • FocalStatistics("YourRaster", NbrRectangle(3, 3, "CELL"), "MAJORITY", "") the "then" component of the Con function. If the above is True, this selects the most frequently occurring surrounding value.
    • "YourRaster" the "else" component of the Con function. If the above is False) the existing pixel value is selected.

Note that if you don't care about the minimum number of identical surrounding values and just want to pick the most frequently occurring (I don't know how ties are handled) just using Con(IsNull("YourRaster"), FocalStatistics("YourRaster", NbrRectangle(3, 3, "CELL"), "MAJORITY", ""), "YourRaster") will do the trick.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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