Search code examples
rrasterspatialterra

Issue using terra::project


I'm currently working with two SpatRaster. One with population density (Pop_moll) and the other with temperature data from 1901 to 2020 (sd_pre). The first one has 10 layers that are the information of the population of each year, and the second one has 2880 which is the temperature data of each month. I want to reproject the SpatRaster of the population density (Pop_moll) to the dimensions of the SpatRaster (sd_pre) but when I use terra::project the raster loses the values.

I am trying to use the function with the next arguments:

terra::project(Pop_moll,sd_pre, method='bilinear', align=TRUE)

The information of the SpatRasters is the following:

sd_pre:

class       : SpatRaster
dimensions  : 360, 720, 2880  (nrow, ncol, nlyr)
resolution  : 0.5, 0.5  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84
sources     : cru_ts4.05.1901.2020.pre.dat.nc:pre  (1440 layers)
              cru_ts4.05.1901.2020.pre.dat.nc:stn  (1440 layers)
varnames    : pre (precipitation)
              stn
names       :    pre_1,    pre_2,    pre_3,    pre_4,    pre_5,    pre_6, ...
unit        : mm/month, mm/month, mm/month, mm/month, mm/month, mm/month, ...
time (days) : 1901-01-16 to 2020-12-16

Pop_moll:

class       : SpatRaster
dimensions  : 213822, 432002, 10  (nrow, ncol, nlyr)
resolution  : 0.0008333333, 0.0008333333  (x, y)
extent      : -180.0004, 180.0012, -89.09292, 89.09208  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
sources     : GHS_POP_E1975_GLOBE_R2023A_4326_3ss_V1_0.tif
              GHS_POP_E1980_GLOBE_R2023A_4326_3ss_V1_0.tif
              GHS_POP_E1985_GLOBE_R2023A_4326_3ss_V1_0.tif
              ... and 7 more source(s)
names       :     1975,     1980,     1985,     1990,     1995,     2000, ...
min values  :     0.00,     0.00,     0.00,     0.00,     0.00,     0.00, ...
max values  : 39114.75, 32527.26, 35725.06, 35705.88, 40233.43, 45619.54, ...

And when I project I get this:

class       : SpatRaster
dimensions  : 360, 720, 10  (nrow, ncol, nlyr)
resolution  : 0.5, 0.5  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84

a SpatRaster without the values of the Pop_moll.

I want to know if theres is something wronge in the arguments of terra::project of there is another way to asign to my new Raster (with the dimensions of the sd_pre) the values of the population of the Pop_moll raster.

PD.Additional information, I'm currently worrking on a server.


Solution

  • Both datasets are using the same coordinate system (lat-long coordinates on the WGS84 sphere) so there is no need to project the GHS data set. You may be confused by the fact that the default GHS coordinate system is Mollweide (EPSG:54009) but the file name and the SpatRaster information clearly indicate that this is 3 arc-second data with EPSG:4326 coordinate system.

    Since the origin and extent of both data sets do not align you can terra::resample() the GHS data to the 0.5 degree resolution and extent of the "sd_pre" data set. Summing the cells to get the totals at the coarser resolution, that looks like this:

    coarse_pop <- terra::resample(Pop_moll, sd_pre, sum)