Hướng dẫn driving distance map python

In our previous article, we mapped 100 data points at random locations in Rwanda. In this article, we are going to see the quickest -- and arguably the simplest -- way to get the distance between one specific location and many others, and the time it takes to travel that distance. We will use very simple Python codes together with Google APIs. The reason we use Google APIs is that it gives you the right/actual distance, as opposed to straight-line distances (known as Euclidian distances) offered by most tools. Google API service uses driving distance (although you can choose to use "walking" distance), which means it will calculate the distance based on the actual fastest routes or paths using the actual road network system (where applicable!). Our task is to make a list of distances (and times) for all the data points (100 data points) we randomly created and used in the previous article. We'll achieve this in three simple tasks: (1) importing libraries and loading data; (2) calculating durations/times; and (3) finally adding the corresponding distances (you can start with distance if you like) to the list. The idea is that we can use these results as variables for further analysis (e.g: think of regression analyses that require distances as variables). As always, the idea is to achieve this in the simplest way possible. No fancy tools!

Let's do this!

First, we import the pandas library and then load the data. Again, you can get the data here, if you want to use mine. You'll notice that I added a new column called coordinates that joined both latitude and longitude data in one column. That is the format required by the code we will use later; so, we can only comply.

import pandas as pd data = pd.read_excel(r'data.xlsx', 'data')

To complete our task, we will need a few things: Google maps library (gmaps), simplejson library, and the Google API key to access the distance matrix. Let's handle that first.

Let's start with gmaps:

pip install googlemaps

Then, we install simplejson:

pip install simplejson

Finally, we install the google-auth-oauthlib. Since we will have to use Google API, we will need a key. Through that process, we will need authorization. That's why we need this.

pip install --upgrade google-auth-oauthlib

We are all set to start the magic.

Let's import our library that we just installed

import googlemaps 

The second step to complete our task is to calculate the time or duration of driving from every location of the hundred random locations we created to the origin. Let's assume that (-2,0123469, 29,377851) is the origin; this is the first entry in our randomly generated data set. To get the API key, go to Google Distance Matrix API. It's free, and you only do it once. Additionally, you use for every project that requires Google APIs.

Once you are done, write the following code to calculate the time/duration. .

API_key = 'Enter your API key here' #enter the key you got from Google. I removed mine here gmaps = googlemaps.Client(key=API_key) origin = (-2.01234699405899,29.377851313693) #Let's say this is the origin destinations = data.coordinates actual_duration = [] for destination in destinations:     result = gmaps.distance_matrix(origin, destination, mode='driving')["rows"][0]["elements"][0]["duration"]["value"]       result = result/3600     actual_duration.append(result)      #Add the list of coordinates to the main data set data["duration (Hours)"] = actual_duration data.head(10)

Our third and last step is to calculate the distance we just traveled(!).

API_key = 'Enter your API key here' #enter the key you got from Google. I removed mine here gmaps = googlemaps.Client(key=API_key) origin = (-2.01234699405899,29.377851313693)  destinations = data.coordinates actual_distance = [] for destination in destinations:     result = gmaps.distance_matrix(origin, destination, mode='driving')["rows"][0]["elements"][0]["distance"]["value"]       result = result/1000     actual_distance.append(result)      #Add the list of coordinates to the main data set data["distance (Km)"] = actual_distance data.head(15)

Simple and sweet! We have our list for 100 entries (only displaying the first 15 entries).

Now, let's check if what we got makes sense. Take two random points and check whether the time and the distance that Google maps service suggests are the same as (or reasonably close to) what we have in the data set we generated.

At first glance notice how the first entry is 0 for distance; that's a good indication. Remember we took the first entry as a reference.

Let's take the second entry (id 2) in our dataset (-1.66881597155677, 30.5838648247755) and see both the distance to origin (which appears to be somewhere in Rutsiro district!) and the time it takes to get there. With our fingers crossed, we then can compare that with what we got from the code.

Google map shows that the shortest route is 233 km long and that it takes around 5 hours to travel that distance. Comparatively, our code determined that the shortest route will be about 240 km long and that it will take 5.2 hours to travel that distance.

Eureka!

One more example. One for the road. Let's check the distance and time for id 11.

Google map says that for id 11, the journey will be 144 km long and it will take around 3 hours to complete. Comparatively, the result from our code shows that the journey will be about 143 km and that it will take about 3.3 hours. Pretty reasonable.

All in all, it works. In some instances, however, it might not be that precise but in the majority of cases, it gives significantly reasonable answers. Remember that there are other factors that may cause minor differences that we might not be factoring in. In any case, it is enough to use these results as variables in statistical analysis. With this in mind, think of how you can calculate the shortest distance to a given physical infrastructure such as hospitals or closest resources such as rivers or lakes. We will do that in other iterations of this series.

But the next iteration of this series will address one question: if you know the coordinates of a given location, can you tell the elevation using Python? This is crucial if you need elevation for your analysis. No need to go for data collection in the field. Python got you.

Ad astra!

Chủ đề