Search code examples
rrasterterra

Error from terra: unused argument (type = "xyz")


I am trying to convert xyz coordinates and values into a SpatRaster however, I keep getting the following error:

unused argument (type = "xyz")

Even when I run it as:

rast(re, type="xyz")

Here's the data I am working with:

structure(list(x = c(-1.18187405993761, -2.90073956374817, -3.08021949587297, 
-6.31776134843182, -6.31776134843182, -3.3218270968102, -3.57033777205992, 
-5.30991249880795, -4.22612983174668, -2.20352905818646, -2.19662598387397, 
-3.21828098212281, -6.29014905118185, -3.39776091424761, -3.39776091424761, 
-3.77052692712219, -4.10877756843431, -6.88381344205618, -3.07331642156048, 
-2.85932111787322, -4.98546800612082, -2.52797355087359, -3.21137790781032, 
-4.42631898680895, -5.06140182355823, -3.22518405643531, -3.63246544087235, 
-3.37014861699764, -2.90764263806066, -4.5643804730588, -3.75672077849721, 
-3.19757175918534, -2.57629507106104, -1.68579848474954, -4.18471138587172, 
-2.9628672325606, -3.95000685924699, -1.58225237006216, -2.78338730043581, 
-6.22111830805693, -5.82764307224487, -6.0692506731821, -7.90546844030503, 
-6.64910891543144, -3.01809182706054, -0.64343426356322, -2.16211061231151, 
-4.63341121618372, -2.85932111787322, -1.16806791131263), y = c(60.1326022736268, 
58.9521765661906, 58.4482521413787, 58.2204506890665, 58.213547614754, 
57.709623189942, 57.6474955211296, 57.6129801495672, 57.4887248119423, 
57.2056987651301, 57.1780864678801, 57.0400249816303, 57.0124126843803, 
57.0124126843803, 57.0055096100678, 56.7155804889432, 56.5291974825059, 
56.4946821109434, 56.4532636650685, 56.377329847631, 56.0252730576939, 
56.0045638347565, 55.970048463194, 55.8457931255692, 55.8388900512567, 
55.8250839026317, 55.7008285650068, 55.7008285650068, 55.5420578558195, 
55.4799301870071, 55.4178025181946, 55.3142564035073, 55.2314195117573, 
55.2176133631324, 55.10026109982, 54.9345873163202, 54.9276842420077, 
54.7689135328204, 54.6722704924455, 54.651561269508, 54.6032397493206, 
54.4513721144457, 54.4375659658207, 54.3478259997583, 54.3409229254458, 
54.1062183988211, 54.0993153245086, 54.0855091758836, 54.0786061015711, 
53.9198353923838), z = c(71, 66, 124, 109, 110, 75, 65, 64, 60, 
35, 29, 8, 98, 18, 17, 42, 4, 115, 88, 72, 9, 33, 38, 92, 21, 
93, 26, 12, 15, 7, 69, 39, 63, 87, 43, 25, 114, 34, 89, 1, 108, 
56, 76, 5, 47, 55, 81, 100, 86, 6)), row.names = c(159527L, 417317L, 
527448L, 576776L, 578285L, 688876L, 702421L, 709714L, 737033L, 
799195L, 805232L, 835264L, 840855L, 841274L, 842783L, 906107L, 
946801L, 953944L, 963550L, 980180L, 1056831L, 1061714L, 1069160L, 
1096146L, 1097563L, 1100847L, 1127950L, 1127988L, 1162762L, 1176103L, 
1189801L, 1212517L, 1230715L, 1233862L, 1259153L, 1295546L, 1296912L, 
1331962L, 1352914L, 1356943L, 1367563L, 1400726L, 1403478L, 1423277L, 
1425312L, 1476962L, 1478251L, 1480911L, 1482677L, 1517629L), class = "data.frame")


Solution

  • The problem is that re is a data.frame and there is no rast-data.frame method. Since a data.frame is a type of list, and because there is a rast-list method, that is where this goes, as you can see like this:

    library(terra)
    r <- rast(re)
    #Error: [rast,list] none of the elements of x are a SpatRaster
    

    The error occurs because the list method expects a list of SpatRaster objects. xyz=TRUE is not an argument to this method, so that error occurs first if you use it:

    r <- rast(re, type="xyz")
    #Error in .local(x, ...) : unused argument (type = "xyz")
    

    What you are looking for is the rast-matrix method:

    m <- as.matrix(re)
    r <- rast(m, type="xyz")
    
    r
    #class       : SpatRaster 
    #dimensions  : 901, 1054, 1  (nrow, ncol, nlyr)
    #resolution  : 0.006903074, 0.006903074  (x, y)
    #extent      : -7.90892, -0.6330797, 53.91638, 60.13605  (xmin, xmax, ymin, ymax)
    #coord. ref. :  
    #source      : memory 
    #name        :   z 
    #min value   :   1 
    #max value   : 124 
    

    I have added a rast,data.frame method to terra such that your approach works in versions >= 1.3-7.