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

QgsZonalStatistics.calculateStatistics() incomplete outputs #43245

Closed
nicogodet opened this issue May 17, 2021 · 4 comments · Fixed by #43332
Closed

QgsZonalStatistics.calculateStatistics() incomplete outputs #43245

nicogodet opened this issue May 17, 2021 · 4 comments · Fixed by #43332
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Processing Relating to QGIS Processing framework or individual Processing algorithms PyQGIS Related to the PyQGIS API

Comments

@nicogodet
Copy link
Member

Describe the bug
Input:

QgsZonalStatistics.calculateStatistics(
  raster.dataProvider(),
  feat.geometry(),
  raster.rasterUnitsPerPixelX(),
  raster.rasterUnitsPerPixelY(),
  1,
  QgsZonalStatistics.Statistic.All
)

Output:

{0: 6.324437375868002}

The dictionary only contains Variance.
Since I specify QgsZonalStatistics.Statistic.All, output should contains all stats.

If I pass a specific stat like QgsZonalStatistics.Statistic.Mean or QgsZonalStatistics.Statistic.Median, the output is correct.
If I build the argument to contain only 2 stats, output is wrong again and contains the only the last stat listed in All.
All = Count | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety | Variance
Mix of Mean and Median will return Median, Median and Max return Max, ...
Examples:
Input:

stats = QgsZonalStatistics.Statistic(QgsZonalStatistics.Statistic.Median | QgsZonalStatistics.Statistic.Mean)
# Both
QgsZonalStatistics.calculateStatistics(
  raster.dataProvider(),
  feat.geometry(),
  raster.rasterUnitsPerPixelX(),
  raster.rasterUnitsPerPixelY(),
  1,
  stats
)
# Only Median
QgsZonalStatistics.calculateStatistics(
  raster.dataProvider(),
  feat.geometry(),
  raster.rasterUnitsPerPixelX(),
  raster.rasterUnitsPerPixelY(),
  1,
  QgsZonalStatistics.Statistic.Median
)

Output:

# Both
{0: 4.880000114440918}
# Only median
{0: 4.880000114440918}

Input:

stats = QgsZonalStatistics.Statistic(QgsZonalStatistics.Statistic.Median | QgsZonalStatistics.Statistic.Max)
# Both
QgsZonalStatistics.calculateStatistics(
  raster.dataProvider(),
  feat.geometry(),
  raster.rasterUnitsPerPixelX(),
  raster.rasterUnitsPerPixelY(),
  1,
  stats
)
# Only Max
QgsZonalStatistics.calculateStatistics(
  raster.dataProvider(),
  feat.geometry(),
  raster.rasterUnitsPerPixelX(),
  raster.rasterUnitsPerPixelY(),
  1,
  QgsZonalStatistics.Statistic.Max
)

Output:

# Both
{0: 24.649999618530273}
# Only Max
{0: 24.649999618530273}

In addition, the key corresponds to nothing. It could be the integer set here : https://qgis.org/api/qgszonalstatistics_8h_source.html#l00051

@nicogodet nicogodet added the Bug Either a bug report, or a bug fix. Let's hope for the latter! label May 17, 2021
@gioman gioman added Processing Relating to QGIS Processing framework or individual Processing algorithms PyQGIS Related to the PyQGIS API labels May 17, 2021
@roya0045
Copy link
Contributor

Is the OR operator truly the intended way to list things?

@gioman gioman added the Feedback Waiting on the submitter for answers label May 17, 2021
@nicogodet
Copy link
Member Author

https://github.com/qgis/QGIS/blob/40988a8901bc746b4b5dc5601a01d03df1aafd65/src/analysis/processing/qgsalgorithmzonalstatisticsfeaturebased.cpp#L166L170

When the same function is called in a C++ script, it works fine.
But from Python, something fails...

@gioman gioman removed the Feedback Waiting on the submitter for answers label May 17, 2021
@roya0045
Copy link
Contributor

Possibly because they should be enum and python is not like c++ with stacking enums?

@nicogodet
Copy link
Member Author

nicogodet commented May 17, 2021

Or because QMap<QgsZonalStatistics::Statistic, QVariant> is not defined here : https://github.com/qgis/QGIS/blob/master/python/core/conversions.sip ?

Because stacking enums seems properly handled but it looks like the issue comes from the conversion from QMap to Dict

elpaso added a commit to elpaso/QGIS that referenced this issue May 21, 2021
Fixes qgis#43245

SIP failed to convert the QMap<QgsZonalStatistics::Statistic, QVariant>
so we give it an QMap<int, QVariant>.
nyalldawson pushed a commit that referenced this issue May 21, 2021
Fixes #43245

SIP failed to convert the QMap<QgsZonalStatistics::Statistic, QVariant>
so we give it an QMap<int, QVariant>.
nicogodet pushed a commit to nicogodet/QGIS that referenced this issue May 25, 2021
Fixes qgis#43245

SIP failed to convert the QMap<QgsZonalStatistics::Statistic, QVariant>
so we give it an QMap<int, QVariant>.
nyalldawson pushed a commit that referenced this issue May 31, 2021
Fixes #43245

SIP failed to convert the QMap<QgsZonalStatistics::Statistic, QVariant>
so we give it an QMap<int, QVariant>.
nyalldawson pushed a commit that referenced this issue Jun 14, 2021
Fixes #43245

SIP failed to convert the QMap<QgsZonalStatistics::Statistic, QVariant>
so we give it an QMap<int, QVariant>.
nyalldawson pushed a commit that referenced this issue Jun 21, 2021
Fixes #43245

SIP failed to convert the QMap<QgsZonalStatistics::Statistic, QVariant>
so we give it an QMap<int, QVariant>.
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! Processing Relating to QGIS Processing framework or individual Processing algorithms PyQGIS Related to the PyQGIS API
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants