简体   繁体   English

对 RasterLayer 使用 RasterPackage 的“提取”会导致 R 中的错​​误

[英]Using "extract" of RasterPackage for RasterLayer gives Error in R

I want to merge precipitation data with a .csv df.我想将降水数据与 .csv df 合并。 I figured out, that it should work with the command "extract" from the raster package.我发现它应该与光栅包中的“提取”命令一起使用。 That's my precipitation data:这是我的降水数据:

str(precipitation_raster_layer)
Formal class 'RasterLayer' [package "raster"] with 12 slots
  ..@ file    :Formal class '.RasterFile' [package "raster"] with 13 slots

Thats my climate-related Aid data:这就是我的气候相关援助数据:

str(AID)
'data.frame':   1050 obs. of  21 variables:
 $ project_location_id     : Factor w/ 1050 levels "P000501_2427123",..: 189 190 191 192 193 194 188 195 196 187 ...
 $ precision_code          : int  3 3 3 3 3 3 3 3 3 2 ...
 $ latitude                : num  6.45 6.74 6.47 5.66 6.6 ...
 $ longitude               : num  -1.583 -3.044 -2.333 -0.39 0.467 ...

Using this command:使用此命令:

test <- extract(precipitation_raster_layer, AID[,3:4])

Error in UseMethod("extract_") : no applicable method for 'extract_' applied to an object of class "c('RasterLayer', 'Raster', 'BasicRaster')" UseMethod(“extract_”)中的错误:没有适用于“extract_”的方法应用于“c('RasterLayer','Raster','BasicRaster')”类的对象

If I transform the .csv to a SpatialPointsDataFrame and try to run "extract", I get this error:如果我将 .csv 转换为 SpatialPointsDataFrame 并尝试运行“提取”,我会收到此错误:

test <- extract(precipitation_raster_layer, AID_spatial_df)

Error in UseMethod("extract_") : no applicable method for 'extract_' applied to an object of class "c('RasterLayer', 'Raster', 'BasicRaster')" UseMethod(“extract_”)中的错误:没有适用于“extract_”的方法应用于“c('RasterLayer','Raster','BasicRaster')”类的对象

I really do not understand why it says that my object is not a RasterLayer.我真的不明白为什么它说我的对象不是 RasterLayer。

Any help appreciated.任何帮助表示赞赏。

My guess is that you have, after loading raster , loaded another package that also has an extract method that hides the method from raster . 我的猜测是,在加载raster之后,您已经加载了另一个包,该包还具有一个extract方法,该方法对raster隐藏了该方法。

Load only the packages you need, and try calling the extract method from raster explicitly: 仅加载所需的包,然后尝试从raster显式调用extract方法:

raster::extract(precipitation_raster_layer, AID[, 4:3])

Note that it should be AID[, 4:3] , not AID[, 3:4] , as the correct order is longitude, latitude. 注意它应该是 AID[, 4:3] 而不是 AID[, 3:4] ,因为正确的顺序是经度,纬度。 But that is not the cause of the error you are getting. 但这不是导致错误的原因。

您可能已经加载了 tidyverse,卸载它并重试

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

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