raster

How to loop through raster brick?

孤者浪人 提交于 2019-12-25 04:42:46
问题 I have a raster brick of SCA(nrow=108,ncol=132,nlayers=365) which contains fractional snow cover. I want to make 46 stacks each of 8 layers from this and calculate maximum fractional snow cover from these 46 stacks.How can i do this? 回答1: I think you may want to do that this way: library(raster) # example data sca <- brick(nrow=108,ncol=132,nl=365) values(sca) <- runif(ncell(sca)*nlayers(sca)) # indices grouping sets of 8 i <- rep(1:ceiling(365/8), each=8) # the last period is not a complete

How to loop through raster brick?

走远了吗. 提交于 2019-12-25 04:42:20
问题 I have a raster brick of SCA(nrow=108,ncol=132,nlayers=365) which contains fractional snow cover. I want to make 46 stacks each of 8 layers from this and calculate maximum fractional snow cover from these 46 stacks.How can i do this? 回答1: I think you may want to do that this way: library(raster) # example data sca <- brick(nrow=108,ncol=132,nl=365) values(sca) <- runif(ncell(sca)*nlayers(sca)) # indices grouping sets of 8 i <- rep(1:ceiling(365/8), each=8) # the last period is not a complete

R focal (raster package) function that calculates relative to center cell of moving window

女生的网名这么多〃 提交于 2019-12-25 03:57:18
问题 I have a large raster data set (several actually). I'm looking for a moving window process for R (like 'focal' in the raster package). However, the function to apply to the window needs to be caclulated relative to the center cell of said window. For a simple example, I would like a moving window function that tells me how many of the cells in the window are within some value 'd' of the center cell of the window. I suspect I could do this easily by simply querying the value of the center cell

calculate median of several raster files with different extent

南笙酒味 提交于 2019-12-25 03:05:15
问题 I'm quite new to R and I have a problem on which I couldn't find a solution so far. I have a folder of 1000 raster files. I have to get the median of all rasters for each cell. The files contain NoData Cells (I think therefore they have different extents) Is there any solution to loop through the folder, adding together all files an getting the median? Error in rep(value, times = ncell(x)) : invalid 'times' argument In addition: Warning message: In setValues(x, rep(value, times = ncell(x))) :

Reclassify a raster based on 2 rasters

喜夏-厌秋 提交于 2019-12-25 02:28:06
问题 I have two rasters ( r1 and r2 ) in R with the same spatial extent and I'd like to create a raster ( r3 ) that is conditional on values in r1 and r2 . I can set up a matrix and use raster::reclassify , but I can only make this work using one of the rasters. I'm looking for an efficient way to do this on the two rasters. For example (see below) if r1 = 0 and r2 < 2 , r3 = 0.5 , but if r1 = 1 and r2 < 2 , r3 = .8 . Then if r1 = 0 and r2 > 2 , r3 = 0.7 , but if r1 = 1 and r2 > 2 , r3 = .9 (I

Calculate maximum length of consecutive days above a certain threshold in a raster stack

我与影子孤独终老i 提交于 2019-12-25 01:56:13
问题 I would like to calculate the maximum length of consecutive days above a threshold t given a raster stack s as shown below: library(raster) set.seed(112) x1 <- raster(nrows=10, ncols=10) x2=x3=x4=x5=x6=x1 x1[]= runif(ncell(x1)) x2[]= runif(ncell(x1)) x3[]= runif(ncell(x1)) x4[]= runif(ncell(x1)) x5[]= runif(ncell(x1)) x6[]= runif(ncell(x1)) s=stack(x1,x2,x3,x4,x5,x6)*56 Here is my current function. fun <- function(x,t){ y <- rle((x > t)*1) z <- y$lengths[y$values==1] return(max(z,0)) } I have

Confusion about the mask() function in the raster package

筅森魡賤 提交于 2019-12-24 23:53:17
问题 I apologise in advance for the very basic nature of this question, but I'm confused about how the mask() function works in the raster package in R. Reading the documentation for the function it sounds like cells in raster x are set to NA (the default) if these cells match a maskvalue in a mask object (the default maskvalue being NA). However, the description of the mask() function in the book Geocomputation with R by Lovelace et al. (https://geocompr.robinlovelace.net/spatial-operations.html

Why do I get different results in plotting a NetCDF layer with “image(x,y,z)” and “plot (raster)” using R-package Raster?

社会主义新天地 提交于 2019-12-24 15:46:36
问题 This might be something really simple to fix. I want to plot over a map a data layer from a NetCDF file using the function plot(raster). I don't know why I'm getting a raster skewed/offset (My guess is that the problem is in the transformation, resolution?) as shown in the following image. Incorrect map If I use the function image(x,y,z...) with the lat,lng, value matrices I get the correct display as is shown here: Correct map This is the code in R that I'm using: library(ncdf) library

adding land boundary to a filled.contour plot

孤者浪人 提交于 2019-12-24 15:14:16
问题 I'd like to add the boundary lines for Scotland into a filledContour plot I have of a raster file. so my code currently looks like: filledContour(neap_hu3, levels=seq(-1,8,0.25), col=rgb.palette(36), plot.title = title(xlab="Longitude", ylab="Latitude"), plot.axes={ axis(1); axis(2); contour(neap_hu3, add=T )} ) where neap_hu3 is my raster file. I've tried adding map(add=TRUE) but that didn't work. also tried plotting a shapefile of the boundary after the filledContour plot but that didn't

Extracting pixels values and coordinates in neighborhood of given buffer in R

这一生的挚爱 提交于 2019-12-24 15:05:04
问题 I want to get values (pixels values), coordinates ( x and y ) and attribute ( status ) in the neighborhood (for example in a buffer=6 meters) of random coordinates ( pts ), using extract function in raster package. I try to organize the results in data.frame without success. library(raster) #create some GeoTIFF rasters r <- raster(ncol=10, nrow=10) s <- stack(lapply(1:8, function(i) setValues(r, runif(ncell(r))))) f1 <- file.path(tempdir(), "sl1.tif") f2 <- file.path(tempdir(), "sl2.tif")