This vignetted describes how simple features, i.e. records that come with a geometry, can be manipulated, for the case where these manipulations involve geometries. Manipulations include:

  • aggregating feature sets
  • summarising feature sets
  • joining two feature sets based on feature geometry

Features are represented by records in an sf object, and have feature attributes (all non-geometry fields) and feature geometry. Since sf objects are a subclass of data.frame or tbl_df, operations on feature attributes work identical to how they work on data.frames, e.g.

prints the first record.

Many of the tidyverse/dplyr verbs have methods for sf objects. This means that given that both sf and dplyr are loaded, manipulations such as selecting a single attribute will return an sf object:

which implies that the geometry is sticky, and gets added automatically. If we want to drop geometry, we can coerce to data.frame first, this drops geometry list-columns:

Subsetting feature sets

We can subset feature sets by using the square bracket notation

and use the drop argument to drop geometries:

but we can also use a spatial object as the row selector, to select features that intersect with another spatial feature:

we see that in the result set Ashe is included, as the default value for argument op in [.sf is st_intersects, and Ashe intersects with itself. We could exclude self-intersection by using predicate st_touches (overlapping features don’t touch):

using dplyr, we can do the same by calling the predicate directly:

Joining two feature sets based on geometries

For joining based on spatial intersections (of any kind), st_join is used:

x = st_sf(a = 1:3, geom = st_sfc(st_point(c(1,1)), st_point(c(2,2)), st_point(c(3,3))))
y = st_buffer(x, 0.1)
x = x[1:2,]
y = y[2:3,]
plot(st_geometry(x), xlim = c(.5, 3.5))
plot(st_geometry(y), add = TRUE)

The join method is a left join, retaining all records of the first attribute:

and the geometry retained is that of the first argument.

The spatial join predicate can be controlled with any function compatible to st_intersects (the default), e.g.