Skip to content
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

Closed
michaeldorman opened this issue May 14, 2019 · 5 comments
Closed

Focal filter with stars #176

michaeldorman opened this issue May 14, 2019 · 5 comments

Comments

@michaeldorman
Copy link
Contributor

This is a general question - hope it's appropriate to ask here.

As someone who just started to work with stars (as replacement for raster), I find the raster::focal function the most significant thing missing from stars 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 transforms stars<->raster and uses raster::focal? Perhaps you are aware of any general focal filtering function in R, which is aimed at array object and thus may work on stars objects as is?

Thanks for the great work - so far enjoyed using stars very much!

edzer added a commit that referenced this issue May 15, 2019
* allow functions in st_apply to return matrices, and assume they refer to non-MARGIN dimensions
* needed to address #176
@edzer
Copy link
Member

edzer commented May 15, 2019

After applying this patch, you can try this:

library(stars)
tif = system.file("tif/L7_ETMs.tif", package = "stars")
x1 = read_stars(tif)
foc = function(x, w) {
	raster::as.matrix(raster::focal(raster::raster(x), w))
}
f = st_apply(x1, 3, foc, w = matrix(1, 3, 3))
plot(f)

x

@michaeldorman
Copy link
Contributor Author

Nice! I will use this approach, thank you very much for the help and quick reply!

@florisvdh
Copy link
Member

I think that the raster::raster(x) part of the above solution should be as(x, "Raster"), i.e. the function provided by stars. It seems that raster::raster() does not yet accept stars objects.

@edzer
Copy link
Member

edzer commented May 24, 2019

Did you try? Functions passed as argument to st_apply receive sub-arrays, in this case matrices, not (subsetted) stars objects.

@florisvdh
Copy link
Member

You are right; my mistake! Thanks for the clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants