Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Adding a Layer of Boundaries

00:00 You’ll now see how to add a layer of boundaries to your Folium map through information in a GeoJSON file. So far in this course, you’ve learned how to create base maps with Folium and update their starting locations and zoom levels. Next, you may want to display your own data on top of your map using a color to represent some quantitative values.

00:24 You’ll need to be able to access the boundaries of your geographic entities in order to link your data up with your Folium map.

00:32 The web tiles that currently form your map do show geographic borders, such as the outlines of France or Spain. However, remember that the map that you see is comprised of several web tiles, and that means the borders are also made by stitching tiles together.

00:52 Furthermore, the tiles that are in view depend on the user’s zoom level. All this is to say that unfortunately, the boundaries formed by the web tiles are not accessible to you, so you won’t be able to connect data directly to the web tiles. Instead, you should add a layer of boundaries to your map. So what is a boundary layer?

01:14 A boundary layer provides you with access to information about the borders of your geographic entities, such as the outlines of countries that you see here in blue.

01:25 You can include these boundaries as an additional layer to your Folium map, and you’ll be able to connect these boundaries to your own data and plot representative colors for each region.

01:37 Boundaries like these are typically included through GeoJSON data, and in Folium, you can link a GeoJSON file from your computer or from a URL that provides such a file. By the way, if you’re not familiar with GeoJSON, it’s just a standard way for structuring geographic data.

01:57 It has regular JSON format, and it commonly comes with fields like "type", "geometry", and "properties". The "geometry" field will provide latitude/ longitude coordinates for the geographic entity, which Folium will use to define the boundaries.

02:14 The "properties" field will provide further information, like the name of the entity or its country code and so on. You’ll use these properties to connect with your dataset.

02:26 Now let’s try adding some boundaries to our map of New York City.

02:31 You previously created a Folium map that focused on New York City by updating the location coordinates and the starting zoom level. Now we’re going to add boundaries to this map for each of the five boroughs, or regions, of New York City. To do so, you’ll need a GeoJSON file that defines the borders of each borough, which you can get from the NYC OpenData website.

02:57 Click on Export, and then download the GeoJSON file associated with the borough boundaries.

03:05 For simplicity, go ahead and save that file in your current working directory, so listing out all the files in our current directory shows the BoroughBoundaries.geojson.

03:16 Let’s save the name of that file as the Python variable boroughs_file, and this is what will pass to Folium to add the boundaries of each borough.

03:27 Before doing that, you might be curious about what this file contains. The next few steps are not necessary to add boundaries to your Folium map, but may give you a better sense for how this data is structured.

03:40 Let’s import the GeoJSON library along with the pprint() function from the pretty print library. You can load the boroughs_file with geojson and save it. Now, boroughs_geojson is a GeoJSON object that you can explore. Let’s print out its contents with pretty print.

04:03 You’ll see a GeoJSON structure that looks similar to a Python dictionary. The first field here is features, which contains a list of objects.

04:13 There are many coordinates within this file, and if you scroll all the way down, you’ll see some properties and types.

04:24 There are five items in the features list, one for each borough of New York City, and each item has fields like type, geometry, and properties.

04:35 Let’s look at the properties of the first item in the features list. This item represents Staten Island, so geometry will give us the latitude/ longitude coordinates of the boundaries of Staten Island, and later, when we want to match data up to the Staten Island region, we can do so through properties like the boro_name.

04:59 Okay, now that you know more about what’s in the GeoJSON, how can you use it to add boundaries to your Folium map? You’ll just need to add one line of code.

05:10 Use Folium’s GeoJson() function, and pass the name of the file containing your JSON information. Then Folium tell to add that to your New York City map, where nyc is the Python variable, where you stored your base map. Running that cell, you should now see the boundaries of the five New York City boroughs included on top of your base map. Great work.

05:34 You can still zoom in, as well as pan it, and you should be able to see those boundaries throughout your interactions.

05:43 In this lesson, you learned more about GeoJSON data and added a layer of boundaries to your Folium map. We focused on the boundaries for the five NYC boroughs, but of course, you could add borders for any geo-entities that you have boundary data for, like countries, states, or provinces. Coming up next, you’ll create a choropleth by connecting the boundaries of your GeoJSON file to your own data.

Become a Member to join the conversation.