/********************************************************************** jlsetmask_lib.cc: program to apply mask image (set invalid values) to raster image 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 "imageclasses/Jim.h" #include "imageclasses/VectorOgr.h" #include "base/Optionjl.h" #include "apps/AppFactory.h" #include "jlsetmask_lib.h" using namespace std; using namespace app; /** * @param app application specific option arguments * @return output image **/ shared_ptr Jim::setMask(JimList& maskReader, app::AppFactory& app){ shared_ptr imgWriter=createImg(); setMask(maskReader, *imgWriter, app); return(imgWriter); } /** * @param app application specific option arguments * @return output image **/ shared_ptr Jim::setMask(VectorOgr& ogrReader, app::AppFactory& app){ shared_ptr imgWriter=createImg(); setMask(ogrReader, *imgWriter, app); return(imgWriter); } /** * @param imgWriter output raster setmask dataset **/ void Jim::setMask(JimList& maskReader, Jim& imgWriter, app::AppFactory& app){ //command line options // Optionjl mask_opt("m", "mask", "Mask image(s)"); Optionjl vectorMask_opt("vm", "vectormask", "Vector mask dataset(s)"); 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 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); Optionjl mskband_opt("mskband", "mskband", "Mask band to read (0 indexed). Provide band for each mask.", 0); Optionjl operator_opt("p", "operator", "Operator: < = > !. Use operator for each msknodata option", '='); Optionjl nodata_opt("nodata", "nodata", "nodata value to put in image if not valid", 0); Optionjl eoption_opt("eo","eo", "special extent options controlling rasterization: ATTRIBUTE|CHUNKYSIZE|ALL_TOUCHED|BURN_VALUE_FROM|MERGE_ALG, e.g., -eo ALL_TOUCHED=TRUE"); Optionjl layernames_opt("ln", "ln", "Layer names"); Optionjl verbose_opt("v", "verbose", "verbose", 0,2); otype_opt.setHide(1); mskband_opt.setHide(1); bool doProcess;//stop process when program was invoked with help option (-h --help) try{ // doProcess=mask_opt.retrieveOption(app); doProcess=vectorMask_opt.retrieveOption(app); msknodata_opt.retrieveOption(app); mskband_opt.retrieveOption(app); nodata_opt.retrieveOption(app); operator_opt.retrieveOption(app); otype_opt.retrieveOption(app); eoption_opt.retrieveOption(app); layernames_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" << nodata_opt[iv] << endl; } } // vector maskReader(nmask); if(vectorMask_opt.size()){ // if(mask_opt.size()){ if(maskReader.size()){ string errorString="Error: either raster mask or vector mask can be set, not both"; throw(errorString); } try{ imgWriter.open(nrOfCol(),nrOfRow(),nrOfBand(),theType); for(int imask=0;imask imgMask=createImg(); imgMask->open(nrOfCol(),nrOfRow(),1,GDT_Float64); double gt[6]; gt[0]=getUlx(); gt[1]=getDeltaX(); gt[2]=0; gt[3]=getUly(); gt[4]=0; gt[5]=-getDeltaY(); imgMask->setGeoTransform(gt); imgMask->setProjection(getProjectionRef()); // imgMask->rasterizeBuf(vectorMask_opt[imask]); VectorOgr ogrReader; int msknodata=(msknodata_opt.size()>=imask)? msknodata_opt[imask] : msknodata_opt[0]; ogrReader.open(vectorMask_opt[imask],layernames_opt,true); imgMask->d_rasterizeBuf(ogrReader,msknodata,eoption_opt,layernames_opt); maskReader.pushImage(imgMask); } } catch(string error){ cerr << error << std::endl; throw; } } // else{ // for(int imask=0;imask lineInput(nrOfBand(),nrOfCol()); Vector2d lineOutput(imgWriter.nrOfBand(),imgWriter.nrOfCol()); assert(lineOutput.size()==lineInput.size()); assert(nrOfCol()==imgWriter.nrOfCol()); Vector2d lineMask(nmask); for(int imask=0;imasknrOfCol() << " columns and " << maskReader.getImage(imask)->nrOfRow() << " rows" << endl; lineMask[imask].resize(maskReader.getImage(imask)->nrOfCol()); } unsigned int irow=0; unsigned int icol=0; const char* pszMessage; void* pProgressArg=NULL; GDALProgressFunc pfnProgress=GDALTermProgress; float progress=0; if(!verbose_opt[0]) MyProgressFunc(progress,pszMessage,pProgressArg); vector oldRowMask(nmask); for(int imask=0;imask1){//multiple masks for(int imask=0;imaskgeo2image(x,y,colMask,rowMask); colMask=static_cast(colMask); rowMask=static_cast(rowMask); bool masked=false; if(rowMask>=0&&rowMasknrOfRow()&&colMask>=0&&colMasknrOfCol()){ if(static_cast(rowMask)!=static_cast(oldRowMask[imask])){ assert(rowMask>=0&&rowMasknrOfRow()); try{ maskReader.getImage(imask)->readData(lineMask[imask],static_cast(rowMask),mskband_opt[imask]); } catch(string errorstring){ cerr << errorstring << endl; throw; } oldRowMask[imask]=rowMask; } } else continue;//no coverage in this mask int ivalue=0; if(nmask==msknodata_opt.size())//one invalid value for each mask ivalue=msknodata_opt[imask]; else//use same invalid value for each mask ivalue=msknodata_opt[0]; char op=(operator_opt.size()==nmask)?operator_opt[imask]:operator_opt[0]; switch(op){ case('='): default: if(lineMask[imask][colMask]==ivalue) masked=true; break; case('<'): if(lineMask[imask][colMask]'): if(lineMask[imask][colMask]>ivalue) masked=true; break; case('!'): if(lineMask[imask][colMask]!=ivalue) masked=true; break; } if(masked){ if(verbose_opt[0]>1) cout << "image masked at (col=" << icol << ",row=" << irow <<") and value " << ivalue << endl; for(unsigned int iband=0;ibandgeo2image(x,y,colMask,rowMask); colMask=static_cast(colMask); rowMask=static_cast(rowMask); bool masked=false; if(rowMask>=0&&rowMasknrOfRow()&&colMask>=0&&colMasknrOfCol()){ if(static_cast(rowMask)!=static_cast(oldRowMask[0])){ assert(rowMask>=0&&rowMasknrOfRow()); try{ // maskReader.getImage(0)->readData(lineMask[0],static_cast(rowMask)); maskReader.getImage(0)->readData(lineMask[0],static_cast(rowMask),mskband_opt[0]); } catch(string errorstring){ cerr << errorstring << endl; throw; } oldRowMask[0]=rowMask; } for(int ivalue=0;ivalue'): if(lineMask[0][colMask]>msknodata_opt[ivalue]) masked=true; break; case('!'): if(lineMask[0][colMask]!=msknodata_opt[ivalue]) masked=true; break; } if(masked){ for(unsigned int iband=0;iband(irow+1.0)/imgWriter.nrOfRow(); MyProgressFunc(progress,pszMessage,pProgressArg); } // for(int imask=0;imask 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 nodata_opt("nodata", "nodata", "nodata value to put in image if not valid", 0); Optionjl eoption_opt("eo","eo", "special extent options controlling rasterization: ATTRIBUTE|CHUNKYSIZE|ALL_TOUCHED|BURN_VALUE_FROM|MERGE_ALG, e.g., -eo ALL_TOUCHED=TRUE"); Optionjl layernames_opt("ln", "ln", "Layer names"); Optionjl verbose_opt("v", "verbose", "verbose", 0,2); otype_opt.setHide(1); bool doProcess;//stop process when program was invoked with help option (-h --help) try{ // doProcess=mask_opt.retrieveOption(app); doProcess=nodata_opt.retrieveOption(app); otype_opt.retrieveOption(app); eoption_opt.retrieveOption(app); layernames_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 imgMask=createImg(); //todo: check if GDT_Float64 is needed (Float32 might be sufficient) imgMask->open(nrOfCol(),nrOfRow(),1,GDT_Float64); double gt[6]; gt[0]=getUlx(); gt[1]=getDeltaX(); gt[2]=0; gt[3]=getUly(); gt[4]=0; gt[5]=-getDeltaY(); imgMask->setGeoTransform(gt); imgMask->setProjection(getProjectionRef()); imgMask->d_rasterizeBuf(ogrReader,1,eoption_opt,layernames_opt); maskReader.pushImage(imgMask); } catch(string error){ cerr << error << std::endl; throw; } int nmask=maskReader.size(); Vector2d lineInput(nrOfBand(),nrOfCol()); Vector2d lineOutput(imgWriter.nrOfBand(),imgWriter.nrOfCol()); assert(lineOutput.size()==lineInput.size()); assert(nrOfCol()==imgWriter.nrOfCol()); Vector2d lineMask(nmask); for(int imask=0;imasknrOfCol() << " columns and " << maskReader.getImage(imask)->nrOfRow() << " rows" << endl; lineMask[imask].resize(maskReader.getImage(imask)->nrOfCol()); } unsigned int irow=0; unsigned int icol=0; const char* pszMessage; void* pProgressArg=NULL; GDALProgressFunc pfnProgress=GDALTermProgress; float progress=0; if(!verbose_opt[0]) MyProgressFunc(progress,pszMessage,pProgressArg); vector oldRowMask(nmask); for(int imask=0;imaskgeo2image(x,y,colMask,rowMask); colMask=static_cast(colMask); rowMask=static_cast(rowMask); bool masked=false; if(rowMask>=0&&rowMasknrOfRow()&&colMask>=0&&colMasknrOfCol()){ if(static_cast(rowMask)!=static_cast(oldRowMask[imask])){ assert(rowMask>=0&&rowMasknrOfRow()); try{ maskReader.getImage(imask)->readData(lineMask[imask],static_cast(rowMask)); } catch(string errorstring){ cerr << errorstring << endl; throw; } oldRowMask[imask]=rowMask; } } else{ if(verbose_opt[0]) std::cerr << "Warning: no coverage in mask " << imask << std::endl; continue;//no coverage in this mask } if(lineMask[imask][colMask]==1){ masked=true; } if(masked){ if(verbose_opt[0]>1) cout << "image masked at (col=" << icol << ",row=" << irow << ")" << endl; for(unsigned int iband=0;iband(irow+1.0)/imgWriter.nrOfRow(); MyProgressFunc(progress,pszMessage,pProgressArg); } maskReader.close(); } catch(string predefinedString){ std::cout << predefinedString << std::endl; throw; } } void Jim::d_setMask(VectorOgr& ogrReader, app::AppFactory& app){ //command line options Optionjl nodata_opt("nodata", "nodata", "nodata value to put in image if not valid", 0); Optionjl eoption_opt("eo","eo", "special extent options controlling rasterization: ATTRIBUTE|CHUNKYSIZE|ALL_TOUCHED|BURN_VALUE_FROM|MERGE_ALG, e.g., -eo ALL_TOUCHED=TRUE"); Optionjl layernames_opt("ln", "ln", "Layer names"); Optionjl verbose_opt("v", "verbose", "verbose", 0,2); bool doProcess;//stop process when program was invoked with help option (-h --help) try{ // doProcess=mask_opt.retrieveOption(app); doProcess=nodata_opt.retrieveOption(app); eoption_opt.retrieveOption(app); layernames_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 imgMask=createImg(); // imgMask->open(nrOfCol(),nrOfRow(),1,GDT_Float64); imgMask->open(nrOfCol(),nrOfRow(),1,GDT_Byte); double gt[6]; gt[0]=getUlx(); gt[1]=getDeltaX(); gt[2]=0; gt[3]=getUly(); gt[4]=0; gt[5]=-getDeltaY(); imgMask->setGeoTransform(gt); imgMask->setProjection(getProjectionRef()); imgMask->d_rasterizeBuf(ogrReader,nodata_opt[0],eoption_opt,layernames_opt); //test // std::cout << imgMask->getMax() << std::endl; d_setMask(*imgMask,*imgMask); // switch(getDataType()){ // case(GDT_Byte): // d_setMask_t(*imgMask,nodata_opt[0]); // break; // case(GDT_Int16): // d_setMask_t(*imgMask,nodata_opt[0]); // break; // case(GDT_UInt16): // d_setMask_t(*imgMask,nodata_opt[0]); // break; // case(GDT_Int32): // d_setMask_t(*imgMask,nodata_opt[0]); // break; // case(GDT_UInt32): // d_setMask_t(*imgMask,nodata_opt[0]); // break; // case(GDT_Float32): // d_setMask_t(*imgMask,nodata_opt[0]); // break; // case(GDT_Float64): // d_setMask_t(*imgMask,nodata_opt[0]); // break; // default: // std::string errorString="Error: data type not supported"; // throw(errorString); // break; // } imgMask->close(); } catch(string error){ cerr << error << std::endl; throw; } } catch(string predefinedString){ std::cout << predefinedString << std::endl; throw; } } void Jim::d_setMask2D(Jim& mask, Jim& other){ if(m_data.empty()){ std::ostringstream s; s << "Error: Jim not initialized, m_data is empty"; std::cerr << s.str() << std::endl; throw(s.str()); } if(nrOfRow()!=mask.nrOfRow()){ std::ostringstream s; s << "Error: number of rows do not match"; std::cerr << s.str() << std::endl; throw(s.str()); } if(nrOfCol()!=mask.nrOfCol()){ std::ostringstream s; s << "Error: number of cols do not match"; std::cerr << s.str() << std::endl; throw(s.str()); } if(nrOfPlane()!=other.nrOfPlane()){ std::ostringstream s; s << "Error: number of planes do not match"; std::cerr << s.str() << std::endl; throw(s.str()); } if(nrOfBand()!=other.nrOfBand()){ std::ostringstream s; s << "Error: number of bands do not match"; std::cerr << s.str() << std::endl; throw(s.str()); } if(nrOfRow()!=other.nrOfRow()){ std::ostringstream s; s << "Error: number of rows do not match"; std::cerr << s.str() << std::endl; throw(s.str()); } if(nrOfCol()!=other.nrOfCol()){ std::ostringstream s; s << "Error: number of cols do not match"; std::cerr << s.str() << std::endl; throw(s.str()); } for(size_t iband=0;iband0){ writeData(other.readData(icol,irow,iband),icol,irow,iband); } else continue; } } } } void Jim::d_setMask(Jim& mask, Jim& other){ if(mask.getGDALDataType()!=GDT_Byte){ std::ostringstream s; s << "Error: mask not of type GDT_Byte"; std::cerr << s.str() << std::endl; throw(s.str()); } // if(getGDALDataType()!=other.getGDALDataType()){ // std::ostringstream s; // s << "Error: target not of same data type as source"; // std::cerr << s.str() << std::endl; // throw(s.str()); // } if(m_data.empty()){ std::ostringstream s; s << "Error: Jim not initialized, m_data is empty"; std::cerr << s.str() << std::endl; throw(s.str()); } if(nrOfRow()!=mask.nrOfRow()){ std::ostringstream s; s << "Error: number of rows do not match"; std::cerr << s.str() << std::endl; throw(s.str()); } if(nrOfCol()!=mask.nrOfCol()){ std::ostringstream s; s << "Error: number of cols do not match"; std::cerr << s.str() << std::endl; throw(s.str()); } if(nrOfRow()!=other.nrOfRow()){ std::ostringstream s; s << "Error: number of rows do not match"; std::cerr << s.str() << std::endl; throw(s.str()); } if(nrOfCol()!=other.nrOfCol()){ std::ostringstream s; s << "Error: number of cols do not match"; std::cerr << s.str() << std::endl; throw(s.str()); } switch(getDataType()){ case(GDT_Byte): switch(other.getDataType()){ case(GDT_Byte): d_setMask_t(mask, other); break; case(GDT_Int16): d_setMask_t(mask, other); break; case(GDT_UInt16): d_setMask_t(mask, other); break; case(GDT_Int32): d_setMask_t(mask, other); break; case(GDT_UInt32): d_setMask_t(mask, other); break; case(GDT_Float32): d_setMask_t(mask, other); break; case(GDT_Float64): d_setMask_t(mask, other); break; default: std::string errorString="Error: data type not supported"; throw(errorString); break; } break; case(GDT_Int16): switch(other.getDataType()){ case(GDT_Byte): d_setMask_t(mask, other); break; case(GDT_Int16): d_setMask_t(mask, other); break; case(GDT_UInt16): d_setMask_t(mask, other); break; case(GDT_Int32): d_setMask_t(mask, other); break; case(GDT_UInt32): d_setMask_t(mask, other); break; case(GDT_Float32): d_setMask_t(mask, other); break; case(GDT_Float64): d_setMask_t(mask, other); break; default: std::string errorString="Error: data type not supported"; throw(errorString); break; } break; case(GDT_UInt16): switch(other.getDataType()){ case(GDT_Byte): d_setMask_t(mask, other); break; case(GDT_Int16): d_setMask_t(mask, other); break; case(GDT_UInt16): d_setMask_t(mask, other); break; case(GDT_Int32): d_setMask_t(mask, other); break; case(GDT_UInt32): d_setMask_t(mask, other); break; case(GDT_Float32): d_setMask_t(mask, other); break; case(GDT_Float64): d_setMask_t(mask, other); break; default: std::string errorString="Error: data type not supported"; throw(errorString); break; } break; case(GDT_Int32): switch(other.getDataType()){ case(GDT_Byte): d_setMask_t(mask, other); break; case(GDT_Int16): d_setMask_t(mask, other); break; case(GDT_UInt16): d_setMask_t(mask, other); break; case(GDT_Int32): d_setMask_t(mask, other); break; case(GDT_UInt32): d_setMask_t(mask, other); break; case(GDT_Float32): d_setMask_t(mask, other); break; case(GDT_Float64): d_setMask_t(mask, other); break; default: std::string errorString="Error: data type not supported"; throw(errorString); break; } break; case(GDT_UInt32): switch(other.getDataType()){ case(GDT_Byte): d_setMask_t(mask, other); break; case(GDT_Int16): d_setMask_t(mask, other); break; case(GDT_UInt16): d_setMask_t(mask, other); break; case(GDT_Int32): d_setMask_t(mask, other); break; case(GDT_UInt32): d_setMask_t(mask, other); break; case(GDT_Float32): d_setMask_t(mask, other); break; case(GDT_Float64): d_setMask_t(mask, other); break; default: std::string errorString="Error: data type not supported"; throw(errorString); break; } break; case(GDT_Float32): switch(other.getDataType()){ case(GDT_Byte): d_setMask_t(mask, other); break; case(GDT_Int16): d_setMask_t(mask, other); break; case(GDT_UInt16): d_setMask_t(mask, other); break; case(GDT_Int32): d_setMask_t(mask, other); break; case(GDT_UInt32): d_setMask_t(mask, other); break; case(GDT_Float32): d_setMask_t(mask, other); break; case(GDT_Float64): d_setMask_t(mask, other); break; default: std::string errorString="Error: data type not supported"; throw(errorString); break; } break; case(GDT_Float64): switch(other.getDataType()){ case(GDT_Byte): d_setMask_t(mask, other); break; case(GDT_Int16): d_setMask_t(mask, other); break; case(GDT_UInt16): d_setMask_t(mask, other); break; case(GDT_Int32): d_setMask_t(mask, other); break; case(GDT_UInt32): d_setMask_t(mask, other); break; case(GDT_Float32): d_setMask_t(mask, other); break; case(GDT_Float64): d_setMask_t(mask, other); break; default: std::string errorString="Error: data type not supported"; throw(errorString); break; } break; default: std::string errorString="Error: data type not supported"; throw(errorString); break; } } void Jim::d_setMask2D(Jim& mask, double value){ if(m_data.empty()){ std::ostringstream s; s << "Error: Jim not initialized, m_data is empty"; std::cerr << s.str() << std::endl; throw(s.str()); } if(nrOfRow()!=mask.nrOfRow()){ std::ostringstream s; s << "Error: number of rows do not match"; std::cerr << s.str() << std::endl; throw(s.str()); } if(nrOfCol()!=mask.nrOfCol()){ std::ostringstream s; s << "Error: number of cols do not match"; std::cerr << s.str() << std::endl; throw(s.str()); } for(size_t iband=0;iband0) writeData(value,icol,irow,iband); else continue; } } } } void Jim::d_setMask(Jim& mask, double value){ if(m_data.empty()){ std::ostringstream s; s << "Error: Jim not initialized, m_data is empty"; std::cerr << s.str() << std::endl; throw(s.str()); } if(nrOfRow()!=mask.nrOfRow()){ std::ostringstream s; s << "Error: number of rows do not match"; std::cerr << s.str() << std::endl; throw(s.str()); } if(nrOfCol()!=mask.nrOfCol()){ std::ostringstream s; s << "Error: number of cols do not match"; std::cerr << s.str() << std::endl; throw(s.str()); } switch(getDataType()){ case(GDT_Byte): d_setMask_t(mask,value); break; case(GDT_Int16): d_setMask_t(mask,value); break; case(GDT_UInt16): d_setMask_t(mask,value); break; case(GDT_Int32): d_setMask_t(mask,value); break; case(GDT_UInt32): d_setMask_t(mask,value); break; case(GDT_Float32): d_setMask_t(mask,value); break; case(GDT_Float64): d_setMask_t(mask,value); break; default: std::string errorString="Error: data type not supported"; throw(errorString); break; } }