if (FALSE) {
library("RSAGA")
library("sp")
library("magrittr")
# construct coordinates of two squares
coords_1 <- matrix(data = c(0, 0, 1, 0, 1, 1, 0, 1, 0, 0),
ncol = 2, byrow = TRUE)
coords_2 <- matrix(data = c(-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5,
-0.5, -0.5),
ncol = 2, byrow = TRUE)
# convert the coordinates into polygons
poly_1 <- SpatialPolygons(list(Polygons(list(Polygon(coords_1)), 1))) %>%
as(., "SpatialPolygonsDataFrame")
poly_2 <- SpatialPolygons(list(Polygons(list(Polygon(coords_2)), 1))) %>%
as(., "SpatialPolygonsDataFrame")
# intersect the two polygons using SAGA and load the output
dir_tmp <- paste0(tempdir(), "/out.shp")
res <- rsaga.intersect.polygons(layer_a = poly_1,
layer_b = poly_2,
result = dir_tmp,
load = TRUE)
# plot input polygons
plot(poly_1, col = "red", axes = TRUE, xlim = c(-1, 1), ylim = c(-1, 1))
plot(poly_2, col = "blue", add = TRUE)
# plot the intersection
plot(res, col = "yellow", add = TRUE)
}
Run the code above in your browser using DataLab