Home > Back-end >  Transferring values from one Spatraster to another: why cant I find them anymore in a new raster?
Transferring values from one Spatraster to another: why cant I find them anymore in a new raster?

Time:10-25

I have a dataframe of of 10 values (lon,lat, degree) of wind data over germany.

I would like to put this data into a raster so I can plot the wind direction in point (lon,lat) later.

To do so I create 2 rasters

  1. with wind data

  2. a raster over germany with no values.

Then I project the first ratser onto second one.

To check if i did it correctly i extract the data of resulting raster with the coordinates from the data table yet get no result. What am I doing wrong?

Thank you in advance. Here is my code:

library(terra)

wind_data<-structure(list(lon = c(9.18175071980108, 9.19724595932033, 9.21274049124152, 
9.25921982682543, 9.27471151376548, 9.29020248581541, 9.30569274151826, 
9.32118227941754, 9.33667109805727, 9.35215919598192), lat = c(54.8795643952007, 
54.8795643952007, 54.8795643952007, 54.8795643952007, 54.8795643952007, 
54.8795643952007, 54.8795643952007, 54.8795643952007, 54.8795643952007, 
54.8795643952007), dd = c(217, 217, 217, 217, 217, 217, 217, 
217, 217, 217)), class = "data.frame", row.names = c(NA, -10L
)) 
###########
    lon      lat  dd
1  9.181751 54.87956 217
2  9.197246 54.87956 217
3  9.212740 54.87956 217
4  9.259220 54.87956 217
5  9.274712 54.87956 217
6  9.290202 54.87956 217
7  9.305693 54.87956 217
8  9.321182 54.87956 217
9  9.336671 54.87956 217
10 9.352159 54.87956 217
#####


 crs_new_wetter<-" proj=longlat  lat_0=90  lat_ts=60  lon_0=10  a=6378137  b=6356752.3142451802  no_defs  x_0=523196.83521777776  y_0=3772588.861931134"
    
   ##new_raster is the empty raster I would like to transfer the values from wind_data into 
   new_raster <- rast(ncols=900, nrows=900, ext= c(3.604382997,14.60482286, 47.07156997, 54.73806893), crs=crs_new_wetter)
    
#r is the raster with wind data

r <- rast(nrow=nrow(wind_data), ncol=1, crs=  crs_new_wetter)
values(r) <- wind_data$dd
#x is the resulting raster which should have the wind data    
x <- resample(r, new_raster, method="bilinear")
# here i lookup in the resulting raster with the coordinates of the points

terra::extract(x, data.frame(wind_data[,1], wind_data[,2]))
# result looks empty:    
 ID lyr.1
1   1   NaN
2   2   NaN
3   3   NaN
4   4   NaN
5   5   NaN
6   6   NaN
7   7   NaN
8   8   NaN
9   9   NaN
10 10   NaN

CodePudding user response:

A couple of things you do are not correct.

First, what is this?

crs_new_wetter<-" proj=longlat  lat_0=90  lat_ts=60  lon_0=10  a=6378137  b=6356752.3142451802  no_defs  x_0=523196.83521777776  y_0=3772588.861931134"

You are using proj=longlat but then adding all kind of other parameters that do not apply to a longitude/latitude crs.

Then you do the below

r <- rast(nrow=nrow(wind_data), ncol=1, crs=  crs_new_wetter)
values(r) <- wind_data$dd
r 
#class       : SpatRaster 
#dimensions  : 10, 1, 1  (nrow, ncol, nlyr)
#resolution  : 360, 18  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. :  proj=longlat  lat_0=90  lon_0=10  a=6378137  b=6356752.3142451802  no_defs  x_0=523196.83521777776  y_0=3772588.861931134 
#source(s)   : memory
#name        : lyr.1 
#min value   :   217 
#max value   :   217 

That is, you create a global raster with one column. You have now lost all your true georeferencing. There is no variability be longitude, and your first and last record are now near the poles.

You need to rethink your entire workflow. You probably want something like this

library(geodata)
g <- gadm("Germany", path=".")
r <- rast(g, res=0.01)
# Or following your example, 
r <- rast(ncols=900, nrows=900, ext= c(3.604382997,14.60482286, 47.07156997, 54.73806893), crs=" proj=longlat")

w <- rasterize(as.matrix(wind_data[,1:2]), r, wind_data[,3])
  • Related