| 1 | /********************************************************************** |
| 2 | jlcomposite_lib.cc: program to mosaic and composite geo-referenced images |
| 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 <algorithm> |
| 22 | #include <vector> |
| 23 | #include <iostream> |
| 24 | #include <string> |
| 25 | #include <memory> |
| 26 | #include "Jim.h" |
| 27 | #include "JimList.h" |
| 28 | #include "VectorOgr.h" |
| 29 | #include "base/Vector2d.h" |
| 30 | #include "base/Optionjl.h" |
| 31 | #include "algorithms/StatFactory.h" |
| 32 | #include "algorithms/Egcs.h" |
| 33 | #include "apps/AppFactory.h" |
| 34 | |
| 35 | using namespace std; |
| 36 | using namespace app; |
| 37 | |
| 38 | /** |
| 39 | * @param app application specific option arguments |
| 40 | * @return output image |
| 41 | **/ |
| 42 | shared_ptr<Jim> JimList::composite(AppFactory& app){ |
| 43 | shared_ptr<Jim> imgWriter=Jim::createImg(); |
| 44 | composite(*imgWriter, app); |
| 45 | return(imgWriter); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param imgWriter output raster composite dataset |
| 50 | * @param app application specific option arguments |
| 51 | **/ |
| 52 | void JimList::composite(Jim& imgWriter, AppFactory& app){ |
| 53 | Optionjl<unsigned int> band_opt("b", "band", "band index(es) to crop (leave empty if all bands must be retained)"); |
| 54 | Optionjl<double> dx_opt("dx", "dx", "Output resolution in x (in meter) (empty: keep original resolution)"); |
| 55 | Optionjl<double> dy_opt("dy", "dy", "Output resolution in y (in meter) (empty: keep original resolution)"); |
| 56 | Optionjl<string> extent_opt("e", "extent", "get boundary from extent from polygons in vector file"); |
| 57 | Optionjl<string> layer_opt("ln", "ln", "layer name of extent to crop"); |
| 58 | Optionjl<bool> cut_opt("cut", "crop_to_cutline", "Crop the extent of the target dataset to the extent of the cutline.",false); |
| 59 | Optionjl<string> eoption_opt("eo","eo", "special extent options controlling rasterization: ATTRIBUTE|CHUNKYSIZE|ALL_TOUCHED|BURN_VALUE_FROM|MERGE_ALG, e.g., -eo ATTRIBUTE=fieldname"); |
| 60 | Optionjl<string> mask_opt("m", "mask", "Use the specified file as a validity mask."); |
| 61 | Optionjl<unsigned int> mskband_opt("mskband", "mskband", "Mask band to read (0 indexed)", 0); |
| 62 | Optionjl<float> msknodata_opt("msknodata", "msknodata", "Mask value not to consider for composite.", 0); |
| 63 | Optionjl<double> ulx_opt("ulx", "ulx", "Upper left x value bounding box", 0.0); |
| 64 | Optionjl<double> uly_opt("uly", "uly", "Upper left y value bounding box", 0.0); |
| 65 | Optionjl<double> lrx_opt("lrx", "lrx", "Lower right x value bounding box", 0.0); |
| 66 | Optionjl<double> lry_opt("lry", "lry", "Lower right y value bounding box", 0.0); |
| 67 | Optionjl<string> crule_opt("cr", "crule", "Composite rule (overwrite, maxndvi, maxband, minband, mean, mode (only for byte images), median, sum, maxallbands, minallbands, stdev", "overwrite"); |
| 68 | Optionjl<unsigned int> ruleBand_opt("cb", "cband", "band index used for the composite rule (e.g., for ndvi, use --cband=0 --cband=1 with 0 and 1 indices for red and nir band respectively", 0); |
| 69 | Optionjl<double> srcnodata_opt("srcnodata", "srcnodata", "invalid value(s) for input raster dataset"); |
| 70 | Optionjl<unsigned int> bndnodata_opt("bndnodata", "bndnodata", "Band(s) in input image to check if pixel is valid (used for srcnodata, min and max options)", 0); |
| 71 | Optionjl<double> minValue_opt("min", "min", "flag values smaller or equal to this value as invalid."); |
| 72 | Optionjl<double> maxValue_opt("max", "max", "flag values larger or equal to this value as invalid."); |
| 73 | Optionjl<double> dstnodata_opt("dstnodata", "dstnodata", "nodata value to put in output raster dataset if not valid or out of bounds.", 0); |
| 74 | Optionjl<string> resample_opt("r", "resample", "Resampling method (near: nearest neighbor, bilinear: bi-linear interpolation).", "near"); |
| 75 | Optionjl<string> 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"); |
| 76 | Optionjl<string> projection_opt("a_srs", "a_srs", "Override the spatial reference for the output file (leave blank to copy from input file, use epsg:3035 to use European projection and force to European grid"); |
| 77 | Optionjl<short> file_opt("file", "file", "write number of observations (1) and/or sequence nr of selected file (2) for each pixels as additional layer in composite"); |
| 78 | Optionjl<short> weight_opt("w", "weight", "Weights (type: short) for the composite, use one weight for each input file in same order as input files are provided). Use value 1 for equal weights.", 1); |
| 79 | Optionjl<short> class_opt("c", "class", "classes for multi-band output image: each band represents the number of observations for one specific class. Use value 0 for no multi-band output image.", 0); |
| 80 | Optionjl<string> colorTable_opt("ct", "ct", "color table file with 5 columns: id R G B ALFA (0: transparent, 255: solid)"); |
| 81 | Optionjl<string> description_opt("d", "description", "Set image description"); |
| 82 | Optionjl<bool> align_opt("align", "align", "Align output bounding box to input image",false); |
| 83 | // Optionjl<unsigned long int> memory_opt("mem", "mem", "Buffer size (in MB) to read image data blocks in memory",0,1); |
| 84 | Optionjl<short> verbose_opt("v", "verbose", "verbose", 0,2); |
| 85 | |
| 86 | extent_opt.setHide(1); |
| 87 | layer_opt.setHide(1); |
| 88 | cut_opt.setHide(1); |
| 89 | eoption_opt.setHide(1); |
| 90 | mask_opt.setHide(1); |
| 91 | msknodata_opt.setHide(1); |
| 92 | mskband_opt.setHide(1); |
| 93 | file_opt.setHide(1); |
| 94 | weight_opt.setHide(1); |
| 95 | class_opt.setHide(1); |
| 96 | colorTable_opt.setHide(1); |
| 97 | description_opt.setHide(1); |
| 98 | // memory_opt.setHide(1); |
| 99 | |
| 100 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 101 | try{ |
| 102 | doProcess=band_opt.retrieveOption(app); |
| 103 | dx_opt.retrieveOption(app); |
| 104 | dy_opt.retrieveOption(app); |
| 105 | extent_opt.retrieveOption(app); |
| 106 | layer_opt.retrieveOption(app); |
| 107 | cut_opt.retrieveOption(app); |
| 108 | eoption_opt.retrieveOption(app); |
| 109 | mask_opt.retrieveOption(app); |
| 110 | msknodata_opt.retrieveOption(app); |
| 111 | mskband_opt.retrieveOption(app); |
| 112 | ulx_opt.retrieveOption(app); |
| 113 | uly_opt.retrieveOption(app); |
| 114 | lrx_opt.retrieveOption(app); |
| 115 | lry_opt.retrieveOption(app); |
| 116 | crule_opt.retrieveOption(app); |
| 117 | ruleBand_opt.retrieveOption(app); |
| 118 | srcnodata_opt.retrieveOption(app); |
| 119 | bndnodata_opt.retrieveOption(app); |
| 120 | minValue_opt.retrieveOption(app); |
| 121 | maxValue_opt.retrieveOption(app); |
| 122 | dstnodata_opt.retrieveOption(app); |
| 123 | resample_opt.retrieveOption(app); |
| 124 | otype_opt.retrieveOption(app); |
| 125 | projection_opt.retrieveOption(app); |
| 126 | file_opt.retrieveOption(app); |
| 127 | weight_opt.retrieveOption(app); |
| 128 | class_opt.retrieveOption(app); |
| 129 | colorTable_opt.retrieveOption(app); |
| 130 | description_opt.retrieveOption(app); |
| 131 | align_opt.retrieveOption(app); |
| 132 | verbose_opt.retrieveOption(app); |
| 133 | |
| 134 | if(!doProcess){ |
| 135 | cout << endl; |
| 136 | std::ostringstream helpStream; |
| 137 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 138 | throw(helpStream.str());//help was invoked, stop processing |
| 139 | } |
| 140 | if(empty()){ |
| 141 | std::ostringstream errorStream; |
| 142 | errorStream << "Input collection is empty. Use --help for more help information" << std::endl; |
| 143 | throw(errorStream.str()); |
| 144 | } |
| 145 | |
| 146 | std::vector<std::string> badKeys; |
| 147 | app.badKeys(badKeys); |
| 148 | if(badKeys.size()){ |
| 149 | std::ostringstream errorStream; |
| 150 | if(badKeys.size()>1) |
| 151 | errorStream << "Error: unknown keys: "; |
| 152 | else |
| 153 | errorStream << "Error: unknown key: "; |
| 154 | for(int ikey=0;ikey<badKeys.size();++ikey){ |
| 155 | errorStream << badKeys[ikey] << " "; |
| 156 | } |
| 157 | errorStream << std::endl; |
| 158 | throw(errorStream.str()); |
| 159 | } |
| 160 | |
| 161 | std::map<std::string, CRULE_TYPE> cruleMap; |
| 162 | // //initialize cruleMap |
| 163 | // enum CRULE_TYPE {overwrite=0, maxndvi=1, maxband=2, minband=3, validband=4, mean=5, mode=6, median=7,sum=8}; |
| 164 | |
| 165 | cruleMap["overwrite"]=overwrite; |
| 166 | cruleMap["maxndvi"]=maxndvi; |
| 167 | cruleMap["maxband"]=maxband; |
| 168 | cruleMap["minband"]=minband; |
| 169 | cruleMap["validband"]=validband; |
| 170 | cruleMap["mean"]=mean; |
| 171 | cruleMap["mode"]=mode; |
| 172 | cruleMap["median"]=median; |
| 173 | cruleMap["sum"]=sum; |
| 174 | cruleMap["maxallbands"]=maxallbands; |
| 175 | cruleMap["minallbands"]=minallbands; |
| 176 | cruleMap["stdev"]=stdev; |
| 177 | |
| 178 | if(srcnodata_opt.size()){ |
| 179 | while(srcnodata_opt.size()<bndnodata_opt.size()) |
| 180 | srcnodata_opt.push_back(srcnodata_opt[0]); |
| 181 | } |
| 182 | while(bndnodata_opt.size()<srcnodata_opt.size()) |
| 183 | bndnodata_opt.push_back(bndnodata_opt[0]); |
| 184 | if(minValue_opt.size()){ |
| 185 | while(minValue_opt.size()<bndnodata_opt.size()) |
| 186 | minValue_opt.push_back(minValue_opt[0]); |
| 187 | while(bndnodata_opt.size()<minValue_opt.size()) |
| 188 | bndnodata_opt.push_back(bndnodata_opt[0]); |
| 189 | } |
| 190 | if(maxValue_opt.size()){ |
| 191 | while(maxValue_opt.size()<bndnodata_opt.size()) |
| 192 | maxValue_opt.push_back(maxValue_opt[0]); |
| 193 | while(bndnodata_opt.size()<maxValue_opt.size()) |
| 194 | bndnodata_opt.push_back(bndnodata_opt[0]); |
| 195 | } |
| 196 | |
| 197 | RESAMPLE theResample; |
| 198 | if(resample_opt[0]=="near"){ |
| 199 | theResample=NEAR; |
| 200 | if(verbose_opt[0]>1) |
| 201 | cout << "resampling: nearest neighbor" << endl; |
| 202 | } |
| 203 | else if(resample_opt[0]=="bilinear"){ |
| 204 | theResample=BILINEAR; |
| 205 | if(verbose_opt[0]>1) |
| 206 | cout << "resampling: bilinear interpolation" << endl; |
| 207 | } |
| 208 | else{ |
| 209 | std::ostringstream errorStream; |
| 210 | errorStream << "Error: resampling method " << resample_opt[0] << " not supported"; |
| 211 | throw(errorStream.str()); |
| 212 | } |
| 213 | |
| 214 | int nband=0; |
| 215 | int nwriteBand=0; |
| 216 | vector<short> bands; |
| 217 | |
| 218 | //get bounding box |
| 219 | double maxLRX=lrx_opt[0]; |
| 220 | double maxULY=uly_opt[0]; |
| 221 | double minULX=ulx_opt[0]; |
| 222 | double minLRY=lry_opt[0]; |
| 223 | double magic_x=1,magic_y=1;//magic pixel for GDAL map info |
| 224 | |
| 225 | GDALDataType theType=GDT_Unknown; |
| 226 | if(otype_opt.size()){ |
| 227 | theType=string2GDAL(otype_opt[0]); |
| 228 | if(theType==GDT_Unknown) |
| 229 | std::cout << "Warning: unknown output pixel type: " << otype_opt[0] << ", using input type as default" << std::endl; |
| 230 | } |
| 231 | if(verbose_opt[0]>1) |
| 232 | cout << "Output pixel type: " << GDALGetDataTypeName(theType) << endl; |
| 233 | |
| 234 | double dx=0; |
| 235 | double dy=0; |
| 236 | //get bounding box from extentReader if defined |
| 237 | VectorOgr extentReader; |
| 238 | if(extent_opt.size()){ |
| 239 | double e_ulx; |
| 240 | double e_uly; |
| 241 | double e_lrx; |
| 242 | double e_lry; |
| 243 | for(int iextent=0;iextent<extent_opt.size();++iextent){ |
| 244 | extentReader.open(extent_opt[iextent],layer_opt,true); |
| 245 | extentReader.getExtent(e_ulx,e_uly,e_lrx,e_lry); |
| 246 | if(!iextent){ |
| 247 | ulx_opt[0]=e_ulx; |
| 248 | uly_opt[0]=e_uly; |
| 249 | lrx_opt[0]=e_lrx; |
| 250 | lry_opt[0]=e_lry; |
| 251 | } |
| 252 | else{ |
| 253 | if(e_ulx<ulx_opt[0]) |
| 254 | ulx_opt[0]=e_ulx; |
| 255 | if(e_uly>uly_opt[0]) |
| 256 | uly_opt[0]=e_uly; |
| 257 | if(e_lrx>lrx_opt[0]) |
| 258 | lrx_opt[0]=e_lrx; |
| 259 | if(e_lry<lry_opt[0]) |
| 260 | lry_opt[0]=e_lry; |
| 261 | } |
| 262 | extentReader.close(); |
| 263 | } |
| 264 | if(maxLRX>minULX&&minULX>ulx_opt[0]) |
| 265 | ulx_opt[0]=minULX; |
| 266 | if(maxLRX>minULX&&maxLRX<lrx_opt[0]) |
| 267 | lrx_opt[0]=maxLRX; |
| 268 | if(maxULY>minLRY&&maxULY<uly_opt[0]) |
| 269 | uly_opt[0]=maxULY; |
| 270 | if(minLRY<maxULY&&minLRY>lry_opt[0]) |
| 271 | lry_opt[0]=minLRY; |
| 272 | if(cut_opt.size()||eoption_opt.size()){ |
| 273 | extentReader.open(extent_opt[0],layer_opt,true); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | if(verbose_opt[0]>1) |
| 278 | cout << "--ulx=" << ulx_opt[0] << " --uly=" << uly_opt[0] << " --lrx=" << lrx_opt[0] << " --lry=" << lry_opt[0] << endl; |
| 279 | |
| 280 | |
| 281 | string theProjection=""; |
| 282 | // GDALColorTable* theColorTable=NULL; |
| 283 | bool init=false; |
| 284 | |
| 285 | // std::vector<std::shared_ptr<Jim> >::const_iterator imit=begin(); |
| 286 | std::list<std::shared_ptr<Jim> >::const_iterator imit=begin(); |
| 287 | for(imit=begin();imit!=end();++imit){ |
| 288 | // for(int ifile=0;ifile<(*imit).size();++ifile){ |
| 289 | //todo: must be in init part only? |
| 290 | if(colorTable_opt.empty()) |
| 291 | // if((*imit)->getColorTable()) |
| 292 | // theColorTable=((*imit)->getColorTable()->Clone()); |
| 293 | if(projection_opt.empty()) |
| 294 | theProjection=(*imit)->getProjection(); |
| 295 | // if(option_opt.findSubstring("INTERLEAVE=")==option_opt.end()){ |
| 296 | // string theInterleave="INTERLEAVE="; |
| 297 | // theInterleave+=(*imit)->getInterleave(); |
| 298 | // option_opt.push_back(theInterleave); |
| 299 | // } |
| 300 | |
| 301 | if((ulx_opt[0]||uly_opt[0]||lrx_opt[0]||lry_opt[0])&&(!(*imit)->covers(ulx_opt[0],uly_opt[0],lrx_opt[0],lry_opt[0]))){ |
| 302 | if(verbose_opt[0]>1) |
| 303 | cout << "Input not within bounding box, skipping..." << endl; |
| 304 | continue; |
| 305 | } |
| 306 | double theULX, theULY, theLRX, theLRY; |
| 307 | (*imit)->getBoundingBox(theULX,theULY,theLRX,theLRY); |
| 308 | if(theLRY>theULY){ |
| 309 | std::ostringstream errorStream; |
| 310 | errorStream << "Error: input is not georeferenced or wrong bounding box, only referenced images are supported for composite "; |
| 311 | throw(errorStream.str()); |
| 312 | } |
| 313 | if(verbose_opt[0]>1) |
| 314 | cout << "Bounding Box (ULX ULY LRX LRY): " << fixed << setprecision(6) << theULX << " " << theULY << " " << theLRX << " " << theLRY << endl; |
| 315 | if(!init){ |
| 316 | if(verbose_opt[0]>1){ |
| 317 | switch(cruleMap[crule_opt[0]]){ |
| 318 | default: |
| 319 | case(overwrite): |
| 320 | cout << "Composite rule: overwrite" << endl; |
| 321 | break; |
| 322 | case(maxndvi): |
| 323 | cout << "Composite rule: max ndvi" << endl; |
| 324 | break; |
| 325 | case(maxband): |
| 326 | cout << "Composite rule: max band" << endl; |
| 327 | break; |
| 328 | case(minband): |
| 329 | cout << "Composite rule: min band" << endl; |
| 330 | break; |
| 331 | case(validband): |
| 332 | cout << "Composite rule: valid band" << endl; |
| 333 | break; |
| 334 | case(mean): |
| 335 | cout << "Composite rule: mean value" << endl; |
| 336 | break; |
| 337 | case(mode): |
| 338 | cout << "Composite rule: max voting (only for byte images)" << endl; |
| 339 | break; |
| 340 | case(median): |
| 341 | cout << "Composite rule: median" << endl; |
| 342 | break; |
| 343 | case(stdev): |
| 344 | cout << "Composite rule: stdev" << endl; |
| 345 | break; |
| 346 | case(sum): |
| 347 | cout << "Composite rule: sum" << endl; |
| 348 | break; |
| 349 | case(minallbands): |
| 350 | cout << "Composite rule: minallbands" << endl; |
| 351 | break; |
| 352 | case(maxallbands): |
| 353 | cout << "Composite rule: maxallbands" << endl; |
| 354 | break; |
| 355 | } |
| 356 | } |
| 357 | if(band_opt.size()){ |
| 358 | nband=band_opt.size(); |
| 359 | bands.resize(band_opt.size()); |
| 360 | for(unsigned int iband=0;iband<band_opt.size();++iband){ |
| 361 | bands[iband]=band_opt[iband]; |
| 362 | assert(bands[iband]<(*imit)->nrOfBand()); |
| 363 | } |
| 364 | } |
| 365 | else{ |
| 366 | nband=(*imit)->nrOfBand(); |
| 367 | bands.resize(nband); |
| 368 | for(unsigned int iband=0;iband<nband;++iband) |
| 369 | bands[iband]=iband; |
| 370 | } |
| 371 | for(unsigned int iband=0;iband<bndnodata_opt.size();++iband){ |
| 372 | assert(bndnodata_opt[iband]>=0&&bndnodata_opt[iband]<nband); |
| 373 | } |
| 374 | //if output type not set, get type from input image |
| 375 | if(theType==GDT_Unknown){ |
| 376 | theType=(*imit)->getGDALDataType(); |
| 377 | if(verbose_opt[0]>1) |
| 378 | cout << "Using data type from input image: " << GDALGetDataTypeName(theType) << endl; |
| 379 | } |
| 380 | |
| 381 | if(verbose_opt[0]>1){ |
| 382 | cout << "input data type: " << theType << endl; |
| 383 | cout << "nband: " << nband << endl; |
| 384 | } |
| 385 | |
| 386 | maxLRX=theLRX; |
| 387 | maxULY=theULY; |
| 388 | minULX=theULX; |
| 389 | minLRY=theLRY; |
| 390 | if(dx_opt.size()) |
| 391 | dx=dx_opt[0]; |
| 392 | else |
| 393 | dx=(*imit)->getDeltaX(); |
| 394 | if(dy_opt.size()) |
| 395 | dy=dy_opt[0]; |
| 396 | else |
| 397 | dy=(*imit)->getDeltaY(); |
| 398 | init=true; |
| 399 | } |
| 400 | else{ |
| 401 | maxLRX=(theLRX>maxLRX)?theLRX:maxLRX; |
| 402 | maxULY=(theULY>maxULY)?theULY:maxULY; |
| 403 | minULX=(theULX<minULX)?theULX:minULX; |
| 404 | minLRY=(theLRY<minLRY)?theLRY:minLRY; |
| 405 | } |
| 406 | // (*imit).close(); |
| 407 | } |
| 408 | if(verbose_opt[0]>1) |
| 409 | cout << "bounding box input images (ULX ULY LRX LRY): " << fixed << setprecision(6) << minULX << " " << maxULY << " " << maxLRX << " " << minLRY << endl; |
| 410 | if(ulx_opt[0]||uly_opt[0]||lrx_opt[0]||lry_opt[0]){ |
| 411 | maxLRX=lrx_opt[0]; |
| 412 | maxULY=uly_opt[0]; |
| 413 | minULX=ulx_opt[0]; |
| 414 | minLRY=lry_opt[0]; |
| 415 | } |
| 416 | |
| 417 | bool forceEUgrid=false; |
| 418 | if(projection_opt.size()) |
| 419 | forceEUgrid=(!(projection_opt[0].compare("EPSG:3035"))||!(projection_opt[0].compare("EPSG:3035"))||projection_opt[0].find("ETRS-LAEA")!=string::npos); |
| 420 | if(forceEUgrid){ |
| 421 | //force to LAEA grid |
| 422 | minULX=floor(minULX); |
| 423 | minULX-=static_cast<unsigned int>(minULX)%(static_cast<unsigned int>(dx)); |
| 424 | maxULY=ceil(maxULY); |
| 425 | if(static_cast<unsigned int>(maxULY)%static_cast<unsigned int>(dy)) |
| 426 | maxULY+=dy; |
| 427 | maxULY-=static_cast<unsigned int>(maxULY)%(static_cast<unsigned int>(dy)); |
| 428 | maxLRX=ceil(maxLRX); |
| 429 | if(static_cast<unsigned int>(maxLRX)%static_cast<unsigned int>(dx)) |
| 430 | maxLRX+=dx; |
| 431 | maxLRX-=static_cast<unsigned int>(maxLRX)%(static_cast<unsigned int>(dx)); |
| 432 | minLRY=floor(minLRY); |
| 433 | minLRY-=static_cast<unsigned int>(minLRY)%(static_cast<unsigned int>(dy)); |
| 434 | } |
| 435 | else if(align_opt[0]){ |
| 436 | if(minULX>front()->getUlx()) |
| 437 | minULX-=fmod(minULX-front()->getUlx(),dx); |
| 438 | else if(minULX<front()->getUlx()) |
| 439 | minULX+=fmod(front()->getUlx()-minULX,dx)-dx; |
| 440 | if(maxLRX<front()->getLrx()) |
| 441 | maxLRX+=fmod(front()->getLrx()-maxLRX,dx); |
| 442 | else if(maxLRX>front()->getLrx()) |
| 443 | maxLRX-=fmod(maxLRX-front()->getLrx(),dx)+dx; |
| 444 | if(minLRY>front()->getLry()) |
| 445 | minLRY-=fmod(minLRY-front()->getLry(),dy); |
| 446 | else if(minLRY<front()->getLry()) |
| 447 | minLRY+=fmod(front()->getLry()-minLRY,dy)-dy; |
| 448 | if(maxULY<front()->getUly()) |
| 449 | maxULY+=fmod(front()->getUly()-maxULY,dy); |
| 450 | else if(maxULY>front()->getUly()) |
| 451 | maxULY-=fmod(maxULY-front()->getUly(),dy)+dy; |
| 452 | } |
| 453 | |
| 454 | if(verbose_opt[0]>1) |
| 455 | cout << "bounding box composite image (ULX ULY LRX LRY): " << fixed << setprecision(6) << minULX << " " << maxULY << " " << maxLRX << " " << minLRY << endl; |
| 456 | //initialize image |
| 457 | if(verbose_opt[0]>1) |
| 458 | cout << "initializing composite image..." << endl; |
| 459 | // double dcol=(maxLRX-minULX+dx-1)/dx; |
| 460 | // double drow=(maxULY-minLRY+dy-1)/dy; |
| 461 | // int ncol=static_cast<unsigned int>(dcol); |
| 462 | // int nrow=static_cast<unsigned int>(drow); |
| 463 | |
| 464 | int ncol=ceil((maxLRX-minULX)/dx); |
| 465 | int nrow=ceil((maxULY-minLRY)/dy); |
| 466 | |
| 467 | if(verbose_opt[0]>1) |
| 468 | cout << "composite image dim (nrow x ncol): " << nrow << " x " << ncol << endl; |
| 469 | while(weight_opt.size()<size()) |
| 470 | weight_opt.push_back(weight_opt[0]); |
| 471 | if(verbose_opt[0]>1){ |
| 472 | std::cout << weight_opt << std::endl; |
| 473 | } |
| 474 | if(cruleMap[crule_opt[0]]==mode){ |
| 475 | nwriteBand=(file_opt.size())? class_opt.size()+file_opt.size():class_opt.size(); |
| 476 | } |
| 477 | else |
| 478 | nwriteBand=bands.size()+file_opt.size(); |
| 479 | // nwriteBand=(file_opt[0])? bands.size()+1:bands.size(); |
| 480 | |
| 481 | imgWriter.open(ncol,nrow,nwriteBand,theType); |
| 482 | if(dstnodata_opt.size()){ |
| 483 | imgWriter.setNoData(dstnodata_opt); |
| 484 | for(int iband=0;iband<nwriteBand;++iband) |
| 485 | imgWriter.GDALSetNoDataValue(dstnodata_opt[0],iband); |
| 486 | } |
| 487 | double gt[6]; |
| 488 | gt[0]=minULX; |
| 489 | gt[1]=dx; |
| 490 | gt[2]=0; |
| 491 | gt[3]=maxULY; |
| 492 | gt[4]=0; |
| 493 | gt[5]=-dy; |
| 494 | imgWriter.setGeoTransform(gt); |
| 495 | |
| 496 | if(projection_opt.size()){ |
| 497 | if(verbose_opt[0]>1) |
| 498 | cout << "projection: " << projection_opt[0] << endl; |
| 499 | imgWriter.setProjectionProj4(projection_opt[0]); |
| 500 | } |
| 501 | else if(theProjection!=""){ |
| 502 | if(verbose_opt[0]>1) |
| 503 | cout << "projection: " << theProjection << endl; |
| 504 | imgWriter.setProjection(theProjection); |
| 505 | } |
| 506 | |
| 507 | Jim maskReader; |
| 508 | if(extent_opt.size()&&(cut_opt[0]||eoption_opt.size())){ |
| 509 | if(mask_opt.size()){ |
| 510 | string errorString="Error: can only either mask or extent extent with cutline, not both"; |
| 511 | throw(errorString); |
| 512 | } |
| 513 | maskReader.open(ncol,nrow,1,GDT_Float64); |
| 514 | double gt[6]; |
| 515 | gt[0]=minULX; |
| 516 | gt[1]=dx; |
| 517 | gt[2]=0; |
| 518 | gt[3]=maxULY; |
| 519 | gt[4]=0; |
| 520 | gt[5]=-dy; |
| 521 | maskReader.setGeoTransform(gt); |
| 522 | if(projection_opt.size()) |
| 523 | maskReader.setProjectionProj4(projection_opt[0]); |
| 524 | else if(theProjection!=""){ |
| 525 | if(verbose_opt[0]>1) |
| 526 | cout << "projection: " << theProjection << endl; |
| 527 | maskReader.setProjection(theProjection); |
| 528 | } |
| 529 | // vector<double> burnValues(1,1);//burn value is 1 (single band) |
| 530 | // maskReader.rasterizeBuf(extentReader,burnValues,eoption_opt); |
| 531 | maskReader.d_rasterizeBuf(extentReader,msknodata_opt[0],eoption_opt,layer_opt); |
| 532 | // if(eoption_opt.size()) |
| 533 | // maskReader.rasterizeBuf(extentReader,eoption_opt); |
| 534 | // else |
| 535 | // maskReader.rasterizeBuf(extentReader); |
| 536 | //todo: support multiple masks |
| 537 | } |
| 538 | else if(mask_opt.size()==1){ |
| 539 | //there is only a single mask |
| 540 | maskReader.open(mask_opt[0]); |
| 541 | if(mskband_opt[0]>=maskReader.nrOfBand()){ |
| 542 | string errorString="Error: illegal mask band"; |
| 543 | throw(errorString); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | //create composite image |
| 548 | if(verbose_opt[0]>1) |
| 549 | cout << "creating composite image" << endl; |
| 550 | Vector2d<double> writeBuffer(nband,imgWriter.nrOfCol()); |
| 551 | Vector2d<short> fileBuffer(file_opt.size());//holds information on the selected files (number of available files and or index of selected file) |
| 552 | for(int bfile=0;bfile<file_opt.size();++bfile) |
| 553 | fileBuffer[bfile].resize(ncol); |
| 554 | Vector2d<short> maxBuffer;//buffer used for maximum voting |
| 555 | // Vector2d<double> readBuffer(nband); |
| 556 | vector<Vector2d<unsigned short> > readBuffer(size()); |
| 557 | // std::list<std::shared_ptr<Jim> >::const_iterator imit=begin(); |
| 558 | unsigned int ifile=0; |
| 559 | for(imit=begin();imit!=end();++imit){ |
| 560 | readBuffer[ifile].resize((*imit)->nrOfBand()); |
| 561 | ++ifile; |
| 562 | } |
| 563 | // for(int ifile=0;ifile<size();++ifile) |
| 564 | // readBuffer[ifile].resize((this->at(ifile))->nrOfBand()); |
| 565 | |
| 566 | statfactory::StatFactory stat; |
| 567 | if(cruleMap[crule_opt[0]]==maxndvi)//ndvi |
| 568 | assert(ruleBand_opt.size()==2); |
| 569 | if(cruleMap[crule_opt[0]]==mode){//max voting |
| 570 | maxBuffer.resize(imgWriter.nrOfCol(),256);//use only byte images for max voting |
| 571 | for(int iclass=0;iclass<class_opt.size();++iclass) |
| 572 | assert(class_opt[iclass]<maxBuffer.size()); |
| 573 | } |
| 574 | double readRow=0; |
| 575 | double readCol=0; |
| 576 | double lowerCol=0; |
| 577 | double upperCol=0; |
| 578 | const char* pszMessage; |
| 579 | void* pProgressArg=NULL; |
| 580 | GDALProgressFunc pfnProgress=GDALTermProgress; |
| 581 | double progress=0; |
| 582 | if(verbose_opt[0]) |
| 583 | pfnProgress(progress,pszMessage,pProgressArg); |
| 584 | for(unsigned int irow=0;irow<imgWriter.nrOfRow();++irow){ |
| 585 | vector<float> lineMask; |
| 586 | Vector2d< vector<double> > storeBuffer; |
| 587 | vector<bool> writeValid(ncol); |
| 588 | |
| 589 | //convert irow to geo |
| 590 | double x=0; |
| 591 | double y=0; |
| 592 | imgWriter.image2geo(0,irow,x,y); |
| 593 | |
| 594 | |
| 595 | if(cruleMap[crule_opt[0]]==mean || |
| 596 | cruleMap[crule_opt[0]]==median || |
| 597 | cruleMap[crule_opt[0]]==sum || |
| 598 | cruleMap[crule_opt[0]]==minallbands || |
| 599 | cruleMap[crule_opt[0]]==maxallbands || |
| 600 | cruleMap[crule_opt[0]]==stdev) |
| 601 | storeBuffer.resize(nband,ncol); |
| 602 | for(unsigned int icol=0;icol<imgWriter.nrOfCol();++icol){ |
| 603 | writeValid[icol]=false; |
| 604 | for(int bfile=0;bfile<file_opt.size();++bfile) |
| 605 | fileBuffer[bfile][icol]=0; |
| 606 | if(cruleMap[crule_opt[0]]==mode){//max voting |
| 607 | for(int iclass=0;iclass<256;++iclass) |
| 608 | maxBuffer[icol][iclass]=0; |
| 609 | } |
| 610 | else{ |
| 611 | for(unsigned int iband=0;iband<nband;++iband) |
| 612 | writeBuffer[iband][icol]=dstnodata_opt[0]; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | double oldRowMask=-1;//keep track of row mask to optimize number of line readings |
| 617 | |
| 618 | unsigned int ifile=0; |
| 619 | for(imit=begin();imit!=end();++imit){ |
| 620 | // for(unsigned int ifile=0;ifile<size();++ifile){ |
| 621 | //(*imit) already open... |
| 622 | // try{ |
| 623 | // (*imit).open((*imit)); |
| 624 | // } |
| 625 | // catch(string error){ |
| 626 | // cout << error << endl; |
| 627 | // } |
| 628 | // assert((*imit).getDataType()==theType); |
| 629 | assert(((*imit))->nrOfBand()>=nband); |
| 630 | if(!((*imit))->covers(minULX,maxULY,maxLRX,minLRY)){ |
| 631 | // ((*imit)).close(); |
| 632 | continue; |
| 633 | } |
| 634 | double uli,ulj,lri,lrj; |
| 635 | ((*imit))->geo2image(minULX+(magic_x-1.0)*((*imit))->getDeltaX(),maxULY-(magic_y-1.0)*((*imit))->getDeltaY(),uli,ulj); |
| 636 | ((*imit))->geo2image(maxLRX+(magic_x-2.0)*((*imit))->getDeltaX(),minLRY-(magic_y-2.0)*((*imit))->getDeltaY(),lri,lrj); |
| 637 | uli=floor(uli); |
| 638 | ulj=floor(ulj); |
| 639 | lri=floor(lri); |
| 640 | lrj=floor(lrj); |
| 641 | |
| 642 | double startCol=uli; |
| 643 | double endCol=lri; |
| 644 | if(uli<0) |
| 645 | startCol=0; |
| 646 | else if(uli>=((*imit))->nrOfCol()) |
| 647 | startCol=((*imit))->nrOfCol()-1; |
| 648 | if(lri<0) |
| 649 | endCol=0; |
| 650 | else if(lri>=((*imit))->nrOfCol()) |
| 651 | endCol=((*imit))->nrOfCol()-1; |
| 652 | |
| 653 | //lookup corresponding row for irow in this file |
| 654 | ((*imit))->geo2image(x,y,readCol,readRow); |
| 655 | if(readRow<0||readRow>=((*imit))->nrOfRow()){ |
| 656 | // ((*imit)).close(); |
| 657 | continue; |
| 658 | } |
| 659 | // for(int iband=0;iband<((*imit)).nrOfBand();++iband){ |
| 660 | for(unsigned int iband=0;iband<nband;++iband){ |
| 661 | unsigned int readBand=(band_opt.size()>iband)? band_opt[iband] : iband; |
| 662 | // readBuffer[iband].resize(readncol); |
| 663 | ((*imit))->readData(readBuffer[ifile][iband],startCol,endCol,readRow,readBand,theResample); |
| 664 | // if(readRow==0&&iband==0){ |
| 665 | // for(unsigned int icol=0;icol<10;++icol) |
| 666 | // cout << readBuffer[0][0][icol] << " "; |
| 667 | // cout << endl; |
| 668 | // } |
| 669 | } |
| 670 | for(int ib=0;ib<ncol;++ib){ |
| 671 | imgWriter.image2geo(ib,irow,x,y); |
| 672 | //check mask first |
| 673 | bool valid=true; |
| 674 | if(maskReader.isInit()){ |
| 675 | //read mask |
| 676 | double colMask=0; |
| 677 | double rowMask=0; |
| 678 | maskReader.geo2image(x,y,colMask,rowMask); |
| 679 | colMask=static_cast<unsigned int>(colMask); |
| 680 | rowMask=static_cast<unsigned int>(rowMask); |
| 681 | if(rowMask>=0&&rowMask<maskReader.nrOfRow()&&colMask>=0&&colMask<maskReader.nrOfCol()){ |
| 682 | if(static_cast<unsigned int>(rowMask)!=static_cast<unsigned int>(oldRowMask)){ |
| 683 | maskReader.readData(lineMask,static_cast<unsigned int>(rowMask),mskband_opt[0]); |
| 684 | oldRowMask=rowMask; |
| 685 | } |
| 686 | for(int ivalue=0;ivalue<msknodata_opt.size();++ivalue){ |
| 687 | if(lineMask[colMask]==msknodata_opt[ivalue]){ |
| 688 | valid=false; |
| 689 | break; |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | if(!valid) |
| 695 | continue; |
| 696 | |
| 697 | //lookup corresponding row for irow in this file |
| 698 | ((*imit))->geo2image(x,y,readCol,readRow); |
| 699 | if(readCol<0||readCol>=((*imit))->nrOfCol()) |
| 700 | continue; |
| 701 | double val_current=0; |
| 702 | double val_new=0; |
| 703 | bool readValid=true; |
| 704 | switch(theResample){ |
| 705 | case(BILINEAR): |
| 706 | lowerCol=readCol-0.5; |
| 707 | lowerCol=static_cast<unsigned int>(lowerCol); |
| 708 | upperCol=readCol+0.5; |
| 709 | upperCol=static_cast<unsigned int>(upperCol); |
| 710 | if(lowerCol<0) |
| 711 | lowerCol=0; |
| 712 | if(upperCol>=((*imit))->nrOfCol()) |
| 713 | upperCol=((*imit))->nrOfCol()-1; |
| 714 | for(int vband=0;vband<bndnodata_opt.size();++vband){ |
| 715 | val_new=(readCol-0.5-lowerCol)*readBuffer[ifile][bndnodata_opt[vband]][upperCol-startCol]+(1-readCol+0.5+lowerCol)*readBuffer[ifile][bndnodata_opt[vband]][lowerCol-startCol]; |
| 716 | if(minValue_opt.size()>vband){ |
| 717 | if(val_new<=minValue_opt[vband]){ |
| 718 | readValid=false; |
| 719 | break; |
| 720 | } |
| 721 | } |
| 722 | if(maxValue_opt.size()>vband){ |
| 723 | if(val_new>=maxValue_opt[vband]){ |
| 724 | readValid=false; |
| 725 | break; |
| 726 | } |
| 727 | } |
| 728 | if(srcnodata_opt.size()>vband){ |
| 729 | if(val_new==srcnodata_opt[vband]){ |
| 730 | readValid=false; |
| 731 | break; |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | break; |
| 736 | default: |
| 737 | readCol=static_cast<unsigned int>(readCol); |
| 738 | for(int vband=0;vband<bndnodata_opt.size();++vband){ |
| 739 | val_new=readBuffer[ifile][bndnodata_opt[vband]][readCol-startCol]; |
| 740 | if(minValue_opt.size()>vband){ |
| 741 | if(val_new<=minValue_opt[vband]){ |
| 742 | readValid=false; |
| 743 | break; |
| 744 | } |
| 745 | } |
| 746 | if(maxValue_opt.size()>vband){ |
| 747 | if(val_new>=maxValue_opt[vband]){ |
| 748 | readValid=false; |
| 749 | break; |
| 750 | } |
| 751 | } |
| 752 | if(srcnodata_opt.size()>vband){ |
| 753 | if(val_new==srcnodata_opt[vband]){ |
| 754 | readValid=false; |
| 755 | break; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | break; |
| 760 | } |
| 761 | if(readValid){ |
| 762 | for(int bfile=0;bfile<file_opt.size();++bfile){ |
| 763 | if(file_opt[bfile]==1) |
| 764 | ++fileBuffer[bfile][ib]; |
| 765 | } |
| 766 | if(writeValid[ib]){ |
| 767 | unsigned int iband=0; |
| 768 | switch(cruleMap[crule_opt[0]]){ |
| 769 | case(maxndvi):{//max ndvi |
| 770 | double red_current=writeBuffer[ruleBand_opt[0]][ib]; |
| 771 | double nir_current=writeBuffer[ruleBand_opt[1]][ib]; |
| 772 | double ndvi_current=0; |
| 773 | if(red_current+nir_current>0&&red_current>=0&&nir_current>=0) |
| 774 | ndvi_current=(nir_current-red_current)/(nir_current+red_current); |
| 775 | double ndvi_new=0; |
| 776 | double red_new=0; |
| 777 | double nir_new=0; |
| 778 | switch(theResample){ |
| 779 | case(BILINEAR): |
| 780 | lowerCol=readCol-0.5; |
| 781 | lowerCol=static_cast<unsigned int>(lowerCol); |
| 782 | upperCol=readCol+0.5; |
| 783 | upperCol=static_cast<unsigned int>(upperCol); |
| 784 | if(lowerCol<0) |
| 785 | lowerCol=0; |
| 786 | if(upperCol>=((*imit))->nrOfCol()) |
| 787 | upperCol=((*imit))->nrOfCol()-1; |
| 788 | red_new=(readCol-0.5-lowerCol)*readBuffer[ifile][ruleBand_opt[0]][upperCol-startCol]+(1-readCol+0.5+lowerCol)*readBuffer[ifile][ruleBand_opt[0]][lowerCol-startCol]; |
| 789 | nir_new=(readCol-0.5-lowerCol)*readBuffer[ifile][ruleBand_opt[1]][upperCol-startCol]+(1-readCol+0.5+lowerCol)*readBuffer[ifile][ruleBand_opt[1]][lowerCol-startCol]; |
| 790 | if(red_new+nir_new>0&&red_new>=0&&nir_new>=0) |
| 791 | ndvi_new=(nir_new-red_new)/(nir_new+red_new); |
| 792 | if(ndvi_new>=ndvi_current){ |
| 793 | for(iband=0;iband<nband;++iband){ |
| 794 | val_new=(readCol-0.5-lowerCol)*readBuffer[ifile][iband][upperCol-startCol]+(1-readCol+0.5+lowerCol)*readBuffer[ifile][iband][lowerCol-startCol]; |
| 795 | writeBuffer[iband][ib]=val_new; |
| 796 | } |
| 797 | for(int bfile=0;bfile<file_opt.size();++bfile){ |
| 798 | if(file_opt[bfile]==2) |
| 799 | fileBuffer[bfile][ib]=ifile; |
| 800 | } |
| 801 | } |
| 802 | break; |
| 803 | default: |
| 804 | readCol=static_cast<unsigned int>(readCol); |
| 805 | red_new=readBuffer[ifile][ruleBand_opt[0]][readCol-startCol]; |
| 806 | nir_new=readBuffer[ifile][ruleBand_opt[1]][readCol-startCol]; |
| 807 | if(red_new+nir_new>0&&red_new>=0&&nir_new>=0) |
| 808 | ndvi_new=(nir_new-red_new)/(nir_new+red_new); |
| 809 | if(ndvi_new>=ndvi_current){ |
| 810 | for(iband=0;iband<nband;++iband){ |
| 811 | val_new=readBuffer[ifile][iband][readCol-startCol]; |
| 812 | writeBuffer[iband][ib]=val_new; |
| 813 | } |
| 814 | for(int bfile=0;bfile<file_opt.size();++bfile){ |
| 815 | if(file_opt[bfile]==2) |
| 816 | fileBuffer[bfile][ib]=ifile; |
| 817 | } |
| 818 | } |
| 819 | break; |
| 820 | } |
| 821 | break; |
| 822 | } |
| 823 | case(maxband): |
| 824 | case(minband): |
| 825 | case(validband)://max,min,valid band |
| 826 | val_current=writeBuffer[ruleBand_opt[0]][ib]; |
| 827 | switch(theResample){ |
| 828 | case(BILINEAR): |
| 829 | lowerCol=readCol-0.5; |
| 830 | lowerCol=static_cast<unsigned int>(lowerCol); |
| 831 | upperCol=readCol+0.5; |
| 832 | upperCol=static_cast<unsigned int>(upperCol); |
| 833 | if(lowerCol<0) |
| 834 | lowerCol=0; |
| 835 | if(upperCol>=((*imit))->nrOfCol()) |
| 836 | upperCol=((*imit))->nrOfCol()-1; |
| 837 | val_new=(readCol-0.5-lowerCol)*readBuffer[ifile][ruleBand_opt[0]][upperCol-startCol]+(1-readCol+0.5+lowerCol)*readBuffer[ifile][ruleBand_opt[0]][lowerCol-startCol]; |
| 838 | val_new*=weight_opt[ifile]; |
| 839 | if((cruleMap[crule_opt[0]]==maxband&&val_new>val_current)||(cruleMap[crule_opt[0]]==minband&&val_new<val_current)||(cruleMap[crule_opt[0]]==validband)){//&&val_new>minValue_opt[0]&&val_new<maxValue_opt[0])){ |
| 840 | for(iband=0;iband<nband;++iband){ |
| 841 | val_new=(readCol-0.5-lowerCol)*readBuffer[ifile][iband][upperCol-startCol]+(1-readCol+0.5+lowerCol)*readBuffer[ifile][iband][lowerCol-startCol]; |
| 842 | val_new*=weight_opt[ifile]; |
| 843 | writeBuffer[iband][ib]=val_new; |
| 844 | } |
| 845 | for(int bfile=0;bfile<file_opt.size();++bfile){ |
| 846 | if(file_opt[bfile]==2) |
| 847 | fileBuffer[bfile][ib]=ifile; |
| 848 | } |
| 849 | } |
| 850 | break; |
| 851 | default: |
| 852 | readCol=static_cast<unsigned int>(readCol); |
| 853 | val_new=readBuffer[ifile][ruleBand_opt[0]][readCol-startCol]; |
| 854 | val_new*=weight_opt[ifile]; |
| 855 | if((cruleMap[crule_opt[0]]==maxband&&val_new>val_current)||(cruleMap[crule_opt[0]]==minband&&val_new<val_current)||(cruleMap[crule_opt[0]]==validband)){//&&val_new>minValue_opt[0]&&val_new<maxValue_opt[0])){ |
| 856 | for(iband=0;iband<nband;++iband){ |
| 857 | val_new=readBuffer[ifile][iband][readCol-startCol]; |
| 858 | val_new*=weight_opt[ifile]; |
| 859 | writeBuffer[iband][ib]=val_new; |
| 860 | } |
| 861 | for(int bfile=0;bfile<file_opt.size();++bfile){ |
| 862 | if(file_opt[bfile]==2) |
| 863 | fileBuffer[bfile][ib]=ifile; |
| 864 | } |
| 865 | } |
| 866 | break; |
| 867 | } |
| 868 | break; |
| 869 | case(mode)://max voting (only for Byte images) |
| 870 | switch(theResample){ |
| 871 | case(BILINEAR): |
| 872 | lowerCol=readCol-0.5; |
| 873 | lowerCol=static_cast<unsigned int>(lowerCol); |
| 874 | upperCol=readCol+0.5; |
| 875 | upperCol=static_cast<unsigned int>(upperCol); |
| 876 | if(lowerCol<0) |
| 877 | lowerCol=0; |
| 878 | if(upperCol>=((*imit))->nrOfCol()) |
| 879 | upperCol=((*imit))->nrOfCol()-1; |
| 880 | for(iband=0;iband<nband;++iband){ |
| 881 | val_new=(readCol-0.5-lowerCol)*readBuffer[ifile][iband][upperCol-startCol]+(1-readCol+0.5+lowerCol)*readBuffer[ifile][iband][lowerCol-startCol]; |
| 882 | maxBuffer[ib][val_new]=maxBuffer[ib][val_new]+weight_opt[ifile]; |
| 883 | // ++(maxBuffer[ib][val_new]); |
| 884 | } |
| 885 | break; |
| 886 | default: |
| 887 | readCol=static_cast<unsigned int>(readCol); |
| 888 | for(iband=0;iband<nband;++iband){ |
| 889 | val_new=readBuffer[ifile][iband][readCol-startCol]; |
| 890 | maxBuffer[ib][val_new]=maxBuffer[ib][val_new]+weight_opt[ifile]; |
| 891 | } |
| 892 | break; |
| 893 | } |
| 894 | break; |
| 895 | case(mean)://mean value |
| 896 | case(median)://median value |
| 897 | case(sum)://sum value |
| 898 | case(minallbands)://minimum for each and every band |
| 899 | case(maxallbands)://maximum for each and every band |
| 900 | case(stdev)://maximum for each and every band |
| 901 | switch(theResample){ |
| 902 | case(BILINEAR): |
| 903 | lowerCol=readCol-0.5; |
| 904 | lowerCol=static_cast<unsigned int>(lowerCol); |
| 905 | upperCol=readCol+0.5; |
| 906 | upperCol=static_cast<unsigned int>(upperCol); |
| 907 | if(lowerCol<0) |
| 908 | lowerCol=0; |
| 909 | if(upperCol>=((*imit))->nrOfCol()) |
| 910 | upperCol=((*imit))->nrOfCol()-1; |
| 911 | for(iband=0;iband<nband;++iband){ |
| 912 | val_new=(readCol-0.5-lowerCol)*readBuffer[ifile][iband][upperCol-startCol]+(1-readCol+0.5+lowerCol)*readBuffer[ifile][iband][lowerCol-startCol]; |
| 913 | val_new*=weight_opt[ifile]; |
| 914 | storeBuffer[iband][ib].push_back(val_new); |
| 915 | } |
| 916 | break; |
| 917 | default: |
| 918 | readCol=static_cast<unsigned int>(readCol); |
| 919 | for(iband=0;iband<nband;++iband){ |
| 920 | val_new=readBuffer[ifile][iband][readCol-startCol]; |
| 921 | val_new*=weight_opt[ifile]; |
| 922 | storeBuffer[iband][ib].push_back(val_new); |
| 923 | // assert(ifile>0); |
| 924 | // assert(weight_opt[ifile]>=0); |
| 925 | // assert(storeBuffer[iband][ib].back()>=0); |
| 926 | } |
| 927 | break; |
| 928 | } |
| 929 | for(int bfile=0;bfile<file_opt.size();++bfile){ |
| 930 | if(file_opt[bfile]==2) |
| 931 | fileBuffer[bfile][ib]=ifile; |
| 932 | } |
| 933 | break; |
| 934 | case(overwrite): |
| 935 | default: |
| 936 | switch(theResample){ |
| 937 | case(BILINEAR): |
| 938 | lowerCol=readCol-0.5; |
| 939 | lowerCol=static_cast<unsigned int>(lowerCol); |
| 940 | upperCol=readCol+0.5; |
| 941 | upperCol=static_cast<unsigned int>(upperCol); |
| 942 | if(lowerCol<0) |
| 943 | lowerCol=0; |
| 944 | if(upperCol>=((*imit))->nrOfCol()) |
| 945 | upperCol=((*imit))->nrOfCol()-1; |
| 946 | for(iband=0;iband<nband;++iband){ |
| 947 | val_new=(readCol-0.5-lowerCol)*readBuffer[ifile][iband][upperCol-startCol]+(1-readCol+0.5+lowerCol)*readBuffer[ifile][iband][lowerCol-startCol]; |
| 948 | val_new*=weight_opt[ifile]; |
| 949 | writeBuffer[iband][ib]=val_new; |
| 950 | } |
| 951 | break; |
| 952 | default: |
| 953 | readCol=static_cast<unsigned int>(readCol); |
| 954 | for(iband=0;iband<nband;++iband){ |
| 955 | val_new=readBuffer[ifile][iband][readCol-startCol]; |
| 956 | val_new*=weight_opt[ifile]; |
| 957 | writeBuffer[iband][ib]=val_new; |
| 958 | } |
| 959 | break; |
| 960 | } |
| 961 | for(int bfile=0;bfile<file_opt.size();++bfile){ |
| 962 | if(file_opt[bfile]==2) |
| 963 | fileBuffer[bfile][ib]=ifile; |
| 964 | } |
| 965 | break; |
| 966 | } |
| 967 | } |
| 968 | else{ |
| 969 | writeValid[ib]=true;//readValid was true |
| 970 | unsigned int iband=0; |
| 971 | switch(cruleMap[crule_opt[0]]){ |
| 972 | case(mean): |
| 973 | case(median): |
| 974 | case(sum): |
| 975 | case(minallbands): |
| 976 | case(maxallbands): |
| 977 | case(stdev): |
| 978 | switch(theResample){ |
| 979 | case(BILINEAR): |
| 980 | lowerCol=readCol-0.5; |
| 981 | lowerCol=static_cast<unsigned int>(lowerCol); |
| 982 | upperCol=readCol+0.5; |
| 983 | upperCol=static_cast<unsigned int>(upperCol); |
| 984 | if(lowerCol<0) |
| 985 | lowerCol=0; |
| 986 | if(upperCol>=((*imit))->nrOfCol()) |
| 987 | upperCol=((*imit))->nrOfCol()-1; |
| 988 | for(iband=0;iband<nband;++iband){ |
| 989 | val_new=(readCol-0.5-lowerCol)*readBuffer[ifile][iband][upperCol-startCol]+(1-readCol+0.5+lowerCol)*readBuffer[ifile][iband][lowerCol-startCol]; |
| 990 | val_new*=weight_opt[ifile]; |
| 991 | storeBuffer[iband][ib].push_back(val_new); |
| 992 | } |
| 993 | break; |
| 994 | default: |
| 995 | readCol=static_cast<unsigned int>(readCol); |
| 996 | for(iband=0;iband<nband;++iband){ |
| 997 | val_new=readBuffer[ifile][iband][readCol-startCol]; |
| 998 | val_new*=weight_opt[ifile]; |
| 999 | storeBuffer[iband][ib].push_back(val_new); |
| 1000 | } |
| 1001 | break; |
| 1002 | } |
| 1003 | for(int bfile=0;bfile<file_opt.size();++bfile){ |
| 1004 | if(file_opt[bfile]==2) |
| 1005 | fileBuffer[bfile][ib]=ifile; |
| 1006 | } |
| 1007 | break; |
| 1008 | case(mode): |
| 1009 | switch(theResample){ |
| 1010 | case(BILINEAR): |
| 1011 | lowerCol=readCol-0.5; |
| 1012 | lowerCol=static_cast<unsigned int>(lowerCol); |
| 1013 | upperCol=readCol+0.5; |
| 1014 | upperCol=static_cast<unsigned int>(upperCol); |
| 1015 | if(lowerCol<0) |
| 1016 | lowerCol=0; |
| 1017 | if(upperCol>=((*imit))->nrOfCol()) |
| 1018 | upperCol=((*imit))->nrOfCol()-1; |
| 1019 | for(iband=0;iband<nband;++iband){ |
| 1020 | val_new=(readCol-0.5-lowerCol)*readBuffer[ifile][iband][upperCol-startCol]+(1-readCol+0.5+lowerCol)*readBuffer[ifile][iband][lowerCol-startCol]; |
| 1021 | maxBuffer[ib][val_new]=maxBuffer[ib][val_new]+weight_opt[ifile]; |
| 1022 | // ++(maxBuffer[ib][val_new]); |
| 1023 | } |
| 1024 | break; |
| 1025 | default: |
| 1026 | readCol=static_cast<unsigned int>(readCol); |
| 1027 | for(iband=0;iband<nband;++iband){ |
| 1028 | val_new=readBuffer[ifile][iband][readCol-startCol]; |
| 1029 | maxBuffer[ib][val_new]=maxBuffer[ib][val_new]+weight_opt[ifile]; |
| 1030 | } |
| 1031 | // ++(maxBuffer[ib][val_new]); |
| 1032 | break; |
| 1033 | } |
| 1034 | break; |
| 1035 | default: |
| 1036 | switch(theResample){ |
| 1037 | case(BILINEAR): |
| 1038 | lowerCol=readCol-0.5; |
| 1039 | lowerCol=static_cast<unsigned int>(lowerCol); |
| 1040 | upperCol=readCol+0.5; |
| 1041 | upperCol=static_cast<unsigned int>(upperCol); |
| 1042 | if(lowerCol<0) |
| 1043 | lowerCol=0; |
| 1044 | if(upperCol>=((*imit))->nrOfCol()) |
| 1045 | upperCol=((*imit))->nrOfCol()-1; |
| 1046 | for(iband=0;iband<nband;++iband){ |
| 1047 | val_new=(readCol-0.5-lowerCol)*readBuffer[ifile][iband][upperCol-startCol]+(1-readCol+0.5+lowerCol)*readBuffer[ifile][iband][lowerCol-startCol]; |
| 1048 | val_new*=weight_opt[ifile]; |
| 1049 | writeBuffer[iband][ib]=val_new; |
| 1050 | } |
| 1051 | break; |
| 1052 | default: |
| 1053 | readCol=static_cast<unsigned int>(readCol); |
| 1054 | for(iband=0;iband<nband;++iband){ |
| 1055 | val_new=readBuffer[ifile][iband][readCol-startCol]; |
| 1056 | val_new*=weight_opt[ifile]; |
| 1057 | writeBuffer[iband][ib]=val_new; |
| 1058 | } |
| 1059 | break; |
| 1060 | } |
| 1061 | for(int bfile=0;bfile<file_opt.size();++bfile){ |
| 1062 | if(file_opt[bfile]==2) |
| 1063 | fileBuffer[bfile][ib]=ifile; |
| 1064 | } |
| 1065 | break; |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | // ((*imit)).close(); |
| 1071 | ++ifile; |
| 1072 | } |
| 1073 | if(cruleMap[crule_opt[0]]==mode){ |
| 1074 | vector<short> classBuffer(imgWriter.nrOfCol()); |
| 1075 | if(class_opt.size()>1){ |
| 1076 | for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 1077 | for(unsigned int icol=0;icol<imgWriter.nrOfCol();++icol) |
| 1078 | classBuffer[icol]=maxBuffer[icol][class_opt[iclass]]; |
| 1079 | imgWriter.writeData(classBuffer,irow,iclass); |
| 1080 | } |
| 1081 | } |
| 1082 | else{ |
| 1083 | for(unsigned int icol=0;icol<imgWriter.nrOfCol();++icol){ |
| 1084 | vector<short>::iterator maxit=maxBuffer[icol].begin(); |
| 1085 | maxit=stat.mymax(maxBuffer[icol],maxBuffer[icol].begin(),maxBuffer[icol].end()); |
| 1086 | writeBuffer[0][icol]=distance(maxBuffer[icol].begin(),maxit); |
| 1087 | for(int bfile=0;bfile<file_opt.size();++bfile){ |
| 1088 | if(file_opt[bfile]==2) |
| 1089 | fileBuffer[bfile][icol]=*(maxit); |
| 1090 | } |
| 1091 | } |
| 1092 | imgWriter.writeData(writeBuffer[0],irow,0); |
| 1093 | for(int bfile=0;bfile<file_opt.size();++bfile) |
| 1094 | imgWriter.writeData(fileBuffer[bfile],irow,1+bfile); |
| 1095 | } |
| 1096 | } |
| 1097 | else{ |
| 1098 | for(unsigned int iband=0;iband<bands.size();++iband){ |
| 1099 | // assert(writeBuffer[bands[iband]].size()==imgWriter.nrOfCol()); |
| 1100 | assert(writeBuffer[iband].size()==imgWriter.nrOfCol()); |
| 1101 | for(unsigned int icol=0;icol<imgWriter.nrOfCol();++icol){ |
| 1102 | try{ |
| 1103 | switch(cruleMap[crule_opt[0]]){ |
| 1104 | case(mean): |
| 1105 | // writeBuffer[iband][icol]=stat.mean(storeBuffer[bands[iband]][icol]); |
| 1106 | writeBuffer[iband][icol]=stat.mean(storeBuffer[iband][icol]); |
| 1107 | break; |
| 1108 | case(median): |
| 1109 | // writeBuffer[iband][icol]=stat.median(storeBuffer[bands[iband]][icol]); |
| 1110 | writeBuffer[iband][icol]=stat.median(storeBuffer[iband][icol]); |
| 1111 | break; |
| 1112 | case(sum): |
| 1113 | // writeBuffer[iband][icol]=stat.sum(storeBuffer[bands[iband]][icol]); |
| 1114 | writeBuffer[iband][icol]=stat.sum(storeBuffer[iband][icol]); |
| 1115 | break; |
| 1116 | case(minallbands): |
| 1117 | // writeBuffer[iband][icol]=stat.mymin(storeBuffer[bands[iband]][icol]); |
| 1118 | writeBuffer[iband][icol]=stat.mymin(storeBuffer[iband][icol]); |
| 1119 | break; |
| 1120 | case(maxallbands): |
| 1121 | // writeBuffer[iband][icol]=stat.mymax(storeBuffer[bands[iband]][icol]); |
| 1122 | writeBuffer[iband][icol]=stat.mymax(storeBuffer[iband][icol]); |
| 1123 | break; |
| 1124 | case(stdev): |
| 1125 | // writeBuffer[iband][icol]=sqrt(stat.var(storeBuffer[bands[iband]][icol])); |
| 1126 | writeBuffer[iband][icol]=sqrt(stat.var(storeBuffer[iband][icol])); |
| 1127 | break; |
| 1128 | default: |
| 1129 | break; |
| 1130 | } |
| 1131 | } |
| 1132 | catch(string error){ |
| 1133 | if(verbose_opt[0]>1) |
| 1134 | cerr << error << endl; |
| 1135 | writeBuffer[iband][icol]=dstnodata_opt[0]; |
| 1136 | continue; |
| 1137 | } |
| 1138 | } |
| 1139 | imgWriter.writeData(writeBuffer[iband],irow,iband); |
| 1140 | } |
| 1141 | for(int bfile=0;bfile<file_opt.size();++bfile) |
| 1142 | imgWriter.writeData(fileBuffer[bfile],irow,imgWriter.nrOfBand()-file_opt.size()+bfile); |
| 1143 | } |
| 1144 | progress=static_cast<float>(irow+1.0)/imgWriter.nrOfRow(); |
| 1145 | if(verbose_opt[0]) |
| 1146 | pfnProgress(progress,pszMessage,pProgressArg); |
| 1147 | } |
| 1148 | if(extent_opt.size()&&(cut_opt[0]||eoption_opt.size())){ |
| 1149 | extentReader.close(); |
| 1150 | } |
| 1151 | if(maskReader.isInit()) |
| 1152 | maskReader.close(); |
| 1153 | } |
| 1154 | catch(string predefinedString){ |
| 1155 | std::cout << predefinedString << std::endl; |
| 1156 | throw; |
| 1157 | } |
| 1158 | } |