In this tutorial, we demonstrate how to overlay vector data (points, lines, and polygons) onto a basemap using the Python library Folium. We show how to work with two common types of vector data files: GeoJSON and Shapefile. We use a GeoJson file of USA state boundaries, and a Shapefile of counties in the state of Colorado.
This article includes an attached companion notebook that you may download and run in EarthAI Notebook.
Import Libraries
We will start by importing the necessary Python libraries.
import folium import geopandas
Create a Basemap
Folium is a Python library that allows users to visualize data on an interactive Leaflet map.
We creata a default basemap object using folium.Map
, without passing in any parameters to the function. The basemap can be modified in a number of ways. Pressing Shift+Tab in the parentheses of folium.Map
displays the range of parameter options that can be adjusted. Typing the variable name produces an interactive basemap in the notebook.
map = folium.Map() map

Add a GeoJSON Vector Layer to the Basemap
Next we'll overlay the US state boundaries to the basemap and display it. For this we use folium.GeoJson
and pass in the path to the GeoJSON file. In this case it is a URL. We then chain the add_to
method and pass in the variable name of our basemap. Finally, we display the interactive map with our vector layer with a simple variable call.
folium.GeoJson('https://raw.githubusercontent.com/datasets/geo-admin1-us/master/data/admin1-us.geojson').add_to(map) map

In this case we just passed the full URL to the location of the geojson file of interest to folium.GeoJson
. If your vector data is stored locally you would replace the URL with the path to the file on your computer or in your EarthAI Notebook workspace.
You can use the controls in the upper left corner of the map widget to zoom in and zoom out, and Click+Drag to pan around the map.
Add a Shapefile Vector Layer to the Basemap
Open a Shapefile using GeoPandas
Folium does not handle shapefile data directly, so we first read the shapefile in via GeoPandas then add the vector data to the basemap via folium.
We use the geopandas.read_file
method to open a Shapefile of the boundaries of Colorado's counties. The read_file
method is very versatile and can take a range of file types as inputs. In this case the Shapefile of interest resides in a zip file.
counties_gdf = geopandas.read_file('https://storage.googleapis.com/co-publicdata/lm_cnty.zip')
We now have a GeoDataFrame without having to unzip the file.
Add the Geometry from a GeoDataFrame to a Basemap
To add the Colorado county boundaries to our basemap, we need to identify the name of the geometry column of our GeoDataFrame. We can do this with the head
method. The column of interest has point, line, polygon, or multipolygon labels followed by numerical coordinates. Below, we can see that the geometry column is named geometry.
counties_gdf.head()

We can now make use of the folium.GeoJson
method and pass in the geometry column of our GeoDataFrame, followed by the add_to
method to add this layer to map. Displaying the map again, we can see that the county boundaries of Colorado are now on our interactive basemap.
folium.GeoJson(data=counties_gdf["geometry"]).add_to(map) map

Comments
0 comments
Please sign in to leave a comment.