I am trying to take data from a raster and push it to KML format so that I get a series of gridded polygons.
Looking at samples on the web it would appear that the way to go is to using grid2poly
with plotKML
. Unfortunately I have hit an error that I can seem to unblock.
library(dismo)
require(plotKML)
library(rgdal)
tmin <- getData("worldclim", var = "tmin", res = 10) # this will download
# global data on minimum temperature at 10' resolution
tmin1 <- raster(paste(getwd(), "/wc10/tmin1.bil", sep = "")) # Tmin for January
newext <- c(-1, 1, 40, 43.5)
tmin1.c <- crop(tmin1, newext)
plot(tmin1.c)
tmin1.c # look at the info
head(tmin1.c,5)
tminvals <- rasterToPoints(tmin1.c)
tminvals # look at the info
head(tminvals,5)
str(tminvals)
###########################
#Everything works down to here
###########################
library(sp)
coordinates(tminvals) <- ~x+y
gridded(tminvals) <- TRUE
proj4string(tminvals) <- CRS("+proj=longlat +datum=WGS84")
data(SAGA_pal)
dem_poly <- grid2poly(tminvals, "tmin1", method = "sp")
## visualize the data in Google Earth:
kml(dem_poly, colour_scale = SAGA_pal[[1]], colour = tmin1, kmz = TRUE)
I get an error for all the lines from coordinates(tminvals) <- ~x+y
which looks like this:
From Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘coordinates<-’ for signature ‘"matrix"’
I don't understand what I am doing wrong. When I look at the dataset tminvals
It looks much the same in content as the original example data eberg_grid
.