从SpatialGridDataFrame和SpatialPolygonsDataFrame中的数据屏蔽栅格

时间:2015-12-18 02:41:15

标签: r mask raster

我试图通过仅包含一些具有多个位置的特定区域('Koeppen Geiger'气候区域)来掩盖光栅文件。我收到了运行最后一行代码的错误消息:

  

(函数(classes,fdef,mtable)中的错误:无法找到   用于签名的函数'mask'的继承方法   '" SpatialGridDataFrame"," SpatialPolygonsDataFrame"'

    ##Read Countries file
library(sp)
library(maptools)
library(rworldmap)
countries = readShapeSpatial("D:/Studies/PhD/SCI/modeling/ne_10m_admin_0_countries/ne_10m_admin_0_countries.shp") [enter link description here][1]
asia.zone = countries[countries$ADMIN=="South Korea"|
                        countries$ADMIN=="North Korea"|
                        countries$ADMIN=="Japan"|
                        countries$ADMIN=="China"|
                        countries$ADMIN=="Taiwan",]

##Read Koeppen Geiger’ climatic zones
tst <- read.csv('D:/Studies/PhD/SCI/modeling/Koeppen-Geiger-ASCII.csv',as.is=TRUE) [enter link description here][1]
tst.l <- tst [tst$Cls=="Cfc"|
                tst$Cls=="Cfa"|
                tst$Cls=="Cfb"|
                tst$Cls=="Cwa"|
                tst$Cls=="Cwb"|
                tst$Cls=="Aw"|
                tst$Cls=="As"|
                tst$Cls=="Am"|
                tst$Cls=="Dwd"|
                tst$Cls=="Dwb"|
                tst$Cls=="Dwa"|
                tst$Cls=="Dwc",]
#convert to sp SpatialPointsDataFrame
coordinates(tst.l) = c("Lon", "Lat")
# promote to SpatialPixelsDataFrame
gridded(tst.l) <- TRUE
# promote to SpatialGridDataFrame
tst.lsGDF = as(tst.l, "SpatialGridDataFrame")
# mask the specific climate zone from some locations
asia.zone2 <- mask(tst.lsGDF,asia.zone)

1 个答案:

答案 0 :(得分:1)

如果您查找?mask,您会看到它已针对Raster*个对象实现,而不是针对SpatialGridDataFrame个对象。因此,您需要将数据强制转换为Raster对象。这样的事情可能有用:

library(raster)
setwd("D:/Studies/PhD/SCI/modeling/")
countries <- shapefile("vne_10m_admin_0_countries/ne_10m_admin_0_countries.shp")
asia.zone <- countries[countries$ADMIN %in% c("South Korea", "North Korea","Japan", "China", "Taiwan"), ]

tst <- read.csv("Koeppen-Geiger-ASCII.csv", stringsAsFactor=FALSE) 
tst.l <- tst [tst$Cls %in% c("Cfc", "Cfa", "Cfb", "Cwa", "Cwb", "Aw", "As", "Am", "Dwd", "Dwb", "Dwa", "Dwc"),]

coordinates(tst.l) = c("Lon", "Lat")
# promote to SpatialPixelsDataFrame
gridded(tst.l) <- TRUE
r <- raster(tst.l)

asia.zone2 <- mask(r, asia.zone)