| 1 | /********************************************************************** |
| 2 | jlstat_lib.cc: program to calculate basic statistics from raster dataset |
| 3 | Author(s): [email protected] |
| 4 | Copyright (C) 2016-2020 European Union (Joint Research Centre) |
| 5 | |
| 6 | This file is part of jiplib. |
| 7 | |
| 8 | jiplib is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | jiplib is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with jiplib. If not, see <https://www.gnu.org/licenses/>. |
| 20 | ***********************************************************************/ |
| 21 | #include <iostream> |
| 22 | #include <fstream> |
| 23 | // #include <math.h> |
| 24 | #include <cmath> |
| 25 | #include "base/Optionjl.h" |
| 26 | #include "algorithms/StatFactory.h" |
| 27 | #include "algorithms/ImgRegression.h" |
| 28 | #if JIPLIB_BUILD_WITH_PYTHON==1 |
| 29 | #include <Python.h> |
| 30 | #endif |
| 31 | /******************************************************************************/ |
| 32 | using namespace std; |
| 33 | using namespace app; |
| 34 | |
| 35 | /** |
| 36 | * @param app application specific option arguments |
| 37 | * @return ostringstream with statistics |
| 38 | **/ |
| 39 | // CPLErr Jim::getStats(AppFactory& app){ |
| 40 | std::multimap<std::string,std::string> Jim::getStats(AppFactory& app){ |
| 41 | JimList singleList; |
| 42 | std::shared_ptr<Jim> imgReader=shared_from_this(); |
| 43 | singleList.pushImage(imgReader); |
| 44 | return(singleList.getStats(app)); |
| 45 | // if(singleList.getStats(app).front()) |
| 46 | // return(CE_None); |
| 47 | // else |
| 48 | // return(CE_Failure); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param scale (type: double) output=scale*input+offset |
| 53 | * @param offset (type: double) output=scale*input+offset |
| 54 | * @param mem (type: unsigned long) (default: 0) Buffer size (in MB) to read image data blocks in memory |
| 55 | * @param function (type: std::string) (default: basic) Statistics function (invalid, valid, filename, basic, gdal, mean, median, var, skewness, kurtosis,stdev, sum, minmax, min, max, histogram, histogram2d, rmse, regression, regressionError, regressionPerpendicular |
| 56 | * @param band (type: unsigned short) (default: 0) band(s) on which to calculate statistics |
| 57 | * @param nodata (type: double) Set nodata value(s) |
| 58 | * @param nbin (type: short) number of bins to calculate histogram |
| 59 | * @param relative (type: bool) (default: 0) use percentiles for histogram to calculate histogram |
| 60 | * @param down (type: short) (default: 1) Down sampling factor (for raster sample datasets only). Can be used to create grid points |
| 61 | * @param rnd (type: unsigned int) (default: 0) generate random numbers |
| 62 | * @param scale (type: double) Scale(s) for reading input image(s) |
| 63 | * @param offset (type: double) Offset(s) for reading input image(s) |
| 64 | * @param src_min (type: double) start reading source from this minimum value |
| 65 | * @param src_max (type: double) stop reading source from this maximum value |
| 66 | * @param kde (type: bool) (default: 0) Use Kernel density estimation when producing histogram. The standard deviation is estimated based on Silverman's rule of thumb |
| 67 | * @return this object |
| 68 | **/ |
| 69 | // JimList& JimList::getStats(AppFactory& app){ |
| 70 | std::multimap<std::string,std::string> JimList::getStats(AppFactory& app){ |
| 71 | Optionjl<unsigned short> band_opt("b","band","band(s) on which to calculate statistics"); |
| 72 | Optionjl<std::string> function_opt("f", "function", "Statistics function (invalid, valid, filename, basic, gdal, mean, median, var, skewness, kurtosis,stdev, sum, minmax, min, max, histogram, histogram2d, rmse, regression, regressionError, regressionPerpendicular)","basic"); |
| 73 | // Optionjl<double> ulx_opt("ulx", "ulx", "Upper left x value bounding box"); |
| 74 | // Optionjl<double> uly_opt("uly", "uly", "Upper left y value bounding box"); |
| 75 | // Optionjl<double> lrx_opt("lrx", "lrx", "Lower right x value bounding box"); |
| 76 | // Optionjl<double> lry_opt("lry", "lry", "Lower right y value bounding box"); |
| 77 | Optionjl<double> nodata_opt("nodata","nodata","Set nodata value(s)"); |
| 78 | Optionjl<short> down_opt("down", "down", "Down sampling factor (for raster sample datasets only). Can be used to create grid points", 1); |
| 79 | Optionjl<unsigned int> random_opt("rnd", "rnd", "generate random numbers", 0); |
| 80 | Optionjl<double> scale_opt("scale", "scale", "Scale(s) for reading input image(s)"); |
| 81 | Optionjl<double> offset_opt("offset", "offset", "Offset(s) for reading input image(s)"); |
| 82 | Optionjl<double> src_min_opt("src_min","src_min","start reading source from this minimum value"); |
| 83 | Optionjl<double> src_max_opt("src_max","src_max","stop reading source from this maximum value"); |
| 84 | Optionjl<short> nbin_opt("nbin","nbin","number of bins to calculate histogram"); |
| 85 | Optionjl<bool> relative_opt("rel","relative","use percentiles for histogram to calculate histogram",false); |
| 86 | Optionjl<bool> kde_opt("kde","kde","Use Kernel density estimation when producing histogram. The standard deviation is estimated based on Silverman's rule of thumb",false); |
| 87 | Optionjl<short> verbose_opt("v", "verbose", "verbose mode when positive", 0,2); |
| 88 | // ulx_opt.setHide(1); |
| 89 | // uly_opt.setHide(1); |
| 90 | // lrx_opt.setHide(1); |
| 91 | // lry_opt.setHide(1); |
| 92 | down_opt.setHide(1); |
| 93 | random_opt.setHide(1); |
| 94 | scale_opt.setHide(1); |
| 95 | offset_opt.setHide(1); |
| 96 | src_min_opt.setHide(1); |
| 97 | src_max_opt.setHide(1); |
| 98 | kde_opt.setHide(1); |
| 99 | |
| 100 | std::multimap<std::string,std::string> mapString; |
| 101 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 102 | try{ |
| 103 | doProcess=function_opt.retrieveOption(app); |
| 104 | //optional options |
| 105 | doProcess=band_opt.retrieveOption(app); |
| 106 | nodata_opt.retrieveOption(app); |
| 107 | nbin_opt.retrieveOption(app); |
| 108 | relative_opt.retrieveOption(app); |
| 109 | //advanced options |
| 110 | // ulx_opt.retrieveOption(app); |
| 111 | // uly_opt.retrieveOption(app); |
| 112 | // lrx_opt.retrieveOption(app); |
| 113 | // lry_opt.retrieveOption(app); |
| 114 | down_opt.retrieveOption(app); |
| 115 | random_opt.retrieveOption(app); |
| 116 | scale_opt.retrieveOption(app); |
| 117 | offset_opt.retrieveOption(app); |
| 118 | src_min_opt.retrieveOption(app); |
| 119 | src_max_opt.retrieveOption(app); |
| 120 | kde_opt.retrieveOption(app); |
| 121 | verbose_opt.retrieveOption(app); |
| 122 | |
| 123 | // std::ostringstream |
| 124 | if(!doProcess){ |
| 125 | cout << endl; |
| 126 | std::ostringstream helpStream; |
| 127 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 128 | throw(helpStream.str());//help was invoked, stop processing |
| 129 | } |
| 130 | |
| 131 | if(empty()){ |
| 132 | std::ostringstream errorStream; |
| 133 | errorStream << "Input collection is empty. Use --help for more help information" << std::endl; |
| 134 | throw(errorStream.str()); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | // std::vector<std::string> badKeys; |
| 139 | // app.badKeys(badKeys); |
| 140 | // if(badKeys.size()){ |
| 141 | // std::ostringstream errorStream; |
| 142 | // if(badKeys.size()>1) |
| 143 | // errorStream << "Error: unknown keys: "; |
| 144 | // else |
| 145 | // errorStream << "Error: unknown key: "; |
| 146 | // for(int ikey=0;ikey<badKeys.size();++ikey){ |
| 147 | // errorStream << badKeys[ikey] << " "; |
| 148 | // } |
| 149 | // errorStream << std::endl; |
| 150 | // throw(errorStream.str()); |
| 151 | // } |
| 152 | |
| 153 | |
| 154 | bool invalid_opt=std::find(function_opt.begin(), function_opt.end(), "invalid") != function_opt.end();//Report number of nodata values in image |
| 155 | bool valid_opt=std::find(function_opt.begin(),function_opt.end(),"valid")!=function_opt.end();//Report number of nodata values (i.e., not nodata) in image |
| 156 | bool filename_opt=std::find(function_opt.begin(),function_opt.end(),"filename")!=function_opt.end();//"fn", "filename", "Shows image filename ", false); |
| 157 | bool stat_opt=std::find(function_opt.begin(),function_opt.end(),"basic")!=function_opt.end();//"basic", "basic", "Shows basic statistics (calculate in memory) (min,max, mean and stdev of the raster datasets)", false); |
| 158 | bool fstat_opt=std::find(function_opt.begin(),function_opt.end(),"gdal")!=function_opt.end();//"gdal", "gdal", "Shows basic statistics using GDAL computeStatistics (min,max, mean and stdev of the raster datasets)", false); |
| 159 | bool mean_opt=std::find(function_opt.begin(),function_opt.end(),"mean")!=function_opt.end();//calculate mean |
| 160 | bool median_opt=std::find(function_opt.begin(),function_opt.end(),"median")!=function_opt.end();//calculate median |
| 161 | bool var_opt=std::find(function_opt.begin(),function_opt.end(),"var")!=function_opt.end();//calculate variance |
| 162 | bool skewness_opt=std::find(function_opt.begin(),function_opt.end(),"skewness")!=function_opt.end();//calculate skewness |
| 163 | bool kurtosis_opt=std::find(function_opt.begin(),function_opt.end(),"kurtosis")!=function_opt.end();//calculate kurtosis |
| 164 | bool stdev_opt=std::find(function_opt.begin(),function_opt.end(),"stdev")!=function_opt.end();//calculate standard deviation |
| 165 | bool sum_opt=std::find(function_opt.begin(),function_opt.end(),"sum")!=function_opt.end();//calculate sum of column |
| 166 | bool minmax_opt=std::find(function_opt.begin(),function_opt.end(),"minmax")!=function_opt.end();//calculate minimum and maximum value |
| 167 | bool min_opt=std::find(function_opt.begin(),function_opt.end(),"min")!=function_opt.end();//calculate minimum value |
| 168 | bool max_opt=std::find(function_opt.begin(),function_opt.end(),"max")!=function_opt.end();//calculate maximum value |
| 169 | bool histogram_opt=std::find(function_opt.begin(),function_opt.end(),"histogram")!=function_opt.end();//calculate histogram |
| 170 | bool histogram2d_opt=std::find(function_opt.begin(),function_opt.end(),"histogram2d")!=function_opt.end();//calculate 2-dimensional histogram based on two images |
| 171 | bool rmse_opt=std::find(function_opt.begin(),function_opt.end(),"rmse")!=function_opt.end();//calculate root mean square error between two raster datasets |
| 172 | bool reg_opt=std::find(function_opt.begin(),function_opt.end(),"regression")!=function_opt.end();//calculate linear regression between two raster datasets and get correlation coefficient |
| 173 | bool regerr_opt=std::find(function_opt.begin(),function_opt.end(),"regressionError")!=function_opt.end();//calculate linear regression between two raster datasets and get root mean square error |
| 174 | bool preg_opt=std::find(function_opt.begin(),function_opt.end(),"regressionPerpendicular")!=function_opt.end();//calculate perpendicular regression between two raster datasets and get correlation coefficient |
| 175 | |
| 176 | |
| 177 | srand(time(NULL)); |
| 178 | |
| 179 | if(!band_opt.size()){ |
| 180 | for(size_t iband=0;iband<(this->getImage(0))->nrOfBand();++iband) |
| 181 | band_opt.push_back(iband); |
| 182 | } |
| 183 | |
| 184 | if(src_min_opt.size()){ |
| 185 | while(src_min_opt.size()<band_opt.size()) |
| 186 | src_min_opt.push_back(src_min_opt[0]); |
| 187 | } |
| 188 | if(src_max_opt.size()){ |
| 189 | while(src_max_opt.size()<band_opt.size()) |
| 190 | src_max_opt.push_back(src_max_opt[0]); |
| 191 | } |
| 192 | |
| 193 | int nbin=0; |
| 194 | double minX=0; |
| 195 | double minY=0; |
| 196 | double maxX=0; |
| 197 | double maxY=0; |
| 198 | double minValue=(src_min_opt.size())? src_min_opt[0] : 0; |
| 199 | double maxValue=(src_max_opt.size())? src_max_opt[0] : 0; |
| 200 | double meanValue=0; |
| 201 | double medianValue=0; |
| 202 | double stdDev=0; |
| 203 | |
| 204 | statfactory::StatFactory stat; |
| 205 | imgregression::ImgRegression imgreg; |
| 206 | std::vector<double> histogramOutput; |
| 207 | double nsample=0; |
| 208 | |
| 209 | // Jim imgReader; |
| 210 | if(scale_opt.size()){ |
| 211 | while(scale_opt.size()<size()) |
| 212 | scale_opt.push_back(scale_opt[0]); |
| 213 | } |
| 214 | if(offset_opt.size()){ |
| 215 | while(offset_opt.size()<size()) |
| 216 | offset_opt.push_back(offset_opt[0]); |
| 217 | } |
| 218 | std::vector< std::vector<double> > orignodata(size()); |
| 219 | std::list<std::shared_ptr<Jim> >::const_iterator imit=begin(); |
| 220 | size_t iimage=0; |
| 221 | for(imit=begin();imit!=end();++imit){ |
| 222 | if(filename_opt) |
| 223 | mapString.insert(std::make_pair("filename",(*imit)->getFileName())); |
| 224 | |
| 225 | if(nodata_opt.size()){ |
| 226 | (*imit)->getNoDataValues(orignodata[iimage]); |
| 227 | for(size_t inodata=0;inodata<nodata_opt.size();++inodata) |
| 228 | (*imit)->pushNoDataValue(nodata_opt[inodata]); |
| 229 | } |
| 230 | |
| 231 | int nband=band_opt.size(); |
| 232 | std::vector<std::string> nvalids; |
| 233 | std::vector<std::string> ninvalids; |
| 234 | std::vector<std::string> means; |
| 235 | std::vector<std::string> medians; |
| 236 | std::vector<std::string> vars; |
| 237 | std::vector<std::string> stdevs; |
| 238 | std::vector<std::string> mins; |
| 239 | std::vector<std::string> maxs; |
| 240 | for(int iband=0;iband<nband;++iband){ |
| 241 | if(src_min_opt.size()) |
| 242 | minValue=(src_min_opt.size()>iband)? src_min_opt[iband] : src_min_opt[0]; |
| 243 | else |
| 244 | minValue= 0; |
| 245 | if(src_max_opt.size()) |
| 246 | maxValue=(src_max_opt.size()>iband)? src_max_opt[iband] : src_max_opt[0]; |
| 247 | else |
| 248 | maxValue= 0; |
| 249 | for(size_t inodata=0;inodata<nodata_opt.size();++inodata){ |
| 250 | if(!inodata) |
| 251 | (*imit)->GDALSetNoDataValue(nodata_opt[0],band_opt[iband]);//only single no data can be set in GDALRasterBand (used for ComputeStatistics) |
| 252 | } |
| 253 | |
| 254 | if(valid_opt) |
| 255 | nvalids.push_back(type2string<unsigned long int>((*imit)->getNvalid(band_opt[iband]))); |
| 256 | // mapString.insert(std::make_pair("nvalid",type2string<unsigned long int>((*imit)->getNvalid(band_opt[iband])))); |
| 257 | // mapString["nvalid"]=type2string<unsigned long int>((*imit)->getNvalid(band_opt[0])); |
| 258 | // outputStream << "--nvalid " << (*imit)->getNvalid(band_opt[0]) << endl; |
| 259 | if(invalid_opt) |
| 260 | ninvalids.push_back(type2string<unsigned long int>((*imit)->getNinvalid(band_opt[iband]))); |
| 261 | // mapString.insert(std::make_pair("ninvalid",type2string<unsigned long int>((*imit)->getNinvalid(band_opt[iband])))); |
| 262 | // mapString["ninvalid"]=type2string<unsigned long int>((*imit)->getNinvalid(band_opt[0])); |
| 263 | // outputStream << "--ninvalid " << (*imit)->getNinvalid(band_opt[0]) << endl; |
| 264 | if(stat_opt||mean_opt||median_opt||var_opt||stdev_opt||min_opt||max_opt||minmax_opt){//the hard way (in memory) |
| 265 | statfactory::StatFactory stat; |
| 266 | vector<double> readBuffer; |
| 267 | vector<double> tmpBuffer; |
| 268 | double varValue; |
| 269 | //test |
| 270 | // (*imit)->readData(readBuffer, 1, 0); |
| 271 | // std::cout << "readBuffer.size(): " << readBuffer.size() << std::endl; |
| 272 | // //test |
| 273 | // for(int i=0;i<10;++i){ |
| 274 | // std::cout << readBuffer[i] << " "; |
| 275 | // } |
| 276 | // std::cout << std::endl; |
| 277 | (*imit)->readDataBlock(readBuffer, 0, (*imit)->nrOfCol()-1, 0, (*imit)->nrOfRow()-1, band_opt[iband]); |
| 278 | // //test |
| 279 | // for(int i=0;i<10;++i) |
| 280 | // std::cout << *(readBuffer.begin()+i) << " "; |
| 281 | // std::cout << std::endl; |
| 282 | // for(int j=0;j<10;++j){ |
| 283 | // for(int i=0;i<10;++i) |
| 284 | // std::cout << readBuffer[j*(*imit)->nrOfCol()+i] << " "; |
| 285 | // std::cout << std::endl; |
| 286 | // } |
| 287 | stat.setNoDataValues(nodata_opt); |
| 288 | if(src_min_opt.size()) |
| 289 | stat.eraseBelow(readBuffer,src_min_opt[iband]); |
| 290 | // stat.eraseBelow(readBuffer,src_min_opt[band_opt[iband]]); |
| 291 | if(src_max_opt.size()) |
| 292 | stat.eraseAbove(readBuffer,src_max_opt[iband]); |
| 293 | // stat.eraseAbove(readBuffer,src_max_opt[band_opt[iband]]); |
| 294 | stat.eraseNoData(readBuffer); |
| 295 | // stat.meanVar(readBuffer,meanValue,varValue); |
| 296 | // medianValue=stat.median(readBuffer); |
| 297 | // stat.minmax(readBuffer,readBuffer.begin(),readBuffer.end(),minValue,maxValue); |
| 298 | size_t stride=1; |
| 299 | if(mean_opt||stat_opt){ |
| 300 | means.push_back(type2string<double>(gsl_stats_mean(&(readBuffer[0]),stride,readBuffer.size()))); |
| 301 | // mapString.insert(std::make_pair("mean",type2string<double>(gsl_stats_mean(&(readBuffer[0]),stride,readBuffer.size())))); |
| 302 | // mapString["mean"]=type2string<double>(gsl_stats_mean(&(readBuffer[0]),stride,readBuffer.size())); |
| 303 | // double data[10]={17,22,18,14,8,3,5,10,10,8}; |
| 304 | // std::vector<double> testv(10); |
| 305 | // for(int i=0;i<10;++i) |
| 306 | // std::cout << *(readBuffer.begin()+i) << " "; |
| 307 | // std::cout << std::endl; |
| 308 | // testv.assign(readBuffer.begin(),readBuffer.begin()+10); |
| 309 | // std::cout << "mean is: " << gsl_stats_mean(&(testv[0]),1,10) << std::endl; |
| 310 | // std::cout << "mean is: " << gsl_stats_mean(data, 1, 10) << std::endl; |
| 311 | // std::cout << "mean is: " << gsl_stats_mean(&(readBuffer[0]),1,10) << std::endl; |
| 312 | // mapString["mean"]=type2string<double>(meanValue); |
| 313 | // outputStream << "--mean " << meanValue << " " << std::endl; |
| 314 | } |
| 315 | if(median_opt){ |
| 316 | tmpBuffer=readBuffer; |
| 317 | gsl_sort(&(tmpBuffer[0]),stride,readBuffer.size()); |
| 318 | medians.push_back(type2string<double>(gsl_stats_median_from_sorted_data(&(tmpBuffer[0]),stride,readBuffer.size()))); |
| 319 | // mapString.insert(std::make_pair("median",type2string<double>(gsl_stats_median_from_sorted_data(&(tmpBuffer[0]),stride,readBuffer.size())))); |
| 320 | tmpBuffer.clear(); |
| 321 | } |
| 322 | if(stdev_opt||stat_opt){ |
| 323 | stdevs.push_back(type2string<double>(gsl_stats_sd(&(readBuffer[0]),stride,readBuffer.size()))); |
| 324 | // mapString.insert(std::make_pair("stdev",type2string<double>(gsl_stats_sd(&(readBuffer[0]),stride,readBuffer.size())))); |
| 325 | // mapString["stdev"]=type2string<double>(gsl_stats_sd(&(readBuffer[0]),stride,readBuffer.size())); |
| 326 | // mapString["stdev"]=type2string<double>(sqrt(varValue)); |
| 327 | // outputStream << "--stdev " << sqrt(varValue) << " " << std::endl; |
| 328 | } |
| 329 | if(var_opt){ |
| 330 | vars.push_back(type2string<double>(gsl_stats_variance(&(readBuffer[0]),stride,readBuffer.size()))); |
| 331 | // mapString.insert(std::make_pair("var",type2string<double>(gsl_stats_variance(&(readBuffer[0]),stride,readBuffer.size())))); |
| 332 | // mapString["var"]=type2string<double>(gsl_stats_variance(&(readBuffer[0]),stride,readBuffer.size())); |
| 333 | // mapString["var"]=type2string<double>(varValue); |
| 334 | // outputStream << "--var " << varValue << " " << std::endl; |
| 335 | } |
| 336 | if(min_opt||minmax_opt||stat_opt) |
| 337 | mins.push_back(type2string<double>(gsl_stats_min(&(readBuffer[0]),stride,readBuffer.size()))); |
| 338 | // mapString.insert(std::make_pair("min",type2string<double>(gsl_stats_min(&(readBuffer[0]),stride,readBuffer.size())))); |
| 339 | // mapString["min"]=type2string<double>(gsl_stats_min(&(readBuffer[0]),stride,readBuffer.size())); |
| 340 | if(max_opt||minmax_opt||stat_opt) |
| 341 | maxs.push_back(type2string<double>(gsl_stats_max(&(readBuffer[0]),stride,readBuffer.size()))); |
| 342 | // mapString.insert(std::make_pair("max",type2string<double>(gsl_stats_max(&(readBuffer[0]),stride,readBuffer.size())))); |
| 343 | // mapString["max"]=type2string<double>(gsl_stats_max(&(readBuffer[0]),stride,readBuffer.size())); |
| 344 | // if(stat_opt){ |
| 345 | // mapString["min"]=type2string<double>(minValue); |
| 346 | // mapString["max"]=type2string<double>(maxValue); |
| 347 | // mapString["mean"]=type2string<double>(meanValue); |
| 348 | // mapString["stdev"]=type2string<double>(sqrt(varValue)); |
| 349 | // outputStream << "--min " << minValue << " --max " << maxValue << " --mean " << meanValue << " --stdev " << sqrt(varValue) << " " << std::endl; |
| 350 | // } |
| 351 | } |
| 352 | |
| 353 | if(fstat_opt){//the fast way |
| 354 | assert(band_opt[iband]<(*imit)->nrOfBand()); |
| 355 | GDALProgressFunc pfnProgress; |
| 356 | void* pProgressData; |
| 357 | GDALRasterBand* rasterBand; |
| 358 | rasterBand=(*imit)->getRasterBand(band_opt[iband]); |
| 359 | rasterBand->ComputeStatistics(0,&minValue,&maxValue,&meanValue,&stdDev,pfnProgress,pProgressData); |
| 360 | mins.push_back(type2string<double>(minValue)); |
| 361 | maxs.push_back(type2string<double>(maxValue)); |
| 362 | means.push_back(type2string<double>(meanValue)); |
| 363 | stdevs.push_back(type2string<double>(stdDev)); |
| 364 | // mapString.insert(std::make_pair("min",type2string<double>(minValue))); |
| 365 | // mapString.insert(std::make_pair("max",type2string<double>(maxValue))); |
| 366 | // mapString.insert(std::make_pair("mean",type2string<double>(meanValue))); |
| 367 | // mapString.insert(std::make_pair("stdev",type2string<double>(stdDev))); |
| 368 | // mapString["min"]=type2string<double>(minValue); |
| 369 | // mapString["max"]=type2string<double>(maxValue); |
| 370 | // mapString["mean"]=type2string<double>(meanValue); |
| 371 | // mapString["stdev"]=type2string<double>(stdDev); |
| 372 | // outputStream << "--min " << minValue << " --max " << maxValue << " --mean " << meanValue << " --stdev " << stdDev << " " << std::endl; |
| 373 | } |
| 374 | // if(minmax_opt||min_opt||max_opt){ |
| 375 | // assert(band_opt[iband]<(*imit)->nrOfBand()); |
| 376 | |
| 377 | // if((ulx_opt.size()||uly_opt.size()||lrx_opt.size()||lry_opt.size())&&((*imit)->covers(ulx_opt[0],uly_opt[0],lrx_opt[0],lry_opt[0]))){ |
| 378 | // double uli,ulj,lri,lrj; |
| 379 | // (*imit)->geo2image(ulx_opt[0],uly_opt[0],uli,ulj); |
| 380 | // (*imit)->geo2image(lrx_opt[0],lry_opt[0],lri,lrj); |
| 381 | // (*imit)->getMinMax(static_cast<int>(uli),static_cast<int>(lri),static_cast<int>(ulj),static_cast<int>(lrj),band_opt[iband],minValue,maxValue); |
| 382 | // } |
| 383 | // else{ |
| 384 | // (*imit)->getMinMax(minValue,maxValue,band_opt[iband]); |
| 385 | // } |
| 386 | // if(minmax_opt){ |
| 387 | // mapString["min"]=type2string<double>(minValue); |
| 388 | // mapString["max"]=type2string<double>(maxValue); |
| 389 | // // outputStream << "--min " << minValue << " --max " << maxValue << " " << std::endl; |
| 390 | // } |
| 391 | // else{ |
| 392 | // if(min_opt) |
| 393 | // mapString["min"]=type2string<double>(minValue); |
| 394 | // // outputStream << "--min " << minValue << " " << std::endl; |
| 395 | // if(max_opt) |
| 396 | // mapString["max"]=type2string<double>(maxValue); |
| 397 | // // outputStream << "--max " << maxValue << " "; |
| 398 | // } |
| 399 | // } |
| 400 | } |
| 401 | if(mins.size()){ |
| 402 | std::ostringstream isminvalues; |
| 403 | for(size_t ivalue=0;ivalue<mins.size();++ivalue){ |
| 404 | if(mins.size()>1){ |
| 405 | if(!ivalue) |
| 406 | isminvalues << "["; |
| 407 | else |
| 408 | isminvalues << ","; |
| 409 | isminvalues << mins[ivalue]; |
| 410 | if(ivalue==mins.size()-1) |
| 411 | isminvalues << "]"; |
| 412 | } |
| 413 | else |
| 414 | isminvalues << mins[0]; |
| 415 | } |
| 416 | mapString.insert(std::make_pair("min",isminvalues.str())); |
| 417 | } |
| 418 | if(maxs.size()){ |
| 419 | std::ostringstream ismaxvalues; |
| 420 | for(size_t ivalue=0;ivalue<maxs.size();++ivalue){ |
| 421 | if(maxs.size()>1){ |
| 422 | if(!ivalue) |
| 423 | ismaxvalues << "["; |
| 424 | else |
| 425 | ismaxvalues << ","; |
| 426 | ismaxvalues << maxs[ivalue]; |
| 427 | if(ivalue==maxs.size()-1) |
| 428 | ismaxvalues << "]"; |
| 429 | } |
| 430 | else |
| 431 | ismaxvalues << maxs[0]; |
| 432 | } |
| 433 | mapString.insert(std::make_pair("max",ismaxvalues.str())); |
| 434 | } |
| 435 | if(means.size()){ |
| 436 | std::ostringstream ismeanvalues; |
| 437 | for(size_t ivalue=0;ivalue<means.size();++ivalue){ |
| 438 | if(means.size()>1){ |
| 439 | if(!ivalue) |
| 440 | ismeanvalues << "["; |
| 441 | else |
| 442 | ismeanvalues << ","; |
| 443 | ismeanvalues << means[ivalue]; |
| 444 | if(ivalue==means.size()-1) |
| 445 | ismeanvalues << "]"; |
| 446 | } |
| 447 | else |
| 448 | ismeanvalues << means[0]; |
| 449 | } |
| 450 | mapString.insert(std::make_pair("mean",ismeanvalues.str())); |
| 451 | } |
| 452 | if(medians.size()){ |
| 453 | std::ostringstream ismedianvalues; |
| 454 | for(size_t ivalue=0;ivalue<medians.size();++ivalue){ |
| 455 | if(medians.size()>1){ |
| 456 | if(!ivalue) |
| 457 | ismedianvalues << "["; |
| 458 | else |
| 459 | ismedianvalues << ","; |
| 460 | ismedianvalues << medians[ivalue]; |
| 461 | if(ivalue==medians.size()-1) |
| 462 | ismedianvalues << "]"; |
| 463 | } |
| 464 | else |
| 465 | ismedianvalues << medians[0]; |
| 466 | } |
| 467 | mapString.insert(std::make_pair("median",ismedianvalues.str())); |
| 468 | } |
| 469 | if(vars.size()){ |
| 470 | std::ostringstream isvarvalues; |
| 471 | for(size_t ivalue=0;ivalue<vars.size();++ivalue){ |
| 472 | if(vars.size()>1){ |
| 473 | if(!ivalue) |
| 474 | isvarvalues << "["; |
| 475 | else |
| 476 | isvarvalues << ","; |
| 477 | isvarvalues << vars[ivalue]; |
| 478 | if(ivalue==vars.size()-1) |
| 479 | isvarvalues << "]"; |
| 480 | } |
| 481 | else |
| 482 | isvarvalues << vars[0]; |
| 483 | } |
| 484 | mapString.insert(std::make_pair("var",isvarvalues.str())); |
| 485 | } |
| 486 | if(stdevs.size()){ |
| 487 | std::ostringstream isstdevvalues; |
| 488 | for(size_t ivalue=0;ivalue<stdevs.size();++ivalue){ |
| 489 | if(stdevs.size()>1){ |
| 490 | if(!ivalue) |
| 491 | isstdevvalues << "["; |
| 492 | else |
| 493 | isstdevvalues << ","; |
| 494 | isstdevvalues << stdevs[ivalue]; |
| 495 | if(ivalue==stdevs.size()-1) |
| 496 | isstdevvalues << "]"; |
| 497 | } |
| 498 | else |
| 499 | isstdevvalues << stdevs[0]; |
| 500 | } |
| 501 | mapString.insert(std::make_pair("stdev",isstdevvalues.str())); |
| 502 | } |
| 503 | if(nvalids.size()){ |
| 504 | std::ostringstream isnvalidvalues; |
| 505 | for(size_t ivalue=0;ivalue<nvalids.size();++ivalue){ |
| 506 | if(nvalids.size()>1){ |
| 507 | if(!ivalue) |
| 508 | isnvalidvalues << "["; |
| 509 | else |
| 510 | isnvalidvalues << ","; |
| 511 | isnvalidvalues << nvalids[ivalue]; |
| 512 | if(ivalue==nvalids.size()-1) |
| 513 | isnvalidvalues << "]"; |
| 514 | } |
| 515 | else |
| 516 | isnvalidvalues << nvalids[0]; |
| 517 | } |
| 518 | mapString.insert(std::make_pair("nvalid",isnvalidvalues.str())); |
| 519 | } |
| 520 | if(ninvalids.size()){ |
| 521 | std::ostringstream isninvalidvalues; |
| 522 | for(size_t ivalue=0;ivalue<ninvalids.size();++ivalue){ |
| 523 | if(ninvalids.size()>1){ |
| 524 | if(!ivalue) |
| 525 | isninvalidvalues << "["; |
| 526 | else |
| 527 | isninvalidvalues << ","; |
| 528 | isninvalidvalues << ninvalids[ivalue]; |
| 529 | if(ivalue==ninvalids.size()-1) |
| 530 | isninvalidvalues << "]"; |
| 531 | } |
| 532 | else |
| 533 | isninvalidvalues << ninvalids[0]; |
| 534 | } |
| 535 | mapString.insert(std::make_pair("ninvalid",isninvalidvalues.str())); |
| 536 | } |
| 537 | |
| 538 | if(histogram_opt){//aggregate results from multiple inputs, but only calculate for first selected band |
| 539 | assert(band_opt[0]<(*imit)->nrOfBand()); |
| 540 | nbin=(nbin_opt.size())? nbin_opt[0]:0; |
| 541 | (*imit)->getMinMax(minValue,maxValue,band_opt[0]); |
| 542 | if(src_min_opt.size()) |
| 543 | minValue=src_min_opt[0]; |
| 544 | if(src_max_opt.size()) |
| 545 | maxValue=src_max_opt[0]; |
| 546 | if(minValue>=maxValue) |
| 547 | (*imit)->getMinMax(minValue,maxValue,band_opt[0]); |
| 548 | |
| 549 | if(verbose_opt[0]) |
| 550 | std::cout << "number of valid pixels in image: " << (*imit)->getNvalid(band_opt[0]) << endl << std::endl; |
| 551 | nsample+=(*imit)->getHistogram(histogramOutput,minValue,maxValue,nbin,band_opt[0],kde_opt[0]); |
| 552 | |
| 553 | //only output for last input file |
| 554 | if(std::next(imit)==end()){ |
| 555 | ostringstream tmpStream; |
| 556 | tmpStream.precision(10); |
| 557 | for(int bin=0;bin<nbin;++bin){ |
| 558 | double binValue=0; |
| 559 | if(nbin==maxValue-minValue+1) |
| 560 | binValue=minValue+bin; |
| 561 | else |
| 562 | binValue=minValue+static_cast<double>(maxValue-minValue)*(bin+0.5)/nbin; |
| 563 | tmpStream << binValue << " "; |
| 564 | if(relative_opt[0]||kde_opt[0]) |
| 565 | tmpStream << 100.0*static_cast<double>(histogramOutput[bin])/static_cast<double>(nsample) << std::endl << std::endl; |
| 566 | else |
| 567 | tmpStream << static_cast<double>(histogramOutput[bin]) << std::endl; |
| 568 | } |
| 569 | mapString.insert(std::make_pair("histogram",tmpStream.str())); |
| 570 | // mapString["histogram"]=tmpStream.str(); |
| 571 | } |
| 572 | } |
| 573 | if(histogram2d_opt&&size()<2){ |
| 574 | assert(band_opt.size()>1); |
| 575 | (*imit)->getMinMax(minX,maxX,band_opt[0]); |
| 576 | (*imit)->getMinMax(minY,maxY,band_opt[1]); |
| 577 | if(src_min_opt.size()){ |
| 578 | minX=src_min_opt[0]; |
| 579 | minY=src_min_opt[1]; |
| 580 | } |
| 581 | if(src_max_opt.size()){ |
| 582 | maxX=src_max_opt[0]; |
| 583 | maxY=src_max_opt[1]; |
| 584 | } |
| 585 | nbin=(nbin_opt.size())? nbin_opt[0]:0; |
| 586 | if(nbin<=1){ |
| 587 | std::cerr << "Warning: number of bins not defined, calculating bins from min and max value" << std::endl; |
| 588 | if(minX>=maxX) |
| 589 | (*imit)->getMinMax(minX,maxX,band_opt[0]); |
| 590 | if(minY>=maxY) |
| 591 | (*imit)->getMinMax(minY,maxY,band_opt[1]); |
| 592 | |
| 593 | minValue=(minX<minY)? minX:minY; |
| 594 | maxValue=(maxX>maxY)? maxX:maxY; |
| 595 | if(verbose_opt[0]) |
| 596 | std::cout << "min and max values: " << minValue << ", " << maxValue << std::endl << std::endl; |
| 597 | nbin=maxValue-minValue+1; |
| 598 | } |
| 599 | assert(nbin>1); |
| 600 | double sigma=0; |
| 601 | //kernel density estimation as in http://en.wikipedia.org/wiki/Kernel_density_estimation |
| 602 | if(kde_opt[0]){ |
| 603 | assert(band_opt[0]<(*imit)->nrOfBand()); |
| 604 | assert(band_opt[1]<(*imit)->nrOfBand()); |
| 605 | GDALProgressFunc pfnProgress; |
| 606 | void* pProgressData; |
| 607 | GDALRasterBand* rasterBand; |
| 608 | double stdDev1=0; |
| 609 | double stdDev2=0; |
| 610 | rasterBand=(*imit)->getRasterBand(band_opt[0]); |
| 611 | rasterBand->ComputeStatistics(0,&minValue,&maxValue,&meanValue,&stdDev1,pfnProgress,pProgressData); |
| 612 | rasterBand=(*imit)->getRasterBand(band_opt[1]); |
| 613 | rasterBand->ComputeStatistics(0,&minValue,&maxValue,&meanValue,&stdDev2,pfnProgress,pProgressData); |
| 614 | |
| 615 | double estimatedSize=1.0*(*imit)->getNvalid(band_opt[0])/down_opt[0]/down_opt[0]; |
| 616 | if(random_opt[0]>0) |
| 617 | estimatedSize*=random_opt[0]/100.0; |
| 618 | sigma=1.06*sqrt(stdDev1*stdDev2)*pow(estimatedSize,-0.2); |
| 619 | } |
| 620 | assert(nbin); |
| 621 | if(verbose_opt[0]){ |
| 622 | if(sigma>0) |
| 623 | std::cout << "calculating 2d kernel density estimate with sigma " << sigma << " for bands " << band_opt[0] << " and " << band_opt[1] << std::endl; |
| 624 | else |
| 625 | std::cout << "calculating 2d histogram for bands " << band_opt[0] << " and " << band_opt[1] << std::endl; |
| 626 | std::cout << "nbin: " << nbin << std::endl; |
| 627 | } |
| 628 | |
| 629 | |
| 630 | vector< vector<double> > output; |
| 631 | |
| 632 | if(maxX<=minX) |
| 633 | (*imit)->getMinMax(minX,maxX,band_opt[0]); |
| 634 | if(maxY<=minY) |
| 635 | (*imit)->getMinMax(minY,maxY,band_opt[1]); |
| 636 | |
| 637 | if(maxX<=minX){ |
| 638 | std::ostringstream s; |
| 639 | s<<"Error: could not calculate distribution (minX>=maxX)"; |
| 640 | throw(s.str()); |
| 641 | } |
| 642 | if(maxY<=minY){ |
| 643 | std::ostringstream s; |
| 644 | s<<"Error: could not calculate distribution (minY>=maxY)"; |
| 645 | throw(s.str()); |
| 646 | } |
| 647 | output.resize(nbin); |
| 648 | for(int i=0;i<nbin;++i){ |
| 649 | output[i].resize(nbin); |
| 650 | for(int j=0;j<nbin;++j) |
| 651 | output[i][j]=0; |
| 652 | } |
| 653 | int binX=0; |
| 654 | int binY=0; |
| 655 | vector<double> inputX((*imit)->nrOfCol()); |
| 656 | vector<double> inputY((*imit)->nrOfCol()); |
| 657 | unsigned long int nvalid=0; |
| 658 | for(unsigned int irow=0;irow<(*imit)->nrOfRow();++irow){ |
| 659 | if(irow%down_opt[0]) |
| 660 | continue; |
| 661 | (*imit)->readData(inputX,irow,band_opt[0]); |
| 662 | (*imit)->readData(inputY,irow,band_opt[1]); |
| 663 | for(unsigned int icol=0;icol<(*imit)->nrOfCol();++icol){ |
| 664 | if(icol%down_opt[0]) |
| 665 | continue; |
| 666 | if(random_opt[0]>0){ |
| 667 | double p=static_cast<double>(rand())/(RAND_MAX); |
| 668 | p*=100.0; |
| 669 | if(p>random_opt[0]) |
| 670 | continue;//do not select for now, go to next column |
| 671 | } |
| 672 | if((*imit)->isNoData(inputX[icol])) |
| 673 | continue; |
| 674 | if((*imit)->isNoData(inputY[icol])) |
| 675 | continue; |
| 676 | ++nvalid; |
| 677 | if(inputX[icol]>=maxX) |
| 678 | binX=nbin-1; |
| 679 | else if(inputX[icol]<=minX) |
| 680 | binX=0; |
| 681 | else |
| 682 | binX=static_cast<int>(static_cast<double>(inputX[icol]-minX)/(maxX-minX)*nbin); |
| 683 | if(inputY[icol]>=maxY) |
| 684 | binY=nbin-1; |
| 685 | else if(inputY[icol]<=minX) |
| 686 | binY=0; |
| 687 | else |
| 688 | binY=static_cast<int>(static_cast<double>(inputY[icol]-minY)/(maxY-minY)*nbin); |
| 689 | assert(binX>=0); |
| 690 | assert(binX<output.size()); |
| 691 | assert(binY>=0); |
| 692 | assert(binY<output[binX].size()); |
| 693 | if(sigma>0){ |
| 694 | //create kde for Gaussian basis function |
| 695 | //todo: speed up by calculating first and last bin with non-zero contriubtion... |
| 696 | for(int ibinX=0;ibinX<nbin;++ibinX){ |
| 697 | double centerX=minX+static_cast<double>(maxX-minX)*ibinX/nbin; |
| 698 | double pdfX=gsl_ran_gaussian_pdf(inputX[icol]-centerX, sigma); |
| 699 | for(int ibinY=0;ibinY<nbin;++ibinY){ |
| 700 | //calculate \integral_ibinX^(ibinX+1) |
| 701 | double centerY=minY+static_cast<double>(maxY-minY)*ibinY/nbin; |
| 702 | double pdfY=gsl_ran_gaussian_pdf(inputY[icol]-centerY, sigma); |
| 703 | output[ibinX][binY]+=pdfX*pdfY; |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | else |
| 708 | ++output[binX][binY]; |
| 709 | } |
| 710 | } |
| 711 | if(verbose_opt[0]) |
| 712 | std::cout << "number of valid pixels: " << nvalid << endl; |
| 713 | |
| 714 | ostringstream tmpStream; |
| 715 | tmpStream.precision(10); |
| 716 | for(int binX=0;binX<nbin;++binX){ |
| 717 | std::cout << endl; |
| 718 | for(int binY=0;binY<nbin;++binY){ |
| 719 | double binValueX=0; |
| 720 | if(nbin==maxX-minX+1) |
| 721 | binValueX=minX+binX; |
| 722 | else |
| 723 | binValueX=minX+static_cast<double>(maxX-minX)*(binX+0.5)/nbin; |
| 724 | double binValueY=0; |
| 725 | if(nbin==maxY-minY+1) |
| 726 | binValueY=minY+binY; |
| 727 | else |
| 728 | binValueY=minY+static_cast<double>(maxY-minY)*(binY+0.5)/nbin; |
| 729 | |
| 730 | double value=static_cast<double>(output[binX][binY]); |
| 731 | |
| 732 | if(relative_opt[0]) |
| 733 | value*=100.0/nvalid; |
| 734 | |
| 735 | tmpStream << binValueX << " " << binValueY << " " << value << std::endl; |
| 736 | // double value=static_cast<double>(output[binX][binY])/nvalid; |
| 737 | // outputStream << (maxX-minX)*bin/(nbin-1)+minX << " " << (maxY-minY)*bin/(nbin-1)+minY << " " << value << std::endl; |
| 738 | mapString.insert(std::make_pair("kde",tmpStream.str())); |
| 739 | // mapString["kde"]=tmpStream.str(); |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | if(reg_opt&&size()<2){ |
| 744 | if(band_opt.size()<2) |
| 745 | continue; |
| 746 | imgreg.setDown(down_opt[0]); |
| 747 | imgreg.setThreshold(random_opt[0]); |
| 748 | double c0=0;//offset |
| 749 | double c1=1;//scale |
| 750 | double r2=imgreg.getR2(**imit,band_opt[0],band_opt[1],c0,c1,verbose_opt[0]); |
| 751 | // outputStream << "-c0 " << c0 << " -c1 " << c1 << " -r2 " << r2 << std::endl; |
| 752 | mapString.insert(std::make_pair("c0",type2string<double>(c0))); |
| 753 | mapString.insert(std::make_pair("c1",type2string<double>(c1))); |
| 754 | mapString.insert(std::make_pair("r2",type2string<double>(r2))); |
| 755 | // mapString["c0"]=type2string<double>(c0); |
| 756 | // mapString["c1"]=type2string<double>(c1); |
| 757 | // mapString["r2"]=type2string<double>(r2); |
| 758 | } |
| 759 | if(regerr_opt&&size()<2){ |
| 760 | if(band_opt.size()<2) |
| 761 | continue; |
| 762 | imgreg.setDown(down_opt[0]); |
| 763 | imgreg.setThreshold(random_opt[0]); |
| 764 | double c0=0;//offset |
| 765 | double c1=1;//scale |
| 766 | double err=imgreg.getRMSE(**imit,band_opt[0],band_opt[1],c0,c1,verbose_opt[0]); |
| 767 | // outputStream << "-c0 " << c0 << " -c1 " << c1 << " -rmse " << err << std::endl; |
| 768 | mapString.insert(std::make_pair("c0",type2string<double>(c0))); |
| 769 | mapString.insert(std::make_pair("c1",type2string<double>(c1))); |
| 770 | mapString.insert(std::make_pair("rmse",type2string<double>(err))); |
| 771 | // mapString["c0"]=type2string<double>(c0); |
| 772 | // mapString["c1"]=type2string<double>(c1); |
| 773 | // mapString["rmse"]=type2string<double>(err); |
| 774 | } |
| 775 | if(rmse_opt&&size()<2){ |
| 776 | if(band_opt.size()<2) |
| 777 | continue; |
| 778 | vector<double> xBuffer((*imit)->nrOfCol()); |
| 779 | vector<double> yBuffer((*imit)->nrOfCol()); |
| 780 | double mse=0; |
| 781 | double nValid=0; |
| 782 | double nPixel=(*imit)->nrOfCol()/down_opt[0]*(*imit)->nrOfRow()/down_opt[0]; |
| 783 | for(unsigned int irow;irow<(*imit)->nrOfRow();irow+=down_opt[0]){ |
| 784 | (*imit)->readData(xBuffer,irow,band_opt[0]); |
| 785 | (*imit)->readData(yBuffer,irow,band_opt[1]); |
| 786 | for(unsigned int icol;icol<(*imit)->nrOfCol();icol+=down_opt[0]){ |
| 787 | double xValue=xBuffer[icol]; |
| 788 | double yValue=yBuffer[icol]; |
| 789 | if((*imit)->isNoData(xValue)||(*imit)->isNoData(yValue)){ |
| 790 | continue; |
| 791 | } |
| 792 | if((*imit)->isNoData(xValue)||(*imit)->isNoData(yValue)){ |
| 793 | continue; |
| 794 | } |
| 795 | if(xValue<src_min_opt[0]||xValue>src_max_opt[0]||yValue<src_min_opt[0]||yValue>src_max_opt[0]) |
| 796 | continue; |
| 797 | ++nValid; |
| 798 | double e=xValue-yValue; |
| 799 | if(relative_opt[0]) |
| 800 | e/=yValue; |
| 801 | mse+=e*e/nPixel; |
| 802 | } |
| 803 | } |
| 804 | double correctNorm=nValid; |
| 805 | correctNorm/=nPixel; |
| 806 | mse/=correctNorm; |
| 807 | // outputStream << " -rmse " << sqrt(mse) << std::endl; |
| 808 | mapString.insert(std::make_pair("rmse",type2string<double>(sqrt(mse)))); |
| 809 | // mapString["rmse"]=type2string<double>(sqrt(mse)); |
| 810 | } |
| 811 | if(preg_opt&&size()<2){ |
| 812 | if(band_opt.size()<2) |
| 813 | continue; |
| 814 | imgreg.setDown(down_opt[0]); |
| 815 | imgreg.setThreshold(random_opt[0]); |
| 816 | double c0=0;//offset |
| 817 | double c1=1;//scale |
| 818 | double r2=imgreg.pgetR2(**imit,band_opt[0],band_opt[1],c0,c1,verbose_opt[0]); |
| 819 | // outputStream << "-c0 " << c0 << " -c1 " << c1 << " -r2 " << r2 << std::endl; |
| 820 | mapString.insert(std::make_pair("c0",type2string<double>(c0))); |
| 821 | mapString.insert(std::make_pair("c1",type2string<double>(c1))); |
| 822 | mapString.insert(std::make_pair("r2",type2string<double>(r2))); |
| 823 | // mapString["c0"]=type2string<double>(c0); |
| 824 | // mapString["c1"]=type2string<double>(c1); |
| 825 | // mapString["r2"]=type2string<double>(r2); |
| 826 | } |
| 827 | // imgReader.close(); |
| 828 | ++iimage; |
| 829 | } |
| 830 | // if(rmse_opt&&(input_opt.size()>1)){ |
| 831 | // while(band_opt.size()<input_opt.size()) |
| 832 | // band_opt.push_back(band_opt[0]); |
| 833 | // if(src_min_opt.size()){ |
| 834 | // while(src_min_opt.size()<input_opt.size()) |
| 835 | // src_min_opt.push_back(src_min_opt[0]); |
| 836 | // } |
| 837 | // if(src_max_opt.size()){ |
| 838 | // while(src_max_opt.size()<input_opt.size()) |
| 839 | // src_max_opt.push_back(src_max_opt[0]); |
| 840 | // } |
| 841 | // Jim imgReader1(input_opt[0]); |
| 842 | // Jim imgReader2(input_opt[1]); |
| 843 | |
| 844 | // if(offset_opt.size()) |
| 845 | // imgReader1.setOffset(offset_opt[0],band_opt[0]); |
| 846 | // if(scale_opt.size()) |
| 847 | // imgReader1.setScale(scale_opt[0],band_opt[0]); |
| 848 | // if(offset_opt.size()>1) |
| 849 | // imgReader2.setOffset(offset_opt[1],band_opt[1]); |
| 850 | // if(scale_opt.size()>1) |
| 851 | // imgReader2.setScale(scale_opt[1],band_opt[1]); |
| 852 | |
| 853 | // for(int inodata=0;inodata<nodata_opt.size();++inodata){ |
| 854 | // imgReader1.pushNoDataValue(nodata_opt[inodata]); |
| 855 | // imgReader2.pushNoDataValue(nodata_opt[inodata]); |
| 856 | // } |
| 857 | // vector<double> xBuffer(imgReader1.nrOfCol()); |
| 858 | // vector<double> yBuffer(imgReader2.nrOfCol()); |
| 859 | // double mse=0; |
| 860 | // double nValid=0; |
| 861 | // double nPixel=imgReader.nrOfCol()/imgReader.nrOfRow()/down_opt[0]/down_opt[0]; |
| 862 | // for(unsigned int irow;irow<imgReader1.nrOfRow();irow+=down_opt[0]){ |
| 863 | // double irow1=irow; |
| 864 | // double irow2=0; |
| 865 | // double icol1=0; |
| 866 | // double icol2=0; |
| 867 | // double geoX=0; |
| 868 | // double geoY=0; |
| 869 | // imgReader1.image2geo(icol1,irow1,geoX,geoY); |
| 870 | // imgReader2.geo2image(geoX,geoY,icol2,irow2); |
| 871 | // irow2=static_cast<int>(irow2); |
| 872 | // imgReader1.readData(xBuffer,irow1,band_opt[0]); |
| 873 | // imgReader2.readData(yBuffer,irow2,band_opt[1]); |
| 874 | // for(unsigned int icol;icol<imgReader.nrOfCol();icol+=down_opt[0]){ |
| 875 | // icol1=icol; |
| 876 | // imgReader1.image2geo(icol1,irow1,geoX,geoY); |
| 877 | // imgReader2.geo2image(geoX,geoY,icol2,irow2); |
| 878 | // double xValue=xBuffer[icol1]; |
| 879 | // double yValue=yBuffer[icol2]; |
| 880 | // if(imgReader.isNoData(xValue)||imgReader.isNoData(yValue)){ |
| 881 | // continue; |
| 882 | // } |
| 883 | // if(xValue<src_min_opt[0]||xValue>src_max_opt[0]||yValue<src_min_opt[1]||yValue>src_max_opt[1]) |
| 884 | // continue; |
| 885 | // ++nValid; |
| 886 | // double e=xValue-yValue; |
| 887 | // if(relative_opt[0]) |
| 888 | // e/=yValue; |
| 889 | // mse+=e*e/nPixel; |
| 890 | // } |
| 891 | // } |
| 892 | // double correctNorm=nValid; |
| 893 | // correctNorm/=nPixel; |
| 894 | // mse/=correctNorm; |
| 895 | // std::cout << " -rmse " << sqrt(mse) << std::endl; |
| 896 | // } |
| 897 | if(reg_opt&&(size()>1)){ |
| 898 | imgreg.setDown(down_opt[0]); |
| 899 | imgreg.setThreshold(random_opt[0]); |
| 900 | double c0=0;//offset |
| 901 | double c1=1;//scale |
| 902 | while(band_opt.size()<size()) |
| 903 | band_opt.push_back(band_opt[0]); |
| 904 | if(src_min_opt.size()){ |
| 905 | while(src_min_opt.size()<size()) |
| 906 | src_min_opt.push_back(src_min_opt[0]); |
| 907 | } |
| 908 | if(src_max_opt.size()){ |
| 909 | while(src_max_opt.size()<size()) |
| 910 | src_max_opt.push_back(src_max_opt[0]); |
| 911 | } |
| 912 | // Jim imgReader1(input_opt[0]); |
| 913 | // Jim imgReader2(input_opt[1]); |
| 914 | |
| 915 | if(offset_opt.size()) |
| 916 | getImage(0)->setOffset(offset_opt[0],band_opt[0]); |
| 917 | if(scale_opt.size()) |
| 918 | getImage(0)->setScale(scale_opt[0],band_opt[0]); |
| 919 | if(offset_opt.size()>1) |
| 920 | getImage(1)->setOffset(offset_opt[1],band_opt[1]); |
| 921 | if(scale_opt.size()>1) |
| 922 | getImage(1)->setScale(scale_opt[1],band_opt[1]); |
| 923 | |
| 924 | for(int inodata=0;inodata<nodata_opt.size();++inodata){ |
| 925 | if(!inodata){ |
| 926 | getImage(0)->GDALSetNoDataValue(nodata_opt[0],band_opt[0]);//only single no data can be set in GDALRasterBand (used for ComputeStatistics) |
| 927 | getImage(1)->GDALSetNoDataValue(nodata_opt[0],band_opt[1]);//only single no data can be set in GDALRasterBand (used for ComputeStatistics) |
| 928 | } |
| 929 | getImage(0)->pushNoDataValue(nodata_opt[inodata]); |
| 930 | getImage(1)->pushNoDataValue(nodata_opt[inodata]); |
| 931 | } |
| 932 | |
| 933 | double r2=imgreg.getR2(*(getImage(0)),*(getImage(1)),c0,c1,band_opt[0],band_opt[1],verbose_opt[0]); |
| 934 | // outputStream << "-c0 " << c0 << " -c1 " << c1 << " -r2 " << r2 << std::endl; |
| 935 | mapString.insert(std::make_pair("c0",type2string<double>(c0))); |
| 936 | mapString.insert(std::make_pair("c1",type2string<double>(c1))); |
| 937 | mapString.insert(std::make_pair("r2",type2string<double>(r2))); |
| 938 | // mapString["c0"]=type2string<double>(c0); |
| 939 | // mapString["c1"]=type2string<double>(c1); |
| 940 | // mapString["r2"]=type2string<double>(r2); |
| 941 | // imgReader1.close(); |
| 942 | // imgReader2.close(); |
| 943 | } |
| 944 | if(preg_opt&&(size()>1)){ |
| 945 | imgreg.setDown(down_opt[0]); |
| 946 | imgreg.setThreshold(random_opt[0]); |
| 947 | double c0=0;//offset |
| 948 | double c1=1;//scale |
| 949 | while(band_opt.size()<size()) |
| 950 | band_opt.push_back(band_opt[0]); |
| 951 | if(src_min_opt.size()){ |
| 952 | while(src_min_opt.size()<size()) |
| 953 | src_min_opt.push_back(src_min_opt[0]); |
| 954 | } |
| 955 | if(src_max_opt.size()){ |
| 956 | while(src_max_opt.size()<size()) |
| 957 | src_max_opt.push_back(src_max_opt[0]); |
| 958 | } |
| 959 | // Jim imgReader1(input_opt[0]); |
| 960 | // Jim imgReader2(input_opt[1]); |
| 961 | |
| 962 | if(offset_opt.size()) |
| 963 | getImage(0)->setOffset(offset_opt[0],band_opt[0]); |
| 964 | if(scale_opt.size()) |
| 965 | getImage(0)->setScale(scale_opt[0],band_opt[0]); |
| 966 | if(offset_opt.size()>1) |
| 967 | getImage(1)->setOffset(offset_opt[1],band_opt[1]); |
| 968 | if(scale_opt.size()>1) |
| 969 | getImage(1)->setScale(scale_opt[1],band_opt[1]); |
| 970 | |
| 971 | for(int inodata=0;inodata<nodata_opt.size();++inodata){ |
| 972 | if(!inodata){ |
| 973 | getImage(0)->GDALSetNoDataValue(nodata_opt[0],band_opt[0]);//only single no data can be set in GDALRasterBand (used for ComputeStatistics) |
| 974 | getImage(1)->GDALSetNoDataValue(nodata_opt[0],band_opt[1]);//only single no data can be set in GDALRasterBand (used for ComputeStatistics) |
| 975 | } |
| 976 | getImage(0)->pushNoDataValue(nodata_opt[inodata]); |
| 977 | getImage(1)->pushNoDataValue(nodata_opt[inodata]); |
| 978 | } |
| 979 | |
| 980 | double r2=imgreg.pgetR2(*(getImage(0)),*(getImage(1)),c0,c1,band_opt[0],band_opt[1],verbose_opt[0]); |
| 981 | // outputStream << "-c0 " << c0 << " -c1 " << c1 << " -r2 " << r2 << std::endl; |
| 982 | mapString.insert(std::make_pair("c0",type2string<double>(c0))); |
| 983 | mapString.insert(std::make_pair("c1",type2string<double>(c1))); |
| 984 | mapString.insert(std::make_pair("r2",type2string<double>(r2))); |
| 985 | // mapString["c0"]=type2string<double>(c0); |
| 986 | // mapString["c1"]=type2string<double>(c1); |
| 987 | // mapString["r2"]=type2string<double>(r2); |
| 988 | // imgReader1.close(); |
| 989 | // imgReader2.close(); |
| 990 | } |
| 991 | if(regerr_opt&&(size()>1)){ |
| 992 | imgreg.setDown(down_opt[0]); |
| 993 | imgreg.setThreshold(random_opt[0]); |
| 994 | double c0=0;//offset |
| 995 | double c1=1;//scale |
| 996 | while(band_opt.size()<size()) |
| 997 | band_opt.push_back(band_opt[0]); |
| 998 | if(src_min_opt.size()){ |
| 999 | while(src_min_opt.size()<size()) |
| 1000 | src_min_opt.push_back(src_min_opt[0]); |
| 1001 | } |
| 1002 | if(src_max_opt.size()){ |
| 1003 | while(src_max_opt.size()<size()) |
| 1004 | src_max_opt.push_back(src_max_opt[0]); |
| 1005 | } |
| 1006 | // Jim imgReader1(input_opt[0]); |
| 1007 | // Jim imgReader2(input_opt[1]); |
| 1008 | |
| 1009 | if(offset_opt.size()) |
| 1010 | getImage(0)->setOffset(offset_opt[0],band_opt[0]); |
| 1011 | if(scale_opt.size()) |
| 1012 | getImage(0)->setScale(scale_opt[0],band_opt[0]); |
| 1013 | if(offset_opt.size()>1) |
| 1014 | getImage(1)->setOffset(offset_opt[1],band_opt[1]); |
| 1015 | if(scale_opt.size()>1) |
| 1016 | getImage(1)->setScale(scale_opt[1],band_opt[1]); |
| 1017 | |
| 1018 | for(int inodata=0;inodata<nodata_opt.size();++inodata){ |
| 1019 | if(!inodata){ |
| 1020 | getImage(0)->GDALSetNoDataValue(nodata_opt[0],band_opt[0]);//only single no data can be set in GDALRasterBand (used for ComputeStatistics) |
| 1021 | getImage(1)->GDALSetNoDataValue(nodata_opt[0],band_opt[1]);//only single no data can be set in GDALRasterBand (used for ComputeStatistics) |
| 1022 | } |
| 1023 | getImage(0)->pushNoDataValue(nodata_opt[inodata]); |
| 1024 | getImage(1)->pushNoDataValue(nodata_opt[inodata]); |
| 1025 | } |
| 1026 | |
| 1027 | double err=imgreg.getRMSE(*(getImage(0)),*(getImage(1)),c0,c1,band_opt[0],band_opt[1],verbose_opt[0]); |
| 1028 | // outputStream << "-c0 " << c0 << " -c1 " << c1 << " -rmse " << err << std::endl; |
| 1029 | mapString.insert(std::make_pair("c0",type2string<double>(c0))); |
| 1030 | mapString.insert(std::make_pair("c1",type2string<double>(c1))); |
| 1031 | mapString.insert(std::make_pair("err",type2string<double>(err))); |
| 1032 | // mapString["c0"]=type2string<double>(c0); |
| 1033 | // mapString["c1"]=type2string<double>(c1); |
| 1034 | // mapString["err"]=type2string<double>(err); |
| 1035 | // imgReader1.close(); |
| 1036 | // imgReader2.close(); |
| 1037 | } |
| 1038 | if(rmse_opt&&(size()>1)){ |
| 1039 | imgreg.setDown(down_opt[0]); |
| 1040 | imgreg.setThreshold(random_opt[0]); |
| 1041 | double c0=0;//offset |
| 1042 | double c1=1;//scale |
| 1043 | while(band_opt.size()<size()) |
| 1044 | band_opt.push_back(band_opt[0]); |
| 1045 | if(src_min_opt.size()){ |
| 1046 | while(src_min_opt.size()<size()) |
| 1047 | src_min_opt.push_back(src_min_opt[0]); |
| 1048 | } |
| 1049 | if(src_max_opt.size()){ |
| 1050 | while(src_max_opt.size()<size()) |
| 1051 | src_max_opt.push_back(src_max_opt[0]); |
| 1052 | } |
| 1053 | // Jim imgReader1(input_opt[0]); |
| 1054 | // Jim imgReader2(input_opt[1]); |
| 1055 | |
| 1056 | if(offset_opt.size()) |
| 1057 | getImage(0)->setOffset(offset_opt[0],band_opt[0]); |
| 1058 | if(scale_opt.size()) |
| 1059 | getImage(0)->setScale(scale_opt[0],band_opt[0]); |
| 1060 | if(offset_opt.size()>1) |
| 1061 | getImage(1)->setOffset(offset_opt[1],band_opt[1]); |
| 1062 | if(scale_opt.size()>1) |
| 1063 | getImage(1)->setScale(scale_opt[1],band_opt[1]); |
| 1064 | |
| 1065 | for(int inodata=0;inodata<nodata_opt.size();++inodata){ |
| 1066 | if(!inodata){ |
| 1067 | getImage(0)->GDALSetNoDataValue(nodata_opt[0],band_opt[0]);//only single no data can be set in GDALRasterBand (used for ComputeStatistics) |
| 1068 | getImage(1)->GDALSetNoDataValue(nodata_opt[0],band_opt[1]);//only single no data can be set in GDALRasterBand (used for ComputeStatistics) |
| 1069 | } |
| 1070 | getImage(0)->pushNoDataValue(nodata_opt[inodata]); |
| 1071 | getImage(1)->pushNoDataValue(nodata_opt[inodata]); |
| 1072 | } |
| 1073 | |
| 1074 | double err=imgreg.getRMSE(*(getImage(0)),*(getImage(1)),c0,c1,band_opt[0],band_opt[1],verbose_opt[0]); |
| 1075 | // outputStream << "-rmse " << err << std::endl; |
| 1076 | mapString.insert(std::make_pair("rmse",type2string<double>(err))); |
| 1077 | // mapString["rmse"]=type2string<double>(err); |
| 1078 | // imgReader1.close(); |
| 1079 | // imgReader2.close(); |
| 1080 | } |
| 1081 | if(histogram2d_opt&&(size()>1)){ |
| 1082 | while(band_opt.size()<size()) |
| 1083 | band_opt.push_back(band_opt[0]); |
| 1084 | if(src_min_opt.size()){ |
| 1085 | while(src_min_opt.size()<size()) |
| 1086 | src_min_opt.push_back(src_min_opt[0]); |
| 1087 | } |
| 1088 | if(src_max_opt.size()){ |
| 1089 | while(src_max_opt.size()<size()) |
| 1090 | src_max_opt.push_back(src_max_opt[0]); |
| 1091 | } |
| 1092 | // Jim imgReader1(input_opt[0]); |
| 1093 | // Jim imgReader2(input_opt[1]); |
| 1094 | |
| 1095 | if(offset_opt.size()) |
| 1096 | getImage(0)->setOffset(offset_opt[0],band_opt[0]); |
| 1097 | if(scale_opt.size()) |
| 1098 | getImage(0)->setScale(scale_opt[0],band_opt[0]); |
| 1099 | if(offset_opt.size()>1) |
| 1100 | getImage(1)->setOffset(offset_opt[1],band_opt[1]); |
| 1101 | if(scale_opt.size()>1) |
| 1102 | getImage(1)->setScale(scale_opt[1],band_opt[1]); |
| 1103 | |
| 1104 | for(int inodata=0;inodata<nodata_opt.size();++inodata){ |
| 1105 | if(!inodata){ |
| 1106 | getImage(0)->GDALSetNoDataValue(nodata_opt[0],band_opt[0]);//only single no data can be set in GDALRasterBand (used for ComputeStatistics) |
| 1107 | getImage(1)->GDALSetNoDataValue(nodata_opt[0],band_opt[1]);//only single no data can be set in GDALRasterBand (used for ComputeStatistics) |
| 1108 | } |
| 1109 | getImage(0)->pushNoDataValue(nodata_opt[inodata]); |
| 1110 | getImage(1)->pushNoDataValue(nodata_opt[inodata]); |
| 1111 | } |
| 1112 | |
| 1113 | getImage(0)->getMinMax(minX,maxX,band_opt[0]); |
| 1114 | getImage(1)->getMinMax(minY,maxY,band_opt[1]); |
| 1115 | |
| 1116 | if(verbose_opt[0]){ |
| 1117 | std::cout << "minX: " << minX << endl; |
| 1118 | std::cout << "maxX: " << maxX << endl; |
| 1119 | std::cout << "minY: " << minY << endl; |
| 1120 | std::cout << "maxY: " << maxY << endl; |
| 1121 | } |
| 1122 | |
| 1123 | if(src_min_opt.size()){ |
| 1124 | minX=src_min_opt[0]; |
| 1125 | minY=src_min_opt[1]; |
| 1126 | } |
| 1127 | if(src_max_opt.size()){ |
| 1128 | maxX=src_max_opt[0]; |
| 1129 | maxY=src_max_opt[1]; |
| 1130 | } |
| 1131 | |
| 1132 | nbin=(nbin_opt.size())? nbin_opt[0]:0; |
| 1133 | if(nbin<=1){ |
| 1134 | std::cerr << "Warning: number of bins not defined, calculating bins from min and max value" << std::endl; |
| 1135 | // getImage(0)->getMinMax(minX,maxX,band_opt[0]); |
| 1136 | // imgReader2.getMinMax(minY,maxY,band_opt[0]); |
| 1137 | if(minX>=maxX) |
| 1138 | getImage(0)->getMinMax(minX,maxX,band_opt[0]); |
| 1139 | if(minY>=maxY) |
| 1140 | getImage(1)->getMinMax(minY,maxY,band_opt[1]); |
| 1141 | |
| 1142 | minValue=(minX<minY)? minX:minY; |
| 1143 | maxValue=(maxX>maxY)? maxX:maxY; |
| 1144 | if(verbose_opt[0]) |
| 1145 | std::cout << "min and max values: " << minValue << ", " << maxValue << std::endl; |
| 1146 | nbin=maxValue-minValue+1; |
| 1147 | } |
| 1148 | assert(nbin>1); |
| 1149 | double sigma=0; |
| 1150 | //kernel density estimation as in http://en.wikipedia.org/wiki/Kernel_density_estimation |
| 1151 | if(kde_opt[0]){ |
| 1152 | GDALProgressFunc pfnProgress; |
| 1153 | void* pProgressData; |
| 1154 | GDALRasterBand* rasterBand; |
| 1155 | double stdDev1=0; |
| 1156 | double stdDev2=0; |
| 1157 | rasterBand=getImage(0)->getRasterBand(band_opt[0]); |
| 1158 | rasterBand->ComputeStatistics(0,&minValue,&maxValue,&meanValue,&stdDev1,pfnProgress,pProgressData); |
| 1159 | rasterBand=getImage(1)->getRasterBand(band_opt[0]); |
| 1160 | rasterBand->ComputeStatistics(0,&minValue,&maxValue,&meanValue,&stdDev2,pfnProgress,pProgressData); |
| 1161 | |
| 1162 | //todo: think of smarter way how to estimate size (nodata!) |
| 1163 | // double estimatedSize=1.0*(*imit)->getNvalid(band_opt[0])/down_opt[0]/down_opt[0]; |
| 1164 | double estimatedSize=1.0*getImage(0)->getNvalid(band_opt[0])/down_opt[0]/down_opt[0]; |
| 1165 | if(random_opt[0]>0) |
| 1166 | estimatedSize*=random_opt[0]/100.0; |
| 1167 | sigma=1.06*sqrt(stdDev1*stdDev2)*pow(estimatedSize,-0.2); |
| 1168 | } |
| 1169 | assert(nbin); |
| 1170 | if(verbose_opt[0]){ |
| 1171 | if(sigma>0) |
| 1172 | std::cout << "calculating 2d kernel density estimate with sigma " << sigma << " for datasets " << getImage(0)->getFileName() << " and " << getImage(1)->getFileName() << std::endl; |
| 1173 | else |
| 1174 | std::cout << "calculating 2d histogram for datasets " << getImage(0)->getFileName() << " and " << getImage(1)->getFileName() << std::endl; |
| 1175 | std::cout << "nbin: " << nbin << std::endl; |
| 1176 | } |
| 1177 | |
| 1178 | vector< vector<double> > output; |
| 1179 | |
| 1180 | if(maxX<=minX) |
| 1181 | getImage(0)->getMinMax(minX,maxX,band_opt[0]); |
| 1182 | if(maxY<=minY) |
| 1183 | getImage(1)->getMinMax(minY,maxY,band_opt[1]); |
| 1184 | |
| 1185 | if(maxX<=minX){ |
| 1186 | std::ostringstream s; |
| 1187 | s<<"Error: could not calculate distribution (minX>=maxX)"; |
| 1188 | throw(s.str()); |
| 1189 | } |
| 1190 | if(maxY<=minY){ |
| 1191 | std::ostringstream s; |
| 1192 | s<<"Error: could not calculate distribution (minY>=maxY)"; |
| 1193 | throw(s.str()); |
| 1194 | } |
| 1195 | if(verbose_opt[0]){ |
| 1196 | std::cout << "minX: " << minX << endl; |
| 1197 | std::cout << "maxX: " << maxX << endl; |
| 1198 | std::cout << "minY: " << minY << endl; |
| 1199 | std::cout << "maxY: " << maxY << endl; |
| 1200 | } |
| 1201 | output.resize(nbin); |
| 1202 | for(int i=0;i<nbin;++i){ |
| 1203 | output[i].resize(nbin); |
| 1204 | for(int j=0;j<nbin;++j) |
| 1205 | output[i][j]=0; |
| 1206 | } |
| 1207 | int binX=0; |
| 1208 | int binY=0; |
| 1209 | vector<double> inputX(getImage(0)->nrOfCol()); |
| 1210 | vector<double> inputY(getImage(1)->nrOfCol()); |
| 1211 | double nvalid=0; |
| 1212 | double geoX=0; |
| 1213 | double geoY=0; |
| 1214 | double icol1=0; |
| 1215 | double irow1=0; |
| 1216 | double icol2=0; |
| 1217 | double irow2=0; |
| 1218 | for(unsigned int irow=0;irow<getImage(0)->nrOfRow();++irow){ |
| 1219 | if(irow%down_opt[0]) |
| 1220 | continue; |
| 1221 | irow1=irow; |
| 1222 | getImage(0)->image2geo(icol1,irow1,geoX,geoY); |
| 1223 | getImage(1)->geo2image(geoX,geoY,icol2,irow2); |
| 1224 | irow2=static_cast<int>(irow2); |
| 1225 | getImage(0)->readData(inputX,irow1,band_opt[0]); |
| 1226 | getImage(1)->readData(inputY,irow2,band_opt[1]); |
| 1227 | for(unsigned int icol=0;icol<getImage(0)->nrOfCol();++icol){ |
| 1228 | if(icol%down_opt[0]) |
| 1229 | continue; |
| 1230 | icol1=icol; |
| 1231 | if(random_opt[0]>0){ |
| 1232 | double p=static_cast<double>(rand())/(RAND_MAX); |
| 1233 | p*=100.0; |
| 1234 | if(p>random_opt[0]) |
| 1235 | continue;//do not select for now, go to next column |
| 1236 | } |
| 1237 | if(getImage(0)->isNoData(inputX[icol])) |
| 1238 | continue; |
| 1239 | getImage(0)->image2geo(icol1,irow1,geoX,geoY); |
| 1240 | getImage(1)->geo2image(geoX,geoY,icol2,irow2); |
| 1241 | icol2=static_cast<int>(icol2); |
| 1242 | if(getImage(1)->isNoData(inputY[icol2])) |
| 1243 | continue; |
| 1244 | // ++nvalid; |
| 1245 | if(inputX[icol1]>=maxX) |
| 1246 | binX=nbin-1; |
| 1247 | else if(inputX[icol]<=minX) |
| 1248 | binX=0; |
| 1249 | else |
| 1250 | binX=static_cast<int>(static_cast<double>(inputX[icol1]-minX)/(maxX-minX)*nbin); |
| 1251 | if(inputY[icol2]>=maxY) |
| 1252 | binY=nbin-1; |
| 1253 | else if(inputY[icol2]<=minY) |
| 1254 | binY=0; |
| 1255 | else |
| 1256 | binY=static_cast<int>(static_cast<double>(inputY[icol2]-minY)/(maxY-minY)*nbin); |
| 1257 | assert(binX>=0); |
| 1258 | assert(binX<output.size()); |
| 1259 | assert(binY>=0); |
| 1260 | assert(binY<output[binX].size()); |
| 1261 | if(sigma>0){ |
| 1262 | //create kde for Gaussian basis function |
| 1263 | //todo: speed up by calculating first and last bin with non-zero contriubtion... |
| 1264 | for(int ibinX=0;ibinX<nbin;++ibinX){ |
| 1265 | double centerX=minX+static_cast<double>(maxX-minX)*ibinX/nbin; |
| 1266 | double pdfX=gsl_ran_gaussian_pdf(inputX[icol1]-centerX, sigma); |
| 1267 | for(int ibinY=0;ibinY<nbin;++ibinY){ |
| 1268 | //calculate \integral_ibinX^(ibinX+1) |
| 1269 | double centerY=minY+static_cast<double>(maxY-minY)*ibinY/nbin; |
| 1270 | double pdfY=gsl_ran_gaussian_pdf(inputY[icol2]-centerY, sigma); |
| 1271 | output[ibinX][binY]+=pdfX*pdfY; |
| 1272 | nvalid+=pdfX*pdfY; |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | else{ |
| 1277 | ++output[binX][binY]; |
| 1278 | ++nvalid; |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | if(verbose_opt[0]) |
| 1283 | std::cout << "number of valid pixels: " << nvalid << endl; |
| 1284 | ostringstream tmpStream; |
| 1285 | tmpStream.precision(10); |
| 1286 | for(int binX=0;binX<nbin;++binX){ |
| 1287 | std::cout << endl; |
| 1288 | for(int binY=0;binY<nbin;++binY){ |
| 1289 | double binValueX=0; |
| 1290 | if(nbin==maxX-minX+1) |
| 1291 | binValueX=minX+binX; |
| 1292 | else |
| 1293 | binValueX=minX+static_cast<double>(maxX-minX)*(binX+0.5)/nbin; |
| 1294 | double binValueY=0; |
| 1295 | if(nbin==maxY-minY+1) |
| 1296 | binValueY=minY+binY; |
| 1297 | else |
| 1298 | binValueY=minY+static_cast<double>(maxY-minY)*(binY+0.5)/nbin; |
| 1299 | double value=static_cast<double>(output[binX][binY]); |
| 1300 | |
| 1301 | if(relative_opt[0]||kde_opt[0]) |
| 1302 | value*=100.0/nvalid; |
| 1303 | |
| 1304 | tmpStream << binValueX << " " << binValueY << " " << value << std::endl; |
| 1305 | // double value=static_cast<double>(output[binX][binY])/nvalid; |
| 1306 | // cout << (maxX-minX)*bin/(nbin-1)+minX << " " << (maxY-minY)*bin/(nbin-1)+minY << " " << value << std::endl; |
| 1307 | } |
| 1308 | } |
| 1309 | mapString.insert(std::make_pair("histogram2d",tmpStream.str())); |
| 1310 | // mapString["histogram2d"]=tmpStream.str(); |
| 1311 | // imgReader1.close(); |
| 1312 | // imgReader2.close(); |
| 1313 | } |
| 1314 | |
| 1315 | if(!histogram_opt||histogram2d_opt) |
| 1316 | std::cout << std::endl; |
| 1317 | |
| 1318 | // if(output_opt.size()){ |
| 1319 | // ofstream outputFile; |
| 1320 | // outputFile.open(output_opt[0].c_str(),ios::out); |
| 1321 | // outputFile << outputStream.str(); |
| 1322 | // } |
| 1323 | // else{ |
| 1324 | // #if(JIPLIB_BUILD_WITH_PYTHON==1) |
| 1325 | // PySys_WriteStdout("%s",outputStream.str().c_str()); |
| 1326 | // #else |
| 1327 | // std::cout << outputStream.str(); |
| 1328 | // #endif |
| 1329 | // } |
| 1330 | //reset nodata values |
| 1331 | if(nodata_opt.size()){ |
| 1332 | size_t iimage=0; |
| 1333 | for(imit=begin();imit!=end();++imit){ |
| 1334 | (*imit)->setNoData(orignodata[iimage]); |
| 1335 | ++iimage; |
| 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | catch(string predefinedString){ |
| 1340 | // #if(BUILD_WITH_PYTHON==1) |
| 1341 | // PySys_WriteStdout("%s",predefinedString.c_str()); |
| 1342 | // #else |
| 1343 | std::cerr << predefinedString << std::endl; |
| 1344 | // #endif |
| 1345 | throw; |
| 1346 | } |
| 1347 | // return(*this); |
| 1348 | return(mapString); |
| 1349 | } |
| 1350 | |
| 1351 | // int nband=(band_opt.size()) ? band_opt.size() : imgReader.nrOfBand(); |
| 1352 | |
| 1353 | // const char* pszMessage; |
| 1354 | // void* pProgressArg=NULL; |
| 1355 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 1356 | // double progress=0; |
| 1357 | // srand(time(NULL)); |
| 1358 | |
| 1359 | |
| 1360 | // statfactory::StatFactory stat; |
| 1361 | // imgregression::ImgRegression imgreg; |
| 1362 | |
| 1363 | // pfnProgress(progress,pszMessage,pProgressArg); |
| 1364 | // for(irow=0;irow<classReader.nrOfRow();++irow){ |
| 1365 | // if(irow%down_opt[0]) |
| 1366 | // continue; |
| 1367 | // // classReader.readData(classBuffer,irow); |
| 1368 | // classReader.readData(classBuffer,irow); |
| 1369 | // double x,y;//geo coordinates |
| 1370 | // double iimg,jimg;//image coordinates in img image |
| 1371 | // for(icol=0;icol<classReader.nrOfCol();++icol){ |
| 1372 | // if(icol%down_opt[0]) |
| 1373 | // continue; |
| 1374 | |
| 1375 | |
| 1376 | // if(rand_opt[0]>0){ |
| 1377 | // gsl_rng* r=stat.getRandomGenerator(time(NULL)); |
| 1378 | // //todo: init random number generator using time... |
| 1379 | // if(verbose_opt[0]) |
| 1380 | // std::cout << "generating " << rand_opt[0] << " random numbers: " << std::endl; |
| 1381 | // for(unsigned int i=0;i<rand_opt[0];++i) |
| 1382 | // std::cout << i << " " << stat.getRandomValue(r,randdist_opt[0],randa_opt[0],randb_opt[0]) << std::endl; |
| 1383 | // } |
| 1384 | |
| 1385 | // imgreg.setDown(down_opt[0]); |
| 1386 | // imgreg.setThreshold(threshold_opt[0]); |
| 1387 | // double c0=0;//offset |
| 1388 | // double c1=1;//scale |
| 1389 | // double err=uncertNodata_opt[0];//start with high initial value in case we do not have first ob err=imgreg.getRMSE(imgReaderModel1,imgReader,c0,c1,verbose_opt[0]); |
| 1390 | |
| 1391 | // int nband=band_opt.size(); |
| 1392 | // if(band_opt[0]<0) |
| 1393 | // nband=imgReader.nrOfBand(); |
| 1394 | // for(int iband=0;iband<nband;++iband){ |
| 1395 | // unsigned short band_opt[iband]=(band_opt[0]<0)? iband : band_opt[iband]; |
| 1396 | |
| 1397 | // if(minmax_opt[0]||min_opt[0]||max_opt[0]){ |
| 1398 | // assert(band_opt[iband]<imgReader.nrOfBand()); |
| 1399 | // if((ulx_opt.size()||uly_opt.size()||lrx_opt.size()||lry_opt.size())&&(imgReader.covers(ulx_opt[0],uly_opt[0],lrx_opt[0],lry_opt[0]))){ |
| 1400 | // double uli,ulj,lri,lrj; |
| 1401 | // imgReader.geo2image(ulx_opt[0],uly_opt[0],uli,ulj); |
| 1402 | // imgReader.geo2image(lrx_opt[0],lry_opt[0],lri,lrj); |
| 1403 | // imgReader.getMinMax(static_cast<int>(uli),static_cast<int>(lri),static_cast<int>(ulj),static_cast<int>(lrj),band_opt[iband],minValue,maxValue); |
| 1404 | // } |
| 1405 | // else |
| 1406 | // imgReader.getMinMax(minValue,maxValue,band_opt[iband],true); |
| 1407 | // if(minmax_opt[0]) |
| 1408 | // std::cout << "-min " << minValue << " -max " << maxValue << " "; |
| 1409 | // else{ |
| 1410 | // if(min_opt[0]) |
| 1411 | // std::cout << "-min " << minValue << " "; |
| 1412 | // if(max_opt[0]) |
| 1413 | // std::cout << "-max " << maxValue << " "; |
| 1414 | // } |
| 1415 | // } |
| 1416 | // } |
| 1417 | // if(relative_opt[0]) |
| 1418 | // hist_opt[0]=true; |
| 1419 | // if(hist_opt[0]){ |
| 1420 | // assert(band_opt[0]<imgReader.nrOfBand()); |
| 1421 | // unsigned int nbin=(nbin_opt.size())? nbin_opt[0]:0; |
| 1422 | // std::vector<unsigned long int> output; |
| 1423 | // minValue=0; |
| 1424 | // maxValue=0; |
| 1425 | // //todo: optimize such that getMinMax is only called once... |
| 1426 | // imgReader.getMinMax(minValue,maxValue,band_opt[0]); |
| 1427 | |
| 1428 | // if(src_min_opt.size()) |
| 1429 | // minValue=src_min_opt[0]; |
| 1430 | // if(src_max_opt.size()) |
| 1431 | // maxValue=src_max_opt[0]; |
| 1432 | // unsigned long int nsample=imgReader.getHistogram(output,minValue,maxValue,nbin,band_opt[0]); |
| 1433 | // std::cout.precision(10); |
| 1434 | // for(int bin=0;bin<nbin;++bin){ |
| 1435 | // double binValue=0; |
| 1436 | // if(nbin==maxValue-minValue+1) |
| 1437 | // binValue=minValue+bin; |
| 1438 | // else |
| 1439 | // binValue=minValue+static_cast<double>(maxValue-minValue)*(bin+0.5)/nbin; |
| 1440 | // std::cout << binValue << " "; |
| 1441 | // if(relative_opt[0]) |
| 1442 | // std::cout << 100.0*static_cast<double>(output[bin])/static_cast<double>(nsample) << std::endl; |
| 1443 | // else |
| 1444 | // std::cout << static_cast<double>(output[bin]) << std::endl; |
| 1445 | // } |
| 1446 | // } |