22 #include "imageclasses/ImgReaderGdal.h" 23 #include "imageclasses/ImgWriterGdal.h" 24 #include "base/Optionpk.h" 72 int main(
int argc,
char **argv) {
75 Optionpk<double> min_opt(
"min",
"min",
"Values smaller than min threshold(s) are masked as invalid. Use one threshold for each band");
76 Optionpk<double> max_opt(
"max",
"max",
"Values greater than max threshold(s) are masked as invalid. Use one threshold for each band");
81 Optionpk<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");
82 Optionpk<string> oformat_opt(
"of",
"oformat",
"Output image format (see also gdal_translate).",
"GTiff");
83 Optionpk<string> option_opt(
"co",
"co",
"Creation option for output file. Multiple options can be specified.");
84 Optionpk<string> colorTable_opt(
"ct",
"ct",
"color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid)");
88 operator_opt.setHide(1);
90 oformat_opt.setHide(1);
91 option_opt.setHide(1);
92 colorTable_opt.setHide(1);
96 doProcess=input_opt.retrieveOption(argc,argv);
97 output_opt.retrieveOption(argc,argv);
98 min_opt.retrieveOption(argc,argv);
99 max_opt.retrieveOption(argc,argv);
100 data_opt.retrieveOption(argc,argv);
101 nodata_opt.retrieveOption(argc,argv);
102 band_opt.retrieveOption(argc,argv);
103 operator_opt.retrieveOption(argc,argv);
104 otype_opt.retrieveOption(argc,argv);
105 oformat_opt.retrieveOption(argc,argv);
106 option_opt.retrieveOption(argc,argv);
107 colorTable_opt.retrieveOption(argc,argv);
108 verbose_opt.retrieveOption(argc,argv);
110 catch(
string predefinedString){
111 std::cout << predefinedString << std::endl;
116 cout <<
"Usage: pkgetmask -i input -o output" << endl;
118 std::cout <<
"short option -h shows basic options only, use long option --help to show all options" << std::endl;
122 const char* pszMessage;
123 void* pProgressArg=NULL;
124 GDALProgressFunc pfnProgress=GDALTermProgress;
126 pfnProgress(progress,pszMessage,pProgressArg);
127 GDALDataType theType=GDT_Unknown;
129 cout <<
"possible output data types: ";
130 for(
int iType = 0; iType < GDT_TypeCount; ++iType){
132 cout <<
" " << GDALGetDataTypeName((GDALDataType)iType);
133 if( GDALGetDataTypeName((GDALDataType)iType) != NULL
134 && EQUAL(GDALGetDataTypeName((GDALDataType)iType),
135 otype_opt[0].c_str()))
136 theType=(GDALDataType) iType;
140 if(theType==GDT_Unknown)
141 cout <<
"Unknown output pixel type: " << otype_opt[0] << endl;
143 cout <<
"Output pixel type: " << GDALGetDataTypeName(theType) << endl;
146 assert(input_opt.size());
148 assert(band_opt.size()>=0);
149 assert(band_opt.size()<=imgReader.nrOfBand());
151 if(min_opt.size()&&max_opt.size()){
152 if(min_opt.size()!=max_opt.size())
153 cerr <<
"Error: number of min and max options must correspond if both min and max options are provide" << endl;
154 assert(min_opt.size()==max_opt.size());
157 while(band_opt.size()>min_opt.size())
158 min_opt.push_back(min_opt[0]);
159 while(min_opt.size()>data_opt.size())
160 data_opt.push_back(data_opt[0]);
163 while(band_opt.size()>max_opt.size())
164 max_opt.push_back(max_opt[0]);
165 while(max_opt.size()>data_opt.size())
166 data_opt.push_back(data_opt[0]);
180 vector< vector<float> > lineBuffer(band_opt.size());
181 for(
int iband=0;iband<band_opt.size();++iband)
182 lineBuffer.resize(imgReader.nrOfCol());
185 if(theType==GDT_Unknown){
188 cout <<
"Using data type from input image: " << GDALGetDataTypeName(theType) << endl;
191 if(oformat_opt.size())
192 imageType=oformat_opt[0];
193 if(option_opt.findSubstring(
"INTERLEAVE=")==option_opt.end()){
194 string theInterleave=
"INTERLEAVE=";
195 theInterleave+=imgReader.getInterleave();
196 option_opt.push_back(theInterleave);
198 assert(output_opt.size());
199 imgWriter.
open(output_opt[0],imgReader.nrOfCol(),imgReader.nrOfRow(),1,theType,imageType,option_opt);
200 if(colorTable_opt.size()){
201 if(colorTable_opt[0]!=
"none")
204 else if (imgReader.getColorTable()!=NULL)
209 imgReader.getGeoTransform(gt);
212 if(nodata_opt.size())
215 vector<char> writeBuffer(imgWriter.
nrOfCol());
216 for(
int irow=0;irow<imgReader.nrOfRow();++irow){
217 for(
int iband=0;iband<band_opt.size();++iband)
218 imgReader.readData(lineBuffer[iband],irow,band_opt[iband]);
219 for(
int icol=0;icol<imgReader.nrOfCol();++icol){
220 bool valid=(operator_opt[0]==
"OR")?
false:
true;
221 unsigned short validValue=data_opt[0];
222 if(min_opt.size()&&max_opt.size()){
223 assert(max_opt.size()==min_opt.size());
224 for(
int ivalid=0;ivalid<min_opt.size();++ivalid){
225 bool validBand=
false;
227 unsigned short theBand=(band_opt.size()==min_opt.size())? ivalid:0;
228 if(lineBuffer[theBand][icol]>=min_opt[ivalid]&&lineBuffer[theBand][icol]<=max_opt[ivalid]){
229 validValue=data_opt[ivalid];
232 valid=(operator_opt[0]==
"OR")?valid||validBand : valid&&validBand;
235 else if(min_opt.size()){
236 for(
int ivalid=0;ivalid<min_opt.size();++ivalid){
237 bool validBand=
false;
239 unsigned short theBand=(band_opt.size()==min_opt.size())? ivalid:0;
240 if(lineBuffer[theBand][icol]>=min_opt[ivalid]){
241 validValue=data_opt[ivalid];
244 valid=(operator_opt[0]==
"OR")?valid||validBand : valid&&validBand;
247 else if(max_opt.size()){
248 for(
int ivalid=0;ivalid<max_opt.size();++ivalid){
249 bool validBand=
false;
251 unsigned short theBand=(band_opt.size()==max_opt.size())? ivalid:0;
252 if(lineBuffer[theBand][icol]<=max_opt[ivalid]){
253 validValue=data_opt[ivalid];
256 valid=(operator_opt[0]==
"OR")?valid||validBand : valid&&validBand;
260 writeBuffer[icol]=validValue;
262 writeBuffer[icol]=nodata_opt[0];
265 progress=(1.0+irow)/imgWriter.
nrOfRow();
266 pfnProgress(progress,pszMessage,pProgressArg);
int nrOfCol(void) const
Get the number of columns of this dataset.
void setColorTable(const std::string &filename, int band=0)
Set the color table using an (ASCII) file with 5 columns (value R G B alpha)
int nrOfRow(void) const
Get the number of rows of this dataset.
CPLErr setGeoTransform(double *gt)
Set the geotransform data for this dataset.
bool writeData(T &value, int col, int row, int band=0)
Write a single pixel cell value at a specific column and row for a specific band (all indices start c...
void open(const std::string &filename, const ImgReaderGdal &imgSrc, const std::vector< std::string > &options=std::vector< std::string >())
Open an image for writing, copying image attributes from a source image. Image is directly written to...
GDALDataType getDataType(int band=0) const
Get the GDAL datatype for this dataset.
CPLErr setProjection(const std::string &projection)
Set the projection for this dataset in well known text (wkt) format.
void close(void)
Close the image.
CPLErr GDALSetNoDataValue(double noDataValue, int band=0)
Set the GDAL (internal) no data value for this data set. Only a single no data value per band is supp...