Extracting depth data from raster using lat and Lon - r

I am trying to get the maximum depth (max_depth) for a given lat and lot using this gebco (tiff file attached) and it keeps coming up with NA. In the past, this has worked so im not sure what is going wrong. The site is used to also extract temperature and nutrient data from WOCE files and that works. Is there something that I am missing in my code?
library(raster)
bathy <- raster("gebco0.5.tif")
site <- cbind(125, -49)
extract(bathy, site)
# [,1]
# [1,] NA
show(bathy)
#class : RasterLayer
#dimensions : 360, 720, 259200 (nrow, ncol, ncell)
#resolution : 1, 1 (x, y)
#extent : 0, 720, 0, 360 (xmin, xmax, ymin, ymax)
#crs : NA
#source : gebco0.5.tif
#names : gebco0.5

There is no attached file, but your code looks good, and this works
r <- raster("https://i.stack.imgur.com/g8WSo.png")
extent(r) <- c(-180,180,-90,90)
site <- cbind(125, -49)
extract(r, site)
# 0
My guess is that the extent of bathy is not what you expect. Can you show(bathy) to us?
Now that we have show(bathy) we can see that you have an unexpected extent for lon/lat data: 0, 720, 0, 360 (xmin, xmax, ymin, ymax) . Such that you get
r <- raster("https://i.stack.imgur.com/g8WSo.png")
site <- cbind(125, -49)
extract(r, site)
# [,1]
#[1,] NA
From what I can see, it appears that you need to do:
extent(r) <- c(-180, 180, -90, 90)
And then things should work (as I already showed above). And if you know whereabouts "site" is, you can visually check that with
plot(r)
points(site)

Related

Get a unique value range of a rasterlayer

My rasterlayer has a range from 0 to 1. I just want the Pixel values from 0.2 to 0.1
I tried this Code:
R<- myraster
R[(R<=0.1) & (R>=0.2)] <- NA
This is my idea for a range of value.
For a single value I don't know.
If I use this code I get online NA or the range from.0 to 1 does not change.
Is my Code wrong or is there another option?
I also used this one only to get the value 0.1
R<- myraster
R[(R<=0.1) & (R>=0.1)] <- NA
You can do it in two steps. For instance,
library(raster)
# Simulate raster
R <- raster(ncol=10, nrow=10)
values(R) <- runif(ncell(R))
#Subset the raster in two steps
R[R >= 0.2] <- NA
R[R <= 0.1] <- NA
R
Here's the output...
> R <- raster(ncol=10, nrow=10)
> values(R) <- runif(ncell(R))
> R
class : RasterLayer
dimensions : 10, 10, 100 (nrow, ncol, ncell)
resolution : 36, 18 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : memory
names : layer
values : 0.01307758, 0.9926841 (min, max)
> R[R>=0.2]<-NA
> R[R<=0.1 ]<-NA
> R
class : RasterLayer
dimensions : 10, 10, 100 (nrow, ncol, ncell)
resolution : 36, 18 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : memory
names : layer
values : 0.1008731, 0.1912601 (min, max)

from magick-image to rasterBrick

I have an image that must be processed with the package magick. So the output belongs to class magick-image. I need to transform it to a class rasterBrick for further processing.
How can I transform an object magick-image to rasterBrick? I need to avoid saving and loading an intermediate temp file.
library(magick)
library(raster)
# load sample image
i <- image_read("https://i.picsum.photos/id/10/2500/1667.jpg?hmac=J04WWC_ebchx3WwzbM-Z4_KC_LeLBWr5LZMaAkWkF68")
# does not work
r <- raster::raster(i)
# workaround that I must avoid
image_write(i,"temp_image.jpg")
t <- brick("temp_image.jpg")
t
You can do this:
library(terra)
r <- as.raster(i) |> as.matrix() |> rast()
r
#class : SpatRaster
#dimensions : 1667, 2500, 3 (nrow, ncol, nlyr)
#resolution : 1, 1 (x, y)
#extent : 0, 2500, 0, 1667 (xmin, xmax, ymin, ymax)
#coord. ref. :
#source : memory
#colors RGB : 1, 2, 3
#names : red, green, blue
#min values : 0, 8, 0
#max values : 252, 250, 248
plot(r)
You may want to stick with terra, but if you want to go back to a RasterBrick, you can add
library(raster)
b <- brick(r)

Finding a value for a variable according to different elevations

I have a DEM raster file of a specific region
DEMRASTER
class : RasterLayer
dimensions : 47, 89, 4183 (nrow, ncol, ncell)
resolution : 0.5, 0.5 (x, y)
extent : 60.75, 105.25, 15.75, 39.25 (xmin, xmax, ymin, ymax)
crs : NA
source : memory
names : newlowelevation1
values : 1, 6136.012 (min, max)
I have another raster file of variable "GPP":
GPPRASTER
class : RasterLayer
dimensions : 47, 89, 4183 (nrow, ncol, ncell)
resolution : 0.5, 0.5 (x, y)
extent : 60.75, 105.25, 15.75, 39.25 (xmin, xmax, ymin, ymax)
crs : NA
source : memory
names : layer
values : -0.333333, 0 (min, max)
How can I find the values of the GPP raster according to elevation? For instance, if I want to find a mean value of GPP for elevation from 0-2000m or 5500 to 6136m? What will be the basic code for this?
I have tried removing pixels based on the elevation that I did not want from the DEM raster file, but it is a method that takes too long. I am sure that there is a code for this, but can't put my hand on it as I am a new beginner at R myself. Thanks in advance!
Here is a minimal self-contained, reproducible example:
library(raster)
elev <- raster(ncol=47, nrow=89, ext=extent(60.75, 105.25, 15.75, 39.25))
elev <- init(elev, "cell")
gdp <- flip(elev, "y")/100
Solution:
ezones <- cut(elev, c(0,1000,2000,Inf))
zonal(gdp, ezones)
# zone mean
#[1,] 1 36.83058
#[2,] 2 26.83396
#[3,] 3 10.92250
Or with terra:
library(terra)
#terra version 1.2.1
ev <- rast(ncol=47, nrow=89, ext=ext(60.75, 105.25, 15.75, 39.25))
ev <- init(ev, "cell")
gd <- flip(ev, "vertical")/100
names(gd) <- "gdp"
names(ev) <- "elevation"
Solution:
ez <- classify(ev, c(0,1000,2000,Inf))
zonal(gd, ez)
# elevation gdp
#1 0 - 1000 36.83580
#2 1000 - 2000 26.84370
#3 2000 - inf 10.92752

oneHotEncode a rasterlayer in R

I have a categorical raster layer that I need to one-hot encode, as I'm using neural networks to run a species distribution model (for a class) and it only works on continuous predictors. I've already run the model with the one-hot encoded data itself, but in order to predict onto a map, the raster itself needs to be one-Hot encoded in the same way.
In the RStoolbox package, oneHotEncode() should do what I need it to do, but I can't get it to work
nn.raster<-oneHotEncode(newraster$NLCD_2016_Land_Cover_oreg, classes=values, background = 0, foreground = 1, na.rm = FALSE)
Error message:
Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :
cannot use this function. Perhaps add '...' or 'na.rm' to the function arguments?
Has anybody used this function and can help me troubleshoot? I think the problem is coming from the class's argument. My categories are numerical (from the national land cover raster), which is why they show up as "values" in the raster info. Do I need to do something to reclassify them? I think I'm naming them wrong but I'm not sure how.
After giving a quick look at RStoolbox::oneHotEncode, I am under the impression that it does what raster::layerize also does.
library(raster)
r <- raster(nrow=20, ncol=20)
values(r) <- c(rep(NA, 50), rep(1:5, 70))
r
#class : RasterLayer
#dimensions : 20, 20, 400 (nrow, ncol, ncell)
#resolution : 18, 9 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +no_defs
#source : memory
#names : layer
#values : 1, 5 (min, max)
b <- layerize(r)
b
#class : RasterBrick
#dimensions : 20, 20, 400, 5 (nrow, ncol, ncell, nlayers)
#resolution : 18, 9 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +no_defs
#source : memory
#names : X1, X2, X3, X4, X5
#min values : 0, 0, 0, 0, 0
#max values : 1, 1, 1, 1, 1
Which is equivalent to terra::separate
library(terra)
r <- rast(nrow=5, ncol=5)
values(r) <- rep(c(1:4, NA), each=5)
b <- separate(r)

Modifying and Masking Environmental Layers within specific asia area in R

I am trying to prepare the environmental layers (constrained in specific Asia area) for use in Maxent model. However, I ran into some error messages in the last line:
library(sp)
library(maptools)
library(rworldmap)
library(dismo)
# A specified range of Asia area that suitable for special species
tsta <- read.csv('CM10_Kop_Shp_V1.2/Asiaclip/Asiaclipt.csv',as.is=TRUE)[https://drive.google.com/file/d/0B4vIx9MCfJgfbHpINTlyUGZVbXc/view?usp=sharing][1]
tsta <- tsta[,seq(1,4)]
coordinates(tsta) = c("Lon", "Lat")
gridded(tsta) <- TRUE
ra <- raster(tsta)
# a Rasterstack contains global range of 40 bioclim variables
files3 <- list.files(path=paste
("CM10_1975H_Bio_ASCII_V1.2/CM10_1975H_Bio_V1.2"),
, pattern='txt',full.names=TRUE )[https://www.climond.org/Core/Authenticated/Data/CM10_V1.2/CM10_Bio_V1.2/CM10_Bio_ASCII_V1.2/CM10_1975H_Bio_ASCII_V1.2.zip][1]
predictors3 <- stack(files3)
asia.predictors3 <- mask(predictors3,ra)
Error in compareRaster(x, mask) : different extent
The details for predictors3 were
predictors3
class : RasterStack
dimensions : 857, 2160, 1851120, 40 (nrow, ncol, ncell, nlayers)
resolution : 0.1666667, 0.1666667 (x, y)
extent : -180, 180, -59.16667, 83.66667 (xmin, xmax, ymin, ymax)
coord. ref. : NA
names : CM10_1975H_Bio01_V1.2, CM10_1975H_Bio02_V1.2, CM10_1975H_Bio03_V1.2, CM10_1975H_Bio04_V1.2, CM10_1975H_Bio05_V1.2, CM10_1975H_Bio06_V1.2, CM10_1975H_Bio07_V1.2, CM10_1975H_Bio08_V1.2, CM10_1975H_Bio09_V1.2, CM10_1975H_Bio10_V1.2, CM10_1975H_Bio11_V1.2, CM10_1975H_Bio12_V1.2, CM10_1975H_Bio13_V1.2, CM10_1975H_Bio14_V1.2, CM10_1975H_Bio15_V1.2, ...
The details for ra were:
ra
class : RasterLayer
dimensions : 213, 290, 61770 (nrow, ncol, ncell)
resolution : 0.1666667, 0.1666667 (x, y)
extent : 97.5, 145.8333, 18.16667, 53.66667 (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : in memory
names : Location
values : 168505, 377653 (min, max)
My goal is to prepare a RasterLayer or Rasterstack contains all variables of "predictors3" but limited in the range of "ra". As you can see the extent of ra was included in the extent of predictors3 and their resolutions were identical. How should I fix the error?
In this case, as the origin and resolution of ra and predictors3 are the same, you can use crop
predictors3 <- raster(xmn=-180, xmx=180, ymn=-59.16667, ymx=83.66667, res=1/6)
ra <- raster(xmn=97.5, xmx=145.8333, ymn=18.16667, ymx=53.66667, res=1/6)
x <- crop(predictors3, ra)
In other cases, you may need to use (dis)aggregate or resample
According to the above suggestions, I crop the the global climate layer "predictors3" to identify the extent of two rasters. Then, mask the latest raster to acquire the targeting variables limited in specific area.
asia.predictors <- crop(predictors3,ra)
asia.predictors3 <- mask(asia.predictors,ra)

Resources