| 1 | /********************************************************************** |
| 2 | jlsetmask_lib.cc: program to apply mask image (set invalid values) to raster image |
| 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 <assert.h> |
| 22 | |
| 23 | #include "imageclasses/Jim.h" |
| 24 | #include "imageclasses/VectorOgr.h" |
| 25 | #include "base/Optionjl.h" |
| 26 | #include "apps/AppFactory.h" |
| 27 | #include "jlsetmask_lib.h" |
| 28 | |
| 29 | using namespace std; |
| 30 | using namespace app; |
| 31 | |
| 32 | /** |
| 33 | * @param app application specific option arguments |
| 34 | * @return output image |
| 35 | **/ |
| 36 | shared_ptr<Jim> Jim::setMask(JimList& maskReader, app::AppFactory& app){ |
| 37 | shared_ptr<Jim> imgWriter=createImg(); |
| 38 | setMask(maskReader, *imgWriter, app); |
| 39 | return(imgWriter); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param app application specific option arguments |
| 44 | * @return output image |
| 45 | **/ |
| 46 | shared_ptr<Jim> Jim::setMask(VectorOgr& ogrReader, app::AppFactory& app){ |
| 47 | shared_ptr<Jim> imgWriter=createImg(); |
| 48 | setMask(ogrReader, *imgWriter, app); |
| 49 | return(imgWriter); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param imgWriter output raster setmask dataset |
| 54 | **/ |
| 55 | void Jim::setMask(JimList& maskReader, Jim& imgWriter, app::AppFactory& app){ |
| 56 | //command line options |
| 57 | // Optionjl<string> mask_opt("m", "mask", "Mask image(s)"); |
| 58 | Optionjl<string> vectorMask_opt("vm", "vectormask", "Vector mask dataset(s)"); |
| 59 | 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"); |
| 60 | Optionjl<int> msknodata_opt("msknodata", "msknodata", "Mask value(s) where image has nodata. Use one value for each mask, or multiple values for a single mask.", 1); |
| 61 | Optionjl<short> mskband_opt("mskband", "mskband", "Mask band to read (0 indexed). Provide band for each mask.", 0); |
| 62 | Optionjl<char> operator_opt("p", "operator", "Operator: < = > !. Use operator for each msknodata option", '='); |
| 63 | Optionjl<double> nodata_opt("nodata", "nodata", "nodata value to put in image if not valid", 0); |
| 64 | Optionjl<string> eoption_opt("eo","eo", "special extent options controlling rasterization: ATTRIBUTE|CHUNKYSIZE|ALL_TOUCHED|BURN_VALUE_FROM|MERGE_ALG, e.g., -eo ALL_TOUCHED=TRUE"); |
| 65 | Optionjl<string> layernames_opt("ln", "ln", "Layer names"); |
| 66 | Optionjl<short> verbose_opt("v", "verbose", "verbose", 0,2); |
| 67 | |
| 68 | otype_opt.setHide(1); |
| 69 | mskband_opt.setHide(1); |
| 70 | |
| 71 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 72 | try{ |
| 73 | // doProcess=mask_opt.retrieveOption(app); |
| 74 | doProcess=vectorMask_opt.retrieveOption(app); |
| 75 | msknodata_opt.retrieveOption(app); |
| 76 | mskband_opt.retrieveOption(app); |
| 77 | nodata_opt.retrieveOption(app); |
| 78 | operator_opt.retrieveOption(app); |
| 79 | otype_opt.retrieveOption(app); |
| 80 | eoption_opt.retrieveOption(app); |
| 81 | layernames_opt.retrieveOption(app); |
| 82 | verbose_opt.retrieveOption(app); |
| 83 | |
| 84 | if(!doProcess){ |
| 85 | cout << endl; |
| 86 | std::ostringstream helpStream; |
| 87 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 88 | throw(helpStream.str());//help was invoked, stop processing |
| 89 | } |
| 90 | |
| 91 | std::vector<std::string> badKeys; |
| 92 | app.badKeys(badKeys); |
| 93 | if(badKeys.size()){ |
| 94 | std::ostringstream errorStream; |
| 95 | if(badKeys.size()>1) |
| 96 | errorStream << "Error: unknown keys: "; |
| 97 | else |
| 98 | errorStream << "Error: unknown key: "; |
| 99 | for(int ikey=0;ikey<badKeys.size();++ikey){ |
| 100 | errorStream << badKeys[ikey] << " "; |
| 101 | } |
| 102 | errorStream << std::endl; |
| 103 | throw(errorStream.str()); |
| 104 | } |
| 105 | // Jim |
| 106 | // open(input_opt[0],memory_opt[0]); |
| 107 | GDALDataType theType=GDT_Unknown; |
| 108 | if(otype_opt.size()){ |
| 109 | theType=string2GDAL(otype_opt[0]); |
| 110 | if(theType==GDT_Unknown) |
| 111 | std::cout << "Warning: unknown output pixel type: " << otype_opt[0] << ", using input type as default" << std::endl; |
| 112 | } |
| 113 | //if output type not set, get type from input image |
| 114 | if(theType==GDT_Unknown){ |
| 115 | theType=getGDALDataType(); |
| 116 | if(verbose_opt[0]) |
| 117 | cout << "Using data type from input image: " << GDALGetDataTypeName(theType) << endl; |
| 118 | } |
| 119 | if(verbose_opt[0]) |
| 120 | cout << "Output pixel type: " << GDALGetDataTypeName(theType) << endl; |
| 121 | // Jim imgWriter; |
| 122 | try{ |
| 123 | imgWriter.open(nrOfCol(),nrOfRow(),nrOfBand(),theType); |
| 124 | // for(unsigned int iband=0;iband<nrOfBand();++iband) |
| 125 | // imgWriter.GDALSetNoDataValue(nodata_opt[0],iband); |
| 126 | imgWriter.setProjection(getProjection()); |
| 127 | imgWriter.copyGeoTransform(*this); |
| 128 | } |
| 129 | catch(string errorstring){ |
| 130 | cout << errorstring << endl; |
| 131 | throw; |
| 132 | } |
| 133 | // if(verbose_opt[0]) |
| 134 | // cout << "opening output image file " << output_opt[0] << endl; |
| 135 | // imgWriter.open(output_opt[0],; |
| 136 | if (getColorTable()!=NULL)//copy colorTable from input image |
| 137 | imgWriter.setColorTable(getColorTable()); |
| 138 | while(nodata_opt.size()<msknodata_opt.size()) |
| 139 | nodata_opt.push_back(nodata_opt.back()); |
| 140 | imgWriter.setNoData(nodata_opt); |
| 141 | if(operator_opt.size()!=msknodata_opt.size()&&operator_opt.size()!=1){ |
| 142 | std::string errorString="Error: number of operators and masks do not match"; |
| 143 | throw(errorString); |
| 144 | } |
| 145 | |
| 146 | // int nmask=vectorMask_opt.size()? vectorMask_opt.size() : mask_opt.size(); |
| 147 | int nmask=vectorMask_opt.size()? vectorMask_opt.size() : maskReader.size(); |
| 148 | if(verbose_opt[0]){ |
| 149 | cout << " mask files selected: " << nmask << endl; |
| 150 | for(int iv=0;iv<msknodata_opt.size();++iv){ |
| 151 | char op=(operator_opt.size()==msknodata_opt.size())?operator_opt[iv]:operator_opt[0]; |
| 152 | cout << op << " " << msknodata_opt[iv] << "->" << nodata_opt[iv] << endl; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // vector<Jim> maskReader(nmask); |
| 157 | if(vectorMask_opt.size()){ |
| 158 | // if(mask_opt.size()){ |
| 159 | if(maskReader.size()){ |
| 160 | string errorString="Error: either raster mask or vector mask can be set, not both"; |
| 161 | throw(errorString); |
| 162 | } |
| 163 | try{ |
| 164 | imgWriter.open(nrOfCol(),nrOfRow(),nrOfBand(),theType); |
| 165 | for(int imask=0;imask<vectorMask_opt.size();++imask){ |
| 166 | shared_ptr<Jim> imgMask=createImg(); |
| 167 | imgMask->open(nrOfCol(),nrOfRow(),1,GDT_Float64); |
| 168 | double gt[6]; |
| 169 | gt[0]=getUlx(); |
| 170 | gt[1]=getDeltaX(); |
| 171 | gt[2]=0; |
| 172 | gt[3]=getUly(); |
| 173 | gt[4]=0; |
| 174 | gt[5]=-getDeltaY(); |
| 175 | imgMask->setGeoTransform(gt); |
| 176 | imgMask->setProjection(getProjectionRef()); |
| 177 | // imgMask->rasterizeBuf(vectorMask_opt[imask]); |
| 178 | VectorOgr ogrReader; |
| 179 | int msknodata=(msknodata_opt.size()>=imask)? msknodata_opt[imask] : msknodata_opt[0]; |
| 180 | ogrReader.open(vectorMask_opt[imask],layernames_opt,true); |
| 181 | imgMask->d_rasterizeBuf(ogrReader,msknodata,eoption_opt,layernames_opt); |
| 182 | maskReader.pushImage(imgMask); |
| 183 | } |
| 184 | } |
| 185 | catch(string error){ |
| 186 | cerr << error << std::endl; |
| 187 | throw; |
| 188 | } |
| 189 | } |
| 190 | // else{ |
| 191 | // for(int imask=0;imask<mask_opt.size();++imask){ |
| 192 | // if(verbose_opt[0]) |
| 193 | // cout << "opening mask image file " << mask_opt[imask] << endl; |
| 194 | // maskReader[imask].open(mask_opt[imask]); |
| 195 | // } |
| 196 | // } |
| 197 | |
| 198 | //duplicate band used for mask if not explicitly provided |
| 199 | while(mskband_opt.size()<maskReader.size()) |
| 200 | mskband_opt.push_back(mskband_opt[0]); |
| 201 | while(msknodata_opt.size()<maskReader.size()) |
| 202 | msknodata_opt.push_back(msknodata_opt[0]); |
| 203 | |
| 204 | Vector2d<double> lineInput(nrOfBand(),nrOfCol()); |
| 205 | Vector2d<double> lineOutput(imgWriter.nrOfBand(),imgWriter.nrOfCol()); |
| 206 | assert(lineOutput.size()==lineInput.size()); |
| 207 | assert(nrOfCol()==imgWriter.nrOfCol()); |
| 208 | Vector2d<double> lineMask(nmask); |
| 209 | for(int imask=0;imask<nmask;++imask){ |
| 210 | if(verbose_opt[0]) |
| 211 | cout << "mask " << imask << " has " << maskReader.getImage(imask)->nrOfCol() << " columns and " << maskReader.getImage(imask)->nrOfRow() << " rows" << endl; |
| 212 | lineMask[imask].resize(maskReader.getImage(imask)->nrOfCol()); |
| 213 | } |
| 214 | unsigned int irow=0; |
| 215 | unsigned int icol=0; |
| 216 | const char* pszMessage; |
| 217 | void* pProgressArg=NULL; |
| 218 | GDALProgressFunc pfnProgress=GDALTermProgress; |
| 219 | float progress=0; |
| 220 | if(!verbose_opt[0]) |
| 221 | MyProgressFunc(progress,pszMessage,pProgressArg); |
| 222 | vector<double> oldRowMask(nmask); |
| 223 | for(int imask=0;imask<nmask;++imask) |
| 224 | oldRowMask[imask]=-1; |
| 225 | for(irow=0;irow<nrOfRow();++irow){ |
| 226 | //read line in lineInput buffer |
| 227 | for(unsigned int iband=0;iband<nrOfBand();++iband){ |
| 228 | try{ |
| 229 | readData(lineInput[iband],irow,iband); |
| 230 | } |
| 231 | catch(string errorstring){ |
| 232 | cerr << errorstring << endl; |
| 233 | throw; |
| 234 | } |
| 235 | } |
| 236 | //todo: support projection difference in mask and input raster |
| 237 | double x,y;//geo coordinates |
| 238 | double colMask,rowMask;//image coordinates in mask image |
| 239 | for(icol=0;icol<nrOfCol();++icol){ |
| 240 | if(nmask>1){//multiple masks |
| 241 | for(int imask=0;imask<nmask;++imask){ |
| 242 | image2geo(icol,irow,x,y); |
| 243 | maskReader.getImage(imask)->geo2image(x,y,colMask,rowMask); |
| 244 | colMask=static_cast<unsigned int>(colMask); |
| 245 | rowMask=static_cast<unsigned int>(rowMask); |
| 246 | bool masked=false; |
| 247 | if(rowMask>=0&&rowMask<maskReader.getImage(imask)->nrOfRow()&&colMask>=0&&colMask<maskReader.getImage(imask)->nrOfCol()){ |
| 248 | if(static_cast<unsigned int>(rowMask)!=static_cast<unsigned int>(oldRowMask[imask])){ |
| 249 | assert(rowMask>=0&&rowMask<maskReader.getImage(imask)->nrOfRow()); |
| 250 | try{ |
| 251 | maskReader.getImage(imask)->readData(lineMask[imask],static_cast<unsigned int>(rowMask),mskband_opt[imask]); |
| 252 | } |
| 253 | catch(string errorstring){ |
| 254 | cerr << errorstring << endl; |
| 255 | throw; |
| 256 | } |
| 257 | oldRowMask[imask]=rowMask; |
| 258 | } |
| 259 | } |
| 260 | else |
| 261 | continue;//no coverage in this mask |
| 262 | int ivalue=0; |
| 263 | if(nmask==msknodata_opt.size())//one invalid value for each mask |
| 264 | ivalue=msknodata_opt[imask]; |
| 265 | else//use same invalid value for each mask |
| 266 | ivalue=msknodata_opt[0]; |
| 267 | char op=(operator_opt.size()==nmask)?operator_opt[imask]:operator_opt[0]; |
| 268 | switch(op){ |
| 269 | case('='): |
| 270 | default: |
| 271 | if(lineMask[imask][colMask]==ivalue) |
| 272 | masked=true; |
| 273 | break; |
| 274 | case('<'): |
| 275 | if(lineMask[imask][colMask]<ivalue) |
| 276 | masked=true; |
| 277 | break; |
| 278 | case('>'): |
| 279 | if(lineMask[imask][colMask]>ivalue) |
| 280 | masked=true; |
| 281 | break; |
| 282 | case('!'): |
| 283 | if(lineMask[imask][colMask]!=ivalue) |
| 284 | masked=true; |
| 285 | break; |
| 286 | } |
| 287 | if(masked){ |
| 288 | if(verbose_opt[0]>1) |
| 289 | cout << "image masked at (col=" << icol << ",row=" << irow <<") and value " << ivalue << endl; |
| 290 | for(unsigned int iband=0;iband<nrOfBand();++iband){ |
| 291 | if(nmask==nodata_opt.size())//one flag value for each mask |
| 292 | lineInput[iband][icol]=nodata_opt[imask]; |
| 293 | else |
| 294 | lineInput[iband][icol]=nodata_opt[0]; |
| 295 | } |
| 296 | masked=false; |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | else{//potentially more invalid values for single mask |
| 302 | image2geo(icol,irow,x,y); |
| 303 | maskReader.getImage(0)->geo2image(x,y,colMask,rowMask); |
| 304 | colMask=static_cast<unsigned int>(colMask); |
| 305 | rowMask=static_cast<unsigned int>(rowMask); |
| 306 | bool masked=false; |
| 307 | if(rowMask>=0&&rowMask<maskReader.getImage(0)->nrOfRow()&&colMask>=0&&colMask<maskReader.getImage(0)->nrOfCol()){ |
| 308 | if(static_cast<unsigned int>(rowMask)!=static_cast<unsigned int>(oldRowMask[0])){ |
| 309 | assert(rowMask>=0&&rowMask<maskReader.getImage(0)->nrOfRow()); |
| 310 | try{ |
| 311 | // maskReader.getImage(0)->readData(lineMask[0],static_cast<unsigned int>(rowMask)); |
| 312 | maskReader.getImage(0)->readData(lineMask[0],static_cast<unsigned int>(rowMask),mskband_opt[0]); |
| 313 | } |
| 314 | catch(string errorstring){ |
| 315 | cerr << errorstring << endl; |
| 316 | throw; |
| 317 | } |
| 318 | oldRowMask[0]=rowMask; |
| 319 | } |
| 320 | for(int ivalue=0;ivalue<msknodata_opt.size();++ivalue){ |
| 321 | assert(msknodata_opt.size()==nodata_opt.size()); |
| 322 | char op=(operator_opt.size()==msknodata_opt.size())?operator_opt[ivalue]:operator_opt[0]; |
| 323 | switch(op){ |
| 324 | case('='): |
| 325 | default: |
| 326 | if(lineMask[0][colMask]==msknodata_opt[ivalue]) |
| 327 | masked=true; |
| 328 | break; |
| 329 | case('<'): |
| 330 | if(lineMask[0][colMask]<msknodata_opt[ivalue]) |
| 331 | masked=true; |
| 332 | break; |
| 333 | case('>'): |
| 334 | if(lineMask[0][colMask]>msknodata_opt[ivalue]) |
| 335 | masked=true; |
| 336 | break; |
| 337 | case('!'): |
| 338 | if(lineMask[0][colMask]!=msknodata_opt[ivalue]) |
| 339 | masked=true; |
| 340 | break; |
| 341 | } |
| 342 | if(masked){ |
| 343 | for(unsigned int iband=0;iband<nrOfBand();++iband) |
| 344 | lineInput[iband][icol]=nodata_opt[ivalue]; |
| 345 | masked=false; |
| 346 | break; |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | for(unsigned int iband=0;iband<lineOutput.size();++iband) |
| 352 | lineOutput[iband][icol]=lineInput[iband][icol]; |
| 353 | } |
| 354 | //write buffer lineOutput to output file |
| 355 | for(unsigned int iband=0;iband<imgWriter.nrOfBand();++iband){ |
| 356 | try{ |
| 357 | imgWriter.writeData(lineOutput[iband],irow,iband); |
| 358 | } |
| 359 | catch(string errorstring){ |
| 360 | cerr << errorstring << endl; |
| 361 | throw; |
| 362 | } |
| 363 | } |
| 364 | //progress bar |
| 365 | progress=static_cast<float>(irow+1.0)/imgWriter.nrOfRow(); |
| 366 | MyProgressFunc(progress,pszMessage,pProgressArg); |
| 367 | } |
| 368 | // for(int imask=0;imask<nmask;++imask) |
| 369 | // maskReader[imask].close(); |
| 370 | } |
| 371 | catch(string predefinedString){ |
| 372 | std::cout << predefinedString << std::endl; |
| 373 | throw; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * @param imgWriter output raster setmask dataset |
| 379 | * @return CE_None if successful, CE_Failure if failed |
| 380 | **/ |
| 381 | void Jim::setMask(VectorOgr& ogrReader, Jim& imgWriter, app::AppFactory& app){ |
| 382 | //command line options |
| 383 | 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"); |
| 384 | Optionjl<double> nodata_opt("nodata", "nodata", "nodata value to put in image if not valid", 0); |
| 385 | Optionjl<string> eoption_opt("eo","eo", "special extent options controlling rasterization: ATTRIBUTE|CHUNKYSIZE|ALL_TOUCHED|BURN_VALUE_FROM|MERGE_ALG, e.g., -eo ALL_TOUCHED=TRUE"); |
| 386 | Optionjl<string> layernames_opt("ln", "ln", "Layer names"); |
| 387 | Optionjl<short> verbose_opt("v", "verbose", "verbose", 0,2); |
| 388 | |
| 389 | otype_opt.setHide(1); |
| 390 | |
| 391 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 392 | try{ |
| 393 | // doProcess=mask_opt.retrieveOption(app); |
| 394 | doProcess=nodata_opt.retrieveOption(app); |
| 395 | otype_opt.retrieveOption(app); |
| 396 | eoption_opt.retrieveOption(app); |
| 397 | layernames_opt.retrieveOption(app); |
| 398 | verbose_opt.retrieveOption(app); |
| 399 | |
| 400 | if(!doProcess){ |
| 401 | cout << endl; |
| 402 | std::ostringstream helpStream; |
| 403 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 404 | throw(helpStream.str());//help was invoked, stop processing |
| 405 | } |
| 406 | |
| 407 | std::vector<std::string> badKeys; |
| 408 | app.badKeys(badKeys); |
| 409 | if(badKeys.size()){ |
| 410 | std::ostringstream errorStream; |
| 411 | if(badKeys.size()>1) |
| 412 | errorStream << "Error: unknown keys: "; |
| 413 | else |
| 414 | errorStream << "Error: unknown key: "; |
| 415 | for(int ikey=0;ikey<badKeys.size();++ikey){ |
| 416 | errorStream << badKeys[ikey] << " "; |
| 417 | } |
| 418 | errorStream << std::endl; |
| 419 | throw(errorStream.str()); |
| 420 | } |
| 421 | GDALDataType theType=GDT_Unknown; |
| 422 | if(otype_opt.size()){ |
| 423 | theType=string2GDAL(otype_opt[0]); |
| 424 | if(theType==GDT_Unknown) |
| 425 | std::cout << "Warning: unknown output pixel type: " << otype_opt[0] << ", using input type as default" << std::endl; |
| 426 | } |
| 427 | //if output type not set, get type from input image |
| 428 | if(theType==GDT_Unknown){ |
| 429 | theType=getGDALDataType(); |
| 430 | if(verbose_opt[0]) |
| 431 | cout << "Using data type from input image: " << GDALGetDataTypeName(theType) << endl; |
| 432 | } |
| 433 | if(verbose_opt[0]) |
| 434 | cout << "Output pixel type: " << GDALGetDataTypeName(theType) << endl; |
| 435 | try{ |
| 436 | imgWriter.open(nrOfCol(),nrOfRow(),nrOfBand(),theType); |
| 437 | imgWriter.setProjection(getProjection()); |
| 438 | imgWriter.copyGeoTransform(*this); |
| 439 | } |
| 440 | catch(string errorstring){ |
| 441 | cout << errorstring << endl; |
| 442 | throw; |
| 443 | } |
| 444 | if (getColorTable()!=NULL)//copy colorTable from input image |
| 445 | imgWriter.setColorTable(getColorTable()); |
| 446 | imgWriter.setNoData(nodata_opt); |
| 447 | |
| 448 | JimList maskReader; |
| 449 | |
| 450 | try{ |
| 451 | imgWriter.open(nrOfCol(),nrOfRow(),nrOfBand(),theType); |
| 452 | shared_ptr<Jim> imgMask=createImg(); |
| 453 | //todo: check if GDT_Float64 is needed (Float32 might be sufficient) |
| 454 | imgMask->open(nrOfCol(),nrOfRow(),1,GDT_Float64); |
| 455 | double gt[6]; |
| 456 | gt[0]=getUlx(); |
| 457 | gt[1]=getDeltaX(); |
| 458 | gt[2]=0; |
| 459 | gt[3]=getUly(); |
| 460 | gt[4]=0; |
| 461 | gt[5]=-getDeltaY(); |
| 462 | imgMask->setGeoTransform(gt); |
| 463 | imgMask->setProjection(getProjectionRef()); |
| 464 | imgMask->d_rasterizeBuf(ogrReader,1,eoption_opt,layernames_opt); |
| 465 | maskReader.pushImage(imgMask); |
| 466 | } |
| 467 | catch(string error){ |
| 468 | cerr << error << std::endl; |
| 469 | throw; |
| 470 | } |
| 471 | |
| 472 | int nmask=maskReader.size(); |
| 473 | Vector2d<double> lineInput(nrOfBand(),nrOfCol()); |
| 474 | Vector2d<double> lineOutput(imgWriter.nrOfBand(),imgWriter.nrOfCol()); |
| 475 | assert(lineOutput.size()==lineInput.size()); |
| 476 | assert(nrOfCol()==imgWriter.nrOfCol()); |
| 477 | Vector2d<double> lineMask(nmask); |
| 478 | for(int imask=0;imask<nmask;++imask){ |
| 479 | if(verbose_opt[0]) |
| 480 | cout << "mask " << imask << " has " << maskReader.getImage(imask)->nrOfCol() << " columns and " << maskReader.getImage(imask)->nrOfRow() << " rows" << endl; |
| 481 | lineMask[imask].resize(maskReader.getImage(imask)->nrOfCol()); |
| 482 | } |
| 483 | unsigned int irow=0; |
| 484 | unsigned int icol=0; |
| 485 | const char* pszMessage; |
| 486 | void* pProgressArg=NULL; |
| 487 | GDALProgressFunc pfnProgress=GDALTermProgress; |
| 488 | float progress=0; |
| 489 | if(!verbose_opt[0]) |
| 490 | MyProgressFunc(progress,pszMessage,pProgressArg); |
| 491 | vector<double> oldRowMask(nmask); |
| 492 | for(int imask=0;imask<nmask;++imask) |
| 493 | oldRowMask[imask]=-1; |
| 494 | for(irow=0;irow<nrOfRow();++irow){ |
| 495 | //read line in lineInput buffer |
| 496 | for(unsigned int iband=0;iband<nrOfBand();++iband){ |
| 497 | try{ |
| 498 | readData(lineInput[iband],irow,iband); |
| 499 | } |
| 500 | catch(string errorstring){ |
| 501 | cerr << errorstring << endl; |
| 502 | throw; |
| 503 | } |
| 504 | } |
| 505 | //todo: support projection difference in mask and input raster |
| 506 | double x,y;//geo coordinates |
| 507 | double colMask,rowMask;//image coordinates in mask image |
| 508 | for(icol=0;icol<nrOfCol();++icol){ |
| 509 | for(int imask=0;imask<nmask;++imask){ |
| 510 | image2geo(icol,irow,x,y); |
| 511 | maskReader.getImage(imask)->geo2image(x,y,colMask,rowMask); |
| 512 | colMask=static_cast<unsigned int>(colMask); |
| 513 | rowMask=static_cast<unsigned int>(rowMask); |
| 514 | bool masked=false; |
| 515 | if(rowMask>=0&&rowMask<maskReader.getImage(imask)->nrOfRow()&&colMask>=0&&colMask<maskReader.getImage(imask)->nrOfCol()){ |
| 516 | if(static_cast<unsigned int>(rowMask)!=static_cast<unsigned int>(oldRowMask[imask])){ |
| 517 | assert(rowMask>=0&&rowMask<maskReader.getImage(imask)->nrOfRow()); |
| 518 | try{ |
| 519 | maskReader.getImage(imask)->readData(lineMask[imask],static_cast<unsigned int>(rowMask)); |
| 520 | } |
| 521 | catch(string errorstring){ |
| 522 | cerr << errorstring << endl; |
| 523 | throw; |
| 524 | } |
| 525 | oldRowMask[imask]=rowMask; |
| 526 | } |
| 527 | } |
| 528 | else{ |
| 529 | if(verbose_opt[0]) |
| 530 | std::cerr << "Warning: no coverage in mask " << imask << std::endl; |
| 531 | continue;//no coverage in this mask |
| 532 | } |
| 533 | if(lineMask[imask][colMask]==1){ |
| 534 | masked=true; |
| 535 | } |
| 536 | if(masked){ |
| 537 | if(verbose_opt[0]>1) |
| 538 | cout << "image masked at (col=" << icol << ",row=" << irow << ")" << endl; |
| 539 | for(unsigned int iband=0;iband<nrOfBand();++iband){ |
| 540 | lineInput[iband][icol]=nodata_opt[0]; |
| 541 | } |
| 542 | masked=false; |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | for(unsigned int iband=0;iband<lineOutput.size();++iband) |
| 547 | lineOutput[iband][icol]=lineInput[iband][icol]; |
| 548 | } |
| 549 | //write buffer lineOutput to output file |
| 550 | for(unsigned int iband=0;iband<imgWriter.nrOfBand();++iband){ |
| 551 | try{ |
| 552 | imgWriter.writeData(lineOutput[iband],irow,iband); |
| 553 | } |
| 554 | catch(string errorstring){ |
| 555 | cerr << errorstring << endl; |
| 556 | throw; |
| 557 | } |
| 558 | } |
| 559 | //progress bar |
| 560 | progress=static_cast<float>(irow+1.0)/imgWriter.nrOfRow(); |
| 561 | MyProgressFunc(progress,pszMessage,pProgressArg); |
| 562 | } |
| 563 | maskReader.close(); |
| 564 | } |
| 565 | catch(string predefinedString){ |
| 566 | std::cout << predefinedString << std::endl; |
| 567 | throw; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | void Jim::d_setMask(VectorOgr& ogrReader, app::AppFactory& app){ |
| 572 | //command line options |
| 573 | Optionjl<double> nodata_opt("nodata", "nodata", "nodata value to put in image if not valid", 0); |
| 574 | Optionjl<string> eoption_opt("eo","eo", "special extent options controlling rasterization: ATTRIBUTE|CHUNKYSIZE|ALL_TOUCHED|BURN_VALUE_FROM|MERGE_ALG, e.g., -eo ALL_TOUCHED=TRUE"); |
| 575 | Optionjl<string> layernames_opt("ln", "ln", "Layer names"); |
| 576 | Optionjl<short> verbose_opt("v", "verbose", "verbose", 0,2); |
| 577 | |
| 578 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 579 | try{ |
| 580 | // doProcess=mask_opt.retrieveOption(app); |
| 581 | doProcess=nodata_opt.retrieveOption(app); |
| 582 | eoption_opt.retrieveOption(app); |
| 583 | layernames_opt.retrieveOption(app); |
| 584 | verbose_opt.retrieveOption(app); |
| 585 | |
| 586 | if(!doProcess){ |
| 587 | cout << endl; |
| 588 | std::ostringstream helpStream; |
| 589 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 590 | throw(helpStream.str());//help was invoked, stop processing |
| 591 | } |
| 592 | |
| 593 | std::vector<std::string> badKeys; |
| 594 | app.badKeys(badKeys); |
| 595 | if(badKeys.size()){ |
| 596 | std::ostringstream errorStream; |
| 597 | if(badKeys.size()>1) |
| 598 | errorStream << "Error: unknown keys: "; |
| 599 | else |
| 600 | errorStream << "Error: unknown key: "; |
| 601 | for(int ikey=0;ikey<badKeys.size();++ikey){ |
| 602 | errorStream << badKeys[ikey] << " "; |
| 603 | } |
| 604 | errorStream << std::endl; |
| 605 | throw(errorStream.str()); |
| 606 | } |
| 607 | |
| 608 | try{ |
| 609 | //todo: check if GDT_Float64 is needed (Float32 might be sufficient) |
| 610 | shared_ptr<Jim> imgMask=createImg(); |
| 611 | // imgMask->open(nrOfCol(),nrOfRow(),1,GDT_Float64); |
| 612 | imgMask->open(nrOfCol(),nrOfRow(),1,GDT_Byte); |
| 613 | double gt[6]; |
| 614 | gt[0]=getUlx(); |
| 615 | gt[1]=getDeltaX(); |
| 616 | gt[2]=0; |
| 617 | gt[3]=getUly(); |
| 618 | gt[4]=0; |
| 619 | gt[5]=-getDeltaY(); |
| 620 | imgMask->setGeoTransform(gt); |
| 621 | imgMask->setProjection(getProjectionRef()); |
| 622 | imgMask->d_rasterizeBuf(ogrReader,nodata_opt[0],eoption_opt,layernames_opt); |
| 623 | //test |
| 624 | // std::cout << imgMask->getMax() << std::endl; |
| 625 | d_setMask(*imgMask,*imgMask); |
| 626 | |
| 627 | // switch(getDataType()){ |
| 628 | // case(GDT_Byte): |
| 629 | // d_setMask_t<unsigned char>(*imgMask,nodata_opt[0]); |
| 630 | // break; |
| 631 | // case(GDT_Int16): |
| 632 | // d_setMask_t<short>(*imgMask,nodata_opt[0]); |
| 633 | // break; |
| 634 | // case(GDT_UInt16): |
| 635 | // d_setMask_t<unsigned short>(*imgMask,nodata_opt[0]); |
| 636 | // break; |
| 637 | // case(GDT_Int32): |
| 638 | // d_setMask_t<int>(*imgMask,nodata_opt[0]); |
| 639 | // break; |
| 640 | // case(GDT_UInt32): |
| 641 | // d_setMask_t<unsigned int>(*imgMask,nodata_opt[0]); |
| 642 | // break; |
| 643 | // case(GDT_Float32): |
| 644 | // d_setMask_t<float>(*imgMask,nodata_opt[0]); |
| 645 | // break; |
| 646 | // case(GDT_Float64): |
| 647 | // d_setMask_t<double>(*imgMask,nodata_opt[0]); |
| 648 | // break; |
| 649 | // default: |
| 650 | // std::string errorString="Error: data type not supported"; |
| 651 | // throw(errorString); |
| 652 | // break; |
| 653 | // } |
| 654 | imgMask->close(); |
| 655 | } |
| 656 | catch(string error){ |
| 657 | cerr << error << std::endl; |
| 658 | throw; |
| 659 | } |
| 660 | |
| 661 | } |
| 662 | catch(string predefinedString){ |
| 663 | std::cout << predefinedString << std::endl; |
| 664 | throw; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | void Jim::d_setMask2D(Jim& mask, Jim& other){ |
| 669 | if(m_data.empty()){ |
| 670 | std::ostringstream s; |
| 671 | s << "Error: Jim not initialized, m_data is empty"; |
| 672 | std::cerr << s.str() << std::endl; |
| 673 | throw(s.str()); |
| 674 | } |
| 675 | if(nrOfRow()!=mask.nrOfRow()){ |
| 676 | std::ostringstream s; |
| 677 | s << "Error: number of rows do not match"; |
| 678 | std::cerr << s.str() << std::endl; |
| 679 | throw(s.str()); |
| 680 | } |
| 681 | if(nrOfCol()!=mask.nrOfCol()){ |
| 682 | std::ostringstream s; |
| 683 | s << "Error: number of cols do not match"; |
| 684 | std::cerr << s.str() << std::endl; |
| 685 | throw(s.str()); |
| 686 | } |
| 687 | if(nrOfPlane()!=other.nrOfPlane()){ |
| 688 | std::ostringstream s; |
| 689 | s << "Error: number of planes do not match"; |
| 690 | std::cerr << s.str() << std::endl; |
| 691 | throw(s.str()); |
| 692 | } |
| 693 | if(nrOfBand()!=other.nrOfBand()){ |
| 694 | std::ostringstream s; |
| 695 | s << "Error: number of bands do not match"; |
| 696 | std::cerr << s.str() << std::endl; |
| 697 | throw(s.str()); |
| 698 | } |
| 699 | if(nrOfRow()!=other.nrOfRow()){ |
| 700 | std::ostringstream s; |
| 701 | s << "Error: number of rows do not match"; |
| 702 | std::cerr << s.str() << std::endl; |
| 703 | throw(s.str()); |
| 704 | } |
| 705 | if(nrOfCol()!=other.nrOfCol()){ |
| 706 | std::ostringstream s; |
| 707 | s << "Error: number of cols do not match"; |
| 708 | std::cerr << s.str() << std::endl; |
| 709 | throw(s.str()); |
| 710 | } |
| 711 | for(size_t iband=0;iband<nrOfBand();++iband){ |
| 712 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 713 | #pragma omp parallel for |
| 714 | #else |
| 715 | #endif |
| 716 | for(int irow=0;irow<nrOfRow();++irow){ |
| 717 | for(int icol=0;icol<nrOfCol();++icol){ |
| 718 | if(mask.readData(icol,irow)>0){ |
| 719 | writeData(other.readData(icol,irow,iband),icol,irow,iband); |
| 720 | } |
| 721 | else |
| 722 | continue; |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | void Jim::d_setMask(Jim& mask, Jim& other){ |
| 729 | if(mask.getGDALDataType()!=GDT_Byte){ |
| 730 | std::ostringstream s; |
| 731 | s << "Error: mask not of type GDT_Byte"; |
| 732 | std::cerr << s.str() << std::endl; |
| 733 | throw(s.str()); |
| 734 | } |
| 735 | // if(getGDALDataType()!=other.getGDALDataType()){ |
| 736 | // std::ostringstream s; |
| 737 | // s << "Error: target not of same data type as source"; |
| 738 | // std::cerr << s.str() << std::endl; |
| 739 | // throw(s.str()); |
| 740 | // } |
| 741 | if(m_data.empty()){ |
| 742 | std::ostringstream s; |
| 743 | s << "Error: Jim not initialized, m_data is empty"; |
| 744 | std::cerr << s.str() << std::endl; |
| 745 | throw(s.str()); |
| 746 | } |
| 747 | if(nrOfRow()!=mask.nrOfRow()){ |
| 748 | std::ostringstream s; |
| 749 | s << "Error: number of rows do not match"; |
| 750 | std::cerr << s.str() << std::endl; |
| 751 | throw(s.str()); |
| 752 | } |
| 753 | if(nrOfCol()!=mask.nrOfCol()){ |
| 754 | std::ostringstream s; |
| 755 | s << "Error: number of cols do not match"; |
| 756 | std::cerr << s.str() << std::endl; |
| 757 | throw(s.str()); |
| 758 | } |
| 759 | if(nrOfRow()!=other.nrOfRow()){ |
| 760 | std::ostringstream s; |
| 761 | s << "Error: number of rows do not match"; |
| 762 | std::cerr << s.str() << std::endl; |
| 763 | throw(s.str()); |
| 764 | } |
| 765 | if(nrOfCol()!=other.nrOfCol()){ |
| 766 | std::ostringstream s; |
| 767 | s << "Error: number of cols do not match"; |
| 768 | std::cerr << s.str() << std::endl; |
| 769 | throw(s.str()); |
| 770 | } |
| 771 | switch(getDataType()){ |
| 772 | case(GDT_Byte): |
| 773 | switch(other.getDataType()){ |
| 774 | case(GDT_Byte): |
| 775 | d_setMask_t<unsigned char,unsigned char>(mask, other); |
| 776 | break; |
| 777 | case(GDT_Int16): |
| 778 | d_setMask_t<unsigned char,short>(mask, other); |
| 779 | break; |
| 780 | case(GDT_UInt16): |
| 781 | d_setMask_t<unsigned char,unsigned short>(mask, other); |
| 782 | break; |
| 783 | case(GDT_Int32): |
| 784 | d_setMask_t<unsigned char,int>(mask, other); |
| 785 | break; |
| 786 | case(GDT_UInt32): |
| 787 | d_setMask_t<unsigned char,unsigned int>(mask, other); |
| 788 | break; |
| 789 | case(GDT_Float32): |
| 790 | d_setMask_t<unsigned char,float>(mask, other); |
| 791 | break; |
| 792 | case(GDT_Float64): |
| 793 | d_setMask_t<unsigned char,double>(mask, other); |
| 794 | break; |
| 795 | default: |
| 796 | std::string errorString="Error: data type not supported"; |
| 797 | throw(errorString); |
| 798 | break; |
| 799 | } |
| 800 | break; |
| 801 | case(GDT_Int16): |
| 802 | switch(other.getDataType()){ |
| 803 | case(GDT_Byte): |
| 804 | d_setMask_t<short,unsigned char>(mask, other); |
| 805 | break; |
| 806 | case(GDT_Int16): |
| 807 | d_setMask_t<short,short>(mask, other); |
| 808 | break; |
| 809 | case(GDT_UInt16): |
| 810 | d_setMask_t<short,unsigned short>(mask, other); |
| 811 | break; |
| 812 | case(GDT_Int32): |
| 813 | d_setMask_t<short,int>(mask, other); |
| 814 | break; |
| 815 | case(GDT_UInt32): |
| 816 | d_setMask_t<short,unsigned int>(mask, other); |
| 817 | break; |
| 818 | case(GDT_Float32): |
| 819 | d_setMask_t<short,float>(mask, other); |
| 820 | break; |
| 821 | case(GDT_Float64): |
| 822 | d_setMask_t<short,double>(mask, other); |
| 823 | break; |
| 824 | default: |
| 825 | std::string errorString="Error: data type not supported"; |
| 826 | throw(errorString); |
| 827 | break; |
| 828 | } |
| 829 | break; |
| 830 | case(GDT_UInt16): |
| 831 | switch(other.getDataType()){ |
| 832 | case(GDT_Byte): |
| 833 | d_setMask_t<unsigned short,unsigned char>(mask, other); |
| 834 | break; |
| 835 | case(GDT_Int16): |
| 836 | d_setMask_t<unsigned short,short>(mask, other); |
| 837 | break; |
| 838 | case(GDT_UInt16): |
| 839 | d_setMask_t<unsigned short,unsigned short>(mask, other); |
| 840 | break; |
| 841 | case(GDT_Int32): |
| 842 | d_setMask_t<unsigned short,int>(mask, other); |
| 843 | break; |
| 844 | case(GDT_UInt32): |
| 845 | d_setMask_t<unsigned short,unsigned int>(mask, other); |
| 846 | break; |
| 847 | case(GDT_Float32): |
| 848 | d_setMask_t<unsigned short,float>(mask, other); |
| 849 | break; |
| 850 | case(GDT_Float64): |
| 851 | d_setMask_t<unsigned short,double>(mask, other); |
| 852 | break; |
| 853 | default: |
| 854 | std::string errorString="Error: data type not supported"; |
| 855 | throw(errorString); |
| 856 | break; |
| 857 | } |
| 858 | break; |
| 859 | case(GDT_Int32): |
| 860 | switch(other.getDataType()){ |
| 861 | case(GDT_Byte): |
| 862 | d_setMask_t<int,unsigned char>(mask, other); |
| 863 | break; |
| 864 | case(GDT_Int16): |
| 865 | d_setMask_t<int,short>(mask, other); |
| 866 | break; |
| 867 | case(GDT_UInt16): |
| 868 | d_setMask_t<int,unsigned short>(mask, other); |
| 869 | break; |
| 870 | case(GDT_Int32): |
| 871 | d_setMask_t<int,int>(mask, other); |
| 872 | break; |
| 873 | case(GDT_UInt32): |
| 874 | d_setMask_t<int,unsigned int>(mask, other); |
| 875 | break; |
| 876 | case(GDT_Float32): |
| 877 | d_setMask_t<int,float>(mask, other); |
| 878 | break; |
| 879 | case(GDT_Float64): |
| 880 | d_setMask_t<int,double>(mask, other); |
| 881 | break; |
| 882 | default: |
| 883 | std::string errorString="Error: data type not supported"; |
| 884 | throw(errorString); |
| 885 | break; |
| 886 | } |
| 887 | break; |
| 888 | case(GDT_UInt32): |
| 889 | switch(other.getDataType()){ |
| 890 | case(GDT_Byte): |
| 891 | d_setMask_t<unsigned int,unsigned char>(mask, other); |
| 892 | break; |
| 893 | case(GDT_Int16): |
| 894 | d_setMask_t<unsigned int,short>(mask, other); |
| 895 | break; |
| 896 | case(GDT_UInt16): |
| 897 | d_setMask_t<unsigned int,unsigned short>(mask, other); |
| 898 | break; |
| 899 | case(GDT_Int32): |
| 900 | d_setMask_t<unsigned int,int>(mask, other); |
| 901 | break; |
| 902 | case(GDT_UInt32): |
| 903 | d_setMask_t<unsigned int,unsigned int>(mask, other); |
| 904 | break; |
| 905 | case(GDT_Float32): |
| 906 | d_setMask_t<unsigned int,float>(mask, other); |
| 907 | break; |
| 908 | case(GDT_Float64): |
| 909 | d_setMask_t<unsigned int,double>(mask, other); |
| 910 | break; |
| 911 | default: |
| 912 | std::string errorString="Error: data type not supported"; |
| 913 | throw(errorString); |
| 914 | break; |
| 915 | } |
| 916 | break; |
| 917 | case(GDT_Float32): |
| 918 | switch(other.getDataType()){ |
| 919 | case(GDT_Byte): |
| 920 | d_setMask_t<float,unsigned char>(mask, other); |
| 921 | break; |
| 922 | case(GDT_Int16): |
| 923 | d_setMask_t<float,short>(mask, other); |
| 924 | break; |
| 925 | case(GDT_UInt16): |
| 926 | d_setMask_t<float,unsigned short>(mask, other); |
| 927 | break; |
| 928 | case(GDT_Int32): |
| 929 | d_setMask_t<float,int>(mask, other); |
| 930 | break; |
| 931 | case(GDT_UInt32): |
| 932 | d_setMask_t<float,unsigned int>(mask, other); |
| 933 | break; |
| 934 | case(GDT_Float32): |
| 935 | d_setMask_t<float,float>(mask, other); |
| 936 | break; |
| 937 | case(GDT_Float64): |
| 938 | d_setMask_t<float,double>(mask, other); |
| 939 | break; |
| 940 | default: |
| 941 | std::string errorString="Error: data type not supported"; |
| 942 | throw(errorString); |
| 943 | break; |
| 944 | } |
| 945 | break; |
| 946 | case(GDT_Float64): |
| 947 | switch(other.getDataType()){ |
| 948 | case(GDT_Byte): |
| 949 | d_setMask_t<double,unsigned char>(mask, other); |
| 950 | break; |
| 951 | case(GDT_Int16): |
| 952 | d_setMask_t<double,short>(mask, other); |
| 953 | break; |
| 954 | case(GDT_UInt16): |
| 955 | d_setMask_t<double,unsigned short>(mask, other); |
| 956 | break; |
| 957 | case(GDT_Int32): |
| 958 | d_setMask_t<double,int>(mask, other); |
| 959 | break; |
| 960 | case(GDT_UInt32): |
| 961 | d_setMask_t<double,unsigned int>(mask, other); |
| 962 | break; |
| 963 | case(GDT_Float32): |
| 964 | d_setMask_t<double,float>(mask, other); |
| 965 | break; |
| 966 | case(GDT_Float64): |
| 967 | d_setMask_t<double,double>(mask, other); |
| 968 | break; |
| 969 | default: |
| 970 | std::string errorString="Error: data type not supported"; |
| 971 | throw(errorString); |
| 972 | break; |
| 973 | } |
| 974 | break; |
| 975 | default: |
| 976 | std::string errorString="Error: data type not supported"; |
| 977 | throw(errorString); |
| 978 | break; |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | void Jim::d_setMask2D(Jim& mask, double value){ |
| 983 | if(m_data.empty()){ |
| 984 | std::ostringstream s; |
| 985 | s << "Error: Jim not initialized, m_data is empty"; |
| 986 | std::cerr << s.str() << std::endl; |
| 987 | throw(s.str()); |
| 988 | } |
| 989 | if(nrOfRow()!=mask.nrOfRow()){ |
| 990 | std::ostringstream s; |
| 991 | s << "Error: number of rows do not match"; |
| 992 | std::cerr << s.str() << std::endl; |
| 993 | throw(s.str()); |
| 994 | } |
| 995 | if(nrOfCol()!=mask.nrOfCol()){ |
| 996 | std::ostringstream s; |
| 997 | s << "Error: number of cols do not match"; |
| 998 | std::cerr << s.str() << std::endl; |
| 999 | throw(s.str()); |
| 1000 | } |
| 1001 | for(size_t iband=0;iband<nrOfBand();++iband){ |
| 1002 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 1003 | #pragma omp parallel for |
| 1004 | #else |
| 1005 | #endif |
| 1006 | for(int irow=0;irow<nrOfRow();++irow){ |
| 1007 | for(int icol=0;icol<nrOfCol();++icol){ |
| 1008 | if(mask.readData(icol,irow)>0) |
| 1009 | writeData(value,icol,irow,iband); |
| 1010 | else |
| 1011 | continue; |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | void Jim::d_setMask(Jim& mask, double value){ |
| 1018 | if(m_data.empty()){ |
| 1019 | std::ostringstream s; |
| 1020 | s << "Error: Jim not initialized, m_data is empty"; |
| 1021 | std::cerr << s.str() << std::endl; |
| 1022 | throw(s.str()); |
| 1023 | } |
| 1024 | if(nrOfRow()!=mask.nrOfRow()){ |
| 1025 | std::ostringstream s; |
| 1026 | s << "Error: number of rows do not match"; |
| 1027 | std::cerr << s.str() << std::endl; |
| 1028 | throw(s.str()); |
| 1029 | } |
| 1030 | if(nrOfCol()!=mask.nrOfCol()){ |
| 1031 | std::ostringstream s; |
| 1032 | s << "Error: number of cols do not match"; |
| 1033 | std::cerr << s.str() << std::endl; |
| 1034 | throw(s.str()); |
| 1035 | } |
| 1036 | switch(getDataType()){ |
| 1037 | case(GDT_Byte): |
| 1038 | d_setMask_t<unsigned char>(mask,value); |
| 1039 | break; |
| 1040 | case(GDT_Int16): |
| 1041 | d_setMask_t<short>(mask,value); |
| 1042 | break; |
| 1043 | case(GDT_UInt16): |
| 1044 | d_setMask_t<unsigned short>(mask,value); |
| 1045 | break; |
| 1046 | case(GDT_Int32): |
| 1047 | d_setMask_t<int>(mask,value); |
| 1048 | break; |
| 1049 | case(GDT_UInt32): |
| 1050 | d_setMask_t<unsigned int>(mask,value); |
| 1051 | break; |
| 1052 | case(GDT_Float32): |
| 1053 | d_setMask_t<float>(mask,value); |
| 1054 | break; |
| 1055 | case(GDT_Float64): |
| 1056 | d_setMask_t<double>(mask,value); |
| 1057 | break; |
| 1058 | default: |
| 1059 | std::string errorString="Error: data type not supported"; |
| 1060 | throw(errorString); |
| 1061 | break; |
| 1062 | } |
| 1063 | } |