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)
Related
I have a raster in working directory:
When I read this using terra package, it reads fine
r1 <- terra::rast(my_rast)
r1
class : SpatRaster
dimensions : 6000, 6000, 8 (nrow, ncol, nlyr)
resolution : 0.0008333333, 0.0008333333 (x, y)
extent : 59.99958, 64.99958, 24.99958, 29.99958 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
source : n25e060.tif
names : n25e060_1, n25e060_2, n25e060_3, n25e060_4, n25e060_5, n25e060_6, ...
But If I read the same raster using the raster package, I get the following warning:
r2 <- raster::raster(my_rast)
Warning message:
In .rasterFromGDAL(x, band = band, objecttype, ...) :
data seems flipped. Consider using: flip(x, direction='y')
class : RasterLayer
band : 1 (of 8 bands)
dimensions : 6000, 6000, 3.6e+07 (nrow, ncol, ncell)
resolution : 0.0008333333, 0.0008333333 (x, y)
extent : 59.99958, 64.99958, 25.00042, 30.00042 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : n25e060.tif
names : n25e060
values : 0, 255 (min, max)
Why is this happening?
That suggests that either {terra} can directly use "flipped" raster data, whereas that {raster} cannot or that {terra} does not notice that the data are flipped. I believe the former to be true, but, while this would be easy for you to verify, I do not have the file so I cannot be 100% sure.
I have a netcdf file that contains 79 variables, and for each variable there are 365 bands (one for each day of the year). I want to read all bands of one variable (i.e., a raster with 365 ayers). With the 'raster' package this would work as follows:
dailyvalues <- brick('GLOBAL_2010_day.nc', varname ='WDEP_PREC')
Result is a RasterBrick with 365 layers:
> dailyvalues
class : RasterBrick
dimensions : 180, 360, 64800, 365 (nrow, ncol, ncell, nlayers)
resolution : 1, 1 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : GLOBAL_2010_day.nc
names : X2010.01.01, X2010.01.02, X2010.01.03, X2010.01.04, X2010.01.05, X2010.01.06, X2010.01.07, X2010.01.08, X2010.01.09, X2010.01.10, X2010.01.11, X2010.01.12, X2010.01.13, X2010.01.14, X2010.01.15, ...
Date : 2010-01-01, 2010-12-31 (min, max)
varname : WDEP_PREC
But I haven't suceeded to do this with the 'terra' package. I tried
dailyvalues <- rast('GLOBAL_2010_day.nc')
> dailyvalues
class : SpatRaster
dimensions : 180, 360, 28835 (nrow, ncol, nlyr)
resolution : 1, 1 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs
sources : GLOBAL_2010_day.nc:WDEP_PREC (365 layers)
GLOBAL_2010_day.nc:WDEP_SOX (365 layers)
GLOBAL_2010_day.nc:WDEP_OXN (365 layers)
... and 76 more source(s)
varnames : WDEP_PREC (WDEP_PREC)
WDEP_SOX (WDEP_SOX)
WDEP_OXN (WDEP_OXN)
...
names : WDEP_PREC_1, WDEP_PREC_2, WDEP_PREC_3, WDEP_PREC_4, WDEP_PREC_5, WDEP_PREC_6, ...
unit : mm, mm, mm, mm, mm, mm, ...
time : 2010-01-01 18:00:00 to 2010-12-31 12:00:00
The resulting SpatRaster has 79 'sources', but what is the syntax to use if I want to extract one 'source'? Adding varname = 'WDEP_PREC' in the rast function doesn't work. I tried dailyvalues$... but that calls single layers only (as listed under 'names').
The argument to use is called subds in terra.
dailyvalues = rast("GLOBAL_2010_day.nc", subds="WDEP_PREC")
You should be able to do (as #dww says)
dailyvalues = rast("GLOBAL_2010_day.nc", subds="WDEP_PREC")
Or you can make a SpatRasterDataset
x = sds("GLOBAL_2010_day.nc")
And then extract the sub-dataset you want with
r <- x["WDEP_PREC"]
or
r <- x[1]
Furthermore, you can do
r <- rast('NETCDF:"GLOBAL_2010_day.nc:WDEP_PREC":WDEP_PREC')
(for the above to work you may need to add the path to the filename, or set your working directory to where it is)
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'm having trouble merging two raster datasets that have different resolution and extent, and have irregular point data. Below is info on each raster. I need these datasets to merge according to the grid points so that I can run a fire spread model (which requires both wind and slope data).
I have tried converting to normal dataframes (using rasterToPoints) before merging but the differences lead to loss of a lot of grid points.
I have tried to align the rasters with project raster and rasterize, but I haven't managed to get it to work. If anyone has an idea that could help, I would really appreciate your answer!
'''
Data from https://globalwindatlas.info/downloads/gis-files
> wind <- raster("ZAF_wind-speed_10m.tif"); wind
class : RasterLayer
dimensions : 11271, 11804, 133042884 (nrow, ncol, ncell)
resolution : 0.0025, 0.0025 (x, y)
extent : 13.33407, 42.84407, -50.31423, -22.13673 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : C:/Users/s2000128/Documents/Flammability modelling/ZAF/ZAF_wind-speed_10m.tif
names : ZAF_wind.speed_10m
Data from https://datacatalog.worldbank.org/dataset/world-slope-model
> slope <- raster("ZAF1_msk_alt.grd"); slope
class : RasterLayer
dimensions : 1548, 1992, 3083616 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 16.4, 33, -34.9, -22 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +ellps=WGS84 +no_defs
source : C:/Users/s2000128/Documents/Flammability modelling/slope_deg_0/slope_deg/ZAF1_msk_alt.grd
names : ZAF1_msk_alt
values : -26, 3264 (min, max)
Here are some examples of what I have tried:
1
windresampled <- projectRaster(wind,slope,method = 'ngb'); windresampled
2
wind_points <- rasterToPoints(wind);
coordinates(wind_points) = ~x+y;
proj4string(wind_points) = CRS("+init=epsg:4326");
gridded(wind_points) = TRUE;
g_wind <- raster(wind_points); g_wind;
extent(wind) <- c(16.4,33, -34.9,-22);
res(wind) <- c(0.0025, 0.0025);
r_wind <- rasterize(wind_points, g_wind, field = wind_points$z, fun = mean, na.rm = TRUE); r_wind
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))