代码之家  ›  专栏  ›  技术社区  ›  radek

在R&SF中,如何更新所选功能的坐标?

sf r
  •  3
  • radek  · 技术社区  · 6 年前

    我遇到了一个看似简单的问题。我想手动更正选定点的地理编码结果。假设需要更新“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))
    

    但这两种解决方案都会产生错误。

    1 回复  |  直到 6 年前
        1
  •  4
  •   Henrik plannapus    6 年前

    使用 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_几何<。- . 替换值需要是一个简单的特征几何图形,因此 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