Preprocessing: Calculate the Distance Between Longitude and Latitude Points with a Function | by The Data Detective | Towards Data Science

As part of a series that will focus on some unique pre-processing techniques, the first in this series will focus on working with latitude / longitude information. This type of data is important for mapping visualizations and other geolocation purposes.
Latitude is the geographical location that specifies the North-South point on the Earth’s surface. Longitude provides the East-West point. When it is written out, the latitude is written first, followed by a comma, then the longitude. The equator represents the zero point for latitude and the prime meridian represents the zero point for longitude.
At times, calculating distance may help in feature engineering data when you have geolocations of several points. I will use the 2018 Austin Crime dataset which can be found at https://data.austintexas.gov/Public-Safety/Crime-Reports-2018/vmn9-3bvu. Regarding geolocation data, this dataset contains separate festures for latitude, longitude and a combined latitude / longitude feature named location.
These geolocations identify where a crime had taken place. Most represent addresses, some are alleys, parking lots, etc. so using geolocation is important for reference.
There are four police stations in Austin. The main headquarters and three substations (north, east, west). Austin PD also provides a dataset that features the locations of these stations. In this dataset, the name of the station, along with separate longitude and latitude points (labeled as X and Y respectively).
The plan is to feature engineer the distance from each crime to each police station. It is fortunate that the geolocation data was separated as it does save a couple of steps in separating that data. If you run into that situation, and you have a combined geolocation that looks similar to the Location feature above (ex: (30.1876534, -97.475630)), you can easily separate this data by first using the python strip() method to remove the parentheses, then using the split() method to separate the values as they will be consider objects if there are any characters in the column.
df[‘Location] = df.Location.strip(‘()’) #will remove the parentheses (can use regex if necessary to remove additional text,etc)df[[‘Longitude’,’Latitude’]] = df.str.strip(‘,’, expand = True)
This article won’t cover the details of the formula to calculate this conversion, but the below function will take a start latitude and longitude and convert the distance (in miles) to a DataFrame column when running the function.
‘’’Distance equation for long,lat data used via stackoverflow from user Michael0x2a. Updated to a function that converts to mileage’’’# constant values, if need to change end lat, long points, change the lat2, lon2 informationlat2 = np.array(clean.Latitude) #identifies iterable latitude columnlon2 = np.array(clean.Longitude) #identifies iterable longitude columnlatr = np.array(list(map(lambda x: np.radians(x), lat2)))lonr = np.array(list(map(lambda x: np.radians(x), lon2)))def distance(lat1,lon1):    lat1 = np.radians(lat1)    lon1 = np.radians(lon1)    dlon = np.array(list(map(lambda x: (x — lon1), lonr)))    dlat = np.array(list(map(lambda x: (x — lat1), latr)))    a = np.sin(dlat/2)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2)**2    c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1 — a))    # 6373.0 represents earth radius in kilometers    kilo = 6373.0 * c    miles = kilo * 0.62137119    return miles
To run the function, be sure to set the new feature as the variable:
clean[‘Main Distance’] = distance(30.267574,-97.735070)
If you need distance in kilometers, just remove the miles conversion line and make sure to return kilo.