Pit Strehl
Pit Strehl

Reputation: 21

st_buffer multipoint with different distance

I have a sfc_multipoint object and want to use st_buffer but with different distances for every single point in the multipoint object. Is that possible?

The multipoint object are coordinates. table = data

Every coordinate point (in the table in "lon" and "lat") should have a buffer with a different size. This buffer size is containt in the table in row "dist". The table is called data.

This is my code:

library(sf)
coords <- matrix(c(data$lon,data$lat), ncol = 2)
tt     <- st_multipoint(coords)
sfc    <- st_sfc(tt, crs = 4326) 
dt     <- st_sf(data.frame(geom = sfc))
web    <- st_transform(dt, crs = 3857)
geom   <- st_geometry(web)
buf    <- st_buffer(geom, dist = data$dist)

But it uses just the first dist of (0.100). This is the result. Just really small buffers. small buffer

For visualization see this picture. It´s just an example to show that the buffer should get bigger. example result

Upvotes: 2

Views: 1067

Answers (1)

lbusett
lbusett

Reputation: 5932

I think that he problem here is in how you are "creating" the points dataset.

Replicating your code with dummy data, doing this:

library(sf)
data   <- data.frame(lat = c(0,1,2,3), lon = c(0,1,2,3), dist = c(0.1,0.2,0.3, 0.4))
coords <- matrix(c(data$lon,data$lat), ncol = 2)
tt     <- st_multipoint(coords)

does not give you multiple points, but a single MULTIPOINT feature:

tt
#> MULTIPOINT (0 0, 1 1, 2 2, 3 3)

Therefore, only a single buffer distance can be "passed" to it and you get:

plot(sf::st_buffer(tt, data$dist))

To solve the problem, you need probably to build the point dataset differently. For example, using:

tt <- st_as_sf(data, coords = c("lon", "lat"))

gives you:

tt
#> Simple feature collection with 4 features and 1 field
#> geometry type:  POINT
#> dimension:      XY
#> bbox:           xmin: 0 ymin: 0 xmax: 3 ymax: 3
#> epsg (SRID):    NA
#> proj4string:    NA
#>  dist    geometry
#> 1  0.1 POINT (0 0)
#> 2  0.2 POINT (1 1)
#> 3  0.3 POINT (2 2)
#> 4  0.4 POINT (3 3)

You see that tt is now a simple feature collection made of 4 points, on which buffering with multiple distances will indeed work:

plot(sf::st_buffer(tt, data$dist))

HTH!

Upvotes: 4

Related Questions