Python Maps with Folium

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everybody in this Python tutorial we're going to go over the folium package folium allows you to create web leaflet maps in this tutorial we're going to do things just a little bit different we're going to stay real high level on the explanation of the code because I actually created this tutorial once before and it was well over an hour when I went into great detail on the code and I wanted to keep this tutorial just a little bit shorter if you would like separate tutorials on any of the examples that we cover with more detail just let me know in the comments also I'll make sure that I put this jupiter' notebook on github that way you can access the code and download the notebook the link to access this jupiter' notebook on github will be in the tutorial description in a few of our examples we will use geo JSON files and CSV files those will be on github as well if you watch my tutorial on I PI leaflet you will already be familiar with a lot of what we will cover in this tutorial however note that the functionality and syntax is different between the folium and I PI leaflet packages and in this tutorial we have a whole bunch more examples than we did in the iPad leaflet tutorial let's go ahead and get started for install instructions portfolium you can go to python org and then go to the python package index in search for folium if you would like to access the folium documentation you can access it at this website the imports that we will be using include folium from folium import plugins i buy widgets geo coder geo pi numpy and pandas and the vega datasets our first map example probably the most basically that you can create a map is to use folium map so if we run it you can see we get our basic map if you'd like to zoom in you can do that you can move the map around to different locations and of course you can zoom back out here we have another example and in this example we have added the location the zoom start the width the height and the control scale the control scale will add the distance scale here for the location that's kind of like your starting location where you would like the map to be centered and that is a set of latitude and longitude coordinates if you would like to change the zoom start the smaller the number the more the map will be zoomed out the larger the number the more the map will be zoomed in so here currently we're at a zoom level of 12 if we put in a 1 you can see it zooms way out now notice on this one we set the width and the height however the map still takes up a large area another way that you can resize your map is to use from Branca element import figure go ahead and create your figure put in the width and the height use the figure variable that add child and put in the map okay and we're using our map from up here for this example you if you would like to save your map you can go ahead and create your map then use that map variable dot save then go ahead and put in the path then give the map a name dot HTML so if we run this and then we go to our desktop that's where we save the map we can actually open that map up and you can see we get our folium map we can move it around we can zoom in and we can zoom out next let's go over a few of the different types of maps that you can use in this case we're going to use I PI widgets so what we've done is we have created a widget which is the Select widget then we create a function that will help us show the maps and then we create an interaction between the widget and the function so if we run this we can use our selection box here this first example that you see is an example of OpenStreetMap then we have the terrain here we have an example of a toner map here we have watercolor positron and Dark Matter now notice for the drain toner and watercolor those are stamen and for the positron and Dark Matter those are cartodb we can also do something very similar without the I PI widgets using layer control so we go ahead and recreate our map then to create our layers we use folium raster layers dot IO layer specify the map type and then add to the map for the layer control you use folium that layer control and add that to the map so if we run it you can see we get our map our layer control is over here and then we can just click these radio buttons to see the different types of maps for this next example we'll go over how you can trade a mini-map a scroll zoom toggle button and a full screen so we've gone ahead and created our map for the mini-map we use plugins that mini-map then we add that to the map using add child for the scroll zoom toddler we use plugins scroll zoom toddler add it to the map and for the full screen we use plugins full screen add that to the map let's run it you can see we get our mini-map here let's actually put this in full screen click this button here and when you move the map around you'll notice that the mini-map changes along with it or you can move the mini-map and that will change the map here so these two maps are linked if you'd like to get out of the full screen you can just click this button or you should be able to hit escape for the scroll zoom toddler you'll see that down here on the bottom if you're using the trackpad or a mouse that will change how that reacts with the map so currently if we scroll in it just Scrolls if you press this button then it will change and scroll in on the map okay so depending on how you have this button set when you use your mouse or your trackpad you can either scroll the webpage or scroll in and out of the map next if you would like to create a marker that looks like this you can go ahead and create your map here we've used the geo coder module to geo code and address into latitude and longitude coordinates then we use folium marker we put in our latitude and longitude coordinates we give a title to the pop-up a title to the tooltip and add it to the map and we run it and you can see it will place the marker based on the address that was converted to latitude and longitude in the correct location so that's one way that you can just put one marker on your map if you'd like to put several markers on your map there's several different ways that you can do that in this example we've used a for loop with it or ropes so here we've gone ahead and created our map then we use the for loop to loop over the data frame of the airport locations and for the airport locations we use the bigger datasets we are only using 25 records from the airport's data frame and this is what the data frame looks like so we loop over the day frame then we use folium marker then we assign the data to the location and the pop up the location we'll use the latitude and longitude and the pop up will use the name the city and the state and we add that to the map let's run it you can see we have the 25 airports placed onto the map with the markers and you can click on each marker to get the information now if you did not want to use a loop to place the markers on the map you can use the apply function and we have an example of that here we've gone ahead and created our map then we use the airport's data frame dot apply we use a lambda function and once again we use folium marker we use the latitude and longitude for the location and the name for the pop-up and add that to the map and you can see we get the same results if you're using a really large data set you might want to use the apply instead of the loop if your datasets not real large it probably won't matter either way next so in the previous examples to be used in data frame but let's say that your data set that you want to use to place the markers on the map is a dictionary you can do that as well so here we have our dictionary with cities and the corresponding latitudes and longitudes go ahead and create our map then we use a for loop we loop over the dictionary we use items then we use folium marker and for the locations we use the index of the dictionary items so it puts it into a tuple this would be index 0 this would be index 1 so for the location we use the latitude and longitude coordinates the pop up we use the name and then we add that to the map and you can see we get our map so we have Los Angeles Las Vegas Denver Chicago Manhattan next what if you wanted to use custom markers one way you can do that is to use either font awesome or the glyph icon bootstrap and there are other ways as well so in this example we're going to use a font awesome custom marker if you'd like to see examples you can just do a web search and put in font awesome icons here we've gone ahead and created our map we use folium marker put in our location pop up an icon and for the custom marker we put in folium dot icon put in the color the custom type here and you can find those like I mentioned by doing a search for font awesome icons now for the prefix we want to use FA for font awesome add that to the map run it and you get your custom icon this next example is basically the same however in this case instead of font awesome we're using glyph icon bootstrap to see examples of those you can do a web search for glyph icon bootstrap icons go ahead and put in your custom marker and for the prefix instead of FA you put glyph icon and you can see we get our custom glyph icon here we have another example with several custom icons now to do this we've gone ahead and created a data frame the data frame includes the city the corresponding latitudes and longitudes and the icon that we want to use in this case we're using a loop again however we're using it or tuples this time and this is kind of cool because when you use it or tuples the output of the tuples looks like this and then you can just use the I from the for loop and a dot and you can access any part of the tuple that you want so to access the cities you would use i dot city to access the latitudes and the longitudes you would use a dot latitude or I that longitude and so on so once again we use only a bat marker and we put in the location to pop up and the icon now for the custom icons we use folium dot icon then we assign the icon using icon and that will use these icons right here for the prefix we use font awesome and add it to the map and you can see we get all of our custom icons now one quick note when I went to the font awesome icon web page not all of the icons that I wanted to use worked so you might have to try out some of the icons and see which ones will work for you another common thing that you might like to do is to place markers on your map and enumerate them or to put numbers inside the markers and those numbers would correspond to your locations so once again we've created a data frame the data frame looks like this we have our cities coordinates and the icons that we want to assign to each city and there's all different kinds of ways that you could use this so for example let's say you have some cities with some populations and you wanted the numbers to correspond to those populations in this case we're assigning the numbers going from the west coast to the east coast so here we have our data frame that we're going to use we create our map once again we are using a four loop iterating over the data frame using it or tuples we use folium marker assign the location pop-up icons and for this one we're using the plugins that beautify icon then we assign the number border color border with text color and then here we have some styling we add that to the map and run it and you can see we have our enumerated icons you can also place circle markers onto your map there are two types of circle markers we use in this example so the green marker that you see here uses a radius in meters the blue and yellow marker we have here is a circle of fixed size with a radius specified in pixels now one thing you'll notice when we zoom in this circle stays the same size and this circle gets bigger okay and that's because the green circle is specified with the radius and meters so that will correspond to the zoom level this circle here is specified in pixels and that should stay the same size no matter the zoom level so it just depends on what type of circle you'd like to create next we have an example of how you could plot a route we've gone ahead and created our map here we have our route latitudes and longitudes and to create the route we use volumes polyline we put in our coordinates and we add that to the map so you run it and you can see we have our route going from the west coast to the east coast and each stop along the way are the cities Los Angeles Las Vegas Denver Chicago Midtown you can do something very similar with an ant path and to create the ant path you use plugins ant path we run that you can see that it's similar however you'll notice that it is animated and it's actually going in the direction that we wanted it to go from west to east next we have an example of how you can overlay a Geo JSON layer a few notes on that you can use a link or a file in this case we're going to use a file and normally we put our files on the desktop however in this case this is a local file in my home directory another note to create these geo JSON layers you can go to geo JSON geo then you can use a draw tool to create the shapes and then save it as a geo JSON file also you can actually use folium to do this and we'll show you an example of that below and last if you have some shapefiles that you would like to use you can convert those to geo JSON using the QGIS application okay so we've gone ahead and created our map then we use volumes geo JSON we put in the file name we give it a name and that will allow us to access it in our layer control here and we add that to the map for the layer control we use this code here let's run it and you can see our geo JSON layer has been added to the map and you can take the layer off like that and put it back on next here we have an example using subgroups with several geo JSON layers here we've created our map to create your main group you use folium feature group and you can reference your map variable add child and pass in your main feature group for your subgroups you can use plugins feature group subgroup and we have created three subgroups here here we've gone ahead and pulled in our geo JSON layers and added them to the map and then here we have created our layer control now in this case the layer control is not collapsed you can see that set to false and then we can control how we want to add the layers so to take them all off you click here if you just want to see a certain layer then you can click on one of those checkboxes or any combination next we have an example of a choropleth map now note on this there are many more examples of how to create choropleth maps in the folium documentation so if you'd like to create choropleth maps be sure to check out those examples in this example we've gone ahead and imported JSON then we pull in two files one is the Geo JSON file that has the coordinates for the shapes and the other one is a data file and the two files have to correspond with a data element in this case it will be in the county and the data file will have the information that will correspond to the colors for the choropleth map so here we have our geo JSON counties we've gone ahead and added an element to that geo JSON file to make sure that there's an element in common between the geo JSON file and the data file and as I mentioned that's the county name then we go ahead and we create our map and we create our core plus we put in all the arguments and add it to the map in this case we've added the layer control and then we use the map variable to display the map let's go and run it and you can see we get our core plus map up here we have the color bar which corresponds to the colors for the map the lighter the color the lower the population for that county the darker the color the higher the population and this might be one of the examples that I'll try to do a separate tutorial on and go into more detail on the code however as we mentioned don't forget there's very good examples in the documentation as well next in this example we'll go over how to create a heat map that looks like this so the first thing we want to do is to get some location data for large cities that we want to put onto our map these are the cities that we're going to use and we're going to use the geo coder module with Open Street Map so this will give us the location information then to get the latitude and longitude we just use these variables and we use dot Lac for latitude and Dot long for longitude so for all of these cities in each of the lists we have the latitude and longitude and then we have the population that will correspond to the intensity of the dot on the map now notice that we've gone ahead and taken all of the populations and divided them by a thousand and that will make the intensity dot or the bubbles just a little bit smaller so it fits better onto the map then we've taken all of these variables here and put them into one list so we end up with the latitude longitude and the intensity and that's what we need to create the heat map then we go ahead and we create the map we use plugins heat map put in the cities with the coordinates in the intensity and add it to the map and here we have our heat map now you can also create an animated heat map to show different data over time so here we've gone ahead and created our data here we've created a time series that will correspond to how the data changes over time then we go ahead and create our map to create the heat map with time we use plugins heat map with time we pass in our data and our date times then we use our variable heat map time plugin and add it to the map let's run it now to see this all we have to do is press play and you can see the data changing over time and the time changes here you can also zoom in and you can speed it up it's pretty cool another cool thing you can do is you can add a latitude and longitude tool to your map and this will allow you to click anywhere on the map and get the coordinates for that point and all you have to do for that is to create your map use the map variable at child and pass in folium dot latitude longitude pop-up let's run it and you can see when you click you get the latitude and longitude for that point moving on if you would like to create a major control for your map and you can use plugins that measure control and you can see here we have our major control then we can click on create a new measurement let's go from the west coast to the east coast and finish and you can see from this point to this point it's approximately two thousand five hundred and eighty four miles now one thing to note when I tried this in a Google Code lab notebook these two arguments here did not work so you might have to take those out if it doesn't work and also note that if you'd like to change your units you can do that here okay moving on if you'd like to create a dual map you can do that by using plugins dual map then for each of your Maps use folium dot IO layer put in your map and add it to the map in this case we've gone ahead and added the layer control but you see here next in this example we're going to show you how you can add draw tools to your map so go ahead and create your map for the draw tools use plugins that drop add those draw tools to your map and run it you can create lines here we have a polygon here's a square circle Merkers and small circles if you'd like to change those click here on edit layers then you can change them as needed and click Save now this export is what we were talking about earlier so you can create shapes and export those as geo json layers and then use those four layers on future maps so on a Mac if you click export it will download it and you can see that it's gone to the Downloads folder here I'm not exactly sure where this would download on a PC but I'm pretty sure that it will work pretty much the same if you would like to delete everything you can click on the trashcan and then clear all here we have an example of overlaying an image onto your map so we've go ahead and create our map then we use folium dot raster layers image overlay we put in the name the name of the image file we put in our bounds and this is the important part here of trying to get the image placed correctly onto your map what you do you have to put in the latitude minimum and the longitude minimum then you put in the latitude max and the longitude max so we've put in those coordinates here and our image includes the flags of Africa and you can see that this overlay it's not perfect but it's pretty good so if we use the layer control we can take that off and then we can put it back here we've assigned an opacity of 0.5 so it's a little bit see-through so you can see the flags for each country next another cool thing you can do is that you could put charts into your marker pop-ups we're going to go ahead and use the Seattle weather data set from the Vega data sets now here we've used all tear to go ahead and create a chart and the chart looks like this and what we want to do is to take this chart with the Seattle weather data and place it onto the map for the Seattle area and then when we click the marker we want this chart to pop up so here we have our chart element then we have our map here we have our marker then we create a pop-up we have the chart to the pop-up and we add the pop-up to the marker and finally we add the marker to the map so let's run it and you can see the marker is placed on Seattle and when we click this we'll get our chart with the Seattle weather data pretty cool next you can also use folium with I PI widgets and we showed a little example of that earlier now for this example we've gone ahead and created a widget with a textbox here we've gone ahead and created our function which will allow us to put in an address and get the coordinates for that address and place a marker onto the map so we have our widget or function and then we have our interaction between the widget and the function so let's go ahead and run this and let's use a few of our test addresses here for this first address will use the indy500 track let's go ahead and put that in and run it we get our map with the marker placed right on the Indy 500 track let's go ahead and zoom in and you can see here we have the Indy 500 track let's try another example for this example let's try the San Diego Zoo let's run it and zoom in and we get our Mercker right by the San Diego Zoo now for our last example this is the address for the Apple Campus but let's say that you don't want to use the full address let's just say you want to use the zip code you can do that as well from it and here the marker will be placed at the nine 501 for zip code go over one more example once again we're going to go ahead and use I PI widgets in this case we've gone ahead and created two widgets and these are two text boxes and what we want to do is to plot a route so here we have our widgets and here we have our function and here we have our interaction between the widgets and the function let's run the code and let's plot a route from the Indy 500 track to the San Diego Zoo now we've also added some functionality in this example that will allow us to see the distance from the start location to the stop location in miles let's run it so you can see we have our ant path with the animation going from the Indy 500 track to the San Diego Zoo and the approximate straight line distance is 1784 miles okay that's all we have for this tutorial join us again next time you
Info
Channel: Ryan Noonan
Views: 35,831
Rating: 4.959712 out of 5
Keywords: Folium, folium maps, python folium, folium python, maps with folium, folium change map size, folium map types, folium markers, folium custom markers, folium enumerate markers, folium multiple markers, folium geojson, folium choropleth, folium heat map, folium measure control, folium dual map, folium draw control, folium overlay image, folium chart popup, folium with ipywidgets, python heatmap
Id: t9Ed5QyO7qY
Channel Id: undefined
Length: 30min 41sec (1841 seconds)
Published: Thu Sep 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.