猿问

如何计算两个 ZIP 之间的距离?

我有一个美国邮政编码列表,我必须计算所有邮政编码点之间的距离。它是一个 6k ZIP 的长列表,每个实体都有 ZIP、城市、州、纬度、经度、面积和人口。

所以,我必须计算所有点之间的距离,即;6000C2 组合。

这是我的数据示例

我已经在 SAS 中尝试过这个,但它太慢且效率低下,因此我正在寻找一种使用 Python 或 R 的方法。

任何线索将不胜感激。


慕田峪9158850
浏览 250回答 3
3回答

森林海

在 SAS 中,使用GEODIST函数.GEODIST 函数返回两个纬度和经度坐标之间的大地距离。…语法GEODIST(latitude-1, longitude-1, latitude-2, longitude-2 <, options>)

撒科打诨

R解决方案#sample data: first three rows of data provideddf <- data.frame( zip = c( "00501", "00544", "00601" ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; longitude = c( -73.045075, -73.045147, -66.750909 ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; latitude = c( 40.816799, 40.817225, 18.181189 ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringsAsFactors = FALSE )library( sf )&nbsp;#create a spatial data.framespdf <- st_as_sf( x = df,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; coords = c( "longitude", "latitude"),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; crs = "+proj=longlat +datum=WGS84" )#create the distance matrix (in meters), round to 0 decimalsm <- round( st_distance( spdf ), digits = 0 )#set row and column names of matrixcolnames( m ) <- df$ziprownames( m ) <- df$zip#show distance matrix in metersm&nbsp;# Units: m#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;00501&nbsp; &nbsp;00544&nbsp; &nbsp;00601# 00501&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; 48 2580481# 00544&nbsp; &nbsp; &nbsp; 48&nbsp; &nbsp; &nbsp; &nbsp;0 2580528# 00601 2580481 2580528&nbsp; &nbsp; &nbsp; &nbsp;0
随时随地看视频慕课网APP

相关分类

Python
我要回答