Skip to content

Add GeoJSON vector data support, STAC layer support, and comprehensive tests#46

Merged
giswqs merged 4 commits into
masterfrom
feature/geojson-and-improvements
Jan 31, 2026
Merged

Add GeoJSON vector data support, STAC layer support, and comprehensive tests#46
giswqs merged 4 commits into
masterfrom
feature/geojson-and-improvements

Conversation

@giswqs
Copy link
Copy Markdown
Member

@giswqs giswqs commented Jan 31, 2026

Summary

This PR adds GeoJSON vector data loading support (issue #4), STAC raster layer support (issue #3), and rewrites the test suite from scratch.


GeoJSON Support (closes #4 — partial: GeoJSON for MapLibre)

Added add_geojson() method to the MapLibre Map class with the following features:

Input formats supported:

  • GeoJSON dictionary (FeatureCollection, Feature, or bare Geometry)
  • Local file path (.geojson, .json)
  • URL string (fetched client-side via JS fetch())
  • Any object with __geo_interface__ (e.g., GeoPandas GeoDataFrame)

Capabilities:

  • Auto-detects geometry types (Point, LineString, Polygon, Multi* variants) and creates the appropriate MapLibre layer type (circle, line, fill)
  • For mixed-geometry FeatureCollections, creates multiple filtered layers automatically
  • Computes bounding box and auto-fits the map (opt-out via fit_bounds=False)
  • Supports custom paint, layout, source_options (e.g., clustering), before_id
  • Sensible default styling for each layer type

Example usage:

from mapwidget.maplibre import Map

m = Map(center=[-77.03, 38.90], zoom=10)

# From a dict
m.add_geojson({"type": "FeatureCollection", "features": [...]})

# From a URL
m.add_geojson("https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_110m_admin_0_countries.geojson")

# From a file
m.add_geojson("data/parcels.geojson")

# From a GeoDataFrame
import geopandas as gpd
gdf = gpd.read_file("data/roads.shp")
m.add_geojson(gdf, paint={"line-color": "red", "line-width": 2})

Note: This addresses the GeoJSON portion of #4 for the MapLibre backend. Shapefile support works indirectly via GeoDataFrame. Full Shapefile/GeoDataFrame direct loading for other backends (Cesium, Mapbox) is left for future work.


STAC Layer Support (addresses #3)

Added add_stac_layer() method that:

  • Fetches a STAC Item JSON from a URL
  • Extracts the specified asset key's COG href (defaults to "visual")
  • Loads it as a raster layer using the existing maplibre-cog-protocol
  • Auto-fits the map to the STAC item's bounding box

Example usage:

m.add_stac_layer(
    "https://planetarycomputer.microsoft.com/api/stac/v1/collections/sentinel-2-l2a/items/...",
    asset_key="visual",
    layer_options={"paint": {"raster-opacity": 0.8}},
)

COG support was already implemented in add_cog_layer(). This builds on top of it for the STAC use case referenced in #3.


Test Suite

Rewrote tests/test_mapwidget.py from a single placeholder test to 33 comprehensive tests covering:

  • TestMapLibreMap — initialization, add_call, navigation methods, source/layer/control management
  • TestAddGeoJSON — dict, file, URL, custom IDs, custom paint, error handling (TypeError, FileNotFoundError)
  • TestComputeBounds — points, polygons, FeatureCollections, empty data, URL passthrough
  • TestGetGeometryTypes — single type, mixed types, URL passthrough
  • TestAddCogLayer — default and custom IDs
  • TestAddStacLayer — default and custom IDs

All 33 tests pass.


Files Changed

  • mapwidget/maplibre.py — Added add_geojson(), add_stac_layer(), and helper methods
  • mapwidget/js/maplibre.js — Added addGeoJSONToMap(), addStacLayer(), computeGeoJSONBounds() JS handlers
  • tests/test_mapwidget.py — Complete rewrite with 33 tests

Refs: #3, #4

giswqs and others added 2 commits January 31, 2026 01:43
…e tests

- Add add_geojson() method to MapLibre Map class (addresses #4)
  - Supports GeoJSON dict, file path, URL, and GeoDataFrame input
  - Auto-detects geometry types and creates appropriate layers
  - Auto-fits map bounds to the data extent
  - Supports custom paint/layout properties, clustering options
  - Creates multiple filtered layers for mixed-geometry FeatureCollections

- Add add_stac_layer() method for STAC item support (addresses #3)
  - Fetches STAC item JSON from URL, extracts asset COG href
  - Leverages existing COG protocol for raster rendering
  - Auto-fits map to STAC item's bounding box

- Add corresponding JavaScript handlers (addGeoJSON, addStacLayer)
  - JS-side URL fetching for remote GeoJSON with auto-bounds
  - JS-side STAC item fetching with asset extraction
  - Helper for computing GeoJSON bounds on the JS side

- Rewrite test suite with 33 comprehensive tests covering:
  - MapLibre Map initialization and basic methods
  - GeoJSON loading from dict, file, URL with various options
  - Bounds computation and geometry type detection
  - COG and STAC layer methods
  - Error handling for unsupported types and missing files
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jan 31, 2026

@github-actions github-actions Bot temporarily deployed to pull request January 31, 2026 06:44 Inactive
@github-actions github-actions Bot temporarily deployed to pull request January 31, 2026 15:01 Inactive
@giswqs giswqs merged commit 9448795 into master Jan 31, 2026
10 checks passed
@giswqs giswqs deleted the feature/geojson-and-improvements branch January 31, 2026 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for loading vector data (GeoJSON, Shapefile, GeoDataFrame, etc.)

1 participant