7.2 Overlays

One very useful tool is the over() function which provides a consistent spatial overlay. For instance, here, I create a set of random points and look which one are in bel2, bel_north and bel_south:

pts <- SpatialPoints(
  coords = cbind(
    runif(50, bel2@bbox[1, 1], bel2@bbox[1, 2]),
    runif(50, bel2@bbox[2, 1], bel2@bbox[2, 2])
  ),
  proj4string = bel2@proj4string
)
# Make a SpatialPolygons
bel2_geom <- SpatialPolygons(bel2@polygons, proj4string = bel2@proj4string)
# plots
plot(bel2_geom)
points(pts, col = is.na(over(pts, bel2_geom)) + 2, pch = 19)