如何计算两个 ZIP 之间的距离?
问题描述
我有一个美国邮政编码列表,我必须计算所有邮政编码点之间的距离.它是一个 6k 长的 ZIP 列表,每个实体都有 ZIP、City、State、Lat、Long、Area 和 Population.
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.
任何线索将不胜感激.
解决方案
Python解决方案
如果您有邮政编码对应的纬度和经度,您可以通过使用'mpu'库的Haversine公式直接计算它们之间的距离,该库确定球体上两点之间的大圆距离.
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))
希望这会有所帮助!
相关文章