/********************************************************************** jlreclass_lib.cc: program to replace categorical pixel values in raster dataset Author(s): Pieter.Kempeneers@ec.europa.eu Copyright (C) 2016-2020 European Union (Joint Research Centre) This file is part of jiplib. jiplib is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. jiplib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with jiplib. If not, see . ***********************************************************************/ #include #include #include "base/Optionjl.h" #include "imageclasses/Jim.h" #include "apps/AppFactory.h" using namespace std; using namespace app; shared_ptr Jim::reclass(app::AppFactory& app){ shared_ptr imgWriter=createImg(); reclass(*imgWriter, app); return(imgWriter); } void Jim::reclass(Jim& imgWriter, app::AppFactory& app){ Optionjl mask_opt("m", "mask", "Mask image(s)"); Optionjl masknodata_opt("msknodata", "msknodata", "Mask value(s) where image has nodata. Use one value for each mask, or multiple values for a single mask.", 1); Optionjl nodata_opt("nodata", "nodata", "nodata value to put in image if not valid (0)", 0); Optionjl colorTable_opt("ct", "ct", "color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid)"); Optionjl band_opt("b", "band", "band index(es) to replace (other bands are copied to output)", 0); Optionjl otype_opt("ot", "otype", "Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image", ""); Optionjl code_opt("code", "code", "Recode text file (2 columns: from to)"); Optionjl class_opt("c", "class", "list of classes to reclass (in combination with reclass option)"); Optionjl reclass_opt("r", "reclass", "list of recoded classes (in combination with class option)"); // Optionjl fieldname_opt("n", "fname", "field name of the shape file to be replaced", "label"); // Optionjl description_opt("d", "description", "Set image description"); Optionjl verbose_opt("v", "verbose", "verbose", 0); bool doProcess;//stop process when program was invoked with help option (-h --help) try{ doProcess=mask_opt.retrieveOption(app); masknodata_opt.retrieveOption(app); nodata_opt.retrieveOption(app); code_opt.retrieveOption(app); class_opt.retrieveOption(app); reclass_opt.retrieveOption(app); colorTable_opt.retrieveOption(app); otype_opt.retrieveOption(app); band_opt.retrieveOption(app); // fieldname_opt.retrieveOption(app); // description_opt.retrieveOption(app); verbose_opt.retrieveOption(app); if(!doProcess){ cout << endl; std::ostringstream helpStream; helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; throw(helpStream.str());//help was invoked, stop processing } std::vector badKeys; app.badKeys(badKeys); if(badKeys.size()){ std::ostringstream errorStream; if(badKeys.size()>1) errorStream << "Error: unknown keys: "; else errorStream << "Error: unknown key: "; for(int ikey=0;ikey codemapString;//map with codes: codemapString[theKey(from)]=theValue(to) map codemap;//map with codes: codemap[theKey(from)]=theValue(to) if(code_opt.size()){ if(verbose_opt[0]) cout << "opening code text file " << code_opt[0] << endl; ifstream codefile; codefile.open(code_opt[0].c_str()); string theKey; string theValue; while(codefile>>theKey){ codefile >> theValue; codemapString[theKey]=theValue; codemap[string2type(theKey)]=string2type(theValue); } codefile.close(); } else{//use combination of class_opt and reclass_opt assert(class_opt.size()==reclass_opt.size()); for(int iclass=0;iclass(class_opt[iclass])]=string2type(reclass_opt[iclass]); } } assert(codemapString.size()); assert(codemap.size()); //if verbose true, print the codes to screen if(verbose_opt[0]){ map::iterator mit; cout << codemapString.size() << " codes used: " << endl; for(mit=codemapString.begin();mit!=codemapString.end();++mit) cout << (*mit).first << " " << (*mit).second << endl; } bool refIsRaster=true; // if(input_opt[0].find(".shp")!=string::npos){//shape file if(!refIsRaster){ std::string errorString="Error: input must be raster dataset"; throw(errorString); } else{//image file vector maskReader(mask_opt.size()); for(int imask=0;imask" << nodata_opt[iv] << endl; } assert(imgWriter.nrOfCol()==nrOfCol()); // Vector2d lineInput(nrOfBand(),nrOfCol()); Vector2d lineInput(nrOfBand(),nrOfCol()); Vector2d lineMask(mask_opt.size()); for(int imask=0;imask lineOutput(imgWriter.nrOfBand(),imgWriter.nrOfCol()); unsigned int irow=0; unsigned int icol=0; const char* pszMessage; void* pProgressArg=NULL; GDALProgressFunc pfnProgress=GDALTermProgress; double progress=0; MyProgressFunc(progress,pszMessage,pProgressArg); double oldRowMask=-1; for(irow=0;irow1){//multiple masks for(int imask=0;imask(rowMask)!=static_cast(oldRowMask)){ assert(rowMask>=0&&rowMask(rowMask)); } catch(string errorstring){ cerr << errorstring << endl; throw; } oldRowMask=rowMask; } short ivalue=0; if(mask_opt.size()==masknodata_opt.size())//one invalid value for each mask ivalue=masknodata_opt[imask]; else//use same invalid value for each mask ivalue=masknodata_opt[0]; if(lineMask[imask][colMask]==ivalue){ for(unsigned int iband=0;iband(rowMask)!=static_cast(oldRowMask)){ assert(rowMask>=0&&rowMask(rowMask)); } catch(string errorstring){ cerr << errorstring << endl; throw; } oldRowMask=rowMask; } for(int ivalue=0;ivalue((irow+1.0)/imgWriter.nrOfRow()); MyProgressFunc(progress,pszMessage,pProgressArg); } } } catch(string predefinedString){ std::cout << predefinedString << std::endl; throw; } } void Jim::d_reclass(app::AppFactory& app){ Optionjl class_opt("c", "class", "list of classes to reclass (in combination with reclass option)"); Optionjl reclass_opt("r", "reclass", "list of recoded classes (in combination with class option)"); Optionjl verbose_opt("v", "verbose", "verbose", 0); bool doProcess;//stop process when program was invoked with help option (-h --help) try{ doProcess=class_opt.retrieveOption(app); reclass_opt.retrieveOption(app); verbose_opt.retrieveOption(app); if(!doProcess){ cout << endl; std::ostringstream helpStream; helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; throw(helpStream.str());//help was invoked, stop processing } std::vector badKeys; app.badKeys(badKeys); if(badKeys.size()){ std::ostringstream errorStream; if(badKeys.size()>1) errorStream << "Error: unknown keys: "; else errorStream << "Error: unknown key: "; for(int ikey=0;ikey codemapString;//map with codes: codemapString[theKey(from)]=theValue(to) map codemap;//map with codes: codemap[theKey(from)]=theValue(to) for(int iclass=0;iclass(class_opt[iclass])]=string2type(reclass_opt[iclass]); } //if verbose true, print the codes to screen if(verbose_opt[0]){ map::iterator mit; cout << codemapString.size() << " codes used: " << endl; for(mit=codemapString.begin();mit!=codemapString.end();++mit) cout << (*mit).first << " " << (*mit).second << endl; } for(int iband=0;iband