From e66a2c0be273301b94dfc336eba7be5a8c400387 Mon Sep 17 00:00:00 2001 From: aavotins <74116320+aavotins@users.noreply.github.com> Date: Sat, 21 Feb 2026 13:02:30 +0200 Subject: [PATCH] Update sf.kde.R I was working wit st_crs(3059) and had to make this change for projection to match MASS::kde2d()->terra::rast()->terra::resample()->terra::crop() routine. Before it was "flipped" --- R/sf.kde.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/sf.kde.R b/R/sf.kde.R index 8597fbe..b1b1a41 100644 --- a/R/sf.kde.R +++ b/R/sf.kde.R @@ -177,7 +177,7 @@ sf.kde <- function(x, y = NULL, bw = NULL, ref = NULL, res = NULL, } else { bw <- c(bw,bw) } - n <- c(terra::nrow(ref), terra::ncol(ref)) + n <- c(terra::ncol(ref), terra::nrow(ref)) if(!is.null(y)) { message("\n","calculating weighted kde","\n") k <- fhat(sf::st_coordinates(x)[,1], sf::st_coordinates(x)[,2], w = y, @@ -189,7 +189,8 @@ sf.kde <- function(x, y = NULL, bw = NULL, ref = NULL, res = NULL, } k$z <- k$z * scale.factor if( standardize == TRUE ) { k$z <- (k$z - min(k$z)) / (max(k$z) - min(k$z)) } - kde.est <- flip(terra::rast(k[[3]], crs=terra::crs(x), extent=terra::ext(ref)) ) + kde.est <- terra::rast(t(k$z), extent = terra::ext(ref), crs = terra::crs(x)) + kde.est <- terra::flip(kde.est, direction = "vertical") # pts <- data.frame(expand.grid(x=k$x, y=k$y), # z=round(as.vector(array(k$z,length(k$z))) * # scale.factor, 10))