SpatialPointsDataFrame
オブジェクトに500以上のポイントがあります。私は1.7GB(200,000行×200,000列)raster
オブジェクトを持っています。私は500 +ポイントのそれぞれの周りのバッファ内のラスターセルの値の集計をしたい。rのラスタ関数を効率的に使用
私は以下のコードでそれを達成できました(私は多くのインスピレーションを受けましたfrom here)。しかし、それは実行が遅く、私はそれをより速く走らせたいと思います。実際には、「小さな」幅のバッファ、例えば5kmの場合は15km(1百万個のセル)でもOKですが、バッファが100km(約4200万個のセル)に増加すると非常に遅くなります。
apply
ファミリからのものやパラレルループを使用すると、以下のループで簡単に改善できます。しかし、私の疑問は、raster
パッケージがループの各相互作用に対して400Mb +一時ファイルを書き込むため、遅いことです。
# packages
library(rgeos)
library(raster)
library(rgdal)
myPoints = readOGR(points_path, 'myLayer')
myRaster = raster(raster_path)
myFunction = function(polygon_obj, raster_obj) {
# this function return a tabulation of the values of raster cells
# inside a polygon (buffer)
# crop to extent of polygon
clip1 = crop(raster_obj, extent(polygon_obj))
# crops to polygon edge & converts to raster
clip2 = rasterize(polygon_obj, clip1, mask = TRUE)
# much faster than extract
ext = getValues(clip2)
# tabulates the values of the raster in the polygon
tab = table(ext)
return(tab)
}
# loop over the points
ids = unique(myPoints$ID)
for (id in ids) {
# select point
myPoint = myPoints[myPoints$ID == id, ]
# create buffer
myPolygon = gBuffer(spgeom = myPoint, byid = FALSE, width = myWidth)
# extract the data I want (projections, etc are fine)
tab = myFunction(myPolygon, myRaster)
# do stuff with tab ...
}
私の質問:
私は右の部分的に書き込み動作を非難するだろうか?私がこれらすべての書き込み操作を避けることができれば、このコードはより速く実行できますか? 32GBのRAMを搭載したマシンにアクセスすることができます。
raster
をメモリにロードして一時ファイルを書き込む必要がないと想定するのは安全でしょうか?このコードの効率を改善するために他に何ができますか?
私はあなたがこのlibrary(raster) library(rgdal) myPoints <- readOGR(points_path, 'myLayer') myRaster <- raster(raster_path) e <- extract(myRaster, myPoints, buffer=myWidth)
のようにそれに近づく。そして
etab <- sapply(e, table)
ような何かする必要があり、我々はあなたについて十分に知っていないとあなたの質問の1位に答えるのは難しいと思い
私のデータに関する何情報が#1に答えるのに十分ですか? – djas