| 1 | /********************************************************************** |
| 2 | jlvalidate_lib.cc: program to validate classified raster image based on reference vector dataset |
| 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 "Jim.h" |
| 23 | #include "JimList.h" |
| 24 | #include "base/Optionjl.h" |
| 25 | #include "apps/AppFactory.h" |
| 26 | #include "algorithms/ConfusionMatrix.h" |
| 27 | #include "imageclasses/VectorOgr.h" |
| 28 | |
| 29 | using namespace std; |
| 30 | using namespace app; |
| 31 | |
| 32 | /** |
| 33 | * @param reference (type: std::string) Reference vector dataset |
| 34 | * @param ln (type: std::string) Layer name(s) in sample. Leave empty to select all (for vector reference datasets only) |
| 35 | * @param band (type: unsigned int) (default: 0) Input (reference) raster band. Optionally, you can define different bands for input and reference bands respectively: -b 1 -b 0. |
| 36 | * @param confusion (type: bool) (default: 1) Create confusion matrix (to std out) |
| 37 | * @param lref (type: std::string) (default: label) Attribute name of the reference label (for vector reference datasets only) |
| 38 | * @param class (type: std::string) List of class names. |
| 39 | * @param reclass (type: short) List of class values (use same order as in classname option). |
| 40 | * @param nodata (type: double) No data value(s) in input or reference dataset are ignored |
| 41 | * @param mask (type: std::string) Use the first band of the specified file as a validity mask. Nodata values can be set with the option msknodata. |
| 42 | * @param msknodata (type: double) (default: 0) Mask value(s) where image is invalid. Use negative value for valid data (example: use -t -1: if only -1 is valid value) |
| 43 | * @param output (type: std::string) Output dataset (optional) |
| 44 | * @param f (type: std::string) (default: SQLite) OGR format for output vector |
| 45 | * @param lclass (type: std::string) (default: class) Attribute name of the classified label |
| 46 | * @param cmf (type: std::string) (default: ascii) Format for confusion matrix (ascii or latex) |
| 47 | * @param cmo (type: std::string) Output file for confusion matrix |
| 48 | * @param se95 (type: bool) (default: 0) Report standard error for 95 confidence interval |
| 49 | * @param boundary (type: short) (default: 1) Boundary for selecting the sample |
| 50 | * @param homogeneous (type: bool) (default: 0) Only take regions with homogeneous boundary into account (for reference datasets only) |
| 51 | * @param circular (type: bool) (default: 0) Use circular boundary |
| 52 | * @return CE_None if successful, CE_Failure if not |
| 53 | **/ |
| 54 | CPLErr Jim::validate(AppFactory& app){ |
| 55 | JimList singleList; |
| 56 | std::shared_ptr<Jim> imgReader=shared_from_this(); |
| 57 | singleList.pushImage(imgReader); |
| 58 | if(singleList.validate(app).size()) |
| 59 | return(CE_None); |
| 60 | else |
| 61 | return(CE_Failure); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param reference (type: std::string) Reference vector dataset |
| 66 | * @param ln (type: std::string) Layer name(s) in sample. Leave empty to select all (for vector reference datasets only) |
| 67 | * @param band (type: unsigned int) (default: 0) Input (reference) raster band. Optionally, you can define different bands for input and reference bands respectively: -b 1 -b 0. |
| 68 | * @param confusion (type: bool) (default: 1) Create confusion matrix (to std out) |
| 69 | * @param lref (type: std::string) (default: label) Attribute name of the reference label (for vector reference datasets only) |
| 70 | * @param class (type: std::string) List of class names. |
| 71 | * @param reclass (type: short) List of class values (use same order as in classname option). |
| 72 | * @param nodata (type: double) No data value(s) in input or reference dataset are ignored |
| 73 | * @param mask (type: std::string) Use the first band of the specified file as a validity mask. Nodata values can be set with the option msknodata. |
| 74 | * @param msknodata (type: double) (default: 0) Mask value(s) where image is invalid. Use negative value for valid data (example: use -t -1: if only -1 is valid value) |
| 75 | * @param output (type: std::string) Output dataset (optional) |
| 76 | * @param f (type: std::string) (default: SQLite) OGR format for output vector |
| 77 | * @param lclass (type: std::string) (default: class) Attribute name of the classified label |
| 78 | * @param cmf (type: std::string) (default: ascii) Format for confusion matrix (ascii or latex) |
| 79 | * @param cmo (type: std::string) Output file for confusion matrix |
| 80 | * @param se95 (type: bool) (default: 0) Report standard error for 95 confidence interval |
| 81 | * @param boundary (type: short) (default: 1) Boundary for selecting the sample |
| 82 | * @param homogeneous (type: bool) (default: 0) Only take regions with homogeneous boundary into account (for reference datasets only) |
| 83 | * @param circular (type: bool) (default: 0) Use circular boundary |
| 84 | * @return reference to the image collection |
| 85 | **/ |
| 86 | JimList& JimList::validate(app::AppFactory& app){ |
| 87 | Optionjl<string> reference_opt("ref", "reference", "Reference vector dataset"); |
| 88 | Optionjl<string> layer_opt("ln", "ln", "Layer name(s) in sample. Leave empty to select all (for vector reference datasets only)"); |
| 89 | Optionjl<string> mask_opt("m", "mask", "Use the first band of the specified file as a validity mask. Nodata values can be set with the option msknodata."); |
| 90 | Optionjl<double> msknodata_opt("msknodata", "msknodata", "Mask value(s) where image is invalid. Use negative value for valid data (example: use -t -1: if only -1 is valid value)", 0); |
| 91 | Optionjl<double> nodata_opt("nodata", "nodata", "No data value(s) in input or reference dataset are ignored"); |
| 92 | Optionjl<unsigned int> band_opt("b", "band", "Input (reference) raster band. Optionally, you can define different bands for input and reference bands respectively: -b 1 -b 0.", 0); |
| 93 | Optionjl<bool> confusion_opt("cm", "confusion", "Create confusion matrix (to std out)", true); |
| 94 | Optionjl<string> cmformat_opt("cmf","cmf","Format for confusion matrix (ascii or latex)","ascii"); |
| 95 | Optionjl<string> cmoutput_opt("cmo","cmo","Output file for confusion matrix"); |
| 96 | Optionjl<bool> se95_opt("se95","se95","Report standard error for 95 confidence interval",false); |
| 97 | Optionjl<string> labelref_opt("lr", "lref", "Attribute name of the reference label (for vector reference datasets only)", "label"); |
| 98 | Optionjl<string> classname_opt("c", "class", "List of class names."); |
| 99 | Optionjl<short> classvalue_opt("r", "reclass", "List of class values (use same order as in classname option)."); |
| 100 | Optionjl<string> output_opt("o", "output", "Output dataset (optional)"); |
| 101 | Optionjl<string> ogrformat_opt("f", "oformat", "OGR format for output vector","SQLite"); |
| 102 | Optionjl<string> labelclass_opt("lc", "lclass", "Attribute name of the classified label", "class"); |
| 103 | Optionjl<short> boundary_opt("bnd", "boundary", "Boundary for selecting the sample", 1,1); |
| 104 | Optionjl<bool> homogeneous_opt("hom", "homogeneous", "Only take regions with homogeneous boundary into account (for reference datasets only)", false,1); |
| 105 | Optionjl<bool> disc_opt("circ", "circular", "Use circular boundary", false,1); |
| 106 | Optionjl<short> verbose_opt("v", "verbose", "Verbose level", 0,2); |
| 107 | |
| 108 | output_opt.setHide(1); |
| 109 | ogrformat_opt.setHide(1); |
| 110 | labelclass_opt.setHide(1); |
| 111 | boundary_opt.setHide(1); |
| 112 | homogeneous_opt.setHide(1); |
| 113 | disc_opt.setHide(1); |
| 114 | |
| 115 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 116 | try{ |
| 117 | doProcess=reference_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 118 | layer_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 119 | band_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 120 | confusion_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 121 | labelref_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 122 | classname_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 123 | classvalue_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 124 | nodata_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 125 | mask_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 126 | msknodata_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 127 | output_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 128 | ogrformat_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 129 | labelclass_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 130 | cmformat_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 131 | cmoutput_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 132 | se95_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 133 | boundary_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 134 | homogeneous_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 135 | disc_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 136 | verbose_opt.retrieveOption(app.getArgc(),app.getArgv()); |
| 137 | if(!doProcess){ |
| 138 | cout << endl; |
| 139 | std::ostringstream helpStream; |
| 140 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 141 | throw(helpStream.str());//help was invoked, stop processing |
| 142 | } |
| 143 | |
| 144 | Jim inputReader; |
| 145 | Jim maskReader; |
| 146 | VectorOgr referenceReaderOgr; |
| 147 | if(verbose_opt[0]){ |
| 148 | cout << "no data flag(s) set to"; |
| 149 | for(int iflag=0;iflag<nodata_opt.size();++iflag) |
| 150 | cout << " " << nodata_opt[iflag]; |
| 151 | cout << endl; |
| 152 | } |
| 153 | |
| 154 | if(empty()){ |
| 155 | std::ostringstream errorStream; |
| 156 | errorStream << "Input collection is empty. Use --help for more help information" << std::endl; |
| 157 | throw(errorStream.str()); |
| 158 | } |
| 159 | if(reference_opt.empty()){ |
| 160 | std::ostringstream errorStream; |
| 161 | errorStream << "No reference file provided (use option -ref). Use --help for help information" << std::endl; |
| 162 | throw(errorStream.str()); |
| 163 | } |
| 164 | |
| 165 | //band_opt[0] is for input |
| 166 | //band_opt[1] is for reference |
| 167 | if(band_opt.size()<2) |
| 168 | band_opt.push_back(band_opt[0]); |
| 169 | |
| 170 | if(mask_opt.size()) |
| 171 | while(mask_opt.size()<this->size()) |
| 172 | mask_opt.push_back(mask_opt[0]); |
| 173 | vector<short> inputRange; |
| 174 | vector<short> referenceRange; |
| 175 | confusionmatrix::ConfusionMatrix cm; |
| 176 | int nclass=0; |
| 177 | map<string,short> classValueMap; |
| 178 | vector<std::string> nameVector(255);//the inverse of the classValueMap |
| 179 | vector<string> classNames; |
| 180 | |
| 181 | unsigned int ntotalValidation=0; |
| 182 | unsigned int nflagged=0; |
| 183 | Vector2d<unsigned int> resultClass; |
| 184 | vector<float> user; |
| 185 | vector<float> producer; |
| 186 | vector<unsigned int> nvalidation; |
| 187 | |
| 188 | const char* pszMessage; |
| 189 | void* pProgressArg=NULL; |
| 190 | GDALProgressFunc pfnProgress=GDALTermProgress; |
| 191 | float progress=0; |
| 192 | // if(reference_opt[0].find(".shp")!=string::npos){ |
| 193 | unsigned int iinput=0; |
| 194 | std::list<std::shared_ptr<Jim> >::const_iterator pimit=begin(); |
| 195 | for(pimit=begin();pimit!=end();++pimit){ |
| 196 | // for(int iinput=0;iinput<this->size();++iinput){ |
| 197 | // if(verbose_opt[0]) |
| 198 | // cout << "Processing input " << (*pimit)->getFilename() << endl; |
| 199 | if(output_opt.size()) |
| 200 | assert(reference_opt.size()==output_opt.size()); |
| 201 | for(int iref=0;iref<reference_opt.size();++iref){ |
| 202 | cout << "reference " << reference_opt[iref] << endl; |
| 203 | // assert(reference_opt[iref].find(".shp")!=string::npos); |
| 204 | // inputReader.open(input_opt[iinput],memory_opt[0]);//,imagicX_opt[0],imagicY_opt[0]); |
| 205 | if(mask_opt.size()){ |
| 206 | maskReader.open(mask_opt[iinput]); |
| 207 | // maskReader.open(mask_opt[iinput],memory_opt[0]); |
| 208 | // assert(this->at(iinput)->nrOfCol()==maskReader.nrOfCol()); |
| 209 | // assert(this->at(iinput)->nrOfRow()==maskReader.nrOfRow()); |
| 210 | } |
| 211 | referenceReaderOgr.open(reference_opt[iref]); |
| 212 | if(confusion_opt[0]) |
| 213 | referenceRange=inputRange; |
| 214 | |
| 215 | VectorOgr ogrWriter; |
| 216 | if(output_opt.size()){ |
| 217 | try{ |
| 218 | ogrWriter.open(output_opt[iref],ogrformat_opt[0]); |
| 219 | } |
| 220 | catch(string error){ |
| 221 | cerr << error << endl; |
| 222 | throw; |
| 223 | } |
| 224 | } |
| 225 | int nlayer=referenceReaderOgr.getLayerCount(); |
| 226 | for(int ilayer=0;ilayer<nlayer;++ilayer){ |
| 227 | progress=0; |
| 228 | OGRLayer *readLayer=referenceReaderOgr.getLayer(ilayer); |
| 229 | // readLayer = referenceReaderOgr.getDataSource()->GetLayer(ilayer); |
| 230 | string currentLayername=readLayer->GetName(); |
| 231 | if(layer_opt.size()) |
| 232 | if(find(layer_opt.begin(),layer_opt.end(),currentLayername)==layer_opt.end()) |
| 233 | continue; |
| 234 | MyProgressFunc(progress,pszMessage,pProgressArg); |
| 235 | |
| 236 | readLayer->ResetReading(); |
| 237 | OGRLayer *writeLayer; |
| 238 | if(output_opt.size()){ |
| 239 | if(verbose_opt[0]) |
| 240 | cout << "creating output vector file " << output_opt[0] << endl; |
| 241 | // assert(output_opt[0].find(".shp")!=string::npos); |
| 242 | char **papszOptions=NULL; |
| 243 | if(verbose_opt[0]) |
| 244 | cout << "creating layer: " << readLayer->GetName() << endl; |
| 245 | // if(ogrWriter.createLayer(layername, referenceReaderOgr.getProjection(ilayer), referenceReaderOgr.getGeometryType(ilayer), papszOptions)==NULL) |
| 246 | ogrWriter.pushLayer(readLayer->GetName(), referenceReaderOgr.getProjection(ilayer), wkbPoint, papszOptions); |
| 247 | writeLayer=ogrWriter.getLayer(readLayer->GetName()); |
| 248 | assert(writeLayer); |
| 249 | if(verbose_opt[0]){ |
| 250 | cout << "created layer" << endl; |
| 251 | cout << "copy fields from " << reference_opt[iref] << endl; |
| 252 | } |
| 253 | ogrWriter.copyFields(referenceReaderOgr,std::vector<std::string>(),ilayer,ilayer); |
| 254 | //create extra field for classified label |
| 255 | short theDim=boundary_opt[0]; |
| 256 | for(int windowJ=-theDim/2;windowJ<(theDim+1)/2;++windowJ){ |
| 257 | for(int windowI=-theDim/2;windowI<(theDim+1)/2;++windowI){ |
| 258 | if(disc_opt[0]&&(windowI*windowI+windowJ*windowJ>(theDim/2)*(theDim/2))) |
| 259 | continue; |
| 260 | ostringstream fs; |
| 261 | if(theDim>1) |
| 262 | fs << labelclass_opt[0] << "_" << windowJ << "_" << windowI; |
| 263 | else |
| 264 | fs << labelclass_opt[0]; |
| 265 | if(verbose_opt[0]) |
| 266 | cout << "creating field " << fs.str() << endl; |
| 267 | ogrWriter.createField(fs.str(),OFTInteger,ilayer); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | OGRFeature *readFeature; |
| 272 | OGRFeature *writeFeature; |
| 273 | int isample=0; |
| 274 | unsigned int nfeatureInLayer=readLayer->GetFeatureCount(); |
| 275 | unsigned int ifeature=0; |
| 276 | while( (readFeature = readLayer->GetNextFeature()) != NULL ){ |
| 277 | if(verbose_opt[0]) |
| 278 | cout << "sample " << ++isample << endl; |
| 279 | //get x and y from readFeature |
| 280 | double x,y; |
| 281 | OGRGeometry *poGeometry; |
| 282 | OGRPoint centroidPoint; |
| 283 | OGRPoint *poPoint; |
| 284 | poGeometry = readFeature->GetGeometryRef(); |
| 285 | // assert( poGeometry != NULL && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint ); |
| 286 | if(poGeometry==NULL) |
| 287 | continue; |
| 288 | else if(wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon){ |
| 289 | OGRMultiPolygon readPolygon = *((OGRMultiPolygon *) poGeometry); |
| 290 | readPolygon = *((OGRMultiPolygon *) poGeometry); |
| 291 | readPolygon.Centroid(¢roidPoint); |
| 292 | poPoint=¢roidPoint; |
| 293 | } |
| 294 | else if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){ |
| 295 | OGRPolygon readPolygon=*((OGRPolygon *) poGeometry); |
| 296 | readPolygon.Centroid(¢roidPoint); |
| 297 | poPoint=¢roidPoint; |
| 298 | } |
| 299 | else if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint ) |
| 300 | poPoint = (OGRPoint *) poGeometry; |
| 301 | else{ |
| 302 | std::cerr << "Warning: skipping feature (not of type point or polygon)" << std::endl; |
| 303 | continue; |
| 304 | } |
| 305 | x=poPoint->getX(); |
| 306 | y=poPoint->getY(); |
| 307 | double inputValue; |
| 308 | vector<double> inputValues; |
| 309 | bool isHomogeneous=true; |
| 310 | short maskValue; |
| 311 | //read referenceValue from feature |
| 312 | unsigned short referenceValue; |
| 313 | string referenceClassName; |
| 314 | if(classValueMap.size()){ |
| 315 | referenceClassName=readFeature->GetFieldAsString(readFeature->GetFieldIndex(labelref_opt[0].c_str())); |
| 316 | referenceValue=classValueMap[referenceClassName]; |
| 317 | } |
| 318 | else |
| 319 | referenceValue=readFeature->GetFieldAsInteger(readFeature->GetFieldIndex(labelref_opt[0].c_str())); |
| 320 | if(verbose_opt[0]) |
| 321 | cout << "reference value: " << referenceValue << endl; |
| 322 | |
| 323 | bool pixelFlagged=false; |
| 324 | bool maskFlagged=false; |
| 325 | for(int iflag=0;iflag<nodata_opt.size();++iflag){ |
| 326 | if(referenceValue==nodata_opt[iflag]) |
| 327 | pixelFlagged=true; |
| 328 | } |
| 329 | if(pixelFlagged) |
| 330 | continue; |
| 331 | double i_centre,j_centre; |
| 332 | //input reader is georeferenced! |
| 333 | // this->at(iinput)->geo2image(x,y,i_centre,j_centre); |
| 334 | (*pimit)->geo2image(x,y,i_centre,j_centre); |
| 335 | // else{ |
| 336 | // i_centre=x; |
| 337 | // j_centre=y; |
| 338 | // } |
| 339 | //nearest neighbour |
| 340 | j_centre=static_cast<unsigned int>(j_centre); |
| 341 | i_centre=static_cast<unsigned int>(i_centre); |
| 342 | //check if j_centre is out of bounds |
| 343 | // if(static_cast<unsigned int>(j_centre)<0||static_cast<unsigned int>(j_centre)>=this->at(iinput)->nrOfRow()) |
| 344 | if(static_cast<unsigned int>(j_centre)<0||static_cast<unsigned int>(j_centre)>=(*pimit)->nrOfRow()) |
| 345 | continue; |
| 346 | //check if i_centre is out of bounds |
| 347 | // if(static_cast<unsigned int>(i_centre)<0||static_cast<unsigned int>(i_centre)>=this->at(iinput)->nrOfCol()) |
| 348 | if(static_cast<unsigned int>(i_centre)<0||static_cast<unsigned int>(i_centre)>=(*pimit)->nrOfCol()) |
| 349 | continue; |
| 350 | |
| 351 | if(output_opt.size()){ |
| 352 | writeFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn()); |
| 353 | assert(readFeature); |
| 354 | int nfield=readFeature->GetFieldCount(); |
| 355 | writeFeature->SetGeometry(poPoint); |
| 356 | if(verbose_opt[0]) |
| 357 | cout << "copying fields from " << reference_opt[0] << endl; |
| 358 | assert(readFeature); |
| 359 | assert(writeFeature); |
| 360 | vector<int> panMap(nfield); |
| 361 | vector< int>::iterator panit=panMap.begin(); |
| 362 | for(unsigned int ifield=0;ifield<nfield;++ifield) |
| 363 | panMap[ifield]=ifield; |
| 364 | writeFeature->SetFieldsFrom(readFeature,&(panMap[0])); |
| 365 | // if(writeFeature->SetFrom(readFeature)!= OGRERR_NONE) |
| 366 | // cerr << "writing feature failed" << endl; |
| 367 | // if(verbose_opt[0]) |
| 368 | // cout << "feature written" << endl; |
| 369 | } |
| 370 | bool windowAllFlagged=true; |
| 371 | bool windowHasFlag=false; |
| 372 | short theDim=boundary_opt[0]; |
| 373 | for(int windowJ=-theDim/2;windowJ<(theDim+1)/2;++windowJ){ |
| 374 | for(int windowI=-theDim/2;windowI<(theDim+1)/2;++windowI){ |
| 375 | if(disc_opt[0]&&(windowI*windowI+windowJ*windowJ>(theDim/2)*(theDim/2))) |
| 376 | continue; |
| 377 | int j=j_centre+windowJ; |
| 378 | //check if j is out of bounds |
| 379 | // if(static_cast<unsigned int>(j)<0||static_cast<unsigned int>(j)>=this->at(iinput)->nrOfRow()) |
| 380 | if(static_cast<unsigned int>(j)<0||static_cast<unsigned int>(j)>=(*pimit)->nrOfRow()) |
| 381 | continue; |
| 382 | int i=i_centre+windowI; |
| 383 | //check if i is out of bounds |
| 384 | // if(static_cast<unsigned int>(i)<0||static_cast<unsigned int>(i)>=this->at(iinput)->nrOfCol()) |
| 385 | if(static_cast<unsigned int>(i)<0||static_cast<unsigned int>(i)>=(*pimit)->nrOfCol()) |
| 386 | continue; |
| 387 | if(verbose_opt[0]) |
| 388 | cout << setprecision(12) << "reading image value at x,y " << x << "," << y << " (" << i << "," << j << "), "; |
| 389 | // this->at(iinput)->readData(inputValue,i,j,band_opt[0]); |
| 390 | (*pimit)->readData(inputValue,i,j,band_opt[0]); |
| 391 | inputValues.push_back(inputValue); |
| 392 | if(inputValues.back()!=*(inputValues.begin())) |
| 393 | isHomogeneous=false; |
| 394 | if(verbose_opt[0]) |
| 395 | cout << "input value: " << inputValue << endl; |
| 396 | pixelFlagged=false; |
| 397 | for(int iflag=0;iflag<nodata_opt.size();++iflag){ |
| 398 | if(inputValue==nodata_opt[iflag]){ |
| 399 | pixelFlagged=true; |
| 400 | break; |
| 401 | } |
| 402 | } |
| 403 | maskFlagged=false;//(msknodata_opt[ivalue]>=0)?false:true; |
| 404 | if(mask_opt.size()){ |
| 405 | maskReader.readData(maskValue,i,j,0); |
| 406 | for(int ivalue=0;ivalue<msknodata_opt.size();++ivalue){ |
| 407 | if(msknodata_opt[ivalue]>=0){//values set in msknodata_opt are invalid |
| 408 | if(maskValue==msknodata_opt[ivalue]){ |
| 409 | maskFlagged=true; |
| 410 | break; |
| 411 | } |
| 412 | } |
| 413 | else{//only values set in msknodata_opt are valid |
| 414 | if(maskValue!=-msknodata_opt[ivalue]) |
| 415 | maskFlagged=true; |
| 416 | else{ |
| 417 | maskFlagged=false; |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | pixelFlagged=pixelFlagged||maskFlagged; |
| 424 | if(pixelFlagged) |
| 425 | windowHasFlag=true; |
| 426 | else |
| 427 | windowAllFlagged=false;//at least one good pixel in neighborhood |
| 428 | } |
| 429 | } |
| 430 | //at this point we know the values for the entire window |
| 431 | |
| 432 | if(homogeneous_opt[0]){//only centre pixel |
| 433 | //flag if not all pixels are homogeneous or if at least one pixel flagged |
| 434 | |
| 435 | if(!windowHasFlag&&isHomogeneous){ |
| 436 | if(output_opt.size()) |
| 437 | writeFeature->SetField(labelclass_opt[0].c_str(),static_cast<int>(inputValue)); |
| 438 | if(confusion_opt[0]){ |
| 439 | ++ntotalValidation; |
| 440 | if(classValueMap.size()){ |
| 441 | assert(inputValue<nameVector.size()); |
| 442 | string className=nameVector[static_cast<unsigned short>(inputValue)]; |
| 443 | cm.incrementResult(type2string<short>(classValueMap[referenceClassName]),type2string<short>(classValueMap[className]),1); |
| 444 | } |
| 445 | else{ |
| 446 | int rc=distance(referenceRange.begin(),find(referenceRange.begin(),referenceRange.end(),static_cast<unsigned short>(referenceValue))); |
| 447 | int ic=distance(inputRange.begin(),find(inputRange.begin(),inputRange.end(),static_cast<unsigned short>(inputValue))); |
| 448 | assert(rc<nclass); |
| 449 | assert(ic<nclass); |
| 450 | ++nvalidation[rc]; |
| 451 | ++resultClass[rc][ic]; |
| 452 | if(verbose_opt[0]>1) |
| 453 | cout << "increment: " << rc << " " << referenceRange[rc] << " " << ic << " " << inputRange[ic] << endl; |
| 454 | cm.incrementResult(cm.getClass(rc),cm.getClass(ic),1); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | else{ |
| 460 | for(int windowJ=-theDim/2;windowJ<(theDim+1)/2;++windowJ){ |
| 461 | for(int windowI=-theDim/2;windowI<(theDim+1)/2;++windowI){ |
| 462 | if(disc_opt[0]&&(windowI*windowI+windowJ*windowJ>(theDim/2)*(theDim/2))) |
| 463 | continue; |
| 464 | int j=j_centre+windowJ; |
| 465 | //check if j is out of bounds |
| 466 | if(static_cast<unsigned int>(j)<0||static_cast<unsigned int>(j)>=(*pimit)->nrOfRow()) |
| 467 | continue; |
| 468 | int i=i_centre+windowI; |
| 469 | //check if i is out of bounds |
| 470 | if(static_cast<unsigned int>(i)<0||static_cast<unsigned int>(i)>=(*pimit)->nrOfCol()) |
| 471 | continue; |
| 472 | if(!windowAllFlagged){ |
| 473 | ostringstream fs; |
| 474 | if(theDim>1) |
| 475 | fs << labelclass_opt[0] << "_" << windowJ << "_" << windowI; |
| 476 | else |
| 477 | fs << labelclass_opt[0]; |
| 478 | if(output_opt.size()) |
| 479 | writeFeature->SetField(fs.str().c_str(),inputValue); |
| 480 | if(!windowJ&&!windowI){//centre pixel |
| 481 | if(confusion_opt[0]){ |
| 482 | ++ntotalValidation; |
| 483 | if(classValueMap.size()){ |
| 484 | assert(inputValue<nameVector.size()); |
| 485 | string className=nameVector[static_cast<unsigned short>(inputValue)]; |
| 486 | cm.incrementResult(type2string<short>(classValueMap[referenceClassName]),type2string<short>(classValueMap[className]),1); |
| 487 | } |
| 488 | else{ |
| 489 | int rc=distance(referenceRange.begin(),find(referenceRange.begin(),referenceRange.end(),static_cast<unsigned short>(referenceValue))); |
| 490 | int ic=distance(inputRange.begin(),find(inputRange.begin(),inputRange.end(),static_cast<unsigned short>(inputValue))); |
| 491 | if(rc>=nclass) |
| 492 | continue; |
| 493 | if(ic>=nclass) |
| 494 | continue; |
| 495 | // assert(rc<nclass); |
| 496 | // assert(ic<nclass); |
| 497 | ++nvalidation[rc]; |
| 498 | ++resultClass[rc][ic]; |
| 499 | if(verbose_opt[0]>1) |
| 500 | cout << "increment: " << rc << " " << referenceRange[rc] << " " << ic << " " << inputRange[ic] << endl; |
| 501 | cm.incrementResult(cm.getClass(rc),cm.getClass(ic),1); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | if(output_opt.size()){ |
| 510 | if(!windowAllFlagged){ |
| 511 | if(verbose_opt[0]) |
| 512 | cout << "creating feature" << endl; |
| 513 | if(writeLayer->CreateFeature( writeFeature ) != OGRERR_NONE ){ |
| 514 | string errorString="Failed to create feature in OGR vector file"; |
| 515 | throw(errorString); |
| 516 | } |
| 517 | } |
| 518 | OGRFeature::DestroyFeature( writeFeature ); |
| 519 | } |
| 520 | ++ifeature; |
| 521 | progress=static_cast<float>(ifeature+1)/nfeatureInLayer; |
| 522 | MyProgressFunc(progress,pszMessage,pProgressArg); |
| 523 | }//next feature |
| 524 | }//next layer |
| 525 | if(output_opt.size()) |
| 526 | ogrWriter.close(); |
| 527 | referenceReaderOgr.close(); |
| 528 | // (*pimit)->close(); |
| 529 | if(mask_opt.size()) |
| 530 | maskReader.close(); |
| 531 | }//next reference |
| 532 | ++iinput; |
| 533 | }//next input |
| 534 | MyProgressFunc(1.0,pszMessage,pProgressArg); |
| 535 | |
| 536 | if(confusion_opt[0]){ |
| 537 | cm.setFormat(cmformat_opt[0]); |
| 538 | cm.reportSE95(se95_opt[0]); |
| 539 | ofstream outputFile; |
| 540 | if(cmoutput_opt.size()){ |
| 541 | outputFile.open(cmoutput_opt[0].c_str(),ios::out); |
| 542 | outputFile << cm << endl; |
| 543 | } |
| 544 | else |
| 545 | cout << cm << endl; |
| 546 | } |
| 547 | // return(CE_None); |
| 548 | } |
| 549 | catch(string predefinedString){ |
| 550 | std::cout << predefinedString << std::endl; |
| 551 | throw; |
| 552 | } |
| 553 | return(*this); |
| 554 | } |