[GIS] How to extract raster information by using point shapefile in r

geotiff-tiffrraster

I would like to extract values of multiple Geotiff image by using multiple points shapefile and organize the results in a time series.

I have this:

library(raster); library(rgeos); library(rgdal);library(maptools)
shps <- dir(getwd(), "*.shp")
list.shape<-list()
for (shp in shps){ #Import individual point shape files
  list.shape[[shp]]<- readShapeSpatial(shp)
}
tifPs <- dir(getwd(), "*.tif")

P<-matrix(,nrow=length(tifPs), ncol=length(shps)) #To store each pixel value along the time.
list.tifP<-list(); r.vals <-list()
for (tif in tifPs){#Import G`enter code here`eotiff files.
  list.tifP[[tif]]<- rotate(raster(tif))
  r.vals[[tif]]<- extract(list.tifP[[tif]], list.shape) 
}

But when I run it, it shows me this error message: Error in round(y) : non-numeric argument to mathematical function

Any idea of how to do this?

EDIT:

Following the advises in the comment box, I attach my files. What I have to do is to extract the pixel information in each raster image (Geotiff files) by using the attached point shapefile and then put all of those time series in an array.

I tried juts to extract the pixel information but the code failed.

Best Answer

In this line:

r.vals[[tif]]<- extract(list.tifP[[tif]], list.shape) 

it looks like you are trying to extract the values from a list of spatial points objects (list.shape). I can duplicate this error using the example objects from help(extract):

> extract(r,sp)
[1] 626 554 482 410 338 266 194 122  50

> extract(r,list(sp,sp))
Error in round(y) : non-numeric argument to mathematical function

You need to loop over your spatial points in list.shape and extract each one individually. So you need a nested loop - an outer loop of rasters and an inner loop of spatial points. You can do this without needing a for loop with an lapply over a list of spatial points objects:

> lapply(list(sp,sp),function(s){extract(r,s)})
[[1]]
[1] 626 554 482 410 338 266 194 122  50

[[2]]
[1] 626 554 482 410 338 266 194 122  50

then its your job to do whatever you need with that output to get it into the matrix or whatever dataframe you want as your final output.