I want to extract from a RasterLayer all boundary cells between classes (possibly according to the 4-cell rule), excluding from this operation cells that neighbour NA cells

The raster::boundaries function detects boundaries as cells that have more than one class (including NA) cells in the 4 or 8 cells surrounding it , or, if classes argument is set to FALSE, cells with values and cells with NA.

What about if I do not want to consider as edge the NA border?


I found a solution on QGIS, maybe it could be useful as well.

Assuming that the two target classes are identified with consecutive integers (e.g. 1 and 2), I used the 'mean' function in the Raster Calculator, which calculates a new raster applying the function to the focal cell using the 4/8 adjacent cells values. Any cell with decimal values has at least 1 adjacent cell with a different value.

For more than two classes (or not consecutive integers) this approach may not be suitable, because it could result in integers values as well.

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.