Antonio de Rosa
Antonio de Rosa

Reputation: 1

Reprojection between CRS for raster data in R

I have two large raster datasets in R with different coordinate reference systems (CRS) and resolutions (they cover the whole world surface): • Raster 1: World Mollweide projection, 10 km² resolution. • Raster 2: WGS84 projection, 0.1-degree resolution.

I need to reproject Raster 2 to match the CRS and resolution of Raster 1, while ensuring that the total sum of values is preserved. That is, the new pixel values should represent the weighted sum of the original pixel values that overlap with the new grid.

I’m using the terra package and the project() function for reprojection. However, I noticed that after reprojection, the global sum of all values in the raster is not preserved and I get far higher values.

My question:

How can I properly reproject Raster 2 so that it matches the CRS and resolution of Raster 1, while ensuring that the total sum of values remains the same?

Any guidance or example code would be greatly appreciated!

I’m using the terra package and the project() function with method = "sum":

r2_moll <- project(r2, r1, method = "sum")

sum_before <- global(r2, "sum", na.rm = TRUE)
sum_after <- global(r2_moll, "sum", na.rm = TRUE)

But sum_before << sum_after.

Upvotes: 0

Views: 23

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47546

You may use "average" instead, but I think your expectations are not reasonable.

Example data

library(terra)
x <- rasterize(geodata::world(), rast())
m <- project(x, "+proj=moll")

m <- mask(init(m, 1), m)
p <- project(m, "+proj=longlat", method="average")

The number of cells with data with data in m and p is not the same, so why would the sum be the same?

global(p, "notNA")
#       notNA
#layer 114431
global(m, "notNA")
#      notNA
#layer 73324

Also, summing may be questionable projection for most data types (the exception would be count data) given the given the variation in cell sizes in either.

Upvotes: 0

Related Questions