Onil90
Onil90

Reputation: 181

Access cells of a raster layer in R

I am totally new to spatial analysis and I have a question regarding raster layers in R. I'm working with the following libraries:

library(sp)
library(raster)

I have a raster layer which represent a climatic variable on a certain geographical region and I have some vectors which represent points in this region.

How can I check whether two points are in the same cell of the raster layer? I know how to extract the climatic value at each point, but I have no clue how get the cell containing a certain point.

Any help will be highly appreciated!

EDIT: The points were given in coordinates (namely each point consist of a vector with the coordinates of the point). All the coordinates were stored in a CSV file, which I have converted into a SpatialPoint class using the function SpatialPoint in library(sp).

Upvotes: 0

Views: 139

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47061

Here is a minimal, self-contained, reproducible example:

library(raster)
r <- raster()
xy <- cbind(1:5, 1:5)

To get the cellnumbers of xy for r:

cellFromXY(r, xy)
[1] 32222 31863 31504 31145 30786

Upvotes: 1

Related Questions