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

2022-10-13Python开发问题
5

本文介绍了如何计算两个 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 之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10