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 omit 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 output [-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)
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=";
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;
213 if(colorTable_opt.size()){
214 if(colorTable_opt[0]!=
"none"){
216 cout <<
"set colortable " << colorTable_opt[0] << endl;
224 if(nodata_opt.size()){
225 for(
int iband=0;iband<output.
nrOfBand();++iband)
230 filter1d.stats(input,output,function_opt);
232 catch(
string errorstring){
233 cerr << errorstring << endl;
void close(void)
Set the memory (in MB) to cache a number of rows in memory.
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.
GDALColorTable * getColorTable(int band=0) const
Get the GDAL color table for this dataset as an instance of the GDALColorTable class.
CPLErr setGeoTransform(double *gt)
Set the geotransform data for this dataset.
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...
std::string getProjection(void) const
Get the projection string (deprecated, use getProjectionRef instead)
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.
std::string getGeoTransform() const
Get the geotransform data for this dataset as a string.
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...
void open(const std::string &filename, const GDALAccess &readMode=GA_ReadOnly)
Open an image.
int nrOfBand(void) const
Get the number of bands of this dataset.
std::string getInterleave() const
Get the band coding (interleave)