Search code examples
geospatialspatialprojectionterra

Project raster error, package terra 1.7.55 xy not numeric, example code from documentation produces same error


I don't know how to fix this does anyone have any suggestions?

I'm having trouble projecting rasters. I get the message

Warning: PROJ support is provided by the sf and terra packages among othersError in project(r, "+proj=longlat +datum=WGS84") : xy not numeric

for both project raster

projected <- project(r, "+proj=longlat +datum=WGS84") 
# Warning: PROJ support is provided by the sf and terra packages among others
# Error in project(r, "+proj=longlat +datum=WGS84") : xy not numeric
# Project raster
projected <- project(r, elevation) 
# Warning: PROJ support is provided by the sf and terra packages among others
# Error in project(r, elevation) : xy not numeric

elevation
#class       : SpatRaster 
#dimensions  : 12000, 9600, 1  (nrow, ncol, nlyr)
#resolution  : 0.008333333, 0.008333333  (x, y)
#extent      : -20, 60, -60, 40  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#source      : Elev_gtopo30_Africa.tif 
#name        : Elev_gtopo30_Africa 
#min value   :                -407 
#max value   :                5825 

The example code below is directly from the documentation (Package ‘terra’---December 15, 2023---Type Package---Title Spatial Data Analysis---Version 1.7-65 ---Date 2023-12-14) for the function "project" also gives the same error so I don't know how to fix this does anyone have any suggestions?

library(terra)
library(sf)
 
packageVersion("terra")
#[1] ‘1.7.55’
 
## SpatRaster
a <- rast(ncols=40, nrows=40, xmin=-110, xmax=-90, ymin=40, ymax=60,
           crs="+proj=longlat +datum=WGS84")
values(a) <- 1:ncell(a)
newcrs="+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +datum=WGS84"
b <- rast(ncols=94, nrows=124, xmin=-944881, xmax=935118, ymin=4664377, ymax=7144377, crs=newcrs)
w <- project(a, b)
#Error in project(a, b) : xy not numeric
#In addition: Warning message:
#PROJ support is provided by the sf and terra packages among others 

Solution

  • When you have trouble like this start R in a fresh session, with nothing loaded, no packages, no data. See ls() and sessionInfo(). Only a few base packages should be loaded.

    Then try the example (and you will see that it works).

    In your case the problem is that you have loaded the (obsolete) "rgdal" package after loading "terra". Both packages have a project method, and the version of the last package loaded hides the other version.

    You can see that this is the problem by stating the namespace from which you want the project method:

    w <- terra::project(a, b)
    

    instead of

    w <- project(a, b)