24 #include "base/Optionpk.h" 25 #include "imageclasses/ImgReaderOgr.h" 26 #include "algorithms/StatFactory.h" 77 int main(
int argc,
char *argv[])
80 Optionpk<string> layer_opt(
"ln",
"lname",
"Layer name(s) in sample (leave empty to select all)");
81 Optionpk<string> fieldname_opt(
"n",
"fname",
"Fields on which to calculate statistics",
"");
83 Optionpk<double> src_min_opt(
"src_min",
"src_min",
"Set minimum value for histogram");
84 Optionpk<double> src_max_opt(
"src_max",
"src_max",
"Set maximum value for histogram");
85 Optionpk<bool> size_opt(
"s",
"size",
"Sample size (number of points)",
false);
86 Optionpk<bool> minmax_opt(
"mm",
"minmax",
"Calculate minimum and maximum value",
false);
89 Optionpk<bool> mean_opt(
"mean",
"mean",
"Calculate mean value",
false);
90 Optionpk<bool> median_opt(
"median",
"median",
"Calculate median value",
false);
91 Optionpk<bool> stdev_opt(
"stdev",
"stdev",
"Calculate standard deviation",
false);
92 Optionpk<bool> histogram_opt(
"hist",
"hist",
"Calculate histogram",
false);
94 Optionpk<bool> relative_opt(
"rel",
"relative",
"Use percentiles for histogram to calculate histogram",
false);
95 Optionpk<bool> kde_opt(
"kde",
"kde",
"Use Kernel density estimation when producing histogram. The standard deviation is estimated based on Silverman's rule of thumb",
false);
100 doProcess=input_opt.retrieveOption(argc,argv);
101 fieldname_opt.retrieveOption(argc,argv);
102 layer_opt.retrieveOption(argc,argv);
103 nodata_opt.retrieveOption(argc,argv);
104 src_min_opt.retrieveOption(argc,argv);
105 src_max_opt.retrieveOption(argc,argv);
106 size_opt.retrieveOption(argc,argv);
107 minmax_opt.retrieveOption(argc,argv);
108 min_opt.retrieveOption(argc,argv);
109 max_opt.retrieveOption(argc,argv);
110 mean_opt.retrieveOption(argc,argv);
111 median_opt.retrieveOption(argc,argv);
112 stdev_opt.retrieveOption(argc,argv);
113 histogram_opt.retrieveOption(argc,argv);
114 nbin_opt.retrieveOption(argc,argv);
115 relative_opt.retrieveOption(argc,argv);
116 kde_opt.retrieveOption(argc,argv);
117 verbose_opt.retrieveOption(argc,argv);
119 catch(
string predefinedString){
120 cout << predefinedString << endl;
125 cout <<
"Usage: pkstatogr -i input [-n attribute]*" << endl;
127 cout <<
"short option -h shows basic options only, use long option --help to show all options" << endl;
133 imgReader.open(input_opt[0]);
135 catch(
string errorstring){
136 cerr << errorstring << endl;
140 vector<double> theData;
144 stat.setNoDataValues(nodata_opt);
147 int nlayerRead=inputReader.getDataSource()->GetLayerCount();
149 cout <<
"number of layers: " << nlayerRead << endl;
151 for(
int ilayer=0;ilayer<nlayerRead;++ilayer){
152 OGRLayer *readLayer=inputReader.getLayer(ilayer);
153 string currentLayername=readLayer->GetName();
155 if(find(layer_opt.begin(),layer_opt.end(),currentLayername)==layer_opt.end())
158 cout <<
"processing layer " << currentLayername << endl;
160 cout <<
" --lname " << currentLayername;
162 for(
int ifield=0;ifield<fieldname_opt.size();++ifield){
164 cout <<
"field: " << ifield << endl;
166 inputReader.readData(theData,OFTReal,fieldname_opt[ifield],ilayer,verbose_opt[0]);
167 vector<double> binData;
170 stat.minmax(theData,theData.begin(),theData.end(),minValue,maxValue);
171 if(src_min_opt.size())
172 minValue=src_min_opt[0];
173 if(src_max_opt.size())
174 maxValue=src_max_opt[0];
175 unsigned int nbin=(nbin_opt.size())? nbin_opt[0]:0;
177 if(histogram_opt[0]){
183 sigma=1.06*sqrt(stat.var(theData))*pow(theData.size(),-0.2);
186 nbin=(maxValue-minValue+1);
188 stat.distribution(theData,theData.begin(),theData.end(),binData,nbin,minValue,maxValue,sigma);
190 catch(
string theError){
191 cerr <<
"Warning: all identical values in data" << endl;
196 cout <<
" --fname " << fieldname_opt[ifield];
200 stat.meanVar(theData,theMean,theVar);
202 cout <<
" --mean " << theMean;
204 cout <<
" --stdev " << sqrt(theVar);
205 if(minmax_opt[0]||min_opt[0]||max_opt[0]){
207 cout <<
" --min " << minValue <<
" --max " << maxValue <<
" ";
210 cout <<
" --min " << minValue <<
" ";
212 cout <<
" --max " << maxValue <<
" ";
216 cout <<
" -median " << stat.median(theData);
218 cout <<
" -size " << theData.size();
220 if(histogram_opt[0]){
221 for(
int ibin=0;ibin<nbin;++ibin){
223 if(nbin==maxValue-minValue+1)
224 binValue=minValue+ibin;
226 binValue=minValue+
static_cast<double>(maxValue-minValue)*(ibin+0.5)/nbin;
227 cout << binValue <<
" ";
228 if(relative_opt[0]||kde_opt[0])
229 cout << 100.0*
static_cast<double>(binData[ibin])/theData.size() << endl;
231 cout << binData[ibin] << endl;
235 catch(
string theError){
237 cout <<
" --mean " << theData.back();
239 cout <<
" --stdev " <<
"0";
241 cout <<
" -min " << theData.back();
243 cout <<
" -max " << theData.back();
245 cout <<
" -median " << theData.back();
247 cout <<
" -size " << theData.size();
249 cerr <<
"Warning: all identical values in data" << endl;