Interesection between spatial points and a raster

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Open this post in threaded view
Report Content as Inappropriate

Interesection between spatial points and a raster

Mirza Cengic
Hi all,

I'm in a need of a function (or a solution with the same results), which
would do something that might look like a spatial full outer join between
spatial points and a raster (in a similar way that .sp::over() kinda does
left outer join).

The raster::extract() function will extract raster values, and put it into
dataframe with the argument df = TRUE, but I would like to have a dataframe
which has the same number of rows as the number of cells of the raster from
which the values were extracted.
Ideally, I would like to have an output such as the variable df in the
example below, but have an extra column where I have TRUE/FALSE for example
if the sp falls in the cell.
I tried different things, but couldn't get a satisfactory result, so I
would appreciate some tips on this.


Below is a reproducible example to get things going:

# ----
# Create raster
r <- raster(nrow = 20, ncol = 20)
r[] <- rnorm(ncell(r))

# Create points
sp <- data.frame(nrow = ncell(r)/20, ncol = 2)
# jitter introduced since points shouldn't fall in the cell center in
x <- jitter(sample(df[, "x"], ncell(r)/20))
y <- jitter(sample(df[, "y"], ncell(r)/20))

sp <-, y))
coordinates(sp) <- ~x+y
proj4string(sp) <- crs(r, asText = TRUE)

df <-, xy = TRUE)
# Ideally, a column where observation is boolean of whether the point falls
in the cell or not
# df$is_sp <- solution()
# ----

        [[alternative HTML version deleted]]

R-sig-Geo mailing list
[hidden email]