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

How to calculate Distance between two ZIPs?(如何计算两个 ZIP 之间的距离?)
本文介绍了如何计算两个 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))

希望这会有所帮助!

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)