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: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: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.
It has regular JSON format, and it commonly comes with fields like
"geometry" field will provide latitude/ longitude coordinates for the geographic entity, which Folium will use to define the boundaries.
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.
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.
Let’s import the GeoJSON library along with the
pprint() function from the pretty print library. You can load the
geojson and save it. Now,
boroughs_geojson is a GeoJSON object that you can explore. Let’s print out its contents with pretty print.
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
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: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.