如何加快从栅格中提取缓冲区中土地覆盖类型的比例? [英] How can I speed up extraction of the proportion of land cover types in a buffer from a raster?

查看:68
本文介绍了如何加快从栅格中提取缓冲区中土地覆盖类型的比例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在大约 30 000 个 SpatialLines 类对象的 10 公里缓冲区中提取空间数据,并计算缓冲线周围每种土地覆盖类型的比例.我第一次使用函数 crop 来裁剪我的栅格.然后,我使用函数extract(封装栅格)计算了10 种土地覆盖类型的比例.这是我的代码:

I would like to extract spatial data in a buffer of 10 km around 30 000 objects of class SpatialLines and calculate proportion of each land cover type around buffered lines. In a first time, I used the function crop to crop my raster. Then, I used the function extract (package raster) to calculate proportion of 10 land cover types. Here is my code:

lapply(1:nrow(tab_lines), FUN=function(k){

第一步:围绕线路建立10公里的缓冲区

buf_line <- gBuffer(seg_line[k], width=10000) ## seg_line =  Lines objects

第二步:从栅格中提取缓冲区中的土地覆盖类型

ha <-extract(x=data_raster,y=buf_line)

第三步:计算10种土地覆盖类型的比例

每种土地覆盖类型的比例必须在列中(一列=一种土地覆盖类型)

Third step: to calculate proportion of 10 land cover types

The proportion of each land cover type must be in columns (one column = one land cover type)

    ha_1 <-length(ha[[1]][ha[[1]]==1])/length(ha[[1]])
    ha_2 <-length(ha[[1]][ha[[1]]==2])/length(ha[[1]])
    ha_3 <-length(ha[[1]][ha[[1]]==3])/length(ha[[1]])
    ha_4 <-length(ha[[1]][ha[[1]]==4])/length(ha[[1]])
    ha_5 <-length(ha[[1]][ha[[1]]==5])/length(ha[[1]])
    ha_6 <-length(ha[[1]][ha[[1]]==6])/length(ha[[1]])
    ha_7 <-length(ha[[1]][ha[[1]]==7])/length(ha[[1]])
    ha_8 <-length(ha[[1]][ha[[1]]==8])/length(ha[[1]])
    ha_9 <-length(ha[[1]][ha[[1]]==9])/length(ha[[1]])
    ha_10 <-length(ha[[1]][ha[[1]]==10])/length(ha[[1]])

     return(cbind(ha_1, ha_2, ha_3, ha_4, ha_5, ha_6, ha_7, ha_8, ha_9, ha_10))  
    })

如何加快 30 000 条空间线的处理速度?R 中有没有其他包可以为这种类型的提取提供更快的处理?

How can I speed up the processing time for 30 000 spatial lines? Is there any other packages in R that can provide faster processing for this type of extraction ?

推荐答案

这里有一个更简洁的表述

Here is a more concise formulation

library(raster)
library(rgeos)

buf_line <- gBuffer(seg_line, width=10000, byid=TRUE)
ha <- extract(x=data_raster, y=buf_line)
h <- sapply(ha, function(x) tabulate(x, 10))
h <- h / colSums(h)

但我不认为这会快得多.您可以尝试 sp::over

But I do not think this will be much faster. Instead of extract you could try sp::over

根据您的计算机,首先运行可能会加快速度

Depending on your computer, things might speed up by first running

beginCluster()

这篇关于如何加快从栅格中提取缓冲区中土地覆盖类型的比例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆