Examples for 'sf::aggregate.sf'


aggregate an 'sf' object

Aliases: aggregate.sf aggregate

Keywords:

### ** Examples

m1 = cbind(c(0, 0, 1, 0), c(0, 1, 1, 0))
m2 = cbind(c(0, 1, 1, 0), c(0, 0, 1, 0))
pol = st_sfc(st_polygon(list(m1)), st_polygon(list(m2)))
set.seed(1985)
d = data.frame(matrix(runif(15), ncol = 3))
p = st_as_sf(x = d, coords = 1:2)
plot(pol)
plot(p, add = TRUE)
plot of chunk example-sf-aggregate.sf-1
(p_ag1 = aggregate(p, pol, mean))
Simple feature collection with 2 features and 1 field
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 0 ymin: 0 xmax: 1 ymax: 1
CRS:           NA
         X3                       geometry
1 0.5951766 POLYGON ((0 0, 0 1, 1 1, 0 0))
2 0.2997908 POLYGON ((0 0, 1 0, 1 1, 0 0))
plot(p_ag1) # geometry same as pol
# works when x overlaps multiple objects in 'by':
p_buff = st_buffer(p, 0.2)
plot(p_buff, add = TRUE)
plot of chunk example-sf-aggregate.sf-1
(p_ag2 = aggregate(p_buff, pol, mean)) # increased mean of second
Simple feature collection with 2 features and 1 field
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 0 ymin: 0 xmax: 1 ymax: 1
CRS:           NA
         X3                       geometry
1 0.5951766 POLYGON ((0 0, 0 1, 1 1, 0 0))
2 0.5958297 POLYGON ((0 0, 1 0, 1 1, 0 0))
# with non-matching features
m3 = cbind(c(0, 0, -0.1, 0), c(0, 0.1, 0.1, 0))
pol = st_sfc(st_polygon(list(m3)), st_polygon(list(m1)), st_polygon(list(m2)))
(p_ag3 = aggregate(p, pol, mean))
Simple feature collection with 3 features and 1 field
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -0.1 ymin: 0 xmax: 1 ymax: 1
CRS:           NA
         X3                       geometry
1        NA POLYGON ((0 0, 0 0.1, -0.1 ...
2 0.5951766 POLYGON ((0 0, 0 1, 1 1, 0 0))
3 0.2997908 POLYGON ((0 0, 1 0, 1 1, 0 0))
plot(p_ag3)
plot of chunk example-sf-aggregate.sf-1
# In case we need to pass an argument to the join function:
(p_ag4 = aggregate(p, pol, mean,
     join = function(x, y) st_is_within_distance(x, y, dist = 0.3)))
Simple feature collection with 3 features and 1 field
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -0.1 ymin: 0 xmax: 1 ymax: 1
CRS:           NA
         X3                       geometry
1        NA POLYGON ((0 0, 0 0.1, -0.1 ...
2 0.5951766 POLYGON ((0 0, 0 1, 1 1, 0 0))
3 0.5999887 POLYGON ((0 0, 1 0, 1 1, 0 0))

[Package sf version 1.0-19 Index]