@@ -99,7 +99,9 @@ sf.kde <- function(x, y = NULL, bw = NULL, ref = NULL, res = NULL,
9999 } else if (inherits(ref , " SpatRaster" )) {
100100 message(" Using " , deparse(substitute(ref )), " as reference raster" )
101101 if (! terra :: crs(x ) == terra :: crs(ref ) )
102- stop(" CRS do not match" )
102+ stop(" CRS do not match" )
103+ if (mask == TRUE ) { m <- ref }
104+ ref [] <- NA
103105 } else if (inherits(ref , " sf" )) {
104106 message(" Using extent from " , deparse(substitute(ref )), " as reference raster" )
105107 if (! terra :: crs(x ) == terra :: crs(ref ))
@@ -121,7 +123,10 @@ sf.kde <- function(x, y = NULL, bw = NULL, ref = NULL, res = NULL,
121123 ref <- terra :: rast(ref , resolution = res ,
122124 crs = terra :: crs(x ))
123125 }
124-
126+ if (mask == TRUE & ! exists(" m" )) {
127+ m <- ref
128+ if (! terra :: hasValues(m )) { m [] <- 1 }
129+ }
125130 # check if points are contained within refrence raster
126131 rext <- sf :: st_as_sf(terra :: as.polygons(terra :: ext(ref )))
127132 xext <- sf :: st_as_sf(terra :: as.polygons(terra :: ext(x )))
@@ -190,7 +195,7 @@ sf.kde <- function(x, y = NULL, bw = NULL, ref = NULL, res = NULL,
190195 if (terra :: hasValues(ref )) {
191196 kde.est <- terra :: mask(kde.est , ref )
192197 } else {
193- warning(" ref has no values so, cannot perform mask" )
198+ warning(" ref mask has no values so, cannot perform mask" )
194199 }
195200 }
196201 terra :: crs(kde.est ) <- terra :: crs(x )
0 commit comments