Skip to content

Comments

Use mask as alpha when reprojecting a masked array#3156

Merged
sgillies merged 6 commits intomainfrom
issue2575
Aug 30, 2024
Merged

Use mask as alpha when reprojecting a masked array#3156
sgillies merged 6 commits intomainfrom
issue2575

Conversation

@sgillies
Copy link
Member

Background: reproject() can take an array and return an array. GDAL's warper requires input and output GDALDatasetH, so we construct them using the GDAL "MEM" driver, pointing at the memory location of two arrays.

What this PR does is add alpha bands to those datasets when the inputs and outputs are masked arrays. For input, 3-D array masks are reduced to a 2-D array via logical_or() (because GDAL needs a single alpha mask channel). For output, the alpha mask band is repeated over the number of image bands.

I suspect the approach could be made more efficient. Copying is required in some cases because the "MEM" driver needs a precise memory layout.

Resolves #2575.

@sgillies sgillies added this to the 1.4.0 milestone Aug 29, 2024
@sgillies sgillies self-assigned this Aug 29, 2024
crs=src_crs,
copy=True)

if hasattr(source, "mask"):
Copy link
Member

Choose a reason for hiding this comment

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

we have twice the if hasattr(source, "mask") test, could those block be merged together?

Copy link
Member Author

Choose a reason for hiding this comment

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

@vincentsarago except that the first block needs to run before the input is reshaped and the second block needs to run after the input is reshaped. It's a bit gross, but I'm inclined to let it go for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

warp.reproject does not handle masked arrays properly

2 participants