I have a raster stack with 100+ files. And I want to extract the values from each file for particular lat-long locations. This gives me the list of values for one Lat-Long combination.
plist <- list.files(pattern = "\\.tif$", include.dirs = TRUE)
pstack <- stack(plist)
#levelplot(pstack)
for (i in 1:length(plist))
t[i]=extract(pstack[[i]], 35,-90)
How can I do it for thousands of locations when I have the lat-long locations in a separate file/dataframe. There is a location ID that I want to preserve too in the final list:
Lat Long LocID
35 -90 001
35 -95 221
30 -95.4 226
31.5 - 90 776
My final objective is to have a dataframe of this type:
Lat Long LocID value
35 -90 001 0.5
35 -95 221 1.4
30 -95.4 226 2.5
31.5 - 90 776 4.5
Though if it is not possible to preserve the LocID, that's fine too.
One of the files: https://www.dropbox.com/s/ank4uxjbjk3chaz/new_conus.tif?dl=0
Testing a solution from comments:
latlong<-structure(list(lon = c(-71.506667, -71.506667, -71.506667, -71.215278,
-71.215278, -71.215278, -71.215278, -71.215278, -71.215278, -71.215278
), lat = c(42.8575, 42.8575, 42.8575, 42.568056, 42.568056, 42.568056,
42.568056, 42.568056, 42.568056, 42.568056)), .Names = c("lon",
"lat"), row.names = c(NA, 10L), class = "data.frame")
ext<-extract(pstack,latlong)
gives
Error in UseMethod("extract_") : no applicable method for 'extract_' applied to an object of class "c('RasterStack', 'Raster', 'RasterStackBrick', 'BasicRaster')"
Update #2:
The error was because it was conflicting with another package. This works:
raster::extract(pstack,latlong)
You can use extract function in raster library. First you read in your data frame and select the lon, lat columns. Let's say you have dataframe dat and the raster stack of pstack
loc <- dat[,c("long", "lat")]
ext <- extract(pstack, loc)
new_d <- cbind(dat, ext) # bind the extracted values back to the previous dataframe
I don't usually work with this type of data, but how about this:
library(sp)
library(raster)
library(rgdal)
# coordinate data
coords <- read.table(text = 'Lat Long LocID
35 -90 001
35 -95 221
30 -95.4 226
31.5 -90 776', header = T)
# list of all files
plist <- c('~/Downloads/new_conus.tif', '~/Downloads/new_conus copy.tif')
# image stack
data.images <- stack(plist)
# make a master data frame containing all necessary data
data.master <- data.frame(file = rep(plist, each = nrow(coords)), file.id = rep(1:length(plist), each = nrow(coords)), coords)
At this point, we have a master data frame that looks like this:
file file.id Lat Long LocID
1 ~/Downloads/new_conus.tif 1 35.0 -90.0 1
2 ~/Downloads/new_conus.tif 1 35.0 -95.0 221
3 ~/Downloads/new_conus.tif 1 30.0 -95.4 226
4 ~/Downloads/new_conus.tif 1 31.5 -90.0 776
5 ~/Downloads/new_conus copy.tif 2 35.0 -90.0 1
6 ~/Downloads/new_conus copy.tif 2 35.0 -95.0 221
7 ~/Downloads/new_conus copy.tif 2 30.0 -95.4 226
8 ~/Downloads/new_conus copy.tif 2 31.5 -90.0 776
Now we just extract the value corresponding to the data in each row of the data frame:
# extract values for each row in the master data frame
data.master$value <- NA
for (i in 1:nrow(data.master)) {
data.master$value[i] <- with(data.master, extract(data.images[[file.id[i]]], Lat[i], Long[i]))
}
file file.id Lat Long LocID value
1 ~/Downloads/new_conus.tif 1 35.0 -90.0 1 255
2 ~/Downloads/new_conus.tif 1 35.0 -95.0 221 255
3 ~/Downloads/new_conus.tif 1 30.0 -95.4 226 259
4 ~/Downloads/new_conus.tif 1 31.5 -90.0 776 249
5 ~/Downloads/new_conus copy.tif 2 35.0 -90.0 1 255
6 ~/Downloads/new_conus copy.tif 2 35.0 -95.0 221 255
7 ~/Downloads/new_conus copy.tif 2 30.0 -95.4 226 259
8 ~/Downloads/new_conus copy.tif 2 31.5 -90.0 776 249
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With