在 R & sf 中,如何更新所选特征的坐标?

In R & sf, how can I update coordinates of selected features?

我遇到了一个看似简单的问题。我想手动更正所选点的地理编码结果。假设 "Dare" 的质心需要更新:

library(sf)     
nc <- st_centroid(st_read(system.file("shape/nc.shp", package = "sf")))
st_coordinates(filter(nc, NAME== "Dare"))

如何更改

的原始值
          X        Y
1 -75.80982 35.73548

进入不同的领域?

我期待

st_coordinates(filter(nc, NAME== "Dare")) <- matrix(c(-73, 33), nrow = 1)

nc %>% 
  mutate(geometry = ifelse(place_full_name == "Dare", 
                           yes = st_set_geometry(c(-73, 33)), 
                           no = geometry))

完成这项工作,但两种解决方案都会产生错误。

使用st_geometry<-.

获取原始几何图形(只是为了检查):

st_geometry(nc[nc$NAME == "Dare", ])
# Geometry set for 1 feature 
# geometry type:  POINT
# dimension:      XY
# bbox:           xmin: -75.80982 ymin: 35.73548 xmax: -75.80982 ymax: 35.73548
# epsg (SRID):    4267
# proj4string:    +proj=longlat +datum=NAD27 +no_defs
# POINT (-75.80982 35.73548)

st_geometry<- 替换 选定的几何图形。替换值需要是简单的要素几何,因此 st_sfc(st_point(....

st_geometry(nc[nc$NAME == "Dare", ]) <-  st_sfc(st_point(c(-80, 40)))

# check again
st_geometry(nc[nc$NAME == "Dare", ])
# Geometry set for 1 feature 
# geometry type:  POINT
# dimension:      XY
# bbox:           xmin: -80 ymin: 40 xmax: -80 ymax: 40
# epsg (SRID):    4267
# proj4string:    +proj=longlat +datum=NAD27 +no_defs
# POINT (-80 40)

注:

Twitter discussion shared by @radek 中,sf 包的作者 @Edzer Pebesma 评论说原始几何体的边界框没有更新。

原始边界框:

st_bbox(nc)
#      xmin      ymin      xmax      ymax 
# -84.05976  34.07663 -75.80982  36.49101

用原始边界框外的坐标替换选定的几何体,这里 x 小于 xmin 并且 y 大于 ymax:

st_geometry(nc[nc$NAME == "Dare", ]) <-  st_sfc(st_point(c(-90, 40)))

对象的bbox未更新:

st_bbox(nc)
#      xmin      ymin      xmax      ymax 
# -84.05976  34.07663 -75.80982  36.49101