从 SpatialGridDataFrame 和 SpatialPolygonsDataFrame 中的数据屏蔽栅格
Masking raster from data in SpatialGridDataFrame and SpatialPolygonsDataFrame
我试图通过只包含一些特定区域(“Koeppen Geiger”气候带)和几个位置来掩盖光栅文件。我收到一条错误消息 运行 最后一行代码:
Error in (function (classes, fdef, mtable) : unable to find an
inherited method for function ‘mask’ for signature
‘"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)
如果您查找 ?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)
我试图通过只包含一些特定区域(“Koeppen Geiger”气候带)和几个位置来掩盖光栅文件。我收到一条错误消息 运行 最后一行代码:
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘mask’ for signature ‘"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)
如果您查找 ?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)