如何计算在其他坐标点的X半径内出现的坐标点个数?

How to Calculate the Number of Coordinate Points that Occur within X Radius of Other Coordinate Points?

我有两组坐标数据(纬度和经度)。第一组表示城市内餐馆的位置。第二组坐标代表城市中的公园。对于每个餐厅,我想计算 160 米以内的公园数量。

我尝试使用简单功能 (sf) 包执行此操作,根据另一个 post,但没有得到我想要的输出。

示例数据:

餐厅:

45.59841    -122.3275           
45.59932    -122.3343           
45.59932    -122.3343           
45.59820    -122.3343           
45.59820    -122.3343           
45.59814    -122.3340           
45.59814    -122.3340           
45.60016    -122.3341           
45.60016    -122.3341           
45.60020    -122.3343   

公园:

45.61221    -122.3426           
45.61065    -122.3402           
45.60868    -122.3384           
45.60624    -122.3361           
45.61082    -122.3388           
45.60983    -122.3379           
45.60910    -122.3372           
45.60829    -122.3365           
45.60653    -122.3349           
45.60503    -122.3335   

使用 sf,我尝试执行以下操作:

将坐标转换为地理空间数据

library(tidyverse)
library(sf)

park_points <- st_as_sf(ride_coords, coords = c(x = "Latitude", y = "Longitude"), crs = 4326)
rest_points <- st_as_sf(can_coords, coords = c(x = "lat", "lon"), crs = 4326)

然后我尝试在每个餐厅周围创建一个 160 米的缓冲区,之后我希望尝试计算每个餐厅缓冲区中的公园数量。

buffer_160 <- park_points %>% st_transform(26910) %>% st_buffer(dist = 160) %>% st_transform(2926)

然后我收到以下错误:

“UseMethod("st_transform") 错误:没有适用于 'st_transform' 的方法应用于 class 的对象“data.frame”

无论如何,目标是在每个餐厅周围创建缓冲区,然后计算每个缓冲区内的公园数量(我知道会有重叠,所以我也必须弄清楚如何处理)

我对 R 空间工具还很陌生。感谢您的帮助!

您可以使用 spatialrisk::points_in_circlemap 而不是 purrr 的餐厅。

没有找到半径为 160 米的公园,因此将半径扩展到 1000 米:

Restaurants <- read.table(text="
lat         lon                          
 45.59841    -122.3275           
 45.59932    -122.3343           
 45.59932    -122.3343           
 45.59820    -122.3343           
 45.59820    -122.3343           
 45.59814    -122.3340           
 45.59814    -122.3340           
 45.60016    -122.3341           
 45.60016    -122.3341           
 45.60020    -122.3343",header = T)

Parks <- read.table(text = "
lat     lon                  
45.61221    -122.3426           
45.61065    -122.3402           
45.60868    -122.3384           
45.60624    -122.3361           
45.61082    -122.3388           
45.60983    -122.3379           
45.60910    -122.3372           
45.60829    -122.3365           
45.60653    -122.3349           
45.60503    -122.3335",header = T)



library(purrr)
library(spatialrisk)

Restaurants %>% pmap(~with(list(...),points_in_circle(Parks, lon_center = lon, lat_center = lat,radius = 1000)))
#> [[1]]
#>         lat       lon distance_m
#> 10 45.60503 -122.3335   872.6082
#> 
#> [[2]]
#>         lat       lon distance_m
#> 10 45.60503 -122.3335   638.6807
#> 4  45.60624 -122.3361   782.9830
#> 9  45.60653 -122.3349   803.9727
#> 
#> [[3]]
#>         lat       lon distance_m
#> 10 45.60503 -122.3335   638.6807
#> 4  45.60624 -122.3361   782.9830
#> 9  45.60653 -122.3349   803.9727
#> 
#> [[4]]
#>         lat       lon distance_m
#> 10 45.60503 -122.3335   762.8609
#> 4  45.60624 -122.3361   905.9215
#> 9  45.60653 -122.3349   928.4681
#> 
#> [[5]]
#>         lat       lon distance_m
#> 10 45.60503 -122.3335   762.8609
#> 4  45.60624 -122.3361   905.9215
#> 9  45.60653 -122.3349   928.4681
#> 
#> [[6]]
#>         lat       lon distance_m
#> 10 45.60503 -122.3335   767.9792
#> 4  45.60624 -122.3361   916.4012
#> 9  45.60653 -122.3349   936.5971
#> 
#> [[7]]
#>         lat       lon distance_m
#> 10 45.60503 -122.3335   767.9792
#> 4  45.60624 -122.3361   916.4012
#> 9  45.60653 -122.3349   936.5971
#> 
#> [[8]]
#>         lat       lon distance_m
#> 10 45.60503 -122.3335   544.1362
#> 4  45.60624 -122.3361   694.5149
#> 9  45.60653 -122.3349   711.8371
#> 8  45.60829 -122.3365   924.1272
#> 
#> [[9]]
#>         lat       lon distance_m
#> 10 45.60503 -122.3335   544.1362
#> 4  45.60624 -122.3361   694.5149
#> 9  45.60653 -122.3349   711.8371
#> 8  45.60829 -122.3365   924.1272
#> 
#> [[10]]
#>         lat       lon distance_m
#> 10 45.60503 -122.3335   541.2711
#> 4  45.60624 -122.3361   686.8285
#> 9  45.60653 -122.3349   706.2001
#> 8  45.60829 -122.3365   916.7284
#> 3  45.60868 -122.3384   996.5307

或者总结一下,使用 concentration 直接给出每个餐厅周围的公园数量:

Parks$count <- 1
spatialrisk::concentration(Restaurants, Parks, value = count, radius = 1000)

        lat       lon amount concentration
1  45.59841 -122.3275      1             1
2  45.59932 -122.3343      1             3
3  45.59932 -122.3343      1             3
4  45.59820 -122.3343      1             3
5  45.59820 -122.3343      1             3
6  45.59814 -122.3340      1             3
7  45.59814 -122.3340      1             3
8  45.60016 -122.3341      1             4
9  45.60016 -122.3341      1             4
10 45.60020 -122.3343      1             5