0

I have a tif file from WORLDCLIM and I need to extract values related to temperature.

Sample code:

t_min_jan2 <-raster::brick("wc2.0_30s_tmin_01.tif")
t_min_fev <-raster::brick("wc2.0_30s_tmin_02.tif")
t_min_mar <-raster::brick("wc2.0_30s_tmin_03.tif")
t_min_abr <- raster::brick("wc2.0_30s_tmin_04.tif")
t_min_maio <- raster::brick("wc2.0_30s_tmin_05.tif")
t_min_jun <- raster::brick("wc2.0_30s_tmin_06.tif")
t_min_jul <-raster::brick("wc2.0_30s_tmin_07.tif")
t_min_ago <-raster::brick("wc2.0_30s_tmin_08.tif")
t_min_set <-raster::brick("wc2.0_30s_tmin_09.tif")
t_min_out <- raster::brick("wc2.0_30s_tmin_10.tif")
t_min_nov <-raster::brick("wc2.0_30s_tmin_11.tif")
t_min_dez <-raster::brick("wc2.0_30s_tmin_12.tif")
t <-stack(t_min_jan2,t_min_fev,t_min_mar,t_min_abr,t_min_maio,t_min_jun,t_min_jul,t_min_ago,t_min_set,t_min_out,t_min_nov,t_min_dez)`

plot(t)

newt <- c(-10, 5, 35, 45)
tmin1 <- crop(t, newt)
plot(tmin1)

With this code I get the map I want...I have a file with coordinates (local) and I need to extract temperature values from these coordinates

xy<-local[,c("Longitude" ,"Latitude")]
spdf <- SpatialPointsDataFrame(coords = xy, data = local,
proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84+towgs84=0,0,0"))
value<-extract(tmin1,spdf)
value

But when I run the code I get NA instead of getting the average temperatures. Maybe I'm not writing the code correctly. Can you spot any mistakes?

user213544
  • 2,046
  • 3
  • 22
  • 52

1 Answers1

0

A simpler way to put the data together:

library(raster)
# get all filenames
ff <- paste0(sprintf("wc2.0_30s_tmin_%02d", 1:12), ".tif")
wtmin <- stack(ff)
tmin <- crop(wtmin, c(-10, 5, 35, 45))

Start with checking if the points are on the raster (they probably are not)

xy <- local[,c("Longitude" ,"Latitude")]
plot(tmin[[1]])
points(xy)

If they are on top, this should work

value <- extract(tmin, xy)

If they are not, and you can't figure out why, show us what is returned by

tmin
extent(xy)
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • >tmin1 class : RasterBrick dimensions : 1200, 1800, 2160000, 12 (nrow, ncol, ncell, nlayers) resolution : 0.008333333, 0.008333333 (x, y) extent : -10, 5, 35, 45 (xmin, xmax, ymin, ymax) >extent(xy) c("x", "y") %in% names(x) are not all TRUE ....I think I got it now. Thank you! –  Feb 11 '19 at 09:57