rsaga.intersect.polygons: Spatial intersection of two polygon layers

Description Usage Arguments Details Value Author(s) Examples

Description

The function rsaga.intersect.polygons calculates the geometric intersection of two overlayed polygon layers using SAGA module "Intersect".

Usage

1
2
rsaga.intersect.polygons(layer_a = NULL, layer_b = NULL,
  result = NULL, split = FALSE, load = FALSE, env = rsaga.env())

Arguments

layer_a

A character-string representing the path to a polygon shapefile or a spatial object of class SpatialPolygonsDataFrame.

layer_b

A character-string representing the path to a polygon shapefile or a spatial object of class SpatialPolygonsDataFrame with which to intersect layer_a.

result

A character-string indicating where the resulting shapefile should be stored.

split

If TRUE, multipart polygons become separated polygons (default: FALSE).

load

If TRUE, the resulting output shapefile will be loaded into R (default: FALSE).

env

RSAGA geoprocessing environment created by rsaga.env(), required because module(s) depend(s) on SAGA version.

Details

Function gIntersection in rgeos package can also be used to define the intersection between two polygon layers. However, rsaga.intersect.polygons() will be usually much faster, especially when intersecting thousands of polygons.

Value

The function saves the output shapefile to the path indicated in function argument result and loads the resulting shapefile into R when function parameter load is set to TRUE.

Author(s)

Jannes Muenchow (R interface), Olaf Conrad and Angus Johnson (SAGA modules)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
## Not run: 
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)

## End(Not run)

RSAGA documentation built on May 2, 2019, 11:01 a.m.