I've got several shapefiles stored as geopackages and I want to merge all of them by using rbind
. But unfortunately the geometry columns are namend differently as geometry
or geom
and because of that I cannot rbind
them. I can't see any reason why R is naming them differently.
They're all pointshapes from the same research area and with the same CRS and basically with same column names (besides the geometry field for some reasons).
I've tried simply renaming the geometry column resulting in the error attr(obj, "sf_column") does not point to a geometry column
as well as recreating the geometry column, but then it takes the same name every time.
But I found a workaround by renaming the columns and simply making the sf
to a data.frame
and assigning the geometry afterwars. But it seems to me a bit hacky, so maybe do you have a more elegant way or maybe you can explain me the reasons for the different names in the first place.
My code:
wd <- setwd("filepath")
files <- list.files(path=wd, pattern="*.gpkg", full.names=TRUE, recursive=FALSE)
points_all <- data.frame()
for (i in c(1:length(files))) {
file <- st_read(files[i])
names(file) <- c("Value", "Coord")
points_all <- rbind(points_all,file)
points_all <- data.frame(points_all)
}
points_all <- st_set_geometry(points_all, points_all$Coord)