7.1 sp objects

7.1.1 Subset

A quick subset:

## Create a subset (A sub-shapefile)
slc <- 8:11
bel_slc <- bel2[slc, ]
class(bel_slc)
#R>  [1] "SpatialPolygonsDataFrame"
#R>  attr(,"package")
#R>  [1] "sp"

Let’s plot our selection:

plot(bel_slc)

7.1.2 Unions

Let’s do the union of a selection of spatial polygons:

bel_south <- gUnionCascaded(bel_slc)
bel_north <- gUnionCascaded(bel2[-slc, ])
bel_one <- gUnionCascaded(bel2)
par(mfrow = c(1, 3), mar = c(1, 1, 1, 1))
plot(bel_south)
plot(bel_north)
plot(bel_one)

Let’s combine them on one plot:

plot(bel2, lwd = .1)
plot(bel_south, add = TRUE, col = 2)
plot(bel_north, add = TRUE, col = 4)
plot(bel2, lwd = .5, add = TRUE)
plot(bel_one, lwd = 2, add = TRUE, border = "grey55")

7.1.3 Buffers

Buffer must be done on planar coordinates.

plot(bel2)
bufs <- gBuffer(bel_south, width = 0.4)
#R>  Warning: GEOS support is provided by the sf and terra packages among others
#R>  Warning in gBuffer(bel_south, width = 0.4): Spatial object is not projected;
#R>  GEOS expects planar coordinates
bufn <- gBuffer(bel_north, width = 0.1)
#R>  Warning: GEOS support is provided by the sf and terra packages among others
#R>  Warning in gBuffer(bel_north, width = 0.1): Spatial object is not projected;
#R>  GEOS expects planar coordinates
plot(bufs, add = TRUE, lwd = 2, lty = 2)
plot(bufn, add = TRUE, lwd = 3)

7.1.4 Intersections

Intersections between bel_south and bel_north:

par(mfrow = c(1, 2))
plot(bel_one)
plot(gIntersection(bel_south, bel_north), col = 5, add = TRUE, lwd = 2)
#R>  Warning: GEOS support is provided by the sf and terra packages among others
plot(bel_one)
plot(gIntersection(bufs, bufn), col = 5, add = TRUE, lwd = 2)
#R>  Warning: GEOS support is provided by the sf and terra packages among others

7.1.5 Differences

bel_diff <- gDifference(bufs, bufn)
#R>  Warning: GEOS support is provided by the sf and terra packages among others
plot(bel_one)
plot(bel_diff, add = TRUE, lwd = 2, lty = 3, col = 2)
plot(bel_south, add = TRUE, col = 2)