Input and output operations, load or write data
r = raster(matrix(sample(1:9, 100, replace = TRUE), 10, 10))
writeRaster(r, "raster.tif")
r = raster("raster.tif")
Local analysis consider cells independently
Direct index r[i]
Using line and columns r[lin, col]
Add vectors
cellFromXY(r, pt)
## [1] 23
colFromX(r, pt$x)
## [1] 3
rowFromY(r, pt$y)
## [1] 3
fourCellsFromXY(r, as.matrix(pt))
## [,1] [,2] [,3] [,4]
## [1,] 23 33 32 22
cellFromLine(r, ln)
## [[1]]
## [1] 35 36 45 55 61 62 63 64 65 71 81 91
cellFromPolygon(r, poly)
## [[1]]
## [1] 24 25 26 34 35 36 37 45 46 47 55 56 57 65 66 67 75 76 77
r_agg = aggregate(r, 2)
r_dis = disaggregate(r, 2)
r_missing = raster(matrix(NA, 10, 10))
r_missing[41:60] = 6
r_covered = cover(r_missing, r)
r_masked = mask(r, r_missing)
fun = function(x) { x * 10 }
r_mul = calc(r, fun)
r1 = init(r, function(x) sample(5:6, x, replace = TRUE))
r2 = init(r, function(x) sample(1:2, x, replace = TRUE))
r3 = overlay(r1, r2, fun=function(x,y){return(x+y)})
We need a raster containing origins to compute distances (so we need units)
r_origin = raster(matrix(NA, 10, 10))
extent(r_origin) = extent(c(0, 10, 0, 10))
projection(r_origin) = "+proj=tmerc +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +units=m"
r_origin[c(15, 16, 45)] = 1
r_dist = distance(r_origin)
m = cbind(
from = c(0, 2, 4),
to = c(2, 4, 6),
becomes = c(1, 2, 3)
)
from | to | becomes |
---|---|---|
0 | 2 | 1 |
2 | 4 | 2 |
4 | 6 | 3 |
r_reclass = reclassify(r_dist, m)
Focal analysis consider a cell plus its direct neighbours in a contiguous and symetrical manner
r_focal = focal(r, fun = mean, w = matrix(1/9,nrow=3,ncol=3))
You can remove edge effect by ignoring NA values
r_focal_nedge = focal(r, fun = mean, w = matrix(1/9,nrow=3,ncol=3), na.rm = TRUE, pad = TRUE)
Zonal analysis consider group of cells in an irregular, but conitguous (in space or value) manner.
zonal(r, z, mean)
zone | value |
---|---|
0 | 4.0 |
1 | 5.6 |
2 | 5.6 |
3 | 4.7 |
r_clumps = clump(r_bin)
require("igraph")
r_clus = init(r, function(x) ifelse(runif(x) > 0.2, 1, NA))
r_boundaries = raster::boundaries(r_clus)
extract(r, pt)
##
## 2
extract(r, ln)
## [[1]]
## [1] 3 7 4 3 6 8 1 2 1 4 6 5
extract(r, poly)
## [[1]]
## [1] 2 7 7 2 3 7 9 4 5 8 3 8 7 1 7 1 9 5 3
rasterize(pt, r)
rasterize(ln, r)
rasterize(poly, r)
Note that polygon rasterization is by default looking at cell centroid overlap,
Statistical operations summarize the raster information
density(r)
hist(r)
Moran(r)
Geary(r)
extent(r)
f = extent(0.23, 0.86, 0.22, 0.73)
crop(r, f)
g = extent(0.43, 1.1, -0.1, 0.53)
intersect(r, g)
cellsFromExtent(r, g)
## [1] 55 56 57 58 59 60 65 66 67 68 69 70 75 76 77 78 79
## [18] 80 85 86 87 88 89 90 95 96 97 98 99 100
union(extent(r), g)
flip(r_split, "y")
extend(r, 3)
mosaic(r, r1, r2, fun = sum)
projection(r)
rotate(r)
pts = rasterToPoints(r)
pols = rasterToPolygons(r_clumps, dissolve = TRUE)
r_terrain = terrain(r_volcano, opt = c("slope", "aspect", "tpi", "tri", "roughness", "flowdir"))
Angular data can sometimes be better expressed using a circular palette. In the following figure, blue is North orientation, while South is red. Both colors reach black at West and white at East. The preceeding figure had some sharp edges on North faces, when angle slightly changed from 360 to 0.
r_hill = hillShade(r_terrain[["slope"]], r_terrain[["aspect"]])
tps = Tps(survey[, c("x", "y")], survey$layer)
## Warning:
## Grid searches over lambda (nugget and sill variances) with minima at the endpoints:
## (GCV) Generalized Cross-Validation
## minimum at right endpoint lambda = 1.406207e-05 (eff. df= 47.5 )
r_interpolate = interpolate(r_volcano, tps)
s = layerize(r)