Search code examples
rr-rasterrgdal

Why are there repeated raster data in r


my_raster.tiff file is the PM2.5 concentration in China in 2013.

I've uploaded the data (my_raster.tiff) to Github (https://github.com/lizhiwei1994/example_data/blob/main/my_raster.tiff) in order to reproduce the code.

I want to extract the concentration of PM2.5 at a particular latitude and longitude. 

I found the following code on the Internet.  It can batch extract PM2.5 concentration at specific latitude and longitude. For simplicity, I only processed one TIFF image (my_raster.tiff).

The code works fine.  But in the output CSV file, my result was repeated 12 times.  I think this code has something to do with it: result<-matrix(rep(0,12*n),ncol = n).

I think the original code downloaded from the Internet is set to 12 to deal with a TIFF image containing 12 layers of data. But in my data, my_raster.tiff contains only one layer of data (PM2.5 concentration in 2013).

So my question is if my my_raster.tiff contains a total of 12 years of data from 2013 to 2024, will there be no duplicate value in the result?

# I think there might be something wrong with the code
result<-matrix(rep(0,12*n),ncol = n) #I think this is where the problem lies

Below is the complete code downloaded from the web

install.packages(c("rgdal","raster"))

library(rgdal) 
library(raster)

name <- strsplit(getwd(),split="/")
number <- length(name[[1]]) 

csvname <- paste(as.character(name[[1]][number]),".csv",sep = '')

lst <- list.files(path=getwd(),pattern='tiff$',full.names = T) 

n <- length(lst) 
data<-data.frame(Lon = 116.3783, Lat = 39.86483) 

attach(data) 

coordinates(data)<-c("Lon","Lat") 

esult<-matrix(rep(0,12*n),ncol = n) #I think this is where the 
#problem lies

bio_number<-vector()

for(i in 1:length(lst)){
 
  filename <- strsplit(lst[[i]],split="/") 
  
  file<-filename[[1]][length(filename[[1]])]
  
  BIO <- raster(file) 
  
  BIO_centroid <- raster::extract(BIO, data, method='simple', buffer=1000, fun=mean, df=TRUE)   
  bio_number[i]<-strsplit(strsplit(lst[[i]],split="/")[[1]][number+1],split=".tif")[[1]]   
  result[,i]<-BIO_centroid[,2]
}

colnames(result)<-bio_number

Solution

  • It looks like you make things much more complicated than need be. Here is a general workflow using terra (the replacement for raster).

    #files <- list.files(path=getwd(),pattern='tiff$',full.names = T) 
    files <- c("my_raster.tiff", "my_raster.tiff", "my_raster.tiff")
    
    library(terra)
    r <- rast(files)
    pts <- data.frame(Lon = 116.3783, Lat = 39.86483) 
    e <- extract(r, pts)
    e
    #  ID my_raster my_raster my_raster
    #1  1  57.80168  57.80168  57.80168
    

    In this case the numbers are the same, because I use the same file three times, but if you use 12 different files you could get 12 different numbers on each row (one row for each point).