Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PyQGIS] as_numpy(use_masking=True) doesn't work well #60205

Closed
2 tasks done
kadyb opened this issue Jan 21, 2025 · 1 comment · Fixed by #60277
Closed
2 tasks done

[PyQGIS] as_numpy(use_masking=True) doesn't work well #60205

kadyb opened this issue Jan 21, 2025 · 1 comment · Fixed by #60277
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! PyQGIS Related to the PyQGIS API

Comments

@kadyb
Copy link

kadyb commented Jan 21, 2025

What is the bug or the crash?

I noticed two problems with as_numpy(use_masking=True):

  1. In case of QgsRasterLayer it doesn't return masked array.
  2. In case of QgsRasterBlock it doesn't use the correct fill value (should be 9999.0 in the example below).

Steps to reproduce the issue

Download this sample dataset from the repository: https://github.com/kadyb/adg2024/blob/main/dane/DEM.tif
Run the code below:

from qgis.core import QgsRasterLayer
import numpy.ma as ma

raster = QgsRasterLayer("DEM.tif", "DEM")
print(raster.isValid())
#> True
print(raster.dataProvider().sourceNoDataValue(1))
#> 9999.0

## QgsRasterLayer
mat = raster.as_numpy(use_masking = True)
print(ma.isMaskedArray(mat))
#> False

## QgsRasterBlock
block = raster.dataProvider().block(1, raster.extent(), raster.width(), raster.height())
print(block.isValid())
#> True
mat = block.as_numpy(use_masking = True)
print(ma.isMaskedArray(mat))
#> True
print(mat.fill_value)
#> 0.0   # should be 9999.0

Versions

QGIS 3.40.0, Windows 10

Supported QGIS version

  • I'm running a supported QGIS version according to the roadmap.

New profile

Additional context

No response

@kadyb kadyb added the Bug Either a bug report, or a bug fix. Let's hope for the latter! label Jan 21, 2025
@agiudiceandrea agiudiceandrea added the PyQGIS Related to the PyQGIS API label Jan 21, 2025
@agiudiceandrea
Copy link
Member

Gentle ping @merydian.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! PyQGIS Related to the PyQGIS API
Projects
None yet
2 participants