Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Displaying local and cloud-hosted rasters with appropriate colormaps

Why raster visualization matters

Raster data, satellite imagery, elevation models, model outputs, is the backbone of most GeoAI workflows. Before you can trust a raster in an analysis, you need to see it: does it cover the area you expect, does it have the bands you think it has, and does anything look obviously wrong?


Core idea

leafmap renders rasters as map tile layers. For multi-band imagery it can automatically composite an RGB view; for single-band rasters, you choose a colormap and a value range yourself.


Workflow

1. Get some sample data. This lesson uses four datasets from a Las Vegas building-detection project, hosted publicly on Source Cooperative: swissimage aerial imagery (four bands, 60 cm resolution), a LiDAR-derived HAG raster, building footprint annotations, and a rasterized building mask. The geoai.download_file() function fetches a file only if it is not already present locally.

import geoai

swissimage_url = "https://source.coop/giuz/sds320/L03/data/willisau_2024_swissimage_rgb_subset.tif"
hag_url = "https://source.coop/giuz/sds320/L03/data/willisau_height_above_ground_ndsm.tif”
s2_url = "https://source.coop/giuz/sds320/L03/data/willisau_2026-07-24_sentinel2_subset.tif"

swissimage_path = geoai.download_file(swissimage_url)
hag_path = geoai.download_file(hag_url)
s2_path = geoai.download_file(s2_url)

2. Add a multi-band raster. For imagery like swissimage, add_raster() automatically composites the first three bands as an RGB composite.

import leafmap

m = leafmap.Map()
m.add_raster(swissimage_path, layer_name="swissimage")
m

3. Add a single-band raster with a colormap. For single-band data such as the HAG raster, specify a colormap and a value range with vmin and vmax. Capping the range keeps outliers from washing out the visual contrast you actually care about.

m.add_raster(
    hag_path,
    vmin=0,
    vmax=10,
    colormap="plasma",
    layer_name="Height Above Ground",
)
m

Setting vmax=10 here caps the display at 10 meters so that building-scale structures stand out clearly against the ground, rather than being compressed by a handful of much taller outliers.

4. Stream a Cloud-Optimized GeoTIFF directly. A Cloud Optimized GeoTIFF (COG) can be streamed without downloading the whole file first, which matters once your rasters get large. add_cog_layer() handles this directly from a URL.

m2 = leafmap.Map()
m2.add_cog_layer(swissimage_url, name="Willisau swissimage (streamed)")
m2

5. Choose a band combination. Multispectral imagery becomes more informative once you combine bands deliberately. A true-color composite (red, green, blue) looks like a natural photograph. A false-color composite substitutes an infrared band to highlight features invisible to the human eye, such as vegetation vigor. The indexes parameter in add_raster() controls which bands are used and in what order.

m3 = leafmap.Map()
m3.add_raster(s2_path, indexes=[5, 4, 3], layer_name="False Color")
m3

Placing the NIR band (band 4 in this Sentinel-2 subset) in the green channel makes healthy vegetation appear bright green, which makes it easy to separate vegetated areas from impervious surfaces like roads and rooftops.


Python reactivation

vmin/vmax and indexes are ordinary keyword arguments, the same pattern you used constantly in SDS210 with matplotlib and rasterio. If a raster call is not behaving the way you expect, checking the keyword arguments you passed is usually the fastest place to start.


Common pitfalls


Mini task

Using your own candidate raster from L02 (or the Sentinel-2 sample above if you do not have one yet), add it to a map twice: once as a true-color composite and once as a false-color composite with the infrared band in the red channel. Compare what each reveals.


Key takeaways