如何计算两个ZIP之间的距离? [英] How to calculate Distance between two ZIPs?

查看:55
本文介绍了如何计算两个ZIP之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a list of US ZIP codes and I have to calculate distance between all the ZIP Code Points. Its a 6k ZIPs long list, each entity has ZIP, City, State, Lat, Long, Area and Population.

因此,我必须计算所有点之间的距离,即:6000C2组合.

So, I have to calculate distance between all the points, ie; 6000C2 combinations.

这是我的数据样本

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

I've tried this in SAS but its too slow and inefficient, hence I'm looking for a way using Python or R.

任何线索都将不胜感激.

Any leads would be appreciated.

推荐答案

Python解决方案

如果邮政编码具有相应的纬度和经度,则可以使用Haversine公式(使用"mpu"库)直接计算它们之间的距离,该库可以确定球面上两点之间的大圆距离.

If you have the corresponding latitudes and longitudes for the Zip codes, you can directly calculate the distance between them by using Haversine formula using 'mpu' library which determines the great-circle distance between two points on a sphere.

示例代码:

import mpu

zip_00501 =(40.817923,-73.045317)
zip_00544 =(40.788827,-73.039405)

dist =round(mpu.haversine_distance(zip_00501,zip_00544),2)
print(dist)

您将获得以公里为单位的合成距离.输出:

You will get the resultant distance in kms. Output:

3.27

PS.如果您没有邮政编码的相应坐标,则可以使用"uszipcode"库的"SearchEngine"模块(仅适用于美国邮政编码)获得相同的坐标

PS. If you don't have the corresponding coordinates for the zip codes, you can get the same using 'SearchEngine' module of 'uszipcode' library (only for US zip codes)

from uszipcode import SearchEngine
#for extensive list of zipcodes, set simple_zipcode =False
search = SearchEngine(simple_zipcode=True)

zip1 = search.by_zipcode('92708')
lat1 =zip1.lat
long1 =zip1.lng

zip2 =search.by_zipcode('53404')
lat2 =zip2.lat
long2 =zip2.lng

mpu.haversine_distance((lat1,long1),(lat2,long2))

希望这会有所帮助!

这篇关于如何计算两个ZIP之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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