I created a kriging map which was created by using the kriging() and image() functions of the kriging package (table is the data with the coords and values):
krig <- kriging(table@coords[ ,1], table@coords[ ,2], response = table@data$Joined.l_8, model = "spherical", lags = 3, pixels = 100)
krig_raster <- image(krig, main = NULL, xlab = "X coords", ylab = "", col = heat.colors(100))
The result can be viewed in the picture attached.
My problem is that I cannot export this map as a raster. When using the following command of the raster package:
writeRaster(krig_raster, "/home/stathis/Desktop/test.tif", format="Gtiff", overwrite = TRUE)
I get the following error:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘writeRaster’ for signature ‘"NULL", "character"’
When you do:
krig_raster <- image(krig, main = NULL, xlab = "X coords", ylab = "", col = heat.colors(100))
it plots the image and returns a NULL, so writeRaster
is trying to write a NULL and gives you an error saying exactly that. You need to convert the output from kriging
to a raster object first. Reading to docs for kriging
tells me the output predictions are in the $map
part of the returned object, and in a three-column format that should be okay to feed into raster::rasterFromXYZ
. If I use the example in ?kriging
and do this with the kriged
object:
> r = rasterFromXYZ(kriged$map)
> plot(r)
I see a plot of a raster class object of the USA which I can then save using writeRaster
via writeRaster(r, "usa.tif")
As a note I'd be wary of using a kriging function that doesn't return prediction variances or let you first examine the variogram before proceeding - check out the gstat
package for more thorough kriging procedures.