14

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)

2 Answers 2

17

I've just written this which I think will safely rename the geometry column by changing the names element and then setting the geometry column with st_geometry - which will affect the sf_column attribute.

rename_geometry <- function(g, name){
    current = attr(g, "sf_column")
    names(g)[names(g)==current] = name
    st_geometry(g)=name
    g
}

I don't see a function that will get the geometry column name, and the attribute is mentioned in the documentation so I think that is a safe way to get the name.

Use like this:

example(st_read)
nc = rename_geometry(nc, "gyom")
plot(nc) # should work
names(nc) # shows "gyom"

I don't know if an sf method for rbind could be written that works by ignoring different names for the geometry columns and uses maybe the first one for the output, but it might be worth a feature request to the sf github.

3
  • It worked perfectly, thank you very much! I'll ask them if an implementation is possible.
    – David
    Commented Feb 7, 2021 at 16:04
  • 1
    Wow! This should be requested as new feature in sf
    – Migue Lo
    Commented Jan 9, 2022 at 19:59
  • See also github.com/r-spatial/sf/issues/1890 for an alternative in sf >= 1.0-6 Commented Jan 21, 2022 at 21:07
8

A way of renaming the geometry variable has been implemented in sf as explained in this issue. Thus in the previous example you just use this command:

st_geometry(nc) <- "gyom"
1
  • Perfect solution! Commented Jun 21, 2022 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.