Cinghio
Cinghio

Reputation: 332

Unable to change raster projection in R

I have a raster object in R. I know is a raster because when I do

class(pres_kriged_rst)

I get

[1] "RasterLayer"
attr(,"package")
[1] "raster" 

Also, if I "look" at it I get:

class      : RasterLayer 
dimensions : 29, 24, 696  (nrow, ncol, ncell)
resolution : 260, 260  (x, y)
extent     : 678366.7, 684606.7, 1994.327, 9534.327  (xmin, xmax, ymin, ymax)
crs        : +proj=utm +zone=15 +datum=WGS84 +units=m +no_defs 
source     : memory
names      : layer 
values     : -0.04301532, 10.20677  (min, max)

Here is the problem. I have used this code before and it always worked, but now is not working anymore. What I would usually do to change the coordinates from UTM to Lon-Lat is the following:

raster::projectRaster(pres_kriged_rst, crs = 4326)

But now when I do it now I get the following error:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘xmax’ for signature ‘"data.frame"’

And I am not sure what the issue is. Does anyone know how to solve the issue?

I have a lot of packages loaded and below is the output of my working session.

R version 4.1.2 (2021-11-01)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] tcltk     grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] wesanderson_0.3.6     viridis_0.6.2         viridisLite_0.4.0     vegan_2.5-7           permute_0.9-5         units_0.7-2           tmaptools_3.1-1      
 [8] forcats_0.5.1         stringr_1.4.0         purrr_0.3.4           readr_2.1.1           tidyverse_1.3.1       tidyr_1.1.4           tibble_3.1.6         
[15] tmap_3.3-2            SIBER_2.1.6           spdep_1.2-1           spData_2.0.1          survival_3.2-13       spatialEco_1.3-7      spatstat_2.3-0       
[22] spatstat.linnet_2.3-1 spatstat.core_2.3-2   rpart_4.1-15          nlme_3.1-153          spatstat.geom_2.3-1   spatstat.data_2.1-2   sf_1.0-5             
[29] scales_1.1.1          rgl_0.108.3           rstudioapi_0.13       rayshader_0.24.10     rayrender_0.23.6      Rcapture_1.4-3        RMySQL_0.10.23       
[36] DBI_1.1.2             Rmisc_1.5             readxl_1.3.1          rasterVis_0.51.1      PBSmapping_2.73.0     plotROC_2.2.1         pander_0.6.4         
[43] plyr_1.8.6            mapview_2.10.0        moveVis_0.10.5        Metrics_0.1.4         moveHMM_1.7           move_4.1.6            rgdal_1.5-28         
[50] geosphere_1.5-14      lattice_0.20-45       lubridate_1.8.0       leaflet_2.0.4.1       hms_1.1.1             hydroTSM_0.6-0        xts_0.12.1           
[57] zoo_1.8-9             gstat_2.0-8           gganimate_1.0.7       gridExtra_2.3         GISTools_0.7-4        rgeos_0.5-9           RColorBrewer_1.1-2   
[64] maptools_1.1-2        ggplotify_0.1.0       ggnewscale_0.4.5      ggspatial_1.1.5       ggridges_0.5.3        ggrepel_0.9.1         ggmap_3.0.0          
[71] ggplot2_3.3.5         fBasics_3042.89.1     timeSeries_3062.100   timeDate_3043.102     dplyr_1.0.7           dismo_1.3-5           data.table_1.14.2    
[78] ctmm_0.6.1            CENFA_1.1.1           raster_3.5-11         cowplot_1.1.1         caTools_1.18.2        corrplot_0.92         colorspace_2.0-2     
[85] chron_2.3-56          binom_1.1-1           av_0.6.0              adehabitatHS_0.3.15   adehabitatHR_0.4.19   adehabitatLT_0.3.25   CircStats_0.2-6      
[92] boot_1.3-28           MASS_7.3-54           adehabitatMA_0.3.14   ade4_1.7-18           deldir_1.0-6          sp_1.4-6              alphahull_2.2        
[99] anytime_0.3.9        

loaded via a namespace (and not attached):
  [1] spacetime_1.2-5         R.methodsS3_1.8.1       intervals_0.15.2        knitr_1.37              R.utils_2.11.0          doParallel_1.0.16      
  [7] generics_0.1.1          snow_0.4-4              terra_1.4-22            proxy_0.4-26            slippymath_0.3.1        tzdb_0.2.0             
 [13] webshot_0.5.2           xml2_1.3.3              wk_0.6.0                assertthat_0.2.1        gifski_1.4.3-1          xfun_0.29              
 [19] evaluate_0.14           satellite_1.0.4         fansi_1.0.0             progress_1.2.2          dbplyr_2.1.1            htmlwidgets_1.5.4      
 [25] reshape_0.8.8           stats4_4.1.2            ellipsis_0.3.2          crosstalk_1.2.0         backports_1.4.1         vctrs_0.3.8            
 [31] abind_1.4-5             cachem_1.0.6            withr_2.4.3             prettyunits_1.1.1       goftest_1.2-3           cluster_2.1.2          
 [37] splancs_2.01-42         crayon_1.4.2            leaflet.providers_1.9.0 pkgconfig_2.0.3         tweenr_1.0.2            rlang_0.4.12           
 [43] spatial_7.3-14          lifecycle_1.0.1         sgeostat_1.0-27         doSNOW_1.0.19           modelr_0.1.8            dichromat_2.0-0        
 [49] cellranger_1.1.0        polyclip_1.10-0         Matrix_1.4-0            reprex_2.0.1            base64enc_0.1-3         png_0.1-7              
 [55] rjson_0.2.21            bitops_1.0-7            R.oo_1.24.0             KernSmooth_2.23-20      classInt_0.4-3          s2_1.0.7               
 [61] jpeg_0.1-9              gridGraphics_0.5-1      tripack_1.3-9.1         memoise_2.0.1           magrittr_2.0.1          hexbin_1.28.2          
 [67] leafsync_0.1.0          compiler_4.1.2          cli_3.1.0               pbapply_1.5-0           mgcv_1.8-38             tidyselect_1.1.1       
 [73] stringi_1.7.6           yaml_2.2.1              latticeExtra_0.6-29     tools_4.1.2             parallel_4.1.2          RgoogleMaps_1.4.5.3    
 [79] foreach_1.5.1           foreign_0.8-81          farver_2.1.0            stars_0.5-5             digest_0.6.29           FNN_1.1.3              
 [85] Rcpp_1.0.7              broom_0.7.11            automap_1.0-14          lwgeom_0.2-8            httr_1.4.2              rvest_1.0.2            
 [91] fs_1.5.2                XML_3.99-0.8            tensor_1.5              splines_4.1.2           yulab.utils_0.0.4       expm_0.999-6           
 [97] spatstat.utils_2.3-0    jsonlite_1.7.2          leafem_0.1.6            R6_2.5.1                pillar_1.6.4            htmltools_0.5.2        
[103] glue_1.6.0              fastmap_1.1.0           class_7.3-19            codetools_0.2-18        utf8_1.2.2              spatstat.sparse_2.1-0  
[109] numDeriv_2016.8-1.1     curl_4.3.2              magick_2.7.3            rmarkdown_2.11          munsell_0.5.0           e1071_1.7-9            
[115] iterators_1.0.13        haven_2.4.3             gtable_0.3.0

I did find a workaround, but it is not a solution. I used the same function and another raster that is in the appropriate projection system to transform my raster of interest!!

After incurring in the error I run traceback() and here is the result:

5: stop(gettextf("unable to find an inherited method for function %s for signature %s", 
       sQuote(fdef@generic), sQuote(cnames)), domain = NA)
4: (function (classes, fdef, mtable) 
   {
       methods <- .findInheritedMethods(classes, fdef, mtable)
       if (length(methods) == 1L) 
           return(methods[[1L]])
       else if (length(methods) == 0L) {
           cnames <- paste0("\"", vapply(classes, as.character, 
               ""), "\"", collapse = ", ")
           stop(gettextf("unable to find an inherited method for function %s for signature %s", 
               sQuote(fdef@generic), sQuote(cnames)), domain = NA)
       }
       else stop("Internal error in finding inherited methods; didn't return a unique method", 
           domain = NA)
   })(list("data.frame"), new("standardGeneric", .Data = function (x) 
   standardGeneric("xmax"), generic = structure("xmax", package = "terra"), 
       package = "terra", group = list(), valueClass = character(0), 
       signature = "x", default = NULL, skeleton = (function (x) 
       stop("invalid call in method dispatch to 'xmax' (no default method)", 
           domain = NA))(x)), <environment>)
3: xmax(outex)
2: .computeRes(from, projto, use_proj6)
1: projectRaster(pres_kriged_rst, crs = 4326)

Not sure what to get out of this.

Upvotes: 1

Views: 1932

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47071

4326 is just a number, not a coordinate reference system description -- even though it may work like that in some cases, it is better not to rely on it. With raster you can do

library(raster)
r <- raster(nrow=29, ncol=24, xmn=678366.7, xmx=684606.7, ymn=1994.327, ymx=9534.327, crs="+proj=utm +zone=15 +datum=WGS84 +units=m")
p1 <- projectRaster(r, crs="+init=epsg:4326")
# or
p2 <- projectRaster(r, crs=CRS(SRS_string = "EPSG:4326"))

But for me, the below does work:

p3 <- projectRaster(r, crs=4326)
p3
#class      : RasterLayer 
#dimensions : 33, 28, 924  (nrow, ncol, ncell)
#resolution : 0.00234, 0.00235  (x, y)
#extent     : -91.40195, -91.33643, 0.01337607, 0.09092607  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +no_defs 

And with terra you can use "+init=epsg:4326" or the shortcut "epsg:4326"

library(terra)
r <- rast(nrow=29, ncol=24, xmin=678366.7, xmax=684606.7, ymin=1994.327, ymax=9534.327, crs="+proj=utm +zone=15 +datum=WGS84 +units=m")
p <- project(r, "epsg:4326")

But you can also do this:

p <- project(r, "+proj=longlat")

(which is much more legible, but not exactly the same definition of longitude/latitude)

Upvotes: 1

Related Questions