簡體   English   中英

在一個數據集中查找與 R 中另一個數據集中的坐標緩沖區匹配的坐標

[英]Find Coordinates in One Dataset That Match A Buffer of Coordinates in Another Dataset in R

我有兩個數據集,一個較小的數據集(4000 行)包含有關不同商店的信息,每個商店的緯度/經度坐標和一個大型數據集(600K 行),其中包含有關居住在該地區的人的信息及其緯度/經度坐標。 我試圖找出有多少人住在離每家商店一定距離內。 我也想為多個距離執行此操作。 IE-對於每家商店,找出距離商店 200 米、500 米、1KM、2M 范圍內有多少人居住。

我如何使用 go 高效地使用 R?

簡要偽代碼如下

for(store in stores){
  for(distance in distances){
    store[distance] <- find_people_within_distance(store,distance)
  }
}

find_people_within_distance(store,distance){
  # Return number of People in People dataset who's geocoordinates fall within the distance range from stores
}

謝謝!

我認為,如果您對人與商店之間的實際距離不感興趣,而只想計算距每家商店在一定閾值距離內居住的人數,則可以采用以下方法。

# packages
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1

該示例位於米蘭,並且從該區域隨機采樣點。

milan <- osmdata::getbb("Milan, Italy", format_out = "sf_polygon") %>% 
  magrittr::extract2("polygon") %>% 
  st_transform(crs = 3003) %>% 
  st_geometry()

模擬商店和人員的數據

stores <- st_sample(milan, size = 4000)
system.time({
  people <- st_sample(milan, size = 600000)
})
#>    user  system elapsed 
#>  127.50    1.80  142.74

緩沖存儲點

system.time({
  stores_buffers <- st_buffer(stores, dist = units::set_units(200, "m"))
})
#>    user  system elapsed 
#>    0.68    0.01    0.75

檢查每個緩沖區中有多少人:

system.time({
  people_ids <- st_contains(stores_buffers, people)
})
#>    user  system elapsed 
#>   15.02    0.36   16.44
head(lengths(people_ids))
#> [1] 430 427 410 393 438 274

也許有更有效的方法來解決這個問題(例如,您可以檢查 R package polyclip )但是,目前估計有多少人住在這些緩沖區中不到 20 秒。

reprex package (v0.3.0) 於 2020 年 7 月 6 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM