| 1 | /********************************************************************** |
| 2 | jlgetmask_lib.cc: program to create mask image based on values in input 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 | #include <vector> |
| 23 | #include "imageclasses/Jim.h" |
| 24 | #include "base/Optionjl.h" |
| 25 | #include "apps/AppFactory.h" |
| 26 | |
| 27 | using namespace std; |
| 28 | using namespace app; |
| 29 | |
| 30 | /** |
| 31 | * @param app application specific option arguments |
| 32 | * @return output image |
| 33 | **/ |
| 34 | shared_ptr<Jim> Jim::getMask(app::AppFactory& app){ |
| 35 | shared_ptr<Jim> imgWriter=createImg(); |
| 36 | getMask(*imgWriter, app); |
| 37 | return(imgWriter); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param imgWriter output raster getmask dataset |
| 42 | * @return CE_None if successful, CE_Failure if failed |
| 43 | **/ |
| 44 | void Jim::getMask(Jim& imgWriter, app::AppFactory& app){ |
| 45 | Optionjl<short> band_opt("b", "band", "band(s) used for mask", 0); |
| 46 | Optionjl<double> min_opt("min", "min", "Values smaller than min threshold(s) are masked as invalid. Use one threshold for each band"); |
| 47 | Optionjl<double> max_opt("max", "max", "Values greater than max threshold(s) are masked as invalid. Use one threshold for each band"); |
| 48 | Optionjl<string> operator_opt("p", "operator", "Operator: [AND,OR].", "OR"); |
| 49 | Optionjl<unsigned short> data_opt("data", "data", "value(s) for valid pixels: between min and max", 1); |
| 50 | Optionjl<unsigned short> nodata_opt("nodata", "nodata", "value(s) for invalid pixels: not between min and max", 0); |
| 51 | 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", "Byte"); |
| 52 | // Optionjl<string> oformat_opt("of", "oformat", "Output image format (see also gdal_translate).","GTiff"); |
| 53 | Optionjl<string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified."); |
| 54 | Optionjl<string> colorTable_opt("ct", "ct", "color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid)"); |
| 55 | Optionjl<unsigned long int> memory_opt("mem", "mem", "Buffer size (in MB) to read image data blocks in memory",0,1); |
| 56 | Optionjl<short> verbose_opt("v", "verbose", "verbose", 0,2); |
| 57 | |
| 58 | band_opt.setHide(1); |
| 59 | operator_opt.setHide(1); |
| 60 | otype_opt.setHide(1); |
| 61 | // oformat_opt.setHide(1); |
| 62 | option_opt.setHide(1); |
| 63 | colorTable_opt.setHide(1); |
| 64 | memory_opt.setHide(1); |
| 65 | |
| 66 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 67 | try{ |
| 68 | doProcess=min_opt.retrieveOption(app); |
| 69 | max_opt.retrieveOption(app); |
| 70 | data_opt.retrieveOption(app); |
| 71 | nodata_opt.retrieveOption(app); |
| 72 | band_opt.retrieveOption(app); |
| 73 | operator_opt.retrieveOption(app); |
| 74 | otype_opt.retrieveOption(app); |
| 75 | colorTable_opt.retrieveOption(app); |
| 76 | verbose_opt.retrieveOption(app); |
| 77 | |
| 78 | if(!doProcess){ |
| 79 | cout << endl; |
| 80 | std::ostringstream helpStream; |
| 81 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 82 | throw(helpStream.str());//help was invoked, stop processing |
| 83 | } |
| 84 | |
| 85 | std::vector<std::string> badKeys; |
| 86 | app.badKeys(badKeys); |
| 87 | if(badKeys.size()){ |
| 88 | std::ostringstream errorStream; |
| 89 | if(badKeys.size()>1) |
| 90 | errorStream << "Error: unknown keys: "; |
| 91 | else |
| 92 | errorStream << "Error: unknown key: "; |
| 93 | for(int ikey=0;ikey<badKeys.size();++ikey){ |
| 94 | errorStream << badKeys[ikey] << " "; |
| 95 | } |
| 96 | errorStream << std::endl; |
| 97 | throw(errorStream.str()); |
| 98 | } |
| 99 | GDALDataType theType=GDT_Unknown; |
| 100 | if(otype_opt.size()){ |
| 101 | theType=string2GDAL(otype_opt[0]); |
| 102 | if(theType==GDT_Unknown) |
| 103 | std::cout << "Warning: unknown output pixel type: " << otype_opt[0] << ", using input type as default" << std::endl; |
| 104 | } |
| 105 | //if output type not set, get type from input image |
| 106 | if(theType==GDT_Unknown){ |
| 107 | theType=getGDALDataType(); |
| 108 | if(verbose_opt[0]) |
| 109 | cout << "Using data type from input image: " << GDALGetDataTypeName(theType) << endl; |
| 110 | } |
| 111 | if(verbose_opt[0]) |
| 112 | cout << "Output pixel type: " << GDALGetDataTypeName(theType) << endl; |
| 113 | const char* pszMessage; |
| 114 | void* pProgressArg=NULL; |
| 115 | GDALProgressFunc pfnProgress=GDALTermProgress; |
| 116 | double progress=0; |
| 117 | MyProgressFunc(progress,pszMessage,pProgressArg); |
| 118 | |
| 119 | if(band_opt.empty()){ |
| 120 | std::string errorString="Error: band is empty, use option -b"; |
| 121 | throw(errorString); |
| 122 | } |
| 123 | for(int iband=0;iband<band_opt.size();++iband){ |
| 124 | if(band_opt[iband]>=nrOfBand()){ |
| 125 | std::string errorString="Error: bands exceed number of band in input image"; |
| 126 | throw(errorString); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if(min_opt.size()&&max_opt.size()){ |
| 131 | if(min_opt.size()!=max_opt.size()){ |
| 132 | std::string errorString="Error: number of min and max options must correspond if both min and max options are provided"; |
| 133 | throw(errorString); |
| 134 | } |
| 135 | } |
| 136 | if(min_opt.size()){ |
| 137 | while(band_opt.size()>min_opt.size()) |
| 138 | min_opt.push_back(min_opt[0]); |
| 139 | while(min_opt.size()>data_opt.size()) |
| 140 | data_opt.push_back(data_opt[0]); |
| 141 | } |
| 142 | if(max_opt.size()){ |
| 143 | while(band_opt.size()>max_opt.size()) |
| 144 | max_opt.push_back(max_opt[0]); |
| 145 | while(max_opt.size()>data_opt.size()) |
| 146 | data_opt.push_back(data_opt[0]); |
| 147 | } |
| 148 | |
| 149 | vector< vector<float> > lineBuffer(band_opt.size()); |
| 150 | for(unsigned int iband=0;iband<band_opt.size();++iband) |
| 151 | lineBuffer.resize(nrOfCol()); |
| 152 | //if output type not set, get type from input image |
| 153 | if(theType==GDT_Unknown){ |
| 154 | theType=getGDALDataType(); |
| 155 | if(verbose_opt[0]) |
| 156 | cout << "Using data type from input image: " << GDALGetDataTypeName(theType) << endl; |
| 157 | } |
| 158 | // string imageType;//=getImageType(); |
| 159 | // if(oformat_opt.size())//default |
| 160 | // imageType=oformat_opt[0]; |
| 161 | // if(option_opt.findSubstring("INTERLEAVE=")==option_opt.end()){ |
| 162 | // string theInterleave="INTERLEAVE="; |
| 163 | // theInterleave+=getInterleave(); |
| 164 | // option_opt.push_back(theInterleave); |
| 165 | // } |
| 166 | imgWriter.open(nrOfCol(),nrOfRow(),1,theType); |
| 167 | if(colorTable_opt.size()){ |
| 168 | if(colorTable_opt[0]!="none") |
| 169 | imgWriter.setColorTable(colorTable_opt[0]); |
| 170 | } |
| 171 | else if (getColorTable()!=NULL)//copy colorTable from input image |
| 172 | imgWriter.setColorTable(getColorTable()); |
| 173 | |
| 174 | imgWriter.setProjection(getProjection()); |
| 175 | double gt[6]; |
| 176 | getGeoTransform(gt); |
| 177 | imgWriter.setGeoTransform(gt);//ulx,uly,getDeltaX(),getDeltaY(),0,0); |
| 178 | |
| 179 | if(nodata_opt.size()) |
| 180 | imgWriter.GDALSetNoDataValue(nodata_opt[0]); |
| 181 | |
| 182 | vector<char> writeBuffer(imgWriter.nrOfCol()); |
| 183 | for(unsigned int irow=0;irow<nrOfRow();++irow){ |
| 184 | for(unsigned int iband=0;iband<band_opt.size();++iband) |
| 185 | readData(lineBuffer[iband],irow,band_opt[iband]); |
| 186 | for(unsigned int icol=0;icol<nrOfCol();++icol){ |
| 187 | bool valid=(operator_opt[0]=="OR")?false:true; |
| 188 | unsigned short validValue=data_opt[0]; |
| 189 | if(min_opt.size()&&max_opt.size()){ |
| 190 | assert(max_opt.size()==min_opt.size()); |
| 191 | for(unsigned int ivalid=0;ivalid<min_opt.size();++ivalid){ |
| 192 | bool validBand=false; |
| 193 | // for(unsigned int iband=0;iband<band_opt.size();++iband){ |
| 194 | unsigned short theBand=(band_opt.size()==min_opt.size())? ivalid:0; |
| 195 | if(lineBuffer[theBand][icol]>=min_opt[ivalid]&&lineBuffer[theBand][icol]<=max_opt[ivalid]){ |
| 196 | validValue=data_opt[ivalid]; |
| 197 | validBand=true; |
| 198 | } |
| 199 | valid=(operator_opt[0]=="OR")?valid||validBand : valid&&validBand; |
| 200 | } |
| 201 | } |
| 202 | else if(min_opt.size()){ |
| 203 | for(int ivalid=0;ivalid<min_opt.size();++ivalid){ |
| 204 | bool validBand=false; |
| 205 | // for(int iband=0;iband<band_opt.size();++iband){ |
| 206 | unsigned short theBand=(band_opt.size()==min_opt.size())? ivalid:0; |
| 207 | if(lineBuffer[theBand][icol]>=min_opt[ivalid]){ |
| 208 | validValue=data_opt[ivalid]; |
| 209 | validBand=true; |
| 210 | } |
| 211 | valid=(operator_opt[0]=="OR")?valid||validBand : valid&&validBand; |
| 212 | } |
| 213 | } |
| 214 | else if(max_opt.size()){ |
| 215 | for(int ivalid=0;ivalid<max_opt.size();++ivalid){ |
| 216 | bool validBand=false; |
| 217 | // for(int iband=0;iband<band_opt.size();++iband){ |
| 218 | unsigned short theBand=(band_opt.size()==max_opt.size())? ivalid:0; |
| 219 | if(lineBuffer[theBand][icol]<=max_opt[ivalid]){ |
| 220 | validValue=data_opt[ivalid]; |
| 221 | validBand=true; |
| 222 | } |
| 223 | valid=(operator_opt[0]=="OR")?valid||validBand : valid&&validBand; |
| 224 | } |
| 225 | } |
| 226 | if(valid) |
| 227 | writeBuffer[icol]=validValue; |
| 228 | else |
| 229 | writeBuffer[icol]=nodata_opt[0]; |
| 230 | } |
| 231 | imgWriter.writeData(writeBuffer,irow); |
| 232 | progress=(1.0+irow)/imgWriter.nrOfRow(); |
| 233 | MyProgressFunc(progress,pszMessage,pProgressArg); |
| 234 | } |
| 235 | } |
| 236 | catch(string predefinedString){ |
| 237 | std::cout << predefinedString << std::endl; |
| 238 | throw; |
| 239 | } |
| 240 | } |