Get startedGet started for free

Street maps with folium

1. Working with Folium

Now that you've explored several ways to get information from the geopandas API, let's return to enhancing our map of downtown art.

2. Map of downtown art so far

Recall that your map thus far consists of the Urban Residents neighborhood polygon - the light green shape you see here - and the locations of public artworks within the neighborhood. While this is useful in some ways, it would be even more useful if we added a street map for context.

3. Folium

For adding a street map, we'll turn to the folium package. Folium is a python package that leverages the Leaflet javascript library for creating and styling interactive maps.

4. folium.Map()

To create a map with folium, you pass your starting coordinate pair as `location` to the folium Map constructor to create a map object. Use display() to show the map. The display function is part of IPython. It is available automatically in interactive environments like jupyter notebooks and the DataCamp exercise environment. Here we have used the latitude and longitude that describe the point location of the Eiffel Tower in Paris, France. It's important to note that folium always wants coordinates as an array, with latitude first.

5. folium.Map()

And here's the result. A street map centered on the Eiffel Tower.

6. Setting the zoom level

You can set an initial zoom level when you construct a map with the zoom_start argument. The higher the number, the closer your map will zoom into the starting coordinate pair. Here we have set zoom_start equal to 12, which will get us nice and close.

7. Setting the zoom level

And here's that map. If you look closely, you can actually see a tiny representation of the Eiffel Tower in the center of the map.

8. Folium location from centroid

Remember our school district one polygon from earlier in this chapter? Here it is again. We want to use the point in the center column of district one as the starting coordinates of a folium map called district_one map. Remember that we need to reverse the order of the coordinate pair for folium so that latitude is first. To do that, slice the first (and only) occurrence of district_one.center. The `center` column is a GeoSeries and the value stored in that column is a Point geometry. We'll save that Point geometry to a variables called `center_point`.

9. Folium location from centroid

By moving the y-value of center_point to the first position in the district_center array and the x-value to the second position, we can create district_center, a folium location (with latitude first) from the shapely Point, center_point, that has longitude first. We'll use the district_center array as the location for a folium map.

10. Adding a polygon to a folium map

Here, we create a map object called district1_map using district_center for the location. Next we pass the polygon for district one to the folium GeoJson constructor in order to draw the shape of school district one. We use the add_to() method to add the polygon to the district one map. Notice that the folium map we have already created - district1_map - is passed in as the argument to the add_to() method. Finally, to show the map, we pass the map object to display().

11. Adding a polygon to a folium map

And here's the result. We see the street map of Nashville, centered around the center point of school district one. A blue polygon, representing school district one, is layered on top. We knew that district one was the largest school district, but this visualization gives even greater insight into just how large school district one is.

12. Let's practice!

You've seen how the folium map constructor creates a map and how to add a polygon from a GeoDataFrame to an existing folium map. Now it's your turn to practice!