如何使用 r 中的缓冲区过滤点

How to filter point using a buffer in r

假设有4个监控站点(monitor_site)和10个家庭地址(family_address)。

我想用monitor_site画一个圆buffer

我想把圈选出来的family_address重新返回(只要有圈圈在family_address周围就可以了)

另外,我想return一张地图,这样我就可以好好看看family_addressmonitor_site的交集了。

示例数据

monitor_site = data.frame(lat = c(39.93912273, 39.93109353, 39.91091467, 39.92758728),
                          lon = c(116.3492345, 116.3567815, 116.3596614, 116.4166152),
                          site_name = c('A', 'B', 'C', 'D'))
    
    
    
    
family_address = data.frame(lat = c(39.849243, 39.982189, 39.982674, 39.932026,39.952347, 
                                    39.936577, 39.929142, 39.996777, 39.926285,39.848591),
                            lon = c(116.378365, 116.35762, 116.360039, 116.380867, 116.325386,
                                    116.353981, 116.203125, 116.315018, 116.25695, 116.278061),
                            family_name = c('leona', 'jamie', 'celeste', 'Cherry', 'Magee',
                                            'sigrid', 'jessica', 'Julius', 'hulda', 'Bob'))
  
    
    
buffer = 10 # unit: km
# I just wrote a random number, but it could have been any number

第一步是将数据框转换为 SpatialPointsDataFrame:https://www.rdocumentation.org/packages/sp/versions/1.4-6/topics/SpatialPoints

不要忘记为您选择的 CRS 添加属性:https://www.nceas.ucsb.edu/sites/default/files/2020-04/OverviewCoordinateReferenceSystems.pdf

然后就可以使用buffer函数了:https://www.rdocumentation.org/packages/raster/versions/3.4-10/topics/buffer 注意默认单位是米:

Unit is meter if x has a longitude/latitude CRS

最后,您可以使用 maps 包表示您的结果:https://www.rdocumentation.org/packages/maps/versions/3.4.0

或者 mapview 包也很有可能提供帮助:https://r-spatial.github.io/mapview/#:~:text=mapview%20provides%20functions%20to%20very,the%20geometries%20and%20their%20attributes

您的数据

monitor_site = data.frame(lat = c(39.93912273, 39.93109353, 39.91091467, 39.92758728),
                          lon = c(116.3492345, 116.3567815, 116.3596614, 116.4166152),
                          site_name = c('A', 'B', 'C', 'D'))   
    
family_address = data.frame(lat = c(39.849243, 39.982189, 39.982674, 39.932026,39.952347, 
                                    39.936577, 39.929142, 39.996777, 39.926285,39.848591),
                            lon = c(116.378365, 116.35762, 116.360039, 116.380867, 116.325386,
                                    116.353981, 116.203125, 116.315018, 116.25695, 116.278061),
                            family_name = c('leona', 'jamie', 'celeste', 'Cherry', 'Magee',
                                            'sigrid', 'jessica', 'Julius', 'hulda', 'Bob'))
  

buffer = 5000   
   

我制作了两个 SpatVector 对象并展示了两种替代方法:

library(terra)
m <- vect(monitor_site, c("lon", "lat"), crs="+proj=longlat")
a <- vect(family_address, c("lon", "lat"), crs="+proj=longlat")

方法一

d <- distance(m, a)
colnames(d) = a$family_name
rownames(d) = m$site_name

d < buffer
#  leona jamie celeste Cherry Magee sigrid jessica Julius hulda   Bob
#A FALSE  TRUE    TRUE   TRUE  TRUE   TRUE   FALSE  FALSE FALSE FALSE
#B FALSE FALSE   FALSE   TRUE  TRUE   TRUE   FALSE  FALSE FALSE FALSE
#C FALSE FALSE   FALSE   TRUE FALSE   TRUE   FALSE  FALSE FALSE FALSE
#D FALSE FALSE   FALSE   TRUE FALSE  FALSE   FALSE  FALSE FALSE FALSE
 

方法二

b <- buffer(m, buffer)
plot(a, xlim=c(116.1,116.5))
lines(b)

x <- intersect(a, b)
values(x)
#   family_name site_name
#1        jamie         A
#2      celeste         A
#3       Cherry         A
#4        Magee         A
#5       sigrid         A
#6       Cherry         B
#7        Magee         B
#8       sigrid         B
#9       Cherry         C
#10      sigrid         C
#11      Cherry         D