25 #include <sys/types.h>
27 #include "base/Optionpk.h"
28 #include "base/Vector2d.h"
29 #include "algorithms/Filter2d.h"
30 #include "algorithms/Filter.h"
31 #include "imageclasses/ImgReaderGdal.h"
32 #include "imageclasses/ImgWriterGdal.h"
33 #include "algorithms/StatFactory.h"
98 int main(
int argc,
char **argv) {
101 Optionpk<std::string> function_opt(
"f",
"function",
"Statistics function (mean, median, var, stdev, min, max, sum, mode (provide classes), ismin, ismax, proportion (provide classes), percentile, nvalid");
102 Optionpk<double> percentile_opt(
"perc",
"perc",
"Percentile value(s) used for rule percentile");
103 Optionpk<short> class_opt(
"class",
"class",
"class value(s) to use for mode, proportion");
105 Optionpk<std::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",
"");
106 Optionpk<string> oformat_opt(
"of",
"oformat",
"Output image format (see also gdal_translate)",
"GTiff");
107 Optionpk<string> colorTable_opt(
"ct",
"ct",
"color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid). Use none to ommit color table");
108 Optionpk<string> option_opt(
"co",
"co",
"Creation option for output file. Multiple options can be specified.");
110 Optionpk<short> verbose_opt(
"v",
"verbose",
"verbose mode if > 0", 0,2);
112 percentile_opt.setHide(1);
113 class_opt.setHide(1);
114 otype_opt.setHide(1);
115 oformat_opt.setHide(1);
116 colorTable_opt.setHide(1);
120 doProcess=input_opt.retrieveOption(argc,argv);
121 output_opt.retrieveOption(argc,argv);
122 function_opt.retrieveOption(argc,argv);
123 percentile_opt.retrieveOption(argc,argv);
124 nodata_opt.retrieveOption(argc,argv);
125 otype_opt.retrieveOption(argc,argv);
126 oformat_opt.retrieveOption(argc,argv);
127 colorTable_opt.retrieveOption(argc,argv);
128 verbose_opt.retrieveOption(argc,argv);
130 catch(
string predefinedString){
131 std::cout << predefinedString << std::endl;
136 cout <<
"Usage: pkstatprofile -i input -o ouptut [-function]*" << endl;
138 std::cout <<
"short option -h shows basic options only, use long option --help to show all options" << std::endl;
142 if(function_opt.empty()){
143 cerr <<
"Error: no function selected, use option -f" << endl;
148 filter1d.setNoDataValues(nodata_opt);
149 for(
int iclass=0;iclass<class_opt.size();++iclass)
150 filter1d.pushClass(class_opt[iclass]);
152 filter1d.setThresholds(percentile_opt);
156 if(input_opt.empty()){
157 cerr <<
"Error: no input file selected, use option -i" << endl;
160 if(output_opt.empty()){
161 cerr <<
"Error: no output file selected, use option -o" << endl;
165 input.open(input_opt[0]);
167 catch(
string errorstring){
168 cout << errorstring << endl;
172 GDALDataType theType=GDT_Unknown;
174 cout <<
"possible output data types: ";
175 for(
int iType = 0; iType < GDT_TypeCount; ++iType){
177 cout <<
" " << GDALGetDataTypeName((GDALDataType)iType);
178 if( GDALGetDataTypeName((GDALDataType)iType) != NULL
179 && EQUAL(GDALGetDataTypeName((GDALDataType)iType),
180 otype_opt[0].c_str()))
181 theType=(GDALDataType) iType;
183 if(theType==GDT_Unknown)
184 theType=input.getDataType();
187 std::cout << std::endl <<
"Output pixel type: " << GDALGetDataTypeName(theType) << endl;
190 if(oformat_opt.size())
191 imageType=oformat_opt[0];
193 if(option_opt.findSubstring(
"INTERLEAVE=")==option_opt.end()){
194 string theInterleave=
"INTERLEAVE=";
195 theInterleave+=input.getInterleave();
196 option_opt.push_back(theInterleave);
199 cout <<
"Calculating statistic metrics: " << function_opt.size() << endl;
201 output.open(output_opt[0],input.nrOfCol(),input.nrOfRow(),function_opt.size(),theType,imageType,option_opt);
203 catch(
string errorstring){
204 cout << errorstring << endl;
208 output.setProjection(input.getProjection());
210 input.getGeoTransform(gt);
211 output.setGeoTransform(gt);
213 if(colorTable_opt.size()){
214 if(colorTable_opt[0]!=
"none"){
216 cout <<
"set colortable " << colorTable_opt[0] << endl;
217 assert(output.getDataType()==GDT_Byte);
218 output.setColorTable(colorTable_opt[0]);
221 else if(input.getColorTable()!=NULL)
222 output.setColorTable(input.getColorTable());
224 if(nodata_opt.size()){
225 for(
int iband=0;iband<output.nrOfBand();++iband)
226 output.GDALSetNoDataValue(nodata_opt[0],iband);
230 filter1d.stats(input,output,function_opt);
232 catch(
string errorstring){
233 cerr << errorstring << endl;