我想將柵格化一個非常大的矢量文件到25米,並且已經在「簇」包中取得了一些成功,適應了這個曲線的here和here,這對於特定數據非常適用。R中使用降雪和sfLapply柵格化多邊形
但是,我現在有一個更大的矢量文件,需要柵格化並可以訪問使用降雪的羣集。我不習慣集羣功能,我只是不知道如何設置sfLapply。我一直得到以下類型的錯誤作爲sfLapply被稱爲集羣:
Error in checkForRemoteErrors(val) :
one node produced an error: 'quote(96)' is not a function, character or symbol
Calls: sfLapply ... clusterApply -> staticClusterApply -> checkForRemoteErrors
我全碼:
library(snowfall)
library(rgeos)
library(maptools)
library(raster)
library(sp)
setwd("/home/dir/")
# Initialise the cluster...
hosts = as.character(read.table(Sys.getenv('PBS_NODEFILE'),header=FALSE)[,1]) # read the nodes to use
sfSetMaxCPUs(length(hosts)) # make sure the maximum allowed number of CPUs matches the number of hosts
sfInit(parallel=TRUE, type="SOCK", socketHosts=hosts, cpus=length(hosts), useRscript=TRUE) # initialise a socket cluster session with the named nodes
sfLibrary(snowfall)
# read in required data
shp <- readShapePoly("my_data.shp")
BNG <- "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs"
crs(shp) <- BNG
### rasterize the uniques to 25m and write (GB and clipped) ###
rw <- raster(res=c(25,25), xmn=0, xmx=600000, ymn=0, ymx=1000000, crs=BNG)
# Number of polygons features in SPDF
features <- 1:nrow(shp[,])
# Split features in n parts
n <- 96
parts <- split(features, cut(features, n))
rasFunction = function(X, shape, raster, nparts){
ras = rasterize(shape[nparts[[X]],], raster, 'CODE')
return(ras)
}
# Export everything in the workspace onto the cluster...
sfExportAll()
# Distribute calculation across the cluster nodes...
rDis = sfLapply(n, fun=rasFunction,X=n, shape=shp, raster=rw, nparts=parts) # equivalent of sapply
rMerge <- do.call(merge, rDis)
writeRaster(rMerge, filename="my_data_25m", format="GTiff", overwrite=TRUE)
# Stop the cluster...
sfStop()
我已經嘗試了一些事情,改變功能和sfLapply,但我無法得到這個運行。感謝
如果您正在尋找具有光柵化(大)矢量數據的速度,請查看'gdalUtils :: gdal_rasterize'。這通常比'raster :: rasterize'快得多。 – joberlin
好的,謝謝,我也會看看這個 – Sam
我放棄了rasFunction,並將rDis改爲「rDis = sfLapply(1:n,fun = function(x)rasterize(shp [parts [[x]],],rw ,'CODE'))「但是現在我在checkForRemoteErrors(val)中得到錯誤: 96個節點產生錯誤;第一個錯誤:'數據'必須是矢量類型,是'NULL'。難住了。 – Sam