custom focal function on rasterstack

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

custom focal function on rasterstack

Ruben Van De Kerchove
Hi,

I have rasterstack consisting out of 252 rasters (hyperspectral image) for
which I want to calculate the mean distance to the centroid in a 3*3(*252)
moving window. As the focal function (from the raster package) only works
for rasters, I am currently using the getValuesBlock function (in a loop),
but this seems rather slow. Therefore, I was wondering, if there is a more
efficient way to tackle this problem.

Best,
Ruben

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: custom focal function on rasterstack

Jonathan Greenberg-3
Ruben:

Give rasterEngine, within the spatial.tools package a whirl.  Here's
the tutorial for it (some things are bit out of date, but this does
cover the focal analyses):

http://publish.illinois.edu/jgrn/software-and-datasets/rasterengine-tutorial/

Note there are examples of focal windows towards the end of the
tutorial.  If you use a parallel engine (e.g. sfQuickInit() ) it will
run your process in parallel -- I've found a significant speedup using
focal windows in parallel mode.

--j

On Fri, Mar 14, 2014 at 10:10 AM, Ruben Van De Kerchove
<[hidden email]> wrote:

> Hi,
>
> I have rasterstack consisting out of 252 rasters (hyperspectral image) for
> which I want to calculate the mean distance to the centroid in a 3*3(*252)
> moving window. As the focal function (from the raster package) only works
> for rasters, I am currently using the getValuesBlock function (in a loop),
> but this seems rather slow. Therefore, I was wondering, if there is a more
> efficient way to tackle this problem.
>
> Best,
> Ruben
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



--
Jonathan A. Greenberg, PhD
Assistant Professor
Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
Department of Geography and Geographic Information Science
University of Illinois at Urbana-Champaign
259 Computing Applications Building, MC-150
605 East Springfield Avenue
Champaign, IL  61820-6371
Phone: 217-300-1924
http://www.geog.illinois.edu/~jgrn/
AIM: jgrn307, MSN: [hidden email], Gchat: jgrn307, Skype: jgrn3007

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: custom focal function on rasterstack

Ruben Van De Kerchove
Great! Exactly what I needed!

Would it be possible to also include some mask into the rasterEngine
function? My image is rotated for about 45° which means that a significant
part of my rasterstack consits out of NA values. I suppose that skipping
these cells would speed up things.

Ruben


2014-03-14 18:38 GMT+01:00 Jonathan Greenberg <[hidden email]>:

> Ruben:
>
> Give rasterEngine, within the spatial.tools package a whirl.  Here's
> the tutorial for it (some things are bit out of date, but this does
> cover the focal analyses):
>
>
> http://publish.illinois.edu/jgrn/software-and-datasets/rasterengine-tutorial/
>
> Note there are examples of focal windows towards the end of the
> tutorial.  If you use a parallel engine (e.g. sfQuickInit() ) it will
> run your process in parallel -- I've found a significant speedup using
> focal windows in parallel mode.
>
> --j
>
> On Fri, Mar 14, 2014 at 10:10 AM, Ruben Van De Kerchove
> <[hidden email]> wrote:
> > Hi,
> >
> > I have rasterstack consisting out of 252 rasters (hyperspectral image)
> for
> > which I want to calculate the mean distance to the centroid in a
> 3*3(*252)
> > moving window. As the focal function (from the raster package) only works
> > for rasters, I am currently using the getValuesBlock function (in a
> loop),
> > but this seems rather slow. Therefore, I was wondering, if there is a
> more
> > efficient way to tackle this problem.
> >
> > Best,
> > Ruben
> >
> >         [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > [hidden email]
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
>
> --
> Jonathan A. Greenberg, PhD
> Assistant Professor
> Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
> Department of Geography and Geographic Information Science
> University of Illinois at Urbana-Champaign
> 259 Computing Applications Building, MC-150
> 605 East Springfield Avenue
> Champaign, IL  61820-6371
> Phone: 217-300-1924
> http://www.geog.illinois.edu/~jgrn/
> AIM: jgrn307, MSN: [hidden email], Gchat: jgrn307, Skype: jgrn3007
>
        [[alternative HTML version deleted]]


_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: custom focal function on rasterstack

ben mack
Hi,

the spatial.tools is really a great thing to accellerate the raster processing. However, I have the same issue and often need to process only a part of the image and thus a mask option for the rasterEngine would also be great for me.
I just wanted to also ask if this is considered an option in future versions (Jonathan?). Or did you, Ruben, or anybody else found a work around?

Thanks a lot,
Ben
Reply | Threaded
Open this post in threaded view
|

Re: custom focal function on rasterstack

Jonathan Greenberg-3
Hi Ben:

In the short term, I don't have a plan to implement a mask in focal
the way I think you are asking -- it has to do with how data is read
from disk to realize the parallel processing -- all of the data HAS to
be read, and you HAVE to have a single output per pixel.  With this
said, the code allows you to implement a mask if need be (set it as a
second input raster). If you use processing_unit="chunk" with a focal
window, you can efficiently use a mask to "skip" those masked pixels
(when I mean skip, I mean your function performs a quick "return NA"
instead of "process all values in the window").

The processing_unit="chunk" for focal takes a bit of getting used to,
so you might want to investigate it with debugmode=TRUE.  Another
trick for learning this mode is to grab the latest spatial.tools from
R-forge (version 1.4.4 or later) is to create a "GLT" file (a 2-band
file where band 1 is the pixel x position (column) and band 2 is the
pixel y position (row)) using raster_to_GLT.  When you take a look at
the test chunk in the debugmode you will see how this an "unspooled"
set of focal windows looks.

--j

On Tue, Jun 17, 2014 at 3:47 AM, ben mack <[hidden email]> wrote:

> Hi,
>
> the spatial.tools is really a great thing to accellerate the raster
> processing. However, I have the same issue and often need to process only a
> part of the image and thus a mask option for the rasterEngine would also be
> great for me.
> I just wanted to also ask if this is considered an option in future versions
> (Jonathan?). Or did you, Ruben, or anybody else found a work around?
>
> Thanks a lot,
> Ben
>
>
>
> --
> View this message in context: http://r-sig-geo.2731867.n2.nabble.com/custom-focal-function-on-rasterstack-tp7585959p7586593.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



--
Jonathan A. Greenberg, PhD
Assistant Professor
Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
Department of Geography and Geographic Information Science
University of Illinois at Urbana-Champaign
259 Computing Applications Building, MC-150
605 East Springfield Avenue
Champaign, IL  61820-6371
Phone: 217-300-1924
http://www.geog.illinois.edu/~jgrn/
AIM: jgrn307, MSN: [hidden email], Gchat: jgrn307, Skype: jgrn3007

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo