将 grid2poly 与栅格一起使用

Using grid2poly with raster

我正在尝试从栅格中获取数据并将其转换为 KML 格式,以便获得一系列网格化的多边形。

查看网络上的样本,似乎要走的路是使用 grid2polyplotKML。不幸的是,我遇到了一个似乎可以解除阻止的错误。

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)

我收到来自 coordinates(tminvals) <- ~x+y 的所有行的错误,如下所示:

From Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘coordinates<-’ for signature ‘"matrix"’

我不明白我做错了什么。当我查看数据集 tminvals 时,它看起来与原始示例数据 eberg_grid.

的内容非常相似

为什么是多边形而不是栅格 KML?

library(raster)
tmin <- getData("worldclim", var = "tmin", res = 10) 
tmin1 <- tmin[[1]]
newext <- c(-1, 1, 40, 43.5)
tmin1.c <- crop(tmin1, newext)

KML(tmin.c, 'test.kml')

如果您需要多边形:

p <- rasterToPolygons(tmin.c)

现在您可以再次使用 KML(或 kml)了

现在解释您收到的错误消息。坐标函数需要一个 data.frame,你给它一个矩阵。如果您这样做,它可能会消失:

tminvals <- data.frame(tminvals)
coordinates(tminvals) <- ~x+y