1

I would like to convert a SpatialPointsDataFrame into a fine raster (5m resolution), using R GIS.

My SpatialPointsDataFrame is made of 593 points, equally distributed along a 8km-grid. Their attributes are monthly amounts of precipitation ("MonthX", X ranging from 1 to 12).

Until now, I have used 'plotKML' package to convert the SpatialPointsDataFrame to a 8km-raster, which I could then disaggregate or resample (using the corresponding functions) to obtain the desired 5m-raster. But with either possibility, I got the same error message: "" Error in matrix(unlist(ini), ncol = 2, byrow = TRUE) : 'data' must be of vector type, was 'NULL' "

Maybe there's a mistake in my code, but I couldn't find it. Has anyone ever faced a similar problem? If yes, how would you solve it?

Here is my code:

library(rgdal)
data_points<-readOGR("data_points.shp") #importing data
library(plotKML)
raster_ref<-vect2rast.SpatialPoints(data_points, fname="Month1", 
cell.size=8000) 
#takes first attribute to create a raster framing each point with a 8km- 
#large cell
library(raster)
values_ref<-as.matrix(raster_ref)
values_ref[!is.na(values_ref)==TRUE] <- 1 #binary 0/1 raster
final_raster_ref<-raster(ext=extent(raster_ref), ncol=ncol(raster_ref), 
nrow=nrow(raster_ref), crs=raster_ref, vals=values_ref)

final_raster_ref<-raster(raster_ref)
plot(final_raster_ref,axes=T)  #projection : Lambert93
plot(data_points,add=T)

enter image description here

#Works fine until here

#Creating a finer, empty raster for function 'resample'
final_raster_ref_5m<-raster(ext=extent(final_raster_ref), 
ncol=ncol(final_raster_ref)*1600, nrow=nrow(final_raster_ref)*1600, 
crs=final_raster_ref)

#This function should now convert the 8km-raster to a 5m-raster
Precip_5m<-function(month_num, data, final, final_5m) {
  precip_8km<-rasterize(x = 
  data[,grep(paste("Month",month_num,sep=""),names(data))[1]],
                    y = final,
                    field = paste("Month",month_num,sep=""))
  precip_5m<-resample(precip_8km, final_5m)
  return(precip_5m)
}
#Applying the function to the points dataset
Month1_5m<-Precip_5m(1,data_points,final_raster_ref,final_raster_ref_5m)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.