Skip to content

Commit 908804e

Browse files
authored
merge.sf rename a geometry column (e.g. geom) to 'geometry'
1 parent 9073a52 commit 908804e

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

R/sf.R

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -124,31 +124,28 @@ st_geometry.sfg = function(obj, ...) st_sfc(obj)
124124
`st_geometry<-` = function(x, value) UseMethod("st_geometry<-")
125125

126126
#' @export
127-
`st_geometry<-.data.frame` = function(x, value) {
128-
stopifnot(inherits(value, "sfc") || is.character(value))
129-
if (inherits(value, "sfc"))
130-
stopifnot(nrow(x) == length(value))
131-
if (is.character(value))
132-
st_sf(x, sf_column_name = value)
133-
else {
134-
a = vapply(x, function(v) inherits(v, "sfc"), TRUE)
135-
if (any(a)) {
136-
w = which(a)
137-
sf_col = attr(x, "sf_column")
138-
if (! is.null(sf_col))
139-
x[[ sf_col ]] = value
140-
else {
141-
if (length(w) > 1)
142-
warning("overwriting first sfc column")
143-
x[[ which(a)[1L] ]] = value
144-
}
145-
} else
146-
x$geometry = value
147-
st_sf(x)
148-
}
127+
`st_geometry<-.data.frame` <- function (x, value) {
128+
stopifnot(inherits(value, "sfc") || is.character(value))
129+
if (inherits(value, "sfc"))
130+
stopifnot(nrow(x) == length(value))
131+
if (is.character(value))
132+
st_sf(x, sf_column_name = value)
133+
else {
134+
a = vapply(x, function(v) inherits(v, "sfc"), TRUE)
135+
sf_col = attr(x, "sf_column")
136+
if (!is.null(sf_col))
137+
x[[sf_col]] = value
138+
else if (any(a)) {
139+
w = which(a)
140+
if (length(w) > 1)
141+
warning("overwriting first sfc column")
142+
x[[which(a)[1L]]] = value
143+
}
144+
else x$geometry = value
145+
st_sf(x)
146+
}
149147
}
150148

151-
152149
#' @export
153150
`st_geometry<-.sf` = function(x, value) {
154151
if (! is.null(value)) {
@@ -457,6 +454,7 @@ merge.sf = function(x, y, ...) {
457454
class(ret) = setdiff(class(ret), "sf")
458455
g = ret[[sf_column]] # may have NULL values in it
459456
ret[[sf_column]] = NULL
457+
attr(ret, "sf_column") <- sf_column
460458
st_set_geometry(ret, st_sfc(g)) # FIXME: set agr
461459
}
462460

0 commit comments

Comments
 (0)