Learn R Programming

RSAGA (version 1.3.0)

rsaga.union.polygons: Spatial union of two polygon layers

Description

The function rsaga.union.polygons uses SAGA function "Union" to calculate the geometric union of two polygon layers. This corresponds to the intersection and the symmetrical difference of the two layers.

Usage

rsaga.union.polygons(layer_a = NULL, layer_b = NULL, result = NULL,
  split = FALSE, load = FALSE, env = rsaga.env())

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.

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 union layer_a.

result

character, path indicating where to store the output shapefile.

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.

Author

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

Details

Function gUnion() in rgeos package can also be used for joining intersecting polygon geometries. However, rsaga.union.polygons() will be usually much faster, especially when joining thousands of polygons.

Examples

Run this code
if (FALSE) {
library("RSAGA")
library("sp")
# 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)))
poly_1 <- SpatialPolygonsDataFrame(poly_1, data = data.frame(id = 1))
poly_2 <- SpatialPolygons(list(Polygons(list(Polygon(coords_2)), 1)))
poly_2 <- SpatialPolygonsDataFrame(poly_2, data = data.frame(id_2 = 2))
# union the two polygons using SAGA and load the output
dir_tmp <- paste0(tempdir(), "/out.shp")
res <- rsaga.union.polygons(layer_a = poly_1,
                            layer_b = poly_2,
                            result = dir_tmp,
                            load = TRUE)
# output attribute table consists of three elements, i.e. the union of poly_1
# and poly_2
dim(res)
res@data
}

Run the code above in your browser using DataLab