Interactive Geospatial AI Visualization in Jupyter Notebook | by Juan Nathaniel | Towards Data Science

Getting to know your data is key to building and deploying a robust AI/ML system in production. It is, therefore, imperative to perform a good Exploratory Data Analysis (EDA) beforehand prior to formulating an AI/ML solution. However, performing an EDA on Geospatial dataset may seem daunting and, often times, challenging, especially if you have to navigate through vast areas and many data layers for analysis. Fret not, with sufficient practice, you will get the hang of it and this post will hopefully help you get started on that!
Table of Content:
InstallationIntroduction to WidgetCreating a Simple MapCustomizing Your MapIntroduction to Layers, Raster, and VectorAdding Layer
Installation
Introduction to Widget
Creating a Simple Map
Customizing Your Map
Introduction to Layers, Raster, and Vector
Adding Layer
For this tutorial on interactive geospatial data visualization in Jupyter Notebook, we are going to install ipyleaflet library.
conda install -c conda-forge ipyleaflet
Leaflet is one of the most popular JavaScript libraries for visualizing geospatial data. It is widely adopted by large institutions such as OpenStreetMap and MapBox.
If you are looking for alternative to ipyleaflet, you can consider folium python package to assist you in the visualization of an interactive leaflet map.
Now that we have covered the setting up phase, let’s deep dive into the topic for this post.
What do you mean by interactive visualization in Jupyter? How can a widget achieve this? Most importantly, what is a widget?
These questions will inevitably pop up when you start reading this post. For those of you who are familiar, take the next section as a quick refresher.
Interactive Visualization? it simply means the ability to interact with your visualization in real-time. Take the following GIF for example. You are able to interact with the slider through a visual mean.
Jupyter Widget? they are called “special objects” that can be initialized and displayed on the notebook. They are also bidirectional, meaning that a widget does not just display, but can take in user inputs that will subsequently trigger new computation. The most important thing is that they are extensible. Jupyter widget allows for other libraries to build additional features on top of its functionality, such as, our ipyleaflet.
Now, we are ready to handle ipyleaflet. To start with, lets display a basic map of the world.
First, we will need to import the ipyleaflet library and its Map object.
from ipyleaflet import Map
Then, we build and visualize the Map object given the coordinates of the center point and the zoom levels.
Map(center = (60, -2.2), zoom = 2, min_zoom = 1, max_zoom = 20)
In the GIF above, you are able to interact with the newly created map by scrolling and panning around the output visuals. Nicely done!
We can also customize our map by styling the basemap, for example.
First, we import another object called basemaps.
from ipyleaflet import Map, basemaps
Then we add in visualization to our Map object as follows.
map = Map(center=(60,-2.2), zoom=2, basemap=basemaps.Stamen.Terrain)
The map becomes prettier with the customized terrain basemaps.
There are plenty of customization available. Do checkout their API reference for a more detailed explanation.
Now, this will be where most of your EDA will take place. I’m going to showcase how to add layers and how to manipulate them.
But first thing first.
What are Layers? Layers are used to display geographic datasets. Their purpose is to separate different geographic information that may or may not correlate with each other. To showcase a map of a city, for example, you might want to display a layer of the basemap (highlighted earlier), the road network in the city, the housing places, the elevation level, and the presence of water bodies & recreational areas.
Now, What is a Raster or Vector? Raster data is stored as a grid of values (think of an image with pixels) while vector data represents a specific feature such as a point, line, or polygon.
The figure below provides a handy illustration to know what are layers, and the difference between raster / vector data.
If you want a more detailed explanation on this topic, do checkout my other blog post below.
towardsdatascience.com
Last but not least, an interactive geospatial map is incomplete without the ability to add your own layers.
Let’s first add a Raster layer by calling the add_layer() method.
from ipyleaflet import basemap_to_tiles as btt, basemaps, Mapraster = btt(basemaps.NASAGIBS.ModisTerraTrueColorCR, "2019-06-24")map.add_layer(raster)map
If you choose to remove this layer, then you can simply call the remove_layer() method.
map.remove_layer(raster)map
But what if you do not want to keep adding or removing layers by writing a code. You can do so on the map itself by adding a layer control.
from ipyleaflet import LayersControlmap.add_control(LayersControl())
You can now have more controls over your layer in the map itself!
That’s it for an introduction to interactive geospatial visualization on Jupyter Notebook. I will create a second part soon to discuss more advanced techniques such as map splitting, adding data from GeoJSON and other file format, etc. Stay tuned!
Do subscribe to my Email newsletter: https://tinyurl.com/2npw2fnz where I regularly summarize AI research papers in plain English and beautiful visualization.