Search code examples
rrasterterra

Projecting points with terra package R


I need to project longitude/latitude coordinates in the terra package, but I don't believe it is working correctly, as I am trying to extract data from a raster with this projection, but the data is not being extracted correctly.

Here's my lon/lat points and the code I am using to try to project them.

latlon_df <- structure(list(Lon = c(-103.289, -96.6735, -96.9041, -96.76864, 
-102.4694, -96.6814, -97.7504, -99.6754, -96.4802, -103.0007, 
-96.8897, -101.8539, -103.9717, -101.253, -99.1134, -96.5849, 
-98.0301, -99.9537, -99.4601, -99.7122, -103.8278, -98.931, -102.1081, 
-101.7162, -100.115, -101.3448, -100.7805, -103.5606, -96.5302, 
-99.4156, -103.281, -100.0063, -97.9928, -100.7208, -98.5289, 
-96.762, -96.9218, -97.1024, -103.3793, -101.0841, -102.6745, 
-96.9188, -97.5154, -100.7435, -98.6938), Lat = c(45.5194, 44.3099, 
43.0526, 44.3252, 45.5183, 43.7316, 45.6796, 45.4406, 44.7154, 
44.0006, 43.7687, 43.9599, 43.4737, 44.9875, 45.0292, 44.0867, 
45.5735, 44.9895, 44.5256, 43.5938, 43.7343, 45.7163, 45.9189, 
43.1672, 45.6716, 45.9154, 45.7963, 44.6783, 44.5073, 43.7982, 
43.3784, 44.2912, 43.3841, 43.2002, 44.8579, 43.5048, 43.5033, 
45.1055, 44.4245, 45.4167, 44.5643, 44.304, 45.2932, 43.5601, 
43.7321)), class = "data.frame", row.names = c(NA, -45L))

latlons <- terra::vect(latlon_df,geom=c('Lon','Lat'),crs="+proj=longlat")
lcc <- terra::project(latlons,"+proj=lcc +lat_0=38.5 +lon_0=262.5 +lat_1=38.5 +lat_2=38.5 +x_0=0 +y_0=0 +R=6371229 +units=m +no_defs")

var_df <- terra::extract(grib_data,lcc)[,-1]

The raster data (grib_data) I am using comes from here (it is way too big for me to put on here). https://nomads.ncep.noaa.gov/pub/data/nccf/com/hrrr/prod/hrrr.20210612/conus/hrrr.t00z.wrfsubhf00.grib2

I am not sure what I am doing wrong here, as I have used this method previously, and it seemed to work fine. Any help would be wonderful.

EDIT: The specific problem I am having is that I am not getting any different values for each lon/lat pair. The value for each variable is different, but all the values for the stations (different lon/lats are the same).


Solution

  • Why do you think it has to do with the projection? Either way, it appears to work for me.

    url <- "https://nomads.ncep.noaa.gov/pub/data/nccf/com/hrrr/prod/hrrr.20210612/conus/hrrr.t00z.wrfsubhf00.grib2"
    if (!file.exist(basename(url))) download.file(url, basename(url), mode="wb")
    url <- paste0(url, ".idx")
    if (!file.exist(basename(url))) download.file(url, basename(url), mode="wb")
    
    library(terra) 
    r <- rast("hrrr.t00z.wrfsubhf00.grib2")
    r
    #class       : SpatRaster 
    #dimensions  : 1059, 1799, 49  (nrow, ncol, nlyr)
    #resolution  : 3000, 3000  (x, y)
    #extent      : -2699020, 2697980, -1588806, 1588194  (xmin, xmax, ymin, ymax)
    #coord. ref. : +proj=lcc +lat_0=38.5 +lon_0=262.5 +lat_1=38.5 +lat_2=38.5 +x_0=0 +y_0=0 +R=6371229 +units=m +no_defs 
    #source      : hrrr.t00z.wrfsubhf00.grib2 
    #names       : 0[-] ~here", 0[-] ~tops", 0[-] ~here", 0[-] ~here", 0[-] ~face", 1000[~ound", ... 
    

    You can check of the points overlap with the raster data

    plot(r, 1)
    points(lcc)
    

    And extract. It takes very long with grib files, but it does appear to work

    e <- extract(r, lcc)
    
    head(e[,c(1,6,9)])
    #  ID 0[-] SFC="Ground or water surface" 0[-] SFC="Ground or water surface".1
    #1  1                              85100                            11.775471
    #2  2                              54400                            11.087971
    #3  3                              79300                             9.900471
    #4  4                              49200                            10.712971
    #5  5                              70800                             9.212971
    #6  6                              56600                            11.400471
    

    Make sure you have the current (CRAN) version, or perhaps the development version that you can install like this: install.packages('terra', repos='https://rspatial.r-universe.dev')

    You can speed things up a lot by doing a single read from disk (by adding zero in this example)

    e <- extract(r+0, lcc)
    

    That is not always possible and I need to do some optimization behind the scences.