两个sf对象的空间合并

5

我正在尝试合并两个数据框(主要和子)。我希望从“子”中获取“变量”数据,并基于距离或最接近“主要”行/站点的任何“子”行/站点将其合并到“主要”中。

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)

我相信 st_intersection 是我想要的,但如果能基于距离进行一对一的匹配,那会更好。有人知道能提供我想要结果的方法吗?

2个回答

8

st_join()允许在一个步骤中进行连接:

st_join(main, sub, join = st_nearest_feature, left = T)


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

这段代码是由Reprex包(v0.3.0)在2020-01-19创建的


2
这个更简洁,所以我会选择这个作为答案。谢谢! - ecology

2
这是我尝试过的方法。似乎你需要使用st_nearest_feature()来获取最近要素的索引。一旦你获得了这些索引,就将它们添加到main中。你还需要将行号(即索引)添加到b中。然后,你想要处理连接。
library(dplyr)
library(sf)

# Which feature in y is closest to each feature in x?
# You get row index
st_nearest_feature(x = main, y = sub)

# Add the index number to main.
mutate(main, ind = st_nearest_feature(x = main, y = sub)) -> main

# Add row numbers (index) to b
mutate(b, ind = 1:n()) -> b

left_join(main, b, by = "ind")

#  `Site#`            geometry   ind Longitude Latitude Variable
#  <chr>           <POINT [°]> <int>     <dbl>    <dbl>    <dbl>
#1 Site1      (-94.609 38.922)     5     -95.9     39.9       99
#2 Site2   (-98.1391 37.48611)     4     -98.9     37.5      201
#3 Site3      (-99.033 37.811)     6     -99.0     37.8      700
#4 Site4       (-98.49 38.364)     1     -98.5     38.3      400
#5 Site5    (-96.4309 39.4402)     2     -96.2     40.0       50
#6 Site6       (-95.99 39.901)     5     -95.9     39.9       99

2
很棒的答案!完美地运作。 - ecology
1
@FISHnR 很高兴听到这个消息。希望你能在任务中继续取得进展。 :) - jazzurro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接