Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have animal gps data with several locations for each day and regular gaps of several days when no location of the animal was recorded. Additionally, I have satellite data in intervals of 16 days. I would now like to extract the pixel value that corresponds to the specific point and to the specific time.

This means if the location of the animals was recorded e.g. 2 days before a satellite image was taken, I want to extract the pixel value of this image (that comes after) rather than from the images that was taken 14 days before the animal location was recorded. I always want to extract from the image that lies closer according to the time.

I created some test data that hopefully illustrates the problem:


### Create test data
# create first raster
edc2012001_m <- raster(ncol=36, nrow=18)
edc2012001_m[] <- sample(1:ncell(edc2012001_m))

# create second raster
edc2012017_m <- raster(ncol=36, nrow=18)
edc2012017_m[] <- sample(1:ncell(edc2012017_m))


# Create xy coordinates
time<-c("2012-01-01", "2012-01-01", "2012-01-01", "2012-01-02", "2012-01-02", "2012-01-02", "2012-01-12", "2012-01-12", "2012-01-13", "2012-01-13")
x <- rep(-50,10)
y <- sample(c(-80:80), 10)

# Convert data to spatial points data frame
coordinates(data) <- c("x","y")

### Extract all data from raster stack
extract(rasters, data)

The number string in the name of the first raster indicates that the image was taken on the first day in 2012, the second image was taken on the 17th day of this year.

The 7th location in the test data for example shall now be extracted from the second raster file as it lies closer according to the time.

All in all I have 87 raster files and 600 observations. I am really not sure how to program this. I guess I could use substr() to retrieve the date info from the raster names. But besides that... I am thankful for every hint I can get, also about functions that might be helpful in this context.

share|improve this question

1 Answer 1

Are your 87 raster files too big to make a raster_stack of all of them at once? You can supply a vector of filenames to stack() to try this.

Then its just a case of matching the layers in the raster stack to the time points of your cases. R has lots of date functionality, so you could parse the filenames and the time points into date objects, do a simple comparison, and that would get you the layer index.

If you can't load all 87 rasters I suggest you first order your data by time, then map time to raster file, then read in one raster at a time and do each of the points that are in that time slice.

Basically, you've got a lot of little problems that need solving...

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.