Skip to main content
added 688 characters in body
Source Link
Elio Diaz
  • 3.5k
  • 11
  • 25

If I understand correctly, there are point and polygon shapefiles in your path (shp can't have two geometry types). You may query with st_geometry_type after having all layers bound together with bind_rows.

library(sf)
library(dplyr)
nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE)

nc_centr = st_centroid(nc) # just to have a point layer to bind

nc_mixed = rbind(nc, nc_centr) %>% mutate(g_type = st_geometry_type(.))

# check geometry types of the layer
unique(nc_mixed$g_type)
[1] MULTIPOLYGON POINT       

nc_filtered = nc_mixed %>% filter(g_type == "MULTIPOLYGON")

# check again
unique(nc_filtered$g_type)
[1] MULTIPOLYGON

then you may use st_buffer() for the only polygon layer. You may do the filtering in a single step with filter(st_geometry_type(.) == "MULTIPOLYGON") and overwrite unnecessary objects.

Edit

I think it's easier to separate both data frames and buffer the point layer, since the other way would involve an if else statement and assigning the geometry column; in this data set we have to first transform to a projected coordinate system with st_transform, you say yours is already projected.


nc_buffered_points = nc_mixed %>% st_transform(32617) %>%
  filter(g_type == "POINT") %>%
  st_buffer(5)

nc_polygons = nc_mixed %>% st_transform(32617) %>%
  filter(g_type != "POINT") 

nc_all = rbind(nc_buffered_points, nc_polygons)

# and we see points have become polygons:
unique(st_geometry_type(nc_all))
[1] POLYGON      MULTIPOLYGON


If I understand correctly, there are point and polygon shapefiles in your path (shp can't have two geometry types). You may query with st_geometry_type after having all layers bound together with bind_rows.

library(sf)
library(dplyr)
nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE)

nc_centr = st_centroid(nc) # just to have a point layer to bind

nc_mixed = rbind(nc, nc_centr) %>% mutate(g_type = st_geometry_type(.))

# check geometry types of the layer
unique(nc_mixed$g_type)
[1] MULTIPOLYGON POINT       

nc_filtered = nc_mixed %>% filter(g_type == "MULTIPOLYGON")

# check again
unique(nc_filtered$g_type)
[1] MULTIPOLYGON

then you may use st_buffer() for the only polygon layer. You may do the filtering in a single step with filter(st_geometry_type(.) == "MULTIPOLYGON") and overwrite unnecessary objects.

If I understand correctly, there are point and polygon shapefiles in your path (shp can't have two geometry types). You may query with st_geometry_type after having all layers bound together with bind_rows.

library(sf)
library(dplyr)
nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE)

nc_centr = st_centroid(nc) # just to have a point layer to bind

nc_mixed = rbind(nc, nc_centr) %>% mutate(g_type = st_geometry_type(.))

# check geometry types of the layer
unique(nc_mixed$g_type)
[1] MULTIPOLYGON POINT       

nc_filtered = nc_mixed %>% filter(g_type == "MULTIPOLYGON")

# check again
unique(nc_filtered$g_type)
[1] MULTIPOLYGON

then you may use st_buffer() for the only polygon layer. You may do the filtering in a single step with filter(st_geometry_type(.) == "MULTIPOLYGON") and overwrite unnecessary objects.

Edit

I think it's easier to separate both data frames and buffer the point layer, since the other way would involve an if else statement and assigning the geometry column; in this data set we have to first transform to a projected coordinate system with st_transform, you say yours is already projected.


nc_buffered_points = nc_mixed %>% st_transform(32617) %>%
  filter(g_type == "POINT") %>%
  st_buffer(5)

nc_polygons = nc_mixed %>% st_transform(32617) %>%
  filter(g_type != "POINT") 

nc_all = rbind(nc_buffered_points, nc_polygons)

# and we see points have become polygons:
unique(st_geometry_type(nc_all))
[1] POLYGON      MULTIPOLYGON


Source Link
Elio Diaz
  • 3.5k
  • 11
  • 25

If I understand correctly, there are point and polygon shapefiles in your path (shp can't have two geometry types). You may query with st_geometry_type after having all layers bound together with bind_rows.

library(sf)
library(dplyr)
nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE)

nc_centr = st_centroid(nc) # just to have a point layer to bind

nc_mixed = rbind(nc, nc_centr) %>% mutate(g_type = st_geometry_type(.))

# check geometry types of the layer
unique(nc_mixed$g_type)
[1] MULTIPOLYGON POINT       

nc_filtered = nc_mixed %>% filter(g_type == "MULTIPOLYGON")

# check again
unique(nc_filtered$g_type)
[1] MULTIPOLYGON

then you may use st_buffer() for the only polygon layer. You may do the filtering in a single step with filter(st_geometry_type(.) == "MULTIPOLYGON") and overwrite unnecessary objects.