简体   繁体   English

将大空间多边形数据帧转换为栅格堆栈时如何提高速度

[英]How to improve speed while converting Big spatial polygon data frame to raster stack

I'm working on spatial polygon data frame (spdf) dataset.我正在研究空间多边形数据框 (spdf) 数据集。 This dataset contains a time series of monthly climate data.该数据集包含月度气候数据的时间序列。 What I'm trying to achieve is to convert the spdf to raster stack with 1000m resolution for further statistical analysis.我想要实现的是将 spdf 转换为分辨率为 1000m 的栅格堆栈,以进行进一步的统计分析。 I've writen an R code but is is very slow and it took ages to convert one column.I would appreciate if any of you could suggest tips to make it faster.我写了一个 R 代码,但速度很慢,转换一列需要很长时间。如果你们中的任何人能提出建议以使其更快,我将不胜感激。

hru<-readOGR("E:\\Tade\\HRU\\ubn_merge.shp",layer="ubn_merge") # spatial polygon
spdf<-merge(hru,spdf.2000,by.x="HRU",by.y="HRU",all.x=T,sort=F) # spdf nrow=565 ncol=375
# convert sp to raster
hru.ras<-raster(hru,resolution=1000) # raster hru shape to 1km
for (i in 1:length(spdf){
  et.ras<-rasterize(spdf,hru.ras,field=paste("m",1,sep="")) # rasterize
  et.stack<-stack(et.stack,et.ras)
}

Thanks谢谢

As Forrest says (and you have experienced), rasterize is a bit slow, but you can do much better than what you have now.正如 Forrest 所说(并且您已经体验过), rasterize有点慢,但您可以做得比现在好得多。 You do not need to use a loop at all:您根本不需要使用循环:

r <- raster(spdf, resolution=1000)
et.ras <-rasterize(spdf, r, field=paste0("m",1:ncol(spdf)))

This will create a single RasterLayer with a Raster Attribute Table.这将创建一个带有栅格属性表的单个 RasterLayer。 To create a RasterStack, do:要创建 RasterStack,请执行以下操作:

s <- deratify(et.ras)

If you were to use a loop, use rasterize only once, the get the polygon IDs, and then use subs for the actual variables of interest.如果您要使用循环,请仅使用一次rasterize ,获取多边形 ID,然后对感兴趣的实际变量使用subs

And of course this saves you the pain of external dependencies.当然,这可以为您省去外部依赖的痛苦。

As of March 2020, you can now consider this new package.从 2020 年 3 月起,您现在可以考虑使用这个新包。

https://cran.r-project.org/web/packages/fasterize/vignettes/using-fasterize.html https://cran.r-project.org/web/packages/fasterize/vignettes/using-fasterize.html

fasterize()

Works exactly as完全一样

rasterize()

But 100-1000 times faster.但要快 100-1000 倍。 I think this can help a lots of people.我认为这可以帮助很多人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从多边形数据中提取栅格值然后加入空间数据框? - How do I extract raster values from polygon data then join into spatial data frame? 如何使用 terra 包将栅格堆栈中提取的值添加到 Spatial 对象的 data.frame 中? - How to add the extracted values from raster stack to the data.frame of the Spatial object using terra package? 提高计算大型栅格气候数据堆栈的速度 - Improve speed of calculating large raster stack of climate data 无需使用gpclib工具即可将空间多边形转换为常规数据框 - Converting spatial polygon to regular data frame without use of gpclib tools 将空间点数据框叠加到栅格后如何保存 - How to save after overlaying a spatial point data frame onto raster 替换空间多边形数据框中的多边形 - Replace a Polygon in a Spatial Polygon Data Frame 如何将类从数据框更改为空间多边形? - How to change class from data frame to spatial polygon? 如何从空间多边形数据集中为数据框指定坐标 - How to assign coordinates to a data frame from a Spatial polygon dataset 如何从空间点数据框中根据treeID创建多边形? - How to create polygon according to treeID from spatial points data frame? 将数据框转换为空间对象 - Converting a data frame to a spatial object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM