Re: [R-sig-Geo] Reclassification of raster

2017-12-10 Thread John Wasige
Thank you Ben,
works great!

Best Rgds
John

On Sat, Dec 9, 2017 at 12:52 AM, Ben Tupper  wrote:

> Hi John,
>
> I'm happy to help, but I have copied the r-sig-geo list as the community
> (and you and I) benefit from using the list.
>
> I'm not very clear on what you mean by 'I loose the negative values'.
> Also, I don't see the connection between the raster you show and the
> quantile values computed.  If you asked for the 0% quantile you should get
> the lowest available value - (since all pixels are at or above this
> value).  According to the raster info you show that should be -0.893809,
> but instead your 0% quantile is shown to be 40.00199   Perhaps you have
> unwittingly mixed up the rasters you are using.
>
> Here is what I get using a small reproducible example (thanks, reprex!
> https://github.com/tidyverse/reprex)  You can see that all of the -1
> values are classified to class #1.
>
> library(raster)
> #> Loading required package: sp
>
> # make a dummy matrix that ranges from -1 to 6
> nx = 10
> ny = 5
> m <- matrix(sample(-1:6, nx*ny, replace = TRUE), nrow = nx, ncol = ny)
>
> # and make a raster
> r <- raster(m)
> r
> #> class   : RasterLayer
> #> dimensions  : 10, 5, 50  (nrow, ncol, ncell)
> #> resolution  : 0.2, 0.1  (x, y)
> #> extent  : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
> #> coord. ref. : NA
> #> data source : in memory
> #> names   : layer
> #> values  : -1, 6  (min, max)
>
>
> # compute the quantiles
> pp <- quantile(r, c(0, 0.15, 0.85))
> pp
> #>  0% 15% 85%
> #>  -1   0   5
>
> # classify the pixels according to the quantile each pixel belongs to
> ix <- findInterval(getValues(r), pp)
>
> # make a classified version of r (class 0, 1 or 3)
> classified_r <- setValues(r, ix)
> classified_r
> #> class   : RasterLayer
> #> dimensions  : 10, 5, 50  (nrow, ncol, ncell)
> #> resolution  : 0.2, 0.1  (x, y)
> #> extent  : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
> #> coord. ref. : NA
> #> data source : in memory
> #> names   : layer
> #> values  : 1, 3  (min, max)
>
> # show how they pair up
> df <- data.frame(value = getValues(r), class = getValues(classified_r))
> df
> #>value class
> #> 1  0 2
> #> 2  0 2
> #> 3  5 2
> #> 4  6 3
> #> 5  5 2
> #> 6  4 2
> #> 7  1 2
> #> 8  5 2
> #> 9 -1 1
> #> 10 2 2
> #> 11 0 2
> #> 12 1 2
> #> 13 3 2
> #> 14 3 2
> #> 15 6 3
> #> 16-1 1
> #> 17 2 2
> #> 18 6 3
> #> 19 1 2
> #> 20 0 2
> #> 21 1 2
> #> 22 5 2
> #> 23 4 2
> #> 24 2 2
> #> 25 1 2
> #> 26 0 2
> #> 27 4 2
> #> 28 6 3
> #> 29 3 2
> #> 30 0 2
> #> 31 6 3
> #> 32 6 3
> #> 33 0 2
> #> 34 2 2
> #> 35 2 2
> #> 36 6 3
> #> 37 5 2
> #> 38 6 3
> #> 39 1 2
> #> 40 5 2
> #> 41 4 2
> #> 42 4 2
> #> 43 3 2
> #> 44 4 2
> #> 45 0 2
> #> 46 4 2
> #> 47 0 2
> #> 48 1 2
> #> 49 1 2
> #> 50-1 1
>
>
> Cheers,
> Ben
>
>
> On Dec 8, 2017, at 7:36 AM, John Wasige  wrote:
>
> Hi Ben,
>
> We had an email exchange on r-sig-geo@r-project.org in June this year on
> raster classification where you made some suggestions. See the link below
>
>
> 
>
>
> 
> http://r-sig-geo.2731867.n2.nabble.com/Thresholds-amp-reclas
> sify-raster-td7591266.html
>
>
> 
> However, when I run the reclassification, I loose the negative values. Any
> suggestion on how I can have all data classified & not loose pixels?
> Thanks for your help
>
> pp <- quantile(r, c(0, 0.15, 0.85))
> # pp
> #   0%  15%  85%
> # 40.00199 47.64569 82.50751
> ix <- findInterval(getValues(r), pp)
> classified_r <- setValues(r, ix)
>
> 
>
> My raster looks;
>
> class   : RasterLayer
> dimensions  : 6557, 4281, 28070517  (nrow, ncol, ncell)
> resolution  : 0.0002335903, 0.0001525088  (x, y)
> extent  : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
> coord. ref. : NA
> data source : J:\LPD_data\gpp.tif
> names   : gpp
> values  : -0.893809, 5.855252  (min, max)
>
>
>
>
> 
>
>
> 
>
>
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 60 Bigelow Drive
> , P.O.
> Box 380
> East Boothbay, Maine 04544
> http://www.bigelow.org
>
> Ecocast Reports: http://seascapemodeling.org/ecocast.html
> Tick 

Re: [R-sig-Geo] Reclassification of raster

2017-12-08 Thread Ben Tupper
Hi John,

I'm happy to help, but I have copied the r-sig-geo list as the community (and 
you and I) benefit from using the list.

I'm not very clear on what you mean by 'I loose the negative values'. Also, I 
don't see the connection between the raster you show and the quantile values 
computed.  If you asked for the 0% quantile you should get the lowest available 
value - (since all pixels are at or above this value).  According to the raster 
info you show that should be -0.893809, but instead your 0% quantile is shown 
to be 40.00199   Perhaps you have unwittingly mixed up the rasters you are 
using.

Here is what I get using a small reproducible example (thanks, reprex! 
https://github.com/tidyverse/reprex )  You 
can see that all of the -1 values are classified to class #1.

library(raster)
#> Loading required package: sp

# make a dummy matrix that ranges from -1 to 6 
nx = 10
ny = 5
m <- matrix(sample(-1:6, nx*ny, replace = TRUE), nrow = nx, ncol = ny)

# and make a raster
r <- raster(m)
r
#> class   : RasterLayer 
#> dimensions  : 10, 5, 50  (nrow, ncol, ncell)
#> resolution  : 0.2, 0.1  (x, y)
#> extent  : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
#> coord. ref. : NA 
#> data source : in memory
#> names   : layer 
#> values  : -1, 6  (min, max)


# compute the quantiles
pp <- quantile(r, c(0, 0.15, 0.85))
pp
#>  0% 15% 85% 
#>  -1   0   5

# classify the pixels according to the quantile each pixel belongs to
ix <- findInterval(getValues(r), pp)

# make a classified version of r (class 0, 1 or 3)
classified_r <- setValues(r, ix) 
classified_r
#> class   : RasterLayer 
#> dimensions  : 10, 5, 50  (nrow, ncol, ncell)
#> resolution  : 0.2, 0.1  (x, y)
#> extent  : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
#> coord. ref. : NA 
#> data source : in memory
#> names   : layer 
#> values  : 1, 3  (min, max)

# show how they pair up
df <- data.frame(value = getValues(r), class = getValues(classified_r))
df
#>value class
#> 1  0 2
#> 2  0 2
#> 3  5 2
#> 4  6 3
#> 5  5 2
#> 6  4 2
#> 7  1 2
#> 8  5 2
#> 9 -1 1
#> 10 2 2
#> 11 0 2
#> 12 1 2
#> 13 3 2
#> 14 3 2
#> 15 6 3
#> 16-1 1
#> 17 2 2
#> 18 6 3
#> 19 1 2
#> 20 0 2
#> 21 1 2
#> 22 5 2
#> 23 4 2
#> 24 2 2
#> 25 1 2
#> 26 0 2
#> 27 4 2
#> 28 6 3
#> 29 3 2
#> 30 0 2
#> 31 6 3
#> 32 6 3
#> 33 0 2
#> 34 2 2
#> 35 2 2
#> 36 6 3
#> 37 5 2
#> 38 6 3
#> 39 1 2
#> 40 5 2
#> 41 4 2
#> 42 4 2
#> 43 3 2
#> 44 4 2
#> 45 0 2
#> 46 4 2
#> 47 0 2
#> 48 1 2
#> 49 1 2
#> 50-1 1


Cheers,
Ben


> On Dec 8, 2017, at 7:36 AM, John Wasige  wrote:
> 
> Hi Ben,
> 
> We had an email exchange on r-sig-geo@r-project.org 
>  in June this year on raster classification 
> where you made some suggestions. See the link below
> 
>  
> 
> 
>  
> 
> http://r-sig-geo.2731867.n2.nabble.com/Thresholds-amp-reclassify-raster-td7591266.html
>  
> 
> 
>  
> 
> However, when I run the reclassification, I loose the negative values. Any 
> suggestion on how I can have all data classified & not loose pixels?
> Thanks for your help
> 
> pp <- quantile(r, c(0, 0.15, 0.85)) 
> # pp 
> #   0%  15%  85% 
> # 40.00199 47.64569 82.50751 
> ix <- findInterval(getValues(r), pp) 
> classified_r <- setValues(r, ix) 
> 
> 
> 
> My raster looks;
> 
> class   : RasterLayer 
> dimensions  : 6557, 4281, 28070517  (nrow, ncol, ncell)
> resolution  : 0.0002335903, 0.0001525088  (x, y)
> extent  : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
> coord. ref. : NA 
> data source : J:\LPD_data\gpp.tif 
> names   : gpp 
> values  : -0.893809, 5.855252  (min, max)
> 
> 
>  
> 
> 
>  
> 
> 

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

Ecocast Reports: http://seascapemodeling.org/ecocast.html
Tick Reports: https://report.bigelow.org/tick/
Jellyfish Reports: https://jellyfish.bigelow.org/jellyfish/




[[alternative HTML version deleted]]