/********************************************************************** jlstatprofile_lib.cc: program to calculate statistics in temporal or spectral profile Author(s): Pieter.Kempeneers@ec.europa.eu Copyright (C) 2016-2020 European Union (Joint Research Centre) This file is part of jiplib. jiplib is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. jiplib 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 General Public License along with jiplib. If not, see . ***********************************************************************/ #include #include #include #include // #include #include #include #include #include "base/Optionjl.h" #include "base/Vector2d.h" #include "algorithms/Filter2d.h" #include "algorithms/Filter.h" #include "imageclasses/Jim.h" #include "imageclasses/JimList.h" #include "algorithms/StatFactory.h" #include "apps/AppFactory.h" #include "config_jiplib.h" using namespace std; using namespace app; /** * @param function (type: std::string) Statistics function (mean, median, var, stdev, min, max, sum, mode (provide classes), minindex, maxindex, proportion (provide classes), percentile, nvalid * @param perc (type: double) Percentile value(s) used for rule percentile * @param otype (type: std::string) (default: GDT_Unknown) Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image * @return shared pointer to image object **/ shared_ptr JimList::statProfile(app::AppFactory& app){ shared_ptr imgWriter=Jim::createImg(); statProfile(*imgWriter, app); return(imgWriter); } /** * @param function (type: std::string) Statistics function (mean, median, var, stdev, min, max, sum, mode (provide classes), minindex, maxindex, proportion (provide classes), percentile, nvalid * @param perc (type: double) Percentile value(s) used for rule percentile * @param otype (type: std::string) (default: GDT_Unknown) Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image * @param imgWriter output raster profile dataset * @param app application specific option arguments * @return CE_None if successful, CE_Failure if failed **/ void JimList::statProfile(Jim& imgWriter, app::AppFactory& app){ Optionjl function_opt("f", "function", "Statistics function (mean, median, var, stdev, min, max, sum, mode (provide classes), minindex, maxindex, proportion (provide classes), percentile, nvalid"); Optionjl percentile_opt("perc","perc","Percentile value(s) used for rule percentile",90); // Optionjl class_opt("class", "class", "class value(s) to use for mode, proportion"); Optionjl nodata_opt("nodata", "nodata", "nodata value"); Optionjl otype_opt("ot", "otype", "Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image","GDT_Unknown"); // Optionjl down_opt("d", "down", "down sampling factor. Use value 1 for no downsampling). Use value n>1 for downsampling (aggregation)", 1); Optionjl verbose_opt("v", "verbose", "verbose mode if > 0", 0,2); // percentile_opt.setHide(1); // class_opt.setHide(1); otype_opt.setHide(1); bool doProcess;//stop process when program was invoked with help option (-h --help) try{ doProcess=function_opt.retrieveOption(app); percentile_opt.retrieveOption(app); nodata_opt.retrieveOption(app); otype_opt.retrieveOption(app); verbose_opt.retrieveOption(app); if(!doProcess){ cout << endl; std::ostringstream helpStream; helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; throw(helpStream.str());//help was invoked, stop processing } std::vector badKeys; app.badKeys(badKeys); if(badKeys.size()){ std::ostringstream errorStream; if(badKeys.size()>1) errorStream << "Error: unknown keys: "; else errorStream << "Error: unknown key: "; for(int ikey=0;ikey parallel_percentile;//used for parallel processing if(function_opt.countSubstring("percentile")){ while(function_opt.countSubstring("percentile")::const_iterator percit=percentile_opt.begin(); for(int ifunction=0;ifunctionfront()->getGDALDataType(); if(verbose_opt[0]>1) std::cout << std::endl << "Output pixel type: " << GDALGetDataTypeName(theType) << endl; if(verbose_opt[0]>1) cout << "Calculating statistic metrics: " << function_opt.size() << endl; // imgWriter.open(output_opt[0],this->nrOfCol(),this->nrOfRow(),function_opt.size(),theType,imageType,memory_opt[0],option_opt); //todo: expand for collections that have different image dimensions and geotransforms? imgWriter.open(this->front()->nrOfCol(),this->front()->nrOfRow(),function_opt.size(),theType); imgWriter.setProjection(this->front()->getProjection()); double gt[6]; this->front()->getGeoTransform(gt); imgWriter.setGeoTransform(gt); if(nodata_opt.size()){ for(int iband=0;iband lineInput(this->size(),this->front()->nrOfCol()); assert(imgWriter.nrOfCol()==this->front()->nrOfCol()); Vector2d lineOutput(function_opt.size(),imgWriter.nrOfCol()); statfactory::StatFactory stat; stat.setNoDataValues(nodata_opt); const char* pszMessage; void* pProgressArg=NULL; GDALProgressFunc pfnProgress=GDALTermProgress; double progress=0; if(verbose_opt[0]) MyProgressFunc(progress,pszMessage,pProgressArg); Vector2d lineInput_transposed(this->front()->nrOfCol(),size());//band interleaved by pixel for(unsigned int y=0;yfront()->nrOfRow();++y){ std::list >::const_iterator imit=begin(); unsigned int ifile=0; for(imit=begin();imit!=end();++imit){ (*imit)->readData(lineInput[ifile],y); ++ifile; } for(int x=0;x lineInput_tmp; size_t stride=1; switch(filter::Filter::getFilterType(function_opt[imethod])){ case(filter::first): lineOutput[imethod]=lineInput.front(); break; case(filter::last): lineOutput[imethod]=lineInput.back(); break; case(filter::nvalid): for(x=0;x Jim::statProfile(app::AppFactory& app){ try{ shared_ptr imgWriter=Jim::createImg(); statProfile(*imgWriter, app); return(imgWriter); } catch(string helpString){ cerr << helpString << endl; throw; } } /** * @param function (type: std::string) Statistics function (mean, median, var, stdev, min, max, sum, mode (provide classes), minindex, maxindex, proportion (provide classes), percentile, nvalid * @param perc (type: double) Percentile value(s) used for rule percentile * @param otype (type: std::string) (default: GDT_Unknown) Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image * @param imgWriter output raster profile dataset * @param app application specific option arguments * @return CE_None if successful, CE_Failure if failed **/ void Jim::statProfile(Jim& imgWriter, app::AppFactory& app){ Optionjl function_opt("f", "function", "Statistics function (mean, median, var, stdev, min, max, sum, mode, minindex, maxindex, proportion (provide classes), percentile, nvalid"); Optionjl percentile_opt("perc","perc","Percentile value(s) used for rule percentile",90); // Optionjl class_opt("class", "class", "class value(s) to use for mode, proportion"); Optionjl nodata_opt("nodata", "nodata", "nodata value)"); Optionjl otype_opt("ot", "otype", "Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image","GDT_Unknown"); // Optionjl down_opt("d", "down", "down sampling factor. Use value 1 for no downsampling). Use value n>1 for downsampling (aggregation)", 1); Optionjl verbose_opt("v", "verbose", "verbose mode if > 0", 0,2); // percentile_opt.setHide(1); // class_opt.setHide(1); otype_opt.setHide(1); bool doProcess;//stop process when program was invoked with help option (-h --help) try{ doProcess=function_opt.retrieveOption(app); percentile_opt.retrieveOption(app); nodata_opt.retrieveOption(app); otype_opt.retrieveOption(app); verbose_opt.retrieveOption(app); if(!doProcess){ cout << endl; std::ostringstream helpStream; helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; throw(helpStream.str());//help was invoked, stop processing } std::vector badKeys; app.badKeys(badKeys); if(badKeys.size()){ std::ostringstream errorStream; if(badKeys.size()>1) errorStream << "Error: unknown keys: "; else errorStream << "Error: unknown key: "; for(int ikey=0;ikey parallel_percentile;//used for parallel processing if(function_opt.countSubstring("percentile")){ while(function_opt.countSubstring("percentile")::const_iterator percit=percentile_opt.begin(); for(int ifunction=0;ifunctiongetGDALDataType(); if(verbose_opt[0]>1) std::cout << std::endl << "Output pixel type: " << GDALGetDataTypeName(theType) << endl; if(verbose_opt[0]>1) cout << "Calculating statistic metrics: " << function_opt.size() << endl; // imgWriter.open(output_opt[0],this->nrOfCol(),this->nrOfRow(),function_opt.size(),theType,imageType,memory_opt[0],option_opt); imgWriter.open(this->nrOfCol(),this->nrOfRow(),function_opt.size(),theType); imgWriter.setProjection(this->getProjection()); double gt[6]; this->getGeoTransform(gt); imgWriter.setGeoTransform(gt); if(nodata_opt.size()){ for(int iband=0;iband lineInput(this->nrOfBand(),this->nrOfCol()); assert(imgWriter.nrOfCol()==this->nrOfCol()); Vector2d lineOutput(function_opt.size(),imgWriter.nrOfCol()); statfactory::StatFactory stat; stat.setNoDataValues(nodata_opt); const char* pszMessage; void* pProgressArg=NULL; GDALProgressFunc pfnProgress=GDALTermProgress; double progress=0; if(verbose_opt[0]) MyProgressFunc(progress,pszMessage,pProgressArg); Vector2d lineInput_transposed(nrOfCol(),nrOfBand());//band interleaved by pixel for(unsigned int y=0;ynrOfRow();++y){ for(unsigned int iband=0;ibandnrOfBand();++iband) this->readData(lineInput[iband],y,iband); for(int x=0;x lineInput_tmp; size_t stride=1; switch(filter::Filter::getFilterType(function_opt[imethod])){ case(filter::first): lineOutput[imethod]=lineInput.front(); break; case(filter::last): lineOutput[imethod]=lineInput.back(); break; case(filter::median): for(x=0;x