【问题标题】:Is there an efficient way to group nearby locations based on longitude and latitude?有没有一种有效的方法可以根据经度和纬度对附近的位置进行分组?
【发布时间】:2020-03-03 09:21:40
【问题描述】:

我正在尝试找出一种基于邻近度对多个地址进行聚类的方法。我有纬度和经度,在这种情况下是理想的,因为一些集群会跨越城市/邮编边界。我将作为起点的内容与此类似,但表中最多有 10,000 行:

Hospital.Addresses <- tibble(Hospital_Name = c("Massachusetts General Hospital","MGH - Blake Building","Shriners Hospitals for Children — Boston","Yale-New Haven Medical Center", "Memorial Sloan Kettering", "MSKCC Urgent Care Center", "Memorial Sloan Kettering Blood Donation Room"),
  Address = c("55 Fruit St", "100 Blossom St", "51 Blossom St", "York St", "1275 York Ave", "425 E 67th St", "1250 1st Avenue Between 67th and 68th Streets"),
  City = c("Boston", "Boston", "Boston", "New Haven", "New York", "New York", "New York"),
  State = c("MA", "MA", "MA", "CT", "NY", "NY","NY"),
  Zip = c("02114","02114","02114", "06504", "10065", "10065", "10065"),
  Latitude = c(42.363230, 42.364030, 42.363090, 41.304507, 40.764390, 40.764248, 40.764793),
  Longitude = c(-71.068680, -71.069430, -71.066630, -72.936781, -73.956810, -73.957127, -73.957818))

我想对彼此相距约 1 英里的地址组进行聚类,可能无需计算 10,000 个单独点之间的 Haversine 距离。我们可能会使数学变得简单,并粗略估计 1 英里为 0.016 度的纬度或经度。

理想的输出是验证波士顿的 3 家医院位于第 1 组(彼此相距 1 英里以内),纽黑文的医院独立于第 2 组(不在 1 英里范围内)否则),纽约的 3 家医院都在第 3 组中(都在 1 英里范围内)。

我更多的是在寻找 group_near(),而不是 group_by()。

非常感谢任何建议!

【问题讨论】:

  • 澄清一下:四舍五入的纬度和经度不能满足您的需求?

标签: r dplyr sp haversine


【解决方案1】:

实际上,geosphere 包中的 distm 函数可以在短短几分钟内处理 10,000 对,与编写此解决方案所需的时间相比,在我的机器上并不算太糟糕。 10,000 个随机点的 dist 矩阵消耗的内存不到一个 gig。

hclust进行聚类,使用geosphere包生成的距离矩阵,可以清楚地显示每个点的接近度。

#create fake data
lat<-runif(10000, min=28, max=42)
long<-runif(10000, min=-109, max=-71)
df<-data.frame(long, lat)

library(geosphere)

start<-Sys.time()
#create a distance matrix in miles
dmat<-distm(df)/1000*.62
print(Sys.time()-start)

#cluster
clusted<-hclust(as.dist(dmat))
#plot(clusted)
#find the clusters ids for 2 mile distances
clustersIDs<-(cutree(clusted, h=2))

【讨论】:

  • 这是一个非常聪明的解决方案,为我节省了数小时的工作时间。非常感谢!
猜你喜欢
  • 2011-11-07
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 2019-04-21
  • 2014-01-09
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
相关资源
最近更新 更多