-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Focal filter with stars
#176
Comments
* allow functions in st_apply to return matrices, and assume they refer to non-MARGIN dimensions * needed to address #176
Nice! I will use this approach, thank you very much for the help and quick reply! |
I think that the |
Did you try? Functions passed as argument to |
You are right; my mistake! Thanks for the clarification. |
Hi there, i hope this the right place. The Discussion above was helping me a lot. Thanks! I did the same as described above using apply_focal_filter <- function(stars_slice, w) {
# Convert the stars slice to a terra raster
raster <- rast(stars_slice)
# Apply the filter
filtered_raster <- focal(
raster,
w,
fun = "sum",
expand = TRUE
)
mat <- as.matrix(filtered_raster)
# mat <- transpose_flat_mat(mat)
return(mat)
}
sf <- st_apply(
s,
MARGIN = "date_time", # Specifies the dimension over which to apply function
FUN = apply_focal_filter, # The filter function wrapper for terra::focal
w = gauss_kernel, # The Weight matrix for the filter
CLUSTER = my.cluster, # Cluster for parallel processing
PROGRESS = TRUE # Display progress
)
# Rename Attribute / Feature
sf <- setNames(sf, "kfz_per_hour_neigh")
# Save the filtered result into the results list
s_filtered <- sf
} However, in my particular case, my data was transposed (mirrored at the diagonal) by the dims <- c("x", "y", "date_time")
crs <- 25833
s <- st_as_stars(input, dims = dims)
st_crs(s) <- st_crs(crs)
s
I found a workaround by transposing the matrix after applying the filter with this function: transpose_flat_mat <- function(mat) {
# get dimension & name
dim <- dim(mat)
sq_dim <- sqrt(dim[1])
name <- colnames(mat)
# transform to square matrix & transpose
dim(mat) <- c(sq_dim, sq_dim)
mat <- t(mat)
# set back to orig dimensions
dim(mat) <- dim
colnames(mat) <- name
return(matrix)
} Is there a more elegant solution to this issue? |
Reading the mat <- as.matrix(filtered_raster, wide = TRUE) in your |
Thanks a lot! |
This is a general question - hope it's appropriate to ask here.
As someone who just started to work with
stars
(as replacement forraster
), I find theraster::focal
function the most significant thing missing fromstars
at the moment.According to the migration table (#122),
raster::focal
is marked as "Not present, could be implemented".I was wondering whether there are any more concrete development paths in mind -
Is it likely that that a focal filter function will be implemented in
stars
? If not, do you think it makes sense to use a wrapper that transformsstars
<->raster
and usesraster::focal
? Perhaps you are aware of any general focal filtering function in R, which is aimed atarray
object and thus may work onstars
objects as is?Thanks for the great work - so far enjoyed using
stars
very much!The text was updated successfully, but these errors were encountered: