I have a DEM tiff file of a specific region:
> imported_raster
class : RasterLayer
dimensions : 28034, 53030, 1486643020 (nrow, ncol, ncell)
resolution : 0.0008333334, 0.0008333334 (x, y)
extent : 60.85375, 105.0454, 15.95708, 39.31875 (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : C:\XX.tif
names : XX
values : -27, 8806 (min, max)
attributes :
ID OBJECTID Value Count
from: 0 1 -27 2
to : 8528 8529 8806 1
I want to set a range of pixel values to NULL. For instance if I want to remove elevation pixel values from -27 to 0 , and 0 to 1000 to NULL. How can I carry this about in R?
In your case this should work well:
library(raster)
library(terra)
# with raster -------------------------------------------------------------
r <- raster()
r[] <- -27:(ncell(r)-28)
new.r <- clamp(r,lower=1000,useValues=F)
# with terra --------------------------------------------------------------
t <- rast(r)
new.t <- clamp(t,lower=1000,values=F)
However, there are several ways to do this, for example with raster::reclassify, terra::classify, but also r[r<1000] <- NA
Related
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)
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
I have a Formal Class Raster and I am trying to apply the boot::inv.logit() function to its raster cells, for example:
r1 <- raster(nrows=25, ncols=25, vals=rtnorm(n = 625, .1, .9))
r2 <- boot::inv.logit(r1)
However, when I try that, it retruns an error:
> Error in plogis(x) : Non-numeric argument to mathematical function
If I turn the raster into a matrix, and then back to raster, it gets the job done, but I loose all the other info associated with the "Formal Class Raster" I had at the beginning, which is not ideal:
r2 <- boot::inv.logit(as.matrix(r1))
r2 <- as.raster(r2)
Is there an easy way to either recover the Formal Class Raster info I had before or apply the inv.logit() to the raster without the as.matrix() transformation? Thank you in advance.
If you want to apply the function to the raster, use the calc method from raster:
r2 <- calc(r1,boot::inv.logit)
> r2
# class : RasterLayer
# dimensions : 25, 25, 625 (nrow, ncol, ncell)
# resolution : 14.4, 7.2 (x, y)
# extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
# data source : in memory
# names : layer
# values : 0.07434905, 0.9498965 (min, max)
Alternatively, you can make an empty copy of r1, and just fill in the values coming out of inv.logit:
r2 <- raster(r1)
r2[] <- boot::inv.logit(as.matrix(r1))
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)
I am having trouble with the extract function in the raster package. The raster is not empty, but when I perform the extract function all the values come back as NAs. I can plot the points over the raster fine and they show up in the same location.
library(dismo)
library(raster)
#Import CSV File
file <- paste("C:/Michelle/Michelle/R/CuskPA_Sept12_2013_1.csv", sep=" ")
#read file
cusk <- read.table(file, header=TRUE, sep=',')
cusks<- subset(cusks, SlongDD!=0 & SlatDD!=0)
cuskll <-cusks[,24:25]
#import raster
salvar <- raster("C:/Michelle/Michelle/R/Variables/salvar")
ext <-extent(-67.6, -64.9, 41.0, 44.7453)
salvarc <-crop(salvar, ext)
#extract from raster
psalvar <- extract(salvarc, cuskll)
This is what I get when I'm finished the extraction
head(psalvar)
[1] NA NA NA NA NA NA
summary(psalvar)
Mode NA's
logical 3859
The raster does not show up as empty.
> salvar
class : RasterLayer
dimensions : 334, 240, 80160 (nrow, ncol, ncell)
resolution : 0.01121539, 0.01121539 (x, y)
extent : -67.59662, -64.90493, 41.00048, 44.74642 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs
data source : in memory
names : salvar
values : 0.01655413, 2.137321 (min, max)
Here is an example of the lat/longs in the file
head(cuskll)
SlatDD SlongDD
1 44.41533 -66.07883
2 44.41167 -66.07733
3 44.44883 -66.07433
4 44.47067 -66.01217
5 44.47050 -66.01183
6 44.47633 -66.01450
Put longitude first and latitude second