/**************************************************************************** Copyright (C) 2010-2012 the Office National des Forêts (ONF), France All rights reserved. Contact : alexandre.piboule@onf.fr Developers : Alexandre PIBOULE (ONF) This file is part of PluginONF library. PluginONF is free library: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. PluginONF is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with PluginONF. If not, see . *****************************************************************************/ #include "onf_steprasterquantileresampling.h" #include "ct_log/ct_logmanager.h" #include "ct_math/ct_mathstatistics.h" ONF_StepRasterQuantileResampling::ONF_StepRasterQuantileResampling() : SuperClass() { _outResolution = 5.0; _quantile = 0.95; _name = tr("Raster %1 m").arg(QString::number(_outResolution, 'f', 2)); _extend = false; } ONF_StepRasterQuantileResampling::~ONF_StepRasterQuantileResampling() { qDeleteAll(_outRasterModelMap.values()); } QString ONF_StepRasterQuantileResampling::description() const { return tr("Rééchantillonnage par quantile"); } QString ONF_StepRasterQuantileResampling::detailledDescription() const { return tr("Cette étape permet de rééchantillonner un raster en fonction d'un quantile."); } QString ONF_StepRasterQuantileResampling::inputDescription() const { return SuperClass::inputDescription() + tr("

"); } QString ONF_StepRasterQuantileResampling::outputDescription() const { return SuperClass::outputDescription() + tr("

"); } QString ONF_StepRasterQuantileResampling::detailsDescription() const { return tr(""); } CT_VirtualAbstractStep* ONF_StepRasterQuantileResampling::createNewInstance() const { // cree une copie de cette etape return new ONF_StepRasterQuantileResampling(); } /////////////////////// PROTECTED /////////////////////// void ONF_StepRasterQuantileResampling::declareInputModels(CT_StepInModelStructureManager& manager) { manager.addResult(_inResultRaster, tr("Raster")); manager.setZeroOrMoreRootGroup(_inResultRaster, _inZeroOrMoreRootGroupRaster); manager.addGroup(_inZeroOrMoreRootGroupRaster, _inGroupRaster); manager.addItem(_inGroupRaster, _inRaster, tr("Raster")); } void ONF_StepRasterQuantileResampling::declareOutputModels(CT_StepOutModelStructureManager& manager) { manager.addResultCopy(_inResultRaster); if (_inRaster.hasAtLeastOnePossibilitySelected(_inResultRaster)) { auto it = _inRaster.iterateSelectedOutputModels(_inResultRaster); auto begin = it.begin(); auto end = it.end(); while(begin != end) { const CT_OutAbstractSingularItemModel* inRasterModel = (*begin); CT_HandleOutSingularItem >* handle = new CT_HandleOutSingularItem >(); _outRasterModelMap.insert(inRasterModel->recursiveOriginalModel(), handle); _name = tr("%1 %2 m").arg(inRasterModel->displayableName()).arg(QString::number(_outResolution, 'f', 2)); manager.addItem(_inGroupRaster, *handle, _name); ++begin; } } } void ONF_StepRasterQuantileResampling::fillPostInputConfigurationDialog(CT_StepConfigurableDialog* postInputConfigDialog) { postInputConfigDialog->addDouble(tr("Résolution de sortie"), "m", 0.01, 999999, 2, _outResolution, 1, tr("La résolution choisie doit être plus grande que celle du raster d'entrée")); postInputConfigDialog->addDouble(tr("Quantile"), "%", 1, 100, 0, _quantile, 100, tr("Quantile pour calculer la synthèse des pixels d'entrée inclus dans chaque pixel de sortie")); postInputConfigDialog->addBool(tr("Etendre le raster si nécessaire"), "", "", _extend, tr("Que faire si la résolution de sortie n'est pas un multiple de la résolution d'entrée :
" "- Vrai : le raster de sortie sera un peu plus étendu que celui d'entrée
" "- Faux : le raster de sortie sera un peu moins étendu que celui d'entrée
")); } void ONF_StepRasterQuantileResampling::compute() { for (CT_StandardItemGroup* group : _inGroupRaster.iterateOutputs(_inResultRaster)) { for (const CT_Image2D* inRaster : group->singularItems(_inRaster)) { if (isStopped()) {return;} double minX = inRaster->minX(); double minY = inRaster->minY(); int dimX = std::floor((inRaster->maxX() - inRaster->minX()) / _outResolution); int dimY = std::floor((inRaster->maxY() - inRaster->minY()) / _outResolution); if (_extend) { dimX = std::ceil((inRaster->maxX() - inRaster->minX()) / _outResolution); dimY = std::ceil((inRaster->maxY() - inRaster->minY()) / _outResolution); } CT_Image2D* outRaster = new CT_Image2D(minX, minY, dimX, dimY, _outResolution, inRaster->minZ(), inRaster->NA(), inRaster->NA()); std::vector > values(outRaster->nCells()); size_t ncells = inRaster->nCells(); for (size_t index = 0 ; index < ncells ; index++) { Eigen::Vector3d center; inRaster->getCellCenterCoordinates(index, center); float value = inRaster->valueAtIndex(index); if (!qFuzzyCompare(value, inRaster->NA())) { size_t outIndex; if (outRaster->indexAtCoords(center(0), center(1), outIndex)) { values[outIndex].append(value); } } setProgress(float(60.0*index/ncells)); } ncells = outRaster->nCells(); for (size_t index = 0 ; index < ncells ; index++) { QList &cellValues = values[index]; if (!cellValues.isEmpty()) { outRaster->setValueAtIndex(index, CT_MathStatistics::computeQuantile(cellValues, _quantile, true)); } setProgress(60.0 + float(30.0*index/ncells)); } // ajout du raster CT_OutAbstractSingularItemModel* itemModel = dynamic_cast(inRaster->model()->recursiveOriginalModel()); CT_HandleOutSingularItem >* handle = _outRasterModelMap.value(itemModel); if (handle != nullptr) { group->addSingularItem(*handle, outRaster); outRaster->computeMinMax(); } else { qDebug() << "Erreur ONF_StepRasterQuantileResampling : cas non prévu !"; PS_LOG->addMessage(LogInterface::error, LogInterface::step, tr("Erreur ONF_StepRasterQuantileResampling : cas non prévu !")); delete outRaster; } } setProgress(100.0f); } }