Skip to content

Instantly share code, notes, and snippets.

@am2222
Created May 4, 2020 20:00
Show Gist options
  • Save am2222/d5db15852ff2d3aa0a08f08c13ae5cba to your computer and use it in GitHub Desktop.
Save am2222/d5db15852ff2d3aa0a08f08c13ae5cba to your computer and use it in GitHub Desktop.
library(dplyr)
library(dggridR)
library(rgeos)
library(rgdal)
library(raster)
library(sf)
library(sp)
library(SpaDES)
library(parallel)
library(foreach)
library(doSNOW)
###############################################################
setwd('F:/DGG/Bio/Tsue_GIS')
############################saving raster###################################
rast=raster("Dem.tif")
path=paste('F:/DGG/Bio/Tsue_GIS')
ncpu=detectCores()
cl=makeCluster(ncpu-1)
registerDoSNOW(cl)
rlist=splitRaster(rast, nx=50, ny=50, 0,buffer = c(2, 2), path=path,cl=cl)
#rast = projectRaster(rast, crs = "+proj=longlat +ellps=GRS80 +units=m +datum=WGS84")
dggs= dgconstruct(res=22, metric=FALSE, resround='nearest',pole_lat_deg = 37,pole_lon_deg =-178)
clusterEvalQ(cl, library(dggridR))
clusterEvalQ(cl, library(raster))
foreach (i=1:2500) %dopar% {
#for (i in 1:100){
ind=which(!is.na(rlist[[i]][]))
if (length(ind)>0){
xy = xyFromCell(rlist[[i]], ind, spatial=FALSE)
z=rlist[[i]][ind]
df = data.frame(VALUE=z)
df$dggid = dgGEO_to_SEQNUM(dggs, xy[,1], xy[,2])$seqnum
data=aggregate.data.frame(df, by=list(df$dggid),mean)
data$Group.1=NULL
name=paste(path,'/RAST_22_',i,'.csv',sep="")
write.csv(data,name,sep=';', row.names=FALSE)
}
print (i)
}
stopCluster(cl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment