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.

Adding and styling GeoJSON, GeoDataFrames, and point markers on top of raster imagery

Open In Colab

1. Motivation

Vector data carries the labels, boundaries, and outputs that give raster imagery meaning: building footprints, glacier outlines, detected objects. Overlaying vector features on their source imagery is the standard way to check whether annotations actually line up with what is on the ground, which matters both for your own labels and for anything you download from elsewhere.


2. Core idea

leafmap accepts vector data from files, URLs, or in-memory GeoDataFrames, and gives you direct control over how each feature is styled.


3. Workflow

A. Prepare sample data

This page uses the same Willisau building sample as the raster page.

B. Load vector data as a GeoDataFrame

This keeps you in familiar territory from SDS210: read a vector file with geopandas, inspect it, then hand it to leafmap.

Features: 327
Loading...

Before mapping, check whether the layer has a CRS and expected geometry type.

CRS: EPSG:2056
Geometry types:
Polygon    327
Name: count, dtype: int64

C. Overlay it on the source imagery

Adding vector labels directly on top of the raster they came from is the fastest way to catch a misaligned or incomplete annotation.

Loading...

D. Add vector data directly from a URL

You do not always need to load a file into a GeoDataFrame first; add_geojson() works directly from a URL, if the dataset is projected in geographic coordinates EPSG:4326.

Warning: The dataset does not have a CRS defined. Assuming EPSG:4326.
Loading...

E. Style vector layers deliberately

A style dictionary follows the Leaflet path-options convention: color (outline), weight (outline width), fillColor, and fillOpacity.

Loading...

F. Add point markers

For point data, such as sample locations or points of interest, use add_marker(). For datasets with many points, consider marker clustering so the map stays readable at broader zoom levels.

Loading...

4. Python reactivation

The style dictionary here is an ordinary Python dictionary with string keys, the same structure you used for project parameters in SDS210. If a style is not applying, check for a typo in a key name (fillColor, not fillcolor) before assuming something is broken with the map itself.


5. Common pitfalls

  • Assuming default styling is good enough for a final figure. The default outline and fill are fine for quick checks, but a final report figure usually needs deliberate color and opacity choices.

  • Forgetting zoom_to_layer=True. Without it, the map may stay at its previous view and your new vector layer can be off-screen.

  • Overlaying too many points without clustering. A dense point layer without clustering becomes unreadable at low zoom levels.


6. Mini task

Load a vector dataset relevant to your project (or the Willisau buildings example above), overlay it on its source imagery, and apply a custom style that makes the features stand out clearly against the basemap.

Loading...

A thin, high-contrast outline with a low fill opacity keeps individual building shapes visible while still letting the underlying imagery show through, which is often more useful for visual quality control than a solid fill.


---

## 7. Key takeaways

- `add_gdf()`, `add_geojson()`, and `add_marker()` cover the common ways of putting vector data on a `leafmap` map.
- A style dictionary controls outline color, outline width, fill color, and fill opacity.
- Overlaying vector labels on their source imagery is the standard way to check annotation quality before relying on them.
- Point layers with many features benefit from clustering to stay readable at broader zoom levels.