0

I have a raster with unusual dimensions and extent, these are global maps of ocean data.

rodd=rast(ymin=-91,ymax=91,xmin=-1,xmax=359,nrows=91,ncols=180,crs='WGS84')

and I would like to get it into this format so I can stack it with well-formatted data

r=rast(ymin=-90,ymax=90,xmin=-180,xmax=180,nrows=90,ncols=180,crs='WGS84')

I have tried different approaches but in all cases the data gets shifted or modified and my data is very sensitive to the location. For example data that was 100km from the coast is now 50km from it.

I have tried different function from terra: first rotate and then resample. I also tried to remove the bottom row as it contains NA values and then crop. But in all cases I experience this geographic shift of data that makes the data not usable. I also tried resample with "mean" but I get the following error "Error: [resample] not a valid warp method". Why is resample blurring data to adjacent cells?

Is there a way to minimize and data shift and loss of information?

Herman Toothrot
  • 1,463
  • 3
  • 23
  • 53

1 Answers1

0

How about this?

library(terra)
#> terra 1.6.33

r = rast(ymin = -91, ymax = 91, xmin = -1, xmax = 359, nrows = 91, ncols = 180, crs = 'WGS84')

# `?rotate`:
# Rotate a SpatRaster that has longitude coordinates from 0 to 360, 
# to standard coordinates between -180 and 180 degrees (or vice-versa).
r2 <- rotate(r, left = TRUE) 
r2
#> class       : SpatRaster 
#> dimensions  : 91, 180, 1  (nrow, ncol, nlyr)
#> resolution  : 2, 2  (x, y)
#> extent      : -181, 179, -91, 91  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)

# adjust for shift in x and y and dimension
r3 <- shift(r2, dx = 1, dy = -1)
r3
#> class       : SpatRaster 
#> dimensions  : 91, 180, 1  (nrow, ncol, nlyr)
#> resolution  : 2, 2  (x, y)
#> extent      : -180, 180, -92, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)

# now you can crop the bottom row (-92° to -90°) consisting of `NA`
r4 <- crop(r3, ext(-180, 180, -90, 90), snap = "in")
r4
#> class       : SpatRaster 
#> dimensions  : 90, 180, 1  (nrow, ncol, nlyr)
#> resolution  : 2, 2  (x, y)
#> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)

Created on 2022-12-16 with reprex v2.0.2

dimfalk
  • 853
  • 1
  • 5
  • 15
  • Thank you for the suggestion, unfortunately I cannot use the shift operation because it means shifting the location of data, so for example any data point that was on the sea near the coast will appear on land after the shift. The NA row is irrelevant, it can be removed at any time. – Herman Toothrot Dec 20 '22 at 08:05
  • @HermanToothrot: Ok, I may have another idea. In the meantime, may I ask where you downloaded these files from? – dimfalk Dec 20 '22 at 15:12
  • These are private data, so unfortunately they are not freely accessible and I can't upload them. But I could try to upload a file with random data if you think that helps. – Herman Toothrot Dec 20 '22 at 16:57
  • No, it's fine. I was just wondering who provides spatial data with such weird extents. However, if I got you right now, starting from `r` and thinking of the grid as a matrix, you would want your most left column `xmin = -1` to actually be your most right column `xmax = 360`? If you do not want to shift your data at all, not even in y-direction, how do you expect to obtain the desired result ranging from -90 to +90? With the bottom row cropped, you'll receive `ymin = -89` and `ymax = 91`. – dimfalk Dec 20 '22 at 21:15
  • I completely agree that is nonsense, it was probably created by someone with limited knowledge of GIS and data integration. The location of the data cannot be moved, imagine if a data point in country X is now in country Y. I think the solution to minimize data loss is to resample the data. Maybe @Robert_Hijman will comment, the GIS wiz. – Herman Toothrot Dec 21 '22 at 18:18