Overlay A Raster Image With The Component Scattermap (plotly) In Streamlit
Introduction
In this article, we will explore how to overlay a raster image with the component scattermap (Plotly) in Streamlit. This is a common requirement in geospatial data visualization, where we need to display a raster image on top of a scattermap to provide additional context. We will use the Plotly library to create the scattermap and the Streamlit library to create the interactive application.
Problem Statement
When working with geospatial data, it is often necessary to display a raster image on top of a scattermap. However, overlaying a raster image with the component scattermap (Plotly) in Streamlit can be challenging. The raster image may not be well georeferenced, and it may appear on top of the scattermap in an unexpected location.
Solution
To solve this problem, we will use the following approach:
- Create a scattermap using the Plotly library.
- Load the raster image using the
rasterio
library. - Use the
plotly.graph_objects
function to create a new figure that includes the scattermap and the raster image. - Use the
streamlit.plotly
function to display the figure in the Streamlit application.
Step 1: Create a Scattermap using Plotly
First, we need to create a scattermap using the Plotly library. We can use the plotly.graph_objects.Scattermapbox
function to create a scattermap.
import plotly.graph_objects as go

fig = go.Figure(go.Scattermapbox(
lat=[45.123, 45.124, 45.125],
lon=[-73.456, -73.457, -73.458],
mode='markers',
marker=go.scattermapbox.Marker(
size=9
),
text=['Point 1', 'Point 2', 'Point 3']
))
fig.update_layout(
mapbox_style="open-street-map",
mapbox_center_lat=45.123,
mapbox_center_lon=-73.456,
margin="r"
)
Step 2: Load the Raster Image using Rasterio
Next, we need to load the raster image using the rasterio
library. We can use the rasterio.open
function to open the raster image.
import rasterio
with rasterio.open('image.tif') as src:
# Get the image data
image_data = src.read(1)
# Get the image metadata
image_meta = src.meta
Step 3: Create a New Figure with the Scattermap and Raster Image
Now, we need to create a new figure that includes the scattermap and the raster image. We can use the plotly.graph_objects
function to create a new figure.
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scattermapbox(
lat=[45.123, 45.124, 45.125],
lon=[-73.456, -73.457, -73.458],
mode='markers',
marker=go.scattermapbox.Marker(
size=9
),
text=['Point 1', 'Point 2', 'Point 3']
))
fig.add_trace(go.Image(
z=image_data,
lat=image_meta['transform'][0],
lon=image_meta['transform'][1],
lonaxis=image_meta['transform'][2],
lataxis=image_meta['transform'][3]
))
Step 4: Display the Figure in Streamlit
Finally, we need to display the figure in the Streamlit application. We can use the streamlit.plotly
function to display the figure.
import streamlit as st
st.plotly_chart(fig, use_container_width=True)
Conclusion
Q: What is the purpose of overlaying a raster image with the component scattermap (Plotly) in Streamlit?
A: The purpose of overlaying a raster image with the component scattermap (Plotly) in Streamlit is to provide additional context to the scattermap. Raster images can be used to display satellite imagery, aerial photography, or other types of geospatial data that can be overlaid on top of a scattermap.
Q: What are the benefits of using Plotly to create a scattermap?
A: The benefits of using Plotly to create a scattermap include:
- Interactive visualization: Plotly allows users to interact with the scattermap by zooming in and out, panning, and hovering over points.
- Customizable appearance: Plotly provides a wide range of customization options for the scattermap, including colors, markers, and layouts.
- Integration with Streamlit: Plotly can be easily integrated with Streamlit to create interactive web applications.
Q: What are the benefits of using Streamlit to create an interactive application?
A: The benefits of using Streamlit to create an interactive application include:
- Easy to use: Streamlit provides a simple and intuitive API for creating interactive applications.
- Fast development: Streamlit allows developers to quickly create and deploy interactive applications.
- Integration with Plotly: Streamlit can be easily integrated with Plotly to create interactive web applications.
Q: How do I load a raster image using the rasterio
library?
A: To load a raster image using the rasterio
library, you can use the rasterio.open
function. Here is an example:
import rasterio
with rasterio.open('image.tif') as src:
# Get the image data
image_data = src.read(1)
# Get the image metadata
image_meta = src.meta
Q: How do I create a new figure with the scattermap and raster image using Plotly?
A: To create a new figure with the scattermap and raster image using Plotly, you can use the plotly.graph_objects
function. Here is an example:
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scattermapbox(
lat=[45.123, 45.124, 45.125],
lon=[-73.456, -73.457, -73.458],
mode='markers',
marker=go.scattermapbox.Marker(
size=9
),
text=['Point 1', 'Point 2', 'Point 3']
))
fig.add_trace(go.Image(
z=image_data,
lat=image_meta['transform'][0],
lon=image_meta['transform'][1],
lonaxis=image_meta['transform'][2],
lataxis=image_meta['transform'][3]
))
Q: How do I display the figure in Streamlit?
A: To display the figure in Streamlit, you can use the streamlit.plotly
function. Here is an example:
import streamlit as st
st.plotly_chart(fig, use_container_width=True)
Conclusion
In this Q&A article, we have answered common questions about overlaying a raster image with the component scattermap (Plotly) in Streamlit. We have covered topics such as the purpose of overlaying a raster image, the benefits of using Plotly and Streamlit, and how to load a raster image, create a new figure, and display the figure in Streamlit.