Skip to content

comparing raster::focal pad options to pfocal #26

Description

@see24

There is one option in raster::focal that I can't replicate in pfocal where pad = FALSE and na.rm = TRUE. This option gives NA for cells near the edge (ie there is no padding) but allows NAs in the raster to be ignored.

Here are some plots showing the implications of the various options

library(raster)
library(pfocal)
library(tmap) # better default NA palette

mat <- matrix(c(rep(1,5000), rep(2, 5000)), nrow = 100, ncol = 100)
exps <- raster(mat, xmn = 0, xmx = 100, ymn = 0, ymx = 100)

# add an NA in the middle
exps[25,25] <- NA 

# raster options for dealing with edge effects #=============
rast_padF_narmF <- focal(exps, gaussian_kernel_radius(3), 
                   pad = FALSE, na.rm = FALSE)
qtm(rast_padF_narmF)

# this is the one I can't replicate with pfocal ***
rast_padF_narmT <- focal(exps, gaussian_kernel_radius(3), 
                         pad = FALSE, na.rm = TRUE)
qtm(rast_padF_narmT)
# the area around the NA value gets a lower value because the default function
# is sum
qtm(rast_padF_narmT < 0.999)

# this is the same as rast_padF_narmF
rast_padT_narmF <- focal(exps, gaussian_kernel_radius(3), 
                   pad = TRUE, na.rm = F, padValue = NA )

qtm(rast_padT_narmF)

# this has a larger area that is not NA
rast_padT_narmT <- focal(exps, gaussian_kernel_radius(3), 
                         pad = TRUE, na.rm = T, padValue = NA )

qtm(rast_padT_narmT)

# pfocal versions #======================

pfoc_evNA_narmNA <- pfocal(exps, gaussian_kernel_radius(3),
                           edge_value = NA, na.rm = NA)
qtm(pfoc_evNA_narmNA)

# seems to be the same as na.rm = NA for this case
pfoc_evNA_narmF <- pfocal(exps, gaussian_kernel_radius(3),
                           edge_value = NA, na.rm = FALSE)
qtm(pfoc_evNA_narmF)

# same as rast_padT_narmT
pfoc_evNA_narmT <- pfocal(exps, gaussian_kernel_radius(3),
                           edge_value = NA, na.rm = TRUE)
qtm(pfoc_evNA_narmT)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions