我正在尝试为印度的特定密码获取相应的纬度和经度。
对于密码,我有以下文件。
https://data.gov.in/sites/default/files/all_india_PO_list_without_APS_offices_ver2_lat_long.csv
数据有15列。我只展示其中一部分,以便您可以看到此数据的样子。
officename pincode officeType Deliverystatus divisionname regionname
1: Achalapur B.O 504273 B.O Delivery Adilabad Hyderabad
2: Ada B.O 504293 B.O Delivery Adilabad Hyderabad
3: Adegaon B.O 504307 B.O Delivery Adilabad Hyderabad
4: Adilabad Collectorate S.O 504001 S.O Non-Delivery Adilabad Hyderabad
5: Adilabad H.O 504001 H.O Delivery Adilabad Hyderabad
此文件具有多个经纬度映射到一个密码。
对于我来说,我需要为特定的密码输入一个纬度(我有两个地址X和Y,然后使用Haversine计算X和Y之间的距离
对我来说可能的选择
我收到此错误的主要原因是我位于办公室防火墙的后面
Error in curl::curl_fetch_memory(url, handle = handle) :
Timeout was reached: [maps.googleapis.com] Connection timed out after 10000 milliseconds
感谢您的任何帮助!
这是我为您尝试的。您的数据在这里称为mydf
。首先,获取具有longitude
和latitude
值的行。对于statename
和pincode
定义的每个组,找到longitude
和latitude
的平均值。这将创建out
。
library(dplyr)
library(tidyr)
library(purrr)
filter(mydf, complete.cases(latitude) & complete.cases(longitude)) %>%
group_by(statename, pincode) %>%
summarize(ave_long = mean(longitude),
ave_lat = mean(latitude)) -> foo
下一步是以可以计算Haversine距离的方式排列foo
。我找到了一种很好的方式来安排这些数据。请参阅下面的链接。我们正在此处创建数据点的所有可能组合。
# Arrange this data in a way that we can calculate Haversine.
# We basically create all possible combinations of rows.
# This post gave me a hand: https://community.rstudio.com/t/create-all-possible-combinations-of-a-data-frame/26848/4
myrows <- foo %>%
group_by_all() %>%
group_split()
out <- t(combn(x = 1:nrow(foo), m = 2)) %>%
as_tibble() %>%
mutate_all(~ map(., ~ pluck(myrows, .x))) %>%
unnest() %>%
setNames(nm = c("start_state", "start_pincode",
"start_long", "start_lat",
"dest_state", "dest_pincode",
"dest_long", "dest_lat"))
我们可以使用distHaversine()或distGeo()。但是,让我们尝试一些新的东西。 SymbolixAU编写了另一个函数。谢谢SymbolixAU!
# https://stackoverflow.com/questions/36817423/how-to-efficiently-calculate-distance-between-pair-of-coordinates-using-data-tab/42014364#42014364
dt.haversine <- function(lat_from, lon_from, lat_to, lon_to, r = 6378137){
radians <- pi/180
lat_to <- lat_to * radians
lat_from <- lat_from * radians
lon_to <- lon_to * radians
lon_from <- lon_from * radians
dLat <- (lat_to - lat_from)
dLon <- (lon_to - lon_from)
a <- (sin(dLat/2)^2) + (cos(lat_from) * cos(lat_to)) * (sin(dLon/2)^2)
return(2 * atan2(sqrt(a), sqrt(1 - a)) * r)
}
最后一步是计算距离。
mutate(out,
distance = dt.haversine(lon_from = start_long, lat_from = start_lat,
lon_to = dest_long, lat_to = dest_lat)) -> result
# A tibble: 6,105 x 9
# start_state start_pincode start_long start_lat dest_state dest_pincode dest_long dest_lat distance
# <chr> <int> <dbl> <dbl> <chr> <int> <dbl> <dbl> <dbl>
# 1 KARNATAKA 560001 77.6 13.0 KARNATAKA 560003 77.6 13.0 3544.
# 2 KARNATAKA 560001 77.6 13.0 KARNATAKA 560004 77.6 12.9 4554.
# 3 KARNATAKA 560001 77.6 13.0 KARNATAKA 560005 77.6 13.0 3178.
# 4 KARNATAKA 560001 77.6 13.0 KARNATAKA 560008 77.6 13.0 4844.
# 5 KARNATAKA 560001 77.6 13.0 KARNATAKA 560010 77.6 13.0 4618.
# 6 KARNATAKA 560001 77.6 13.0 KARNATAKA 560011 77.6 12.9 5510.
# 7 KARNATAKA 560001 77.6 13.0 KARNATAKA 560013 77.6 13.1 9491.
# 8 KARNATAKA 560001 77.6 13.0 KARNATAKA 560014 77.5 13.1 12047.
# 9 KARNATAKA 560001 77.6 13.0 KARNATAKA 560017 77.7 13.0 6831.
#10 KARNATAKA 560001 77.6 13.0 KARNATAKA 560021 77.6 13.0 5148.