简体   繁体   English

计算数据框中两个经纬度之间的距离

[英]Calculating Distance Between Two Lat's and Longs Within A Data Frame

I have a data set that includes the latitude and longitude for each individual property address.我有一个数据集,其中包括每个单独的属性地址的纬度和经度。 As well, I have created two new columns (icelat, icelog) that include the latitude and longitude for one specific building in the state of Utah.同样,我创建了两个新列(icelat、icelog),其中包括犹他州 state 中一栋特定建筑物的纬度和经度。

The data looks like this:数据如下所示:

                               RowID PropertyAddressLatitude PropertyAddressLongitude  icelat    icelog
1: 000D655E-1AEA-E811-80C3-3863BB430E3F                38.65195                -109.4085 40.2351 -111.6384
2: 000F655E-1AEA-E811-80C3-3863BB430E3F                38.50952                -109.4763 40.2351 -111.6384
3: 0012CB31-D004-E911-80C7-3863BB43E813                      NA                       NA 40.2351 -111.6384
4: 0013655E-1AEA-E811-80C3-3863BB430E3F                38.54184                -109.5031 40.2351 -111.6384
5: 0014655E-1AEA-E811-80C3-3863BB430E3F                      NA                       NA 40.2351 -111.6384
6: 0015655E-1AEA-E811-80C3-3863BB430E3F                      NA                       NA 40.2351 -111.6384

I would like to create a new column, called 'distance' that is the distance, in miles, from each property's latitude and longitude to the specific building in Utah.我想创建一个名为“距离”的新列,它是从每个属性的纬度和经度到犹他州特定建筑物的距离(以英里为单位)。

I have tried several different methods of using the Geosphere package but am unable to get it to run through all of the 'PropertyAddressLatitude' and 'PropertyAddressLongitude' observations and automatically do the math against 'icelat' and 'icelog.'我尝试了几种使用 Geosphere package 的不同方法,但无法让它运行所有的“PropertyAddressLatitude”和“PropertyAddressLongitude”观察结果并自动对“icelat”和“icelog”进行数学运算。

The default units will be in meters, so I'll convert in-place.默认单位将以米为单位,因此我将就地转换。

meter2mile <- 0.000621371
dat[, distance := meter2mile * geosphere::distVincentyEllipsoid(
        cbind(PropertyAddressLongitude, PropertyAddressLatitude),
        cbind(icelog, icelat)) ]
dat
#                                   RowID PropertyAddressLatitude PropertyAddressLongitude  icelat    icelog distance
# 1: 000D655E-1AEA-E811-80C3-3863BB430E3F                38.65195                -109.4085 40.2351 -111.6384 161.7148
# 2: 000F655E-1AEA-E811-80C3-3863BB430E3F                38.50952                -109.4763 40.2351 -111.6384 166.0397
# 3: 0012CB31-D004-E911-80C7-3863BB43E813                      NA                       NA 40.2351 -111.6384       NA
# 4: 0013655E-1AEA-E811-80C3-3863BB430E3F                38.54184                -109.5031 40.2351 -111.6384 163.4240
# 5: 0014655E-1AEA-E811-80C3-3863BB430E3F                      NA                       NA 40.2351 -111.6384       NA
# 6: 0015655E-1AEA-E811-80C3-3863BB430E3F                      NA                       NA 40.2351 -111.6384       NA

Data数据

dat <- as.data.table(structure(list(RowID = c("000D655E-1AEA-E811-80C3-3863BB430E3F", "000F655E-1AEA-E811-80C3-3863BB430E3F", "0012CB31-D004-E911-80C7-3863BB43E813", "0013655E-1AEA-E811-80C3-3863BB430E3F", "0014655E-1AEA-E811-80C3-3863BB430E3F", "0015655E-1AEA-E811-80C3-3863BB430E3F"), PropertyAddressLatitude = c(38.65195, 38.50952, NA, 38.54184, NA, NA), PropertyAddressLongitude = c(-109.4085, -109.4763, NA, -109.5031, NA, NA), icelat = c(40.2351, 40.2351, 40.2351, 40.2351, 40.2351, 40.2351), icelog = c(-111.6384, -111.6384, -111.6384, -111.6384, -111.6384, -111.6384)), row.names = c(NA, -6L), class = c("data.table", "data.frame")))

(I inferred data.table from your sample data, please advise if this was incorrect.) (我从您的示例数据中推断出data.table ,如果这不正确,请告知。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM