Skip to content

handle list of attr_name in apply/get raster data#303

Merged
wang-boyu merged 1 commit intomesa:mainfrom
wang-boyu:multiple-raster-band-names
Feb 5, 2026
Merged

handle list of attr_name in apply/get raster data#303
wang-boyu merged 1 commit intomesa:mainfrom
wang-boyu:multiple-raster-band-names

Conversation

@wang-boyu
Copy link
Member

This PR improves RasterLayer’s band handling by allowing multiple raster bands to be mapped to cell attribute names.

It extends RasterLayer.apply_raster() / RasterLayer.get_raster() / RasterLayer.from_file() / RasterLayer.to_file() to support multi-band rasters while keeping existing single-band usage unchanged.

Motivation

Mesa-Geo currently assumes raster values map to a single attribute name, which is limiting when reading multi-band datasets (e.g., RGB, multispectral, multiple variables). Users need to manually split bands and call apply_raster() repeatedly.

This PR provides a more convenient way to name and retrieve multi-band rasters.

Changes

Updated API behavior

RasterLayer.apply_raster(data, attr_name=...)

  • Single-band (data.shape == (1, H, W)):

    • attr_name: str behaves as before (creates one attribute).
    • attr_name: None auto-generates an attribute name (existing behavior).
  • Multi-band (data.shape == (B, H, W)):

    • attr_name: list[str] assigns one attribute name per band (length must equal B).
    • attr_name: str uses it as a base name and creates per-band attributes (e.g., elevation_1, elevation_2, ...).
    • attr_name: None auto-generates band names.

RasterLayer.get_raster(attr_name=...)

  • attr_name: str returns a single band as (1, H, W) (as before).
  • attr_name: list[str] returns selected bands stacked as (B, H, W) in the provided name order.
  • attr_name: None returns all stored raster attributes stacked as (B, H, W).

RasterLayer.from_file(..., attr_name=...) and RasterLayer.to_file(..., attr_name=...)

from_file() and to_file() now supports the same attr_name options as apply_raster() for multi-band rasters.

Validation

  • Raises a ValueError when attr_name is a list but its length does not match the number of bands in data.

Backward compatibility

  • Existing single-band workflows using attr_name: str | None continue to work unchanged.
  • No changes required for callers that only use single-band rasters.

@wang-boyu wang-boyu requested review from EwoutH and tpike3 February 4, 2026 22:40
@wang-boyu wang-boyu added the enhancement Release notes label label Feb 4, 2026
@coderabbitai
Copy link

coderabbitai bot commented Feb 4, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

  • 🔍 Trigger a full review
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link

codecov bot commented Feb 4, 2026

Codecov Report

❌ Patch coverage is 92.85714% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.08%. Comparing base (77200e8) to head (c6fe3c4).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
mesa_geo/raster_layers.py 92.85% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #303      +/-   ##
==========================================
+ Coverage   76.34%   78.08%   +1.73%     
==========================================
  Files          10       10              
  Lines         909      940      +31     
  Branches      133      144      +11     
==========================================
+ Hits          694      734      +40     
+ Misses        179      169      -10     
- Partials       36       37       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@EwoutH EwoutH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn’t review the code, but I think the API is sensibel

@wang-boyu
Copy link
Member Author

Thanks!

@wang-boyu wang-boyu merged commit b19c8ba into mesa:main Feb 5, 2026
13 of 14 checks passed
@wang-boyu wang-boyu deleted the multiple-raster-band-names branch February 5, 2026 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants