从经度和纬度列表中查找近点 [英] Find close points from a list of longitudes and latitudes

查看:85
本文介绍了从经度和纬度列表中查找近点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,该数据框由"Point_name",经度"和纬度"三列组成

I have a dataframe consisting of three columns, "Point_name" "longitude" and "latitude"

Point_Name              Longitude   Latitude
University of Arkansas  36.067832   -94.173655
Lehigh University       40.601458   -75.360063
Harvard University      42.379393   -71.115897

是否可以使用R包来计算每个点之间的距离?目的是让R测量"点之间的距离,然后生成点半径为"X"(例如500米)以内的另一个点的数据框,或生成像这样的数据框.

Is there an R package I can use to calculate distances between each point? The aim is to get R to "measure" the distance between points and then produce either a dataframe of points that have another point within "X" (eg. 500 meters) distance radius or produce a dataframe like this one...

Point_Name              Longitude   Latitude    Nearest_Point       Distance_km
University of Arkansas  36.067832   -94.173655  Lehigh University   1750        
Lehigh University       40.601458   -75.360063  Harvard University  450
Harvard University      42.379393   -71.115897  Lehigh University   450

推荐答案

如何解决

df<-read.table(header=T, text="Point_Name|Longitude|Latitude
University of Arkansas|36.067832|-94.173655
Lehigh University|40.601458|-75.360063
Harvard University|42.379393|-71.115897", sep="|")

great_circle_distance <- function(lat1, long1, lat2, long2) {
  a <- sin(0.5 * (lat2*pi/180 - lat1*pi/180))
  b <- sin(0.5 * (long2*pi/180 - long1*pi/180))
  12742 * asin(sqrt(a * a + cos(lat1*pi/180) * cos(lat2*pi/180) * b * b))
}

cross<-expand.grid(1:nrow(df),1:nrow(df))

cross$dist<-apply(cross,1,function(x){great_circle_distance(df[x[1],3],
                                               df[x[1],2],
                                               df[x[2],3],
                                               df[x[2],2])
}
      )

cross$from<-df[cross$Var1,1]
cross$to<-df[cross$Var2,1]

require(plyr)
ddply(cross[cross$Var1!=cross$Var2,],.(from),summarise,min(dist),to[dist==min(dist)])

from       ..1                ..2
1     Harvard University  475.3078  Lehigh University
2      Lehigh University  475.3078 Harvard University
3 University of Arkansas 2090.8387  Lehigh University

这篇关于从经度和纬度列表中查找近点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆