简体   繁体   English

无法更改 R 中的光栅投影

[英]Unable to change raster projection in R

I have a raster object in R.我在 R 中有一个光栅 object。 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:我通常会将坐标从 UTM 更改为 Lon-Lat 的方法如下:

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.我加载了很多包,下面是我工作的 session 的 output。

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!!我使用相同的 function 和另一个位于适当投影系统中的栅格来转换我感兴趣的栅格!

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. 4326只是一个数字,而不是坐标参考系统描述——即使在某些情况下它可能会这样工作,但最好不要依赖它。 With raster you can do使用raster你可以做

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"使用terra您可以使用"+init=epsg:4326"或快捷方式"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) (这更清晰,但经度/纬度的定义不完全相同)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM