Folium Mapping, Geopy Distance Calculations, and OpenStreetMap API Lookups in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there and welcome to another bug bites tutorial in this one we're going to look at how to take a postcode or a zip code and convert that to latitude and longitude in python and then we're going to look at how we can plot the points that we get back on a map using the folium library we're also going to look at how to calculate the distance between two points in this lesson so let's get started so what we want to do is let's assume we're given a zip code that could have been entered by a user in a form on a website and what we want to do is then do a lookup on the postcode to get more detailed geographical data so that is taking a postcode and converting it to coordinates latitude and longitude that you can then plot on a map and then you can do things like calculations to see where are your customers or your users whether they based and you can do that based on the coordinates that you get back from this lookup so what we're going to do is look up the nominatum openstreetmap api there is a subset of that api which is the search functionality on the api which i'm going to open up on a new tab and we are going to make a api request to the api using python's requests library so import requests and the pretty print module and what we're going to do is define the base url for the nominating api and we specify that the format of the result data we want is json data and let's now take a quick look at the nominating api the search api so it allows you to look up a location from a textual description or address and in our case what we're going to do is we're going to look up by the postal code and this is a query parameter that you add to the end of the base url so we're going to do that now and you get an output options like format such as json and we have already specified that so let's get started so what we're going to do is we're going to use the requests library to send an http get request and we're going to construct the url now it's going to be an f string here the base url to begin with and that's just defined above here and to that we're going to append another query parameter and it's going to be the postal code postal code and that's going to equal the postcode that we define in this variable here and if we look at the search api for nominatum that's the query parameter we need to be looking up here postal code so we use the and because we already have a one query parameter to specify the format and the second query parameter we set to the postal code so that's the http request and we'll store that in a response variable and if we then convert that to json and we know we're going to get jason back here because of the format that will send the request and it should decode the json finally we'll get back the dictionary or rather the list of json data that's a single element within that list and it's an object where you can see that the postcode that we've looked up um is we're now getting back geographical data for that postcode and this is glasgow and it has a particular longitude and a particular latitude and also a bounding box to show the area of where this postcode actually occurs so that's how we get back the data we can extract from that data the latitude and longitude and let's set it to data and from there we can get the latitude and the longitude and by indexing into the data that we've got back at element zero remember it's a list with a single element which is a dictionary and then we can call the dot get function on the python dictionaries to get the latitude which is represented by lat in the dictionary that's the key and we can do something very similar for a longitude we'll just copy that and change the key to lon and the variable will be called longitude so if we then print these out as a tuple we can see the latitude and longitude of that particular point that best represents the zip code that we've had so that's how we convert the postcode or the zip code into geographical data now i'm going to copy the same code and do this again for another postcode and it's going to be zip code 2601 which is a more generic zip code and if we execute this code and set the postal code to that zip code we get back a list with more than one element in this case you can see that there's this zip code is found in australia and italy and in other countries as well um germany i believe here or austria so what we want to do now is just show you one quick uh example of another query parameter let's say we only wanted to get the postcode that's in italy so we can append another query parameter let's say and country equals italia and then we would get back a single element within this list that represents the italian zip code so that's going to represent our next point and i'm going to define i'm going to copy the code up here to get the second latitude longitude pair we'll do that here it's going to be latitude 2 and longitude 2 and again grab this data and again we're going to extract the lat and the long keys from that and if we print out these second latitudes and longitudes we get a different point on the earth's surface so let's skip by these ones and we'll get to mapping the points now what we're going to do now is we've got two points we want to plot them on a map python has this library called volume that you can install with pip or conda and volume allows you to basically get an interface into the leaflet javascript mapping library so let's see how to do that now we're going to import volume and in collab this is already installed so you should be able to do this without any problems and now we're going to define two variables for our locations so location one just call it location we're going to convert the latitude to a float and the longitude to a float and this is for the first point in glasgow location 2 is going to be the point in italy and we're going to convert latitude 2 to a float and longitude 2. now we convert them to flow because by default from the api they come back as strings we want to convert these to floating point numbers so we can actually do the mapping now what i want to do is and we create a folio map object which we're going to store in a variable called m and it's going to be an instance of folium.map and then to that we can pass any parameters that we want so i could pass location equals location and then that would center that on the original location in glasgow we can also pass width and height parameters to this so i'll say on an 800 by 400 map and to show that in a collab notebook just put m there that's the variable we've defined and you'll see that we get a map that's centered around glasgow that's very interesting we're able to create a map so easily using python and center in a particular location let's now go one step further and plot the marker for the point that we have in glasgow so we can use the volume.marker instance so volume.marker and again we specify the location the coordinates which is a latitude longitude pair and then we can also add it to the map with the add to function and we pass the map as a parameter so if i execute the exact same code we get the same map but it has this marker object as well so that allows us to do that and we can actually even add things like pop-ups and i can say glasgow postcode to that and then if i click that you get the pop-up of glasgow postcode so this is very interactive it's very dynamic you can you can zoom out you can zoom in um so that's how you do that so let's now add the second location to this map as well i'm going to copy the volume dot marker and we'll pass location 2 and we'll say italian postcode now if i call m of course we only see glasgow but if we zoom out enough we see down in italy and northern italy we see the second point so that's how you plot points and markers on maps using python using the folium library it's very simple and it's also very useful for notebook based analytics so with that i want to move on to how to find the distances between these two points if you want to check the blog post out or the jupiter notebook on github you'll find that there is some additional calculations that we do we find the midpoint between these two points and we plot that on the map we're not going to do that in this video check out the blog post for that but what we are going to do is find distances between the two points and we're going to use a library called geopi and this has a distance module which has a distance function within it so we're going to use that distance function to get the distance in kilometers between the two points so we'll pass location and location two to the distance function and we can also get it in miles um by using the same function but chaining dot miles onto the end of it so if we print that out let's print the kilometers first and then the miles we'll get the distance between the two points in scotland and italy is 1527 kilometers and it's nearly 949 miles so that's very simple way to get the distance between two points in python you get the coordinates of the two points and you pass them to this geo geopi distance function and that will calculate as it says here the geodesic distance between the two points so that's how you do that in python geo pi is a useful library for a lot of geographical operations and distance is one of them so as a folium bonus let's plot the line between the two points i'm going to copy the code that we had up here just this code here and what we want to do now look at the map we want to to draw a line between the two points essentially so we have two points we want a line between them volume has one other routine that you can use it's called folium.polylane and it allows you to pass in a tuple with two or more locations and you can then draw the line between them and again you use the add to function and add it to the map m and if we do that you'll find that you'll find that that hasn't actually worked i don't think and that's because we are not passing location two in here so we'll try to draw a line between the same point essentially which won't work but if we put in location two as the second parameter we see this line has suddenly appeared and this will connect the two points that we've passed in as our location so that's just a bonus how to connect two points and folium using a line so we can do that as well that's all for this video there is an associated blog post with more information and check out the jupiter notebook on github as well if you're interested thank you very much for watching if you've enjoyed the video please like and subscribe that would be really appreciated and until next time thank you
Info
Channel: BugBytes
Views: 115
Rating: 5 out of 5
Keywords:
Id: AeYxEDM1o2E
Channel Id: undefined
Length: 11min 52sec (712 seconds)
Published: Sun Sep 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.