raster_breaks: Extract pixels from raster object and breaks it into n bins.

Description Usage Arguments Details Value Author(s) References Examples

View source: R/raster_breaks.R

Description

Take a raster object (RasterLayer, RasterStack, RasterBrick), and extract pixels values contain by SpatialPolygonsDataFrame object. Then, raster_break() generates bins, given by breaks argument, from pixels values. Finally, it converts all bins into a SpatialPointsDataFrame object. See details for further explanation

Usage

1
2
3
4
raster_breaks(x.ras, pol.ext, bb = c(t(sp::bbox(pol.ext))),
  breaks = NULL, breaks.by = 0.1)

aux_foo(x, breaks, breaks.by, proj4string = sp::proj4string(x))

Arguments

x.ras

A raster object (RasterLayer, RasterStack, RasterBrick).

pol.ext

A SpatialPolygonsDataFrame object. An object from grd_build() is recommended.

bb

A integer vector of the form c(min['long'], max['long'], min['lat'], max['lat']). By default bb = c(t(sp::bbox(po.ext))).

breaks

NULL, integer or numeric vector. A vector of length >= 3, indicating the lower limit and upper limit of each bin. If breaks has length = 3, 2 bins are created. By default breaks = NULL, if NULL or integer bins are calculated with quantile(). Se details for further explanation.

breaks.by

double. A value between 0 and 1. Argument pass to quantile function, it requires breaks = NULL.

Details

This function extract all pixels values and its coordinates from raster object. Valid raster objects are (RasterLayer, RasterStack, RasterBrick). Then, divides values into bins given by breaks argument. Finally, it converts this data into a SpatialPointsDataFrame.

bb argument is used for corpping raster object from pol.ext' bounding box. After cropping, cropped raster object is masked into pol.ext' boundaries. Both step are important in order to reduce processing time.

Extraction of values and coordinates are done by values and coordinates. Both function are applied to the raster object. output is concatenated into data.frame object.

breaks argument can be pass as NULL, integer or double vector. If breaks = NULL, the bins are calculated trough quantile(x, probs, na.rm = TRUE) function, where x argument is values(rasterobj), probs = seq(0, 1, by = breaks.by). If breaks = integer >= 1, the bins are calculated in similar way as breaks = NULL, but probs = seq(0, 1, length = (breaks +1)). If breaks = c(), where a vector of the form c(2.1, 2.5, 2.9) produce two bins as [2.1, 2.5) and [2.5, 2.9). Then the raster values are compared with the range of each bins.

Value

A SpatialPointsDataFrame object. It includes all raster values and its coordinates, and the bin associated.

Author(s)

Enrique Del Callejo Canal ([email protected]), based on implemented algortihms in web platform SPECIES (see References).

References

http://species.conabio.gob.mx/

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
library(sp)
library(rgeos)
library(raster)

data(Mex0)

# Generating de grid from Mex0 data
Mex0.grd<-grd_build(Mex0)

# Extracting bioclim variables from worldclim
bioclim<-getData('worldclim', var='bio', res=2.5)

# Extracting values from 19 bioclim variables
system.time(bio.sp<-raster_breaks(bioclim, Mex0.grd))

# Use x11() for new graphic devices.
plot(Mex0.grd)
plot(bio.sp[which(bio.sp$nameID == 'bio1.p01'),], pch = 20,
    col = 'blue', add = TRUE)

# Leaflet interaction

library(leaflet)
leaflet() %>%
    addProviderTiles('OpenStreetMap.Mapnik',
        options = providerTileOptions(noWrap = TRUE))%>%
    addPolygons(data = Mex0.grd, stroke = TRUE, color = '#FFFFFF',
        layerId = Mex0.grd@data$ID, weight = 1, opacity = 0.3,
        fillColor = '#A9A9A9', fillOpacity = 0.6,
        popup = row.names(Mex0.grd@data)) %>%
    addCircleMarkers(data = bio.sp[which(bio.sp$nameID == 'bio1.p01'),],
        radius = 3,
        popup = paste(bio.sp[which(bio.sp$nameID == 'bio1.p01'),]$nameID,
                  bio.sp[which(bio.sp$nameID == 'bio1.p01'),]$val.p, sep = ' '))


# Extracting values from first bioclim variable
system.time(bio1.sp<-raster_breaks(bioclim$bio1, Mex0.grd))

#' # Leaflet interaction

library(leaflet)
library(RColorBrewer)

# Not run, it can take time
pal <- colorFactor('RdYlBu', levels = rev(levels(as.factor(bio1.sp$nameID))))

samp <- sample(1:length(bio1.sp), 1000)

leaflet() %>%
    addProviderTiles('OpenStreetMap.Mapnik',
        options = providerTileOptions(noWrap = TRUE))%>%
    addPolygons(data = Mex0.grd, stroke = TRUE, color = '#FFFFFF',
        layerId = Mex0.grd@data$ID, weight = 1, opacity = 0.3,
        fillColor = '#A9A9A9', fillOpacity = 0.6,
        popup = row.names(Mex0.grd@data)) %>%
    addCircleMarkers(data = bio1.sp[samp,], radius = 1, color = ~pal(bio1.sp$nameID[samp]),
        popup = paste(bio1.sp$nameID[samp], bio1.sp$val.p[samp], sep = ' '))

edelcallejoc/rspecies documentation built on Dec. 17, 2018, 9:15 a.m.