两个 sf 对象的空间合并

FIS*_*HnR 5 r r-sf

我正在尝试合并两个数据框(主数据框和子数据框)。我希望根据距离或更好的方式将“子”中的“变量”数据合并到“主”,无论哪个“子”行/站点最接近“主”行/站点。

library(sf)

a <- structure(list(`Site#` = c("Site1", "Site2", "Site3", "Site4", "Site5", "Site6"), Longitude = c(-94.609, -98.1391, -99.033, -98.49, -96.4309, -95.99), `Latitude` = c(38.922, 37.486111, 37.811, 38.364, 39.4402, 39.901)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))

main <- st_as_sf(a, coords = c("Longitude", "Latitude"), crs = 4326)

b <- structure(list(Longitude = c(-98.49567, -96.22451, -98.49567, -98.941391, -95.91411, -99.031113), `Latitude` = c(38.31264,39.97692, 38.31264, 37.486111, 39.92143, 37.814171), Variable = c(400, 50, 100, 201, 99, 700)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))

sub <- st_as_sf(b, coords = c("Longitude", "Latitude"), crs = 4326)

c <- st_intersection(main,sub)
c <- st_is_within_distance(main,sub,dist=0.001)
Run Code Online (Sandbox Code Playgroud)

我相信 st_intersection 是我想要的,但如果我可以根据距离进行一对一的操作,那就可以了。有谁知道什么可以提供我正在寻找的结果?

mrh*_*ann 9

st_join()允许一步加入:

\n\n
st_join(main, sub, join = st_nearest_feature, left = T)\n\n\n#> although coordinates are longitude/latitude, st_nearest_feature assumes that they are planar\n#> Simple feature collection with 6 features and 2 fields\n#> geometry type:  POINT\n#> dimension:      XY\n#> bbox:           xmin: -99.033 ymin: 37.48611 xmax: -94.609 ymax: 39.901\n#> epsg (SRID):    4326\n#> proj4string:    +proj=longlat +datum=WGS84 +no_defs\n#> # A tibble: 6 x 3\n#>   `Site#`            geometry Variable\n#>   <chr>           <POINT [\xc2\xb0]>    <dbl>\n#> 1 Site1      (-94.609 38.922)       99\n#> 2 Site2   (-98.1391 37.48611)      201\n#> 3 Site3      (-99.033 37.811)      700\n#> 4 Site4       (-98.49 38.364)      400\n#> 5 Site5    (-96.4309 39.4402)       50\n#> 6 Site6       (-95.99 39.901)       99\n
Run Code Online (Sandbox Code Playgroud)\n\n

reprex 包(v0.3.0)于 2020-01-19 创建

\n

  • 这更简洁,所以我会给出答案。谢谢! (2认同)