How to set the factor in disaggregate function - r

I have a question regarding the aggregation of rasters/pixels.
I have a rasterstack with spatial resolution of 25km x 25km and I want to change it to 500m x 500m and preserve its values.
I believe that I must use the disaggregate function but I do not understand from the help page how to set the factor.

You want to go from 25 km to 500 m resolution. In that case, the factor is 25000 / 500 = 50
If you want to change the resolution with a factor that is not an integer, you can use resample instead.

Related

Resize raster for dimensions to match extent

I have a list of Raster Objects, with different extents and dimensions (columns/rows), namely (0, 1400, 0, 2000) and (500,350). The resolution is 4 (dimensions * resolution equals extent). The data values in each raster are either NA or 1.
I would like to index in the object, but using its extent, and not dimensions (so index in the range 0-2000 instead of 0-500). So, I would like to change the raster dimensions to match the extent of the object. Adjusting the resolution does not seem to apply in this case.
I tried to use resample function after creating a new raster with the appropriate dimensions, but after resampling the new raster had correct dimensions but the extent and values (all NA) were messed up. I suppose the solution might be adjusting this approach
ref
a <- raster(nrow = ymax(ref), ncol = xmax(ref))
a <- resample(ref,a)
If the raster was 500x350 pixels before, I would like it to be 2000x1400. So I would guess that where 1 pixel was, there will be 4. And I would like those 4 pixels to all have the same value as the one before.
Any ideas would be appreciated!
I have attached an example raster: https://ucarecdn.com/7cc71657-c829-4a3c-b522-7024b7996efe/

Correlating rasters with divisible resolution

I am using a multibeam echosounder to create a raster stack in R with layers all in the same resolution, which I then convert to a data frame so I can create additive models to describe the distribution of fish around bathymetry variables (depth, aspect, slope, roughness etc.).
The issue I have is that I would like to keep my resonse variable (fish school volume) fine and my predictive variables (bathymetry) coarse, such that I have say 1 x 1m cells representing the distribution of fish schools and 10 x 10m cells representing bathymetry (so the coarse cell is divisible by the fine cell with no remainder).
I can easily create these rasters individually but relating them is the problem. As each coarser cell would contain 10 x 10 = 100 finer cells, I am not sure how to program this into R so that the values are in the right location relative to an x and a y column (for cell addresses). But I realise in this case, I would need each coarse cell value to be repeated 100 times in the data frame.
Any advice would be greatly appreciated! Thanks!

R understanding raster's corLocal neighborhood size parameter

I am calculating the Pearson correlation between two rasters (identical in dimensions and cell size) in a moving window with the corLocal from the raster package. It is not clear (to me) from the manual what the neighborhood size parameter (ngb) actually means. E.g., does a ngb = 5 mean that the correlation is calculated for the focal cell plus the top-bottom-right-left cells?
I looked at the code and corLocal calls getValuesFocal():
getValuesFocal(x, 1, nrow(x), ngb=ngb)
but I couldn't understand what getValuesFocal actually does.
Thanks,
Ilik
The ngb parameter defines the neighborhood size. For example, I believe ngb=5 defines a 5 x 5 neighborhood. This should be equivalent to ngb=c(5,5) which is a vector of two integers defining the numbers of rows and cols in the neighborhood or focal window. In this example, an individual cell in the output raster would represent the correlation calculated from a 5 x 5 cell neighborhood in the two input rasters.
The raster library documentation on p. 118 might help too.

Area estimation using image in R

So i'm looking for a way to estimate the area of a region, using only the image of the map. The reason i'm doing this is I want to calculate the area that would be lost upon a certain increase in sea level and I can't find any kind of meta data for that only the maps (in image formats). Here is the link to such a map:
(source: cresis.ku.edu)
So what i have in mind is to convert this image to a gray scale image using EBimage package and then using the pixel intensity as a criteria to count the number of pixels that represent potentially threaten area.
My question is it possible? How can we you the pixel intensity as a criteria? And if there are any other approach to solve this issue?
Also if there are way to gain access to the meta data used to plot such map that I'm not aware of please tell me.
Thank you everyone.
Edit:
Thank to hrbrmstr I was able to read the grid data int R using rgdal packages. So in order to calculate the area I tried to used the rgeos package, but the dataset from CRESIS doesn't have the shape file. So how can we define the polygon and calculate the area?
Sorry if this question seems silly. This is the first time I've ever dealt with spatial data and analysis
The data is in the ESRI files:
library(rgdal)
grid_file_1m <- new("GDALReadOnlyDataset", "/full/path/to/inund1/w001001.adf")
grid_1m <- asSGDF_GROD(grid_file_1m, output.dim=c(1000, 1000))
plot(grid_1m, bg="black")
grid_1m_df <- as.data.frame(grid_1m)
str(grid_1m_df)
## 'data.frame': 2081 obs. of 3 variables:
## $ band1: int 1 1 1 1 1 1 1 1 1 1 ...
## $ x : num -77.2 -76.9 -76.5 -76.1 -75.8 ...
## $ y : num 83.1 83.1 83.1 83.1 83.1 ...

How can I find the pixel-wise maximum of multiple rasters?

I have hundreds of rasters with same resolution and extent. It's a time series and each raster represent one point of time.
I know how to find the absolute maximum value in a raster.
But how can I find the maximum value in each cell in the entire time series of rasters?
If a1,a2,......a1000 are rasters, I want to create a raster x where each pixel is the maximum of all corresponding pixels of a1-a1000.
If you first put the rasters in a stack, you can then simply apply min() or max() to the stack to get the summary RasterLayer you're after
## Example rasters and stack
r1 <- raster(matrix(1:4,ncol=4))
r2 <- -2*r1
r3 <- 2*r1
rr <- list(r1,r2,r3)
s <- stack(rr)
## Extract the pixel-wise min and max values
min(s)
max(s)
(To apply some other, more complicated function that returns a scalar for each pixel in the stack, you may want to use calc(), as demonstrated (for example) here.)

Resources