-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcalculate_distance_from_lat_long_corrdinates.py
62 lines (45 loc) · 1.72 KB
/
calculate_distance_from_lat_long_corrdinates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import geopy
import geopandas
import math
# convert degrees to radians
def degreesToRadians(degrees):
return degrees * math.pi / 180
# compute the distance in miles between a pair of (latitude,longitude) coordinates
def distanceInMilesBetweenEarthCoordinates(lat1, lon1, lat2, lon2):
earth_radius_miles = 6371 * 0.62137119
lat_diff = degreesToRadians(lat2-lat1)
lon_diff = degreesToRadians(lon2-lon1)
lat1 = degreesToRadians(lat1)
lat2 = degreesToRadians(lat2)
a = math.sin(lat_diff/2) * math.sin(lat_diff/2) + \
math.sin(lon_diff/2) * math.sin(lon_diff/2) * math.cos(lat1) * math.cos(lat2);
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a));
return earth_radius_miles * c
# set path to the data
interactions = "UUWI_Interactions_2020-2022.csv"
agencies = "uwwi_dataset_agencies.csv"
services = "uwwi_dataset_services.csv"
sites = "uwwi_dataset_sites.csv"
# import data into a dataframe
interactions = pd.read_csv(interactions)
sites = pd.read_csv(sites)
print()
print("Sites")
print(sites.head())
print(sites.info())
print()
site_geo_locs = pd.concat([sites['SiteAddressus_SiteAddressus_zip_latitude'], \
sites['SiteAddressus_SiteAddressus_zip_longitude'], \
sites['Site_Id']], axis=1, ignore_index=True)
site_geo_locs.columns = ['latitude', 'longitude', 'Site_Id']
site_geo_locs.dropna(axis=0, how='any', inplace=True)
print(site_geo_locs.info())
print()
lat1, lon1 = site_geo_locs.iloc[0][:2]
lat2, lon2 = site_geo_locs.iloc[1][:2]
distance = distanceInMilesBetweenEarthCoordinates(lat1,lon1,lat2,lon2)
print(round(distance, 2))