简体   繁体   中英

How to calculate Distance between two ZIPs?

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.

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

Here is a sample of my data

在此处输入图片说明

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.

In SAS, use the GEODIST function .

GEODIST Function

Returns the geodetic distance between two latitude and longitude coordinates.

Syntax

GEODIST(latitude-1, longitude-1, latitude-2, longitude-2 <, options>)

R solution

#sample data: first three rows of data provided
df <- data.frame( zip = c( "00501", "00544", "00601" ),
                  longitude = c( -73.045075, -73.045147, -66.750909 ),
                  latitude = c( 40.816799, 40.817225, 18.181189 ),
                  stringsAsFactors = FALSE )

library( sf ) 

#create a spatial data.frame
spdf <- st_as_sf( x = df, 
                  coords = c( "longitude", "latitude"), 
                  crs = "+proj=longlat +datum=WGS84" )

#create the distance matrix (in meters), round to 0 decimals
m <- round( st_distance( spdf ), digits = 0 )

#set row and column names of matrix
colnames( m ) <- df$zip
rownames( m ) <- df$zip

#show distance matrix in meters
m 

# Units: m
#         00501   00544   00601
# 00501       0      48 2580481
# 00544      48       0 2580528
# 00601 2580481 2580528       0

Python Solution

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.

Example Code :

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. 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))

Hope this helps!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM