我一直在使用Rstudio进行练习。我想做一个克里格插值,但我得到了一个我无法解决的错误。
如果你们中的一些人能告诉我如何修复它,我将不胜感激。
这是我的代码:
#importing libraries
library(raster)
library(sf)
library(sp)
library(rgdal)
library(gstat)
library(tmap)
library(tidyverse)
#reading csv data
id <- "1u18s9g15USnzNhj-t2EEFYHsW6GZEIgd" # google file ID
read.csv(sprintf("https://docs.google.com/uc?id=%s&export=download", id))
silver_data = read.csv(sprintf("https://docs.google.com/uc?id=%s&export=download", id))
#creating spatial dataframe
silver_2 <- st_as_sf(silver_data, coords = c("long", "lat"))
st_crs(silver_2) <- 4326
#coordinates transformation
silver_utm2 <- st_transform(silver_2, crs=9155)
#setting variogram
ve <- variogram(silver ~ 1, silver_utm2, cutoff = 5000,width = 200)
plot(ve, plot.numbers = T, asp=1)
# experimental variogram
vt <- vgm(psill = 350, model = "Sph", range = 2000,nugget = 0)
plot(ve, pl = T, model = vt)
#automatic adjustment
va <- fit.variogram(ve, vt)
plot(ve, pl = T, model = va)
#empty raster in order to create grid for kriging interpolation
z <- raster(as(silver_utm2, "Spatial"), ncols = 200, nrows = 80)
# using raster to create grid
grilla2 <- rasterToPoints(z, spatial = TRUE)
gridded(grilla2) <- TRUE
grilla2 <- as(grilla2, "SpatialPixels")
# using kriging function
ok <- krige(silver~1, locations=silver_utm2, newdata=grilla2, model=va)
出现的错误:
> ok <- krige(silver~1, locations=silver_utm2, newdata=grilla2, model=va)
Error in UseMethod("st_crs<-") :
no applicable method for 'st_crs<-' applied to an object of class "c('SpatialPixels', 'SpatialPoints', 'Spatial', 'SpatialVector')
我正在学习的教程是https://rstudio-pubs-static.s3.amazonaws.com/416462_1463d00750c54fce89955a925eaa4957.html,但数据不相同。
st_crs()
是包sf
中的一个函数,设计用于仅对sf
对象进行操作。按照您的代码,grilla2
是一个sp
对象。在使用krige()
之前将其转换为sf
。
grilla2 <- sf::st_as_sf(grilla2)