[R-sig-Geo] Interesection between spatial points and a raster

Mirza Cengic mirzaceng at gmail.com
Tue Mar 7 17:33:09 CET 2017


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.

Thanks,
Mirza.

Below is a reproducible example to get things going:

# ----
library(raster)
# 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
reality
x <- jitter(sample(df[, "x"], ncell(r)/20))
y <- jitter(sample(df[, "y"], ncell(r)/20))

sp <- as.data.frame(cbind(x, y))
coordinates(sp) <- ~x+y
proj4string(sp) <- crs(r, asText = TRUE)

#
df <- raster::as.data.frame(r, 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]]



More information about the R-sig-Geo mailing list