| 1 | /********************************************************************** |
| 2 | jlextractimg_lib.cc: extract pixel values from raster image using a raster sample |
| 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 <math.h> |
| 23 | #include <cmath> |
| 24 | #include <stdlib.h> |
| 25 | #include <sstream> |
| 26 | #include <string> |
| 27 | #include <algorithm> |
| 28 | #include <random> |
| 29 | #include <ctime> |
| 30 | #include <vector> |
| 31 | #include "imageclasses/Jim.h" |
| 32 | #include "imageclasses/VectorOgr.h" |
| 33 | #include "base/Optionjl.h" |
| 34 | #include "algorithms/StatFactory.h" |
| 35 | #include "apps/AppFactory.h" |
| 36 | #include "jlextractimg_lib.h" |
| 37 | |
| 38 | using namespace std; |
| 39 | using namespace app; |
| 40 | |
| 41 | #ifndef PI |
| 42 | #define PI 3.1415926535897932384626433832795 |
| 43 | #endif |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * @param app application specific option arguments |
| 48 | * @return output Vector |
| 49 | **/ |
| 50 | shared_ptr<VectorOgr> Jim::extractImg(Jim& classReader, AppFactory& app){ |
| 51 | shared_ptr<VectorOgr> ogrWriter=VectorOgr::createVector(); |
| 52 | extractImg(classReader, *ogrWriter, app); |
| 53 | return(ogrWriter); |
| 54 | } |
| 55 | |
| 56 | void Jim::extractImg(Jim& classReader, VectorOgr& ogrWriter, app::AppFactory& app){ |
| 57 | if(classReader.getDataType()!=GDT_Byte){ |
| 58 | std::ostringstream errorStream; |
| 59 | errorStream << "Error: data type must be GDT_Byte" << std::endl; |
| 60 | throw(errorStream.str()); |
| 61 | } |
| 62 | if(classReader.nrOfCol()!=nrOfCol()){ |
| 63 | std::ostringstream errorStream; |
| 64 | errorStream << "Error: number of columns of image and classReader do not match" << std::endl; |
| 65 | throw(errorStream.str()); |
| 66 | } |
| 67 | if(classReader.nrOfRow()!=nrOfRow()){ |
| 68 | std::ostringstream errorStream; |
| 69 | errorStream << "Error: number of rows of image and classReader do not match" << std::endl; |
| 70 | throw(errorStream.str()); |
| 71 | } |
| 72 | switch(getDataType()){ |
| 73 | case(GDT_Byte): |
| 74 | extractImg_t<unsigned char>(classReader, ogrWriter, app); |
| 75 | break; |
| 76 | case(GDT_Int16): |
| 77 | extractImg_t<short>(classReader, ogrWriter, app); |
| 78 | break; |
| 79 | case(GDT_UInt16): |
| 80 | extractImg_t<unsigned short>(classReader, ogrWriter, app); |
| 81 | break; |
| 82 | case(GDT_Int32): |
| 83 | extractImg_t<int>(classReader, ogrWriter, app); |
| 84 | break; |
| 85 | case(GDT_UInt32): |
| 86 | extractImg_t<unsigned int>(classReader, ogrWriter, app); |
| 87 | break; |
| 88 | case(GDT_Float32): |
| 89 | extractImg_t<float>(classReader, ogrWriter, app); |
| 90 | break; |
| 91 | case(GDT_Float64): |
| 92 | extractImg_t<double>(classReader, ogrWriter, app); |
| 93 | break; |
| 94 | default: |
| 95 | std::string errorString="Error: data type not supported"; |
| 96 | throw(errorString); |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
| 102 | // CPLErr Jim::extractImgOld(Jim& classReader, VectorOgr& ogrWriter, app::AppFactory& app){ |
| 103 | // // Optionjl<string> sample_opt("s", "sample", "Raster dataset with features to be extracted from input data. Output will contain features with input band information included."); |
| 104 | // Optionjl<string> output_opt("o", "output", "Output sample dataset"); |
| 105 | // Optionjl<std::string> layer_opt("ln", "ln", "output layer name","sample"); |
| 106 | // Optionjl<int> class_opt("c", "class", "Class(es) to extract from input sample image. Leave empty to extract all valid data pixels from sample dataset"); |
| 107 | // Optionjl<float> threshold_opt("t", "threshold", "Probability threshold for selecting samples (randomly). Provide probability in percentage (>0) or absolute (<0). If using raster land cover maps as a sample dataset, you can provide a threshold value for each class (e.g. -t 80 -t 60). Use value 100 to select all pixels for selected class(es)", 100); |
| 108 | // Optionjl<string> ogrformat_opt("f", "oformat", "Output sample dataset format","SQLite"); |
| 109 | // Optionjl<std::string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified."); |
| 110 | // Optionjl<string> ftype_opt("ft", "ftype", "Field type (only Real or Integer)", "Real"); |
| 111 | // Optionjl<string> ltype_opt("lt", "ltype", "Label type: In16 or String", "Integer"); |
| 112 | // Optionjl<unsigned int> band_opt("b", "band", "Band index(es) to extract (0 based). Leave empty to use all bands"); |
| 113 | // Optionjl<string> bandNames_opt("bn", "bandname", "Band name(s) corresponding to band index(es)","b"); |
| 114 | // Optionjl<unsigned short> bstart_opt("sband", "startband", "Start band sequence number"); |
| 115 | // Optionjl<unsigned short> bend_opt("eband", "endband", "End band sequence number"); |
| 116 | // Optionjl<double> srcnodata_opt("srcnodata", "srcnodata", "Invalid value(s) for input image"); |
| 117 | // Optionjl<unsigned int> bndnodata_opt("bndnodata", "bndnodata", "Band in input image to check if pixel is valid (used for srcnodata)", 0); |
| 118 | // Optionjl<string> label_opt("cn", "cname", "Name of the class label in the output vector dataset", "label"); |
| 119 | // Optionjl<std::string> fid_opt("fid", "fid", "Create extra field with field identifier (sequence in which the features have been read"); |
| 120 | // Optionjl<short> down_opt("down", "down", "Down sampling factor", 1); |
| 121 | // Optionjl<unsigned long int> memory_opt("mem", "mem", "Buffer size (in MB) to read image data blocks in memory",0,1); |
| 122 | // Optionjl<short> verbose_opt("v", "verbose", "Verbose mode if > 0", 0,2); |
| 123 | |
| 124 | // bstart_opt.setHide(1); |
| 125 | // bend_opt.setHide(1); |
| 126 | // bndnodata_opt.setHide(1); |
| 127 | // srcnodata_opt.setHide(1); |
| 128 | // label_opt.setHide(1); |
| 129 | // fid_opt.retrieveOption(app); |
| 130 | // down_opt.setHide(1); |
| 131 | // option_opt.setHide(1); |
| 132 | // memory_opt.setHide(1); |
| 133 | |
| 134 | // bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 135 | // try{ |
| 136 | // // doProcess=sample_opt.retrieveOption(app); |
| 137 | // doProcess=output_opt.retrieveOption(app); |
| 138 | // layer_opt.retrieveOption(app); |
| 139 | // class_opt.retrieveOption(app); |
| 140 | // threshold_opt.retrieveOption(app); |
| 141 | // ogrformat_opt.retrieveOption(app); |
| 142 | // option_opt.retrieveOption(app); |
| 143 | // ftype_opt.retrieveOption(app); |
| 144 | // ltype_opt.retrieveOption(app); |
| 145 | // band_opt.retrieveOption(app); |
| 146 | // bandNames_opt.retrieveOption(app); |
| 147 | // bstart_opt.retrieveOption(app); |
| 148 | // bend_opt.retrieveOption(app); |
| 149 | // bndnodata_opt.retrieveOption(app); |
| 150 | // srcnodata_opt.retrieveOption(app); |
| 151 | // label_opt.retrieveOption(app); |
| 152 | // fid_opt.retrieveOption(app); |
| 153 | // down_opt.retrieveOption(app); |
| 154 | // memory_opt.retrieveOption(app); |
| 155 | // verbose_opt.retrieveOption(app); |
| 156 | // if(!doProcess){ |
| 157 | // cout << endl; |
| 158 | // std::ostringstream helpStream; |
| 159 | // helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 160 | // throw(helpStream.str());//help was invoked, stop processing |
| 161 | // } |
| 162 | |
| 163 | // std::vector<std::string> badKeys; |
| 164 | // app.badKeys(badKeys); |
| 165 | // if(badKeys.size()){ |
| 166 | // std::ostringstream errorStream; |
| 167 | // if(badKeys.size()>1) |
| 168 | // errorStream << "Error: unknown keys: "; |
| 169 | // else |
| 170 | // errorStream << "Error: unknown key: "; |
| 171 | // for(int ikey=0;ikey<badKeys.size();++ikey){ |
| 172 | // errorStream << badKeys[ikey] << " "; |
| 173 | // } |
| 174 | // errorStream << std::endl; |
| 175 | // throw(errorStream.str()); |
| 176 | // } |
| 177 | // statfactory::StatFactory stat; |
| 178 | // if(srcnodata_opt.size()){ |
| 179 | // while(srcnodata_opt.size()<bndnodata_opt.size()) |
| 180 | // srcnodata_opt.push_back(srcnodata_opt[0]); |
| 181 | // stat.setNoDataValues(srcnodata_opt); |
| 182 | // } |
| 183 | // Vector2d<unsigned int> posdata; |
| 184 | // unsigned long int nsample=0; |
| 185 | // unsigned long int ntotalvalid=0; |
| 186 | // unsigned long int ntotalinvalid=0; |
| 187 | |
| 188 | // map<int,unsigned long int> nvalid; |
| 189 | // map<int,unsigned long int> ninvalid; |
| 190 | // // vector<unsigned long int> nvalid(class_opt.size()); |
| 191 | // // vector<unsigned long int> ninvalid(class_opt.size()); |
| 192 | // // if(class_opt.empty()){ |
| 193 | // // nvalid.resize(256); |
| 194 | // // ninvalid.resize(256); |
| 195 | // // } |
| 196 | // // for(int it=0;it<nvalid.size();++it){ |
| 197 | // // nvalid[it]=0; |
| 198 | // // ninvalid[it]=0; |
| 199 | // // } |
| 200 | |
| 201 | // map <int,short> classmap;//class->index |
| 202 | // for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 203 | // nvalid[class_opt[iclass]]=0; |
| 204 | // ninvalid[class_opt[iclass]]=0; |
| 205 | // classmap[class_opt[iclass]]=iclass; |
| 206 | // } |
| 207 | |
| 208 | // // Jim imgReader; |
| 209 | // // if(image_opt.empty()){ |
| 210 | // // std::cerr << "No image dataset provided (use option -i). Use --help for help information"; |
| 211 | // // exit(0); |
| 212 | // // } |
| 213 | // if(output_opt.empty()){ |
| 214 | // string errorstring="Error: No output dataset provided (use option -o). Use --help for help information"; |
| 215 | // throw(errorstring); |
| 216 | // } |
| 217 | // // try{ |
| 218 | // // imgReader.open(image_opt[0],memory_opt[0]); |
| 219 | // // } |
| 220 | // // catch(std::string errorstring){ |
| 221 | // // std::cout << errorstring << std::endl; |
| 222 | // // exit(0); |
| 223 | // // } |
| 224 | |
| 225 | // //convert start and end band options to vector of band indexes |
| 226 | // if(bstart_opt.size()){ |
| 227 | // if(bend_opt.size()!=bstart_opt.size()){ |
| 228 | // string errorstring="Error: options for start and end band indexes must be provided as pairs, missing end band"; |
| 229 | // throw(errorstring); |
| 230 | // } |
| 231 | // band_opt.clear(); |
| 232 | // for(int ipair=0;ipair<bstart_opt.size();++ipair){ |
| 233 | // if(bend_opt[ipair]<=bstart_opt[ipair]){ |
| 234 | // string errorstring="Error: index for end band must be smaller then start band"; |
| 235 | // throw(errorstring); |
| 236 | // } |
| 237 | // for(int iband=bstart_opt[ipair];iband<=bend_opt[ipair];++iband) |
| 238 | // band_opt.push_back(iband); |
| 239 | // } |
| 240 | // } |
| 241 | // int nband=(band_opt.size()) ? band_opt.size() : this->nrOfBand(); |
| 242 | |
| 243 | // if(bandNames_opt.size()<nband){ |
| 244 | // std::string bandString=bandNames_opt[0]; |
| 245 | // bandNames_opt.clear(); |
| 246 | // bandNames_opt.resize(nband); |
| 247 | // for(int iband=0;iband<nband;++iband){ |
| 248 | // int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 249 | // ostringstream fs; |
| 250 | // fs << bandString << theBand; |
| 251 | // bandNames_opt[iband]=fs.str(); |
| 252 | // } |
| 253 | // } |
| 254 | |
| 255 | // if(verbose_opt[0]) |
| 256 | // std::cout << bandNames_opt << std::endl; |
| 257 | // if(verbose_opt[0]>1) |
| 258 | // std::cout << "Number of bands in input image: " << this->nrOfBand() << std::endl; |
| 259 | |
| 260 | // OGRFieldType fieldType; |
| 261 | // OGRFieldType labelType; |
| 262 | // int ogr_typecount=11;//hard coded for now! |
| 263 | // if(verbose_opt[0]>1) |
| 264 | // std::cout << "field and label types can be: "; |
| 265 | // for(int iType = 0; iType < ogr_typecount; ++iType){ |
| 266 | // if(verbose_opt[0]>1) |
| 267 | // std::cout << " " << OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType); |
| 268 | // if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL |
| 269 | // && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType), |
| 270 | // ftype_opt[0].c_str())) |
| 271 | // fieldType=(OGRFieldType) iType; |
| 272 | // if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL |
| 273 | // && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType), |
| 274 | // ltype_opt[0].c_str())) |
| 275 | // labelType=(OGRFieldType) iType; |
| 276 | // } |
| 277 | // switch( fieldType ){ |
| 278 | // case OFTInteger: |
| 279 | // case OFTReal: |
| 280 | // case OFTRealList: |
| 281 | // case OFTString: |
| 282 | // if(verbose_opt[0]>1) |
| 283 | // std::cout << std::endl << "field type is: " << OGRFieldDefn::GetFieldTypeName(fieldType) << std::endl; |
| 284 | // break; |
| 285 | // default: |
| 286 | // cerr << "field type " << OGRFieldDefn::GetFieldTypeName(fieldType) << " not supported" << std::endl; |
| 287 | // exit(0); |
| 288 | // break; |
| 289 | // } |
| 290 | // switch( labelType ){ |
| 291 | // case OFTInteger: |
| 292 | // case OFTReal: |
| 293 | // case OFTRealList: |
| 294 | // case OFTString: |
| 295 | // if(verbose_opt[0]>1) |
| 296 | // std::cout << std::endl << "label type is: " << OGRFieldDefn::GetFieldTypeName(labelType) << std::endl; |
| 297 | // break; |
| 298 | // default: |
| 299 | // cerr << "label type " << OGRFieldDefn::GetFieldTypeName(labelType) << " not supported" << std::endl; |
| 300 | // exit(0); |
| 301 | // break; |
| 302 | // } |
| 303 | |
| 304 | // const char* pszMessage; |
| 305 | // void* pProgressArg=NULL; |
| 306 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 307 | // double progress=0; |
| 308 | // srand(time(NULL)); |
| 309 | |
| 310 | // bool sampleIsRaster=true; |
| 311 | |
| 312 | // // Jim classReader; |
| 313 | // // ImgWriterOgr sampleWriterOgr; |
| 314 | // // VectorOgr sampleWriterOgr; |
| 315 | |
| 316 | // // if(sample_opt.size()){ |
| 317 | // // try{ |
| 318 | // // classReader.open(sample_opt[0],memory_opt[0]); |
| 319 | // // } |
| 320 | // // catch(string errorString){ |
| 321 | // // //todo: sampleIsRaster will not work from GDAL 2.0!!?? (unification of driver for raster and vector datasets) |
| 322 | // // sampleIsRaster=false; |
| 323 | // // } |
| 324 | // // } |
| 325 | // // else{ |
| 326 | // // std::cerr << "No raster sample dataset provided (use option -s filename). Use --help for help information"; |
| 327 | // // exit(1); |
| 328 | // // } |
| 329 | |
| 330 | // char **papszOptions=NULL; |
| 331 | // for(std::vector<std::string>::const_iterator optionIt=option_opt.begin();optionIt!=option_opt.end();++optionIt) |
| 332 | // papszOptions=CSLAddString(papszOptions,optionIt->c_str()); |
| 333 | |
| 334 | // // OGRSpatialReference classSRS(classReader.getProjectionRef().c_str()); |
| 335 | // OGRSpatialReference classSpatialRef; |
| 336 | // OGRSpatialReference thisSpatialRef; |
| 337 | // // OGRSpatialReference thisSRS; |
| 338 | |
| 339 | // classSpatialRef=(classReader.getSpatialRef()); |
| 340 | // thisSpatialRef=getSpatialRef(); |
| 341 | // OGRCoordinateTransformation *ref2img= OGRCreateCoordinateTransformation(&classSpatialRef, &thisSpatialRef); |
| 342 | // if(thisSpatialRef.IsSame(&classSpatialRef)){ |
| 343 | // // img2ref=0; |
| 344 | // ref2img=0; |
| 345 | // } |
| 346 | // else if(!ref2img){ |
| 347 | // std::ostringstream errorStream; |
| 348 | // errorStream << "Error: cannot create OGRCoordinateTransformation" << std::endl; |
| 349 | // throw(errorStream.str()); |
| 350 | // } |
| 351 | // // std::vector<double> xvector; |
| 352 | // // std::vector<double> yvector; |
| 353 | // // std::vector<double> ivector; |
| 354 | // // std::vector<double> jvector; |
| 355 | // // if(thisSRS.IsSame(classSpatialRef)) |
| 356 | // // classSpatialRef=0;//no need to transform |
| 357 | // // else{ |
| 358 | // // xvector.resize(classReader.nrOfCol()/down_opt[0]*classReader.nrOfRow()/down_opt[0]); |
| 359 | // // yvector.resize(classReader.nrOfCol()/down_opt[0]*classReader.nrOfRow()/down_opt[0]); |
| 360 | // // ivector.resize(classReader.nrOfCol()/down_opt[0]*classReader.nrOfRow()/down_opt[0]); |
| 361 | // // jvector.resize(classReader.nrOfCol()/down_opt[0]*classReader.nrOfRow()/down_opt[0]); |
| 362 | // // for(int irow=0;irow<classReader.nrOfRow();++irow){ |
| 363 | // // // if(irow%down_opt[0]) |
| 364 | // // // continue; |
| 365 | // // for(int icol=0;icol<classReader.nrOfCol();++icol){ |
| 366 | // // // if(icol%down_opt[0]) |
| 367 | // // // continue; |
| 368 | // // classReader.image2geo(icol,irow,xvector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]],yvector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]]); |
| 369 | // // //test |
| 370 | // // if(irow==100&&icol==100||irow==200&&icol==200) |
| 371 | // // std::cout << "icol,irow,xclass,yclass:" << icol << "," << irow << "," << xvector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]] << "," << yvector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]] << std::endl; |
| 372 | // // } |
| 373 | // // } |
| 374 | // // //transform |
| 375 | // // OGRCoordinateTransformation *poCT = OGRCreateCoordinateTransformation(classSpatialRef,thisSpatialRef); |
| 376 | // // if( !poCT ){ |
| 377 | // // std::ostringstream errorStream; |
| 378 | // // errorStream << "Error: cannot create OGRCoordinateTransformation" << std::endl; |
| 379 | // // throw(errorStream.str()); |
| 380 | // // } |
| 381 | // // if(!poCT->Transform(xvector.size(),&(xvector[0]),&(yvector[0]))){ |
| 382 | // // std::ostringstream errorStream; |
| 383 | // // errorStream << "Error: cannot apply OGRCoordinateTransformation" << std::endl; |
| 384 | // // throw(errorStream.str()); |
| 385 | // // } |
| 386 | // // for(int irow=0;irow<classReader.nrOfRow();++irow){ |
| 387 | // // // if(irow%down_opt[0]) |
| 388 | // // // continue; |
| 389 | // // for(int icol=0;icol<classReader.nrOfCol();++icol){ |
| 390 | // // // if(icol%down_opt[0]) |
| 391 | // // // continue; |
| 392 | // // geo2image(xvector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]],yvector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]],ivector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]],jvector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]]); |
| 393 | // // //test |
| 394 | // // // if(!irow%100&&!icol%100) |
| 395 | // // if(irow==100&&icol==100||irow==200&&icol==200) |
| 396 | // // std::cout << "ximg,yimg,iimg,jimg:" << xvector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]] << "," << yvector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]] << "," << ivector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]] << "," << jvector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]] << std::endl; |
| 397 | // // } |
| 398 | // // } |
| 399 | // // |
| 400 | // if(sampleIsRaster){ |
| 401 | // if(class_opt.empty()){ |
| 402 | // // ImgWriterOgr ogrWriter; |
| 403 | // // VectorOgr ogrWriter; |
| 404 | // // if(sample_opt.empty()){ |
| 405 | // // string errorString="Error: sample raster dataset is missing"; |
| 406 | // // throw(errorString); |
| 407 | // // } |
| 408 | // // classReader.open(sample_opt[0],memory_opt[0]); |
| 409 | // // vector<int> classBuffer(classReader.nrOfCol()); |
| 410 | // stl::vector<double> classBuffer(classReader.nrOfCol()); |
| 411 | // Vector2d<double> imgBuffer(nband,this->nrOfCol());//[band][col] |
| 412 | // // vector<double> imgBuffer(nband);//[band] |
| 413 | // stl::vector<double> sample(2+nband);//x,y,band values |
| 414 | // Vector2d<double> writeBuffer; |
| 415 | // vector<int> writeBufferClass; |
| 416 | // vector<int> selectedClass; |
| 417 | // Vector2d<double> selectedBuffer; |
| 418 | // int irow=0; |
| 419 | // int icol=0; |
| 420 | // if(verbose_opt[0]>1) |
| 421 | // std::cout << "extracting sample from image..." << std::endl; |
| 422 | // progress=0; |
| 423 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 424 | // for(irow=0;irow<classReader.nrOfRow();++irow){ |
| 425 | // if(irow%down_opt[0]) |
| 426 | // continue; |
| 427 | // classReader.readData(classBuffer,irow); |
| 428 | // double x=0;//geo x coordinate |
| 429 | // double y=0;//geo y coordinate |
| 430 | // double iimg=0;//image x-coordinate in img image |
| 431 | // double jimg=0;//image y-coordinate in img image |
| 432 | |
| 433 | // //find col in img |
| 434 | // classReader.image2geo(icol,irow,x,y); |
| 435 | // this->geo2image(x,y,iimg,jimg,ref2img); |
| 436 | // // iimg=ivector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]]; |
| 437 | // // jimg=jvector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]]; |
| 438 | // //nearest neighbour |
| 439 | // if(static_cast<int>(jimg)<0||static_cast<int>(jimg)>=this->nrOfRow()) |
| 440 | // continue; |
| 441 | // for(int iband=0;iband<nband;++iband){ |
| 442 | // int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 443 | // this->readData(imgBuffer[iband],static_cast<int>(jimg),theBand); |
| 444 | // } |
| 445 | // for(icol=0;icol<classReader.nrOfCol();++icol){ |
| 446 | // if(icol%down_opt[0]) |
| 447 | // continue; |
| 448 | // int theClass=classBuffer[icol]; |
| 449 | // int processClass=0; |
| 450 | // bool valid=false; |
| 451 | // if(class_opt.empty()){ |
| 452 | // valid=true;//process every class |
| 453 | // processClass=theClass; |
| 454 | // } |
| 455 | // else{ |
| 456 | // for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 457 | // if(classBuffer[icol]==class_opt[iclass]){ |
| 458 | // processClass=iclass; |
| 459 | // theClass=class_opt[iclass]; |
| 460 | // valid=true;//process this class |
| 461 | // break; |
| 462 | // } |
| 463 | // } |
| 464 | // } |
| 465 | // classReader.image2geo(icol,irow,x,y); |
| 466 | // sample[0]=x; |
| 467 | // sample[1]=y; |
| 468 | // if(verbose_opt[0]>2){ |
| 469 | // std::cout.precision(12); |
| 470 | // std::cout << theClass << " " << x << " " << y << std::endl; |
| 471 | // } |
| 472 | // // find col in img |
| 473 | // this->geo2image(x,y,iimg,jimg,ref2img); |
| 474 | // // this->geo2image(ivector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]],jvector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]],iimg,jimg); |
| 475 | // // iimg=ivector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]]; |
| 476 | // //nearest neighbour |
| 477 | // iimg=static_cast<int>(iimg); |
| 478 | // if(static_cast<int>(iimg)<0||static_cast<int>(iimg)>=this->nrOfCol()) |
| 479 | // continue; |
| 480 | |
| 481 | // for(int iband=0;iband<nband&&valid;++iband){ |
| 482 | // int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 483 | // if(srcnodata_opt.size()&&theBand==bndnodata_opt[0]){ |
| 484 | // // vector<int>::const_iterator bndit=bndnodata_opt.begin(); |
| 485 | // for(int inodata=0;inodata<srcnodata_opt.size()&&valid;++inodata){ |
| 486 | // if(imgBuffer[iband][iimg]==srcnodata_opt[inodata]) |
| 487 | // valid=false; |
| 488 | // } |
| 489 | // } |
| 490 | // } |
| 491 | // // oldimgrow=jimg; |
| 492 | |
| 493 | // if(valid){ |
| 494 | // for(int iband=0;iband<imgBuffer.size();++iband){ |
| 495 | // sample[iband+2]=imgBuffer[iband][iimg]; |
| 496 | // } |
| 497 | // float theThreshold=(threshold_opt.size()>1)?threshold_opt[processClass]:threshold_opt[0]; |
| 498 | // if(theThreshold>0){//percentual value |
| 499 | // double p=static_cast<double>(rand())/(RAND_MAX); |
| 500 | // p*=100.0; |
| 501 | // if(p>theThreshold) |
| 502 | // continue;//do not select for now, go to next column |
| 503 | // } |
| 504 | // // else if(nvalid.size()>processClass){//absolute value |
| 505 | // // if(nvalid[processClass]>=-theThreshold) |
| 506 | // // continue;//do not select any more pixels for this class, go to next column to search for other classes |
| 507 | // // } |
| 508 | // writeBuffer.push_back(sample); |
| 509 | // writeBufferClass.push_back(theClass); |
| 510 | // ++ntotalvalid; |
| 511 | // if(nvalid.count(theClass)) |
| 512 | // nvalid[theClass]+=1; |
| 513 | // else |
| 514 | // nvalid[theClass]=1; |
| 515 | // } |
| 516 | // else{ |
| 517 | // ++ntotalinvalid; |
| 518 | // if(ninvalid.count(theClass)) |
| 519 | // ninvalid[theClass]+=1; |
| 520 | // else |
| 521 | // ninvalid[theClass]=1; |
| 522 | // } |
| 523 | // } |
| 524 | // progress=static_cast<float>(irow+1.0)/classReader.nrOfRow(); |
| 525 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 526 | // }//irow |
| 527 | // progress=100; |
| 528 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 529 | // if(writeBuffer.size()>0){ |
| 530 | // assert(ntotalvalid==writeBuffer.size()); |
| 531 | // if(verbose_opt[0]>0){ |
| 532 | // map<int,unsigned long int>::const_iterator mapit=nvalid.begin(); |
| 533 | // for(mapit=nvalid.begin();mapit!=nvalid.end();++mapit) |
| 534 | // std::cout << "nvalid for class " << mapit->first << ": " << mapit->second << std::endl; |
| 535 | // std::cout << "creating image sample writer " << output_opt[0] << " with " << writeBuffer.size() << " samples (" << ntotalinvalid << " invalid)" << std::endl; |
| 536 | // } |
| 537 | // // if(ogrWriter.open(output_opt[0],layer_opt,ogrformat_opt[0], wkbPoint, this->getProjectionRef(),papszOptions)!=OGRERR_NONE){ |
| 538 | // if(ogrWriter.open(output_opt[0],ogrformat_opt[0])!=OGRERR_NONE){ |
| 539 | // ostringstream fs; |
| 540 | // fs << "open ogrWriter failed "; |
| 541 | // fs << "output name: " << output_opt[0] << ", "; |
| 542 | // fs << "format: "<< ogrformat_opt[0] << std::endl; |
| 543 | // throw(fs.str()); |
| 544 | // } |
| 545 | // if(ogrWriter.pushLayer(layer_opt[0], this->getProjection(),wkbPoint,papszOptions)!=OGRERR_NONE){ |
| 546 | // ostringstream fs; |
| 547 | // fs << "push layer to ogrWriter with points failed "; |
| 548 | // fs << "layer name: "<< layer_opt[0] << std::endl; |
| 549 | // throw(fs.str()); |
| 550 | // } |
| 551 | // // ogrWriter.open(sample_opt[0],layer_opt[0]); |
| 552 | // // ostringstream slayer; |
| 553 | // // slayer << "training data"; |
| 554 | // // std::string layername=slayer.str(); |
| 555 | // // ogrWriter.createLayer(layername, this->getProjection(), wkbPoint, papszOptions); |
| 556 | // // std::string fieldname="fid";//number of the point |
| 557 | // // ogrWriter.createField(fieldname,OFTInteger); |
| 558 | // map<std::string,double> pointAttributes; |
| 559 | // ogrWriter.createField(label_opt[0],labelType); |
| 560 | // if(fid_opt.size()) |
| 561 | // ogrWriter.createField(fid_opt[0],OFTInteger64); |
| 562 | // for(int iband=0;iband<nband;++iband){ |
| 563 | // ogrWriter.createField(bandNames_opt[iband],fieldType); |
| 564 | // } |
| 565 | // progress=0; |
| 566 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 567 | |
| 568 | // map<int,short> classDone; |
| 569 | // Vector2d<double> writeBufferTmp; |
| 570 | // vector<int> writeBufferClassTmp; |
| 571 | |
| 572 | // if(threshold_opt[0]<0){//absolute threshold |
| 573 | // map<int,unsigned long int>::iterator mapit; |
| 574 | // map<int,unsigned long int> ncopied; |
| 575 | // for(mapit=nvalid.begin();mapit!=nvalid.end();++mapit) |
| 576 | // ncopied[mapit->first]=0; |
| 577 | // while(classDone.size()<nvalid.size()){ |
| 578 | // unsigned int index=rand()%writeBufferClass.size(); |
| 579 | // int theClass=writeBufferClass[index]; |
| 580 | // float theThreshold=threshold_opt[0]; |
| 581 | // if(threshold_opt.size()>1&&class_opt.size()) |
| 582 | // theThreshold=threshold_opt[classmap[theClass]]; |
| 583 | // theThreshold=-theThreshold; |
| 584 | // if(ncopied[theClass]<theThreshold){ |
| 585 | // writeBufferClassTmp.push_back(*(writeBufferClass.begin()+index)); |
| 586 | // writeBufferTmp.push_back(*(writeBuffer.begin()+index)); |
| 587 | // writeBufferClass.erase(writeBufferClass.begin()+index); |
| 588 | // writeBuffer.erase(writeBuffer.begin()+index); |
| 589 | // ++(ncopied[theClass]); |
| 590 | // } |
| 591 | // else |
| 592 | // classDone[theClass]=1; |
| 593 | // if(ncopied[theClass]>=nvalid[theClass]){ |
| 594 | // classDone[theClass]=1; |
| 595 | // } |
| 596 | // } |
| 597 | // writeBuffer=writeBufferTmp; |
| 598 | // writeBufferClass=writeBufferClassTmp; |
| 599 | |
| 600 | // // while(classDone.size()<nvalid.size()){ |
| 601 | // // unsigned int index=rand()%writeBufferClass.size(); |
| 602 | // // int theClass=writeBufferClass[index]; |
| 603 | // // float theThreshold=threshold_opt[0]; |
| 604 | // // if(threshold_opt.size()>1&&class_opt.size()) |
| 605 | // // theThreshold=threshold_opt[classmap[theClass]]; |
| 606 | // // theThreshold=-theThreshold; |
| 607 | // // if(nvalid[theClass]>theThreshold){ |
| 608 | // // writeBufferClass.erase(writeBufferClass.begin()+index); |
| 609 | // // writeBuffer.erase(writeBuffer.begin()+index); |
| 610 | // // --(nvalid[theClass]); |
| 611 | // // } |
| 612 | // // else |
| 613 | // // classDone[theClass]=1; |
| 614 | // // } |
| 615 | // } |
| 616 | // for(unsigned int isample=0;isample<writeBuffer.size();++isample){ |
| 617 | // if(verbose_opt[0]>1) |
| 618 | // std::cout << "writing sample " << isample << std::endl; |
| 619 | // pointAttributes[label_opt[0]]=writeBufferClass[isample]; |
| 620 | // if(fid_opt.size()) |
| 621 | // pointAttributes[fid_opt[0]]=isample; |
| 622 | // for(int iband=0;iband<writeBuffer[0].size()-2;++iband){ |
| 623 | // pointAttributes[bandNames_opt[iband]]=writeBuffer[isample][iband+2]; |
| 624 | // } |
| 625 | // if(verbose_opt[0]>1) |
| 626 | // std::cout << "all bands written" << std::endl; |
| 627 | // // ogrWriter.addPoint(writeBuffer[isample][0],writeBuffer[isample][1],pointAttributes,fieldname,isample); |
| 628 | // ogrWriter.addPoint(writeBuffer[isample][0],writeBuffer[isample][1],pointAttributes); |
| 629 | // progress=static_cast<float>(isample+1.0)/writeBuffer.size(); |
| 630 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 631 | // } |
| 632 | // } |
| 633 | // else{ |
| 634 | // std::cout << "No data found for any class " << std::endl; |
| 635 | // } |
| 636 | // // classReader.close(); |
| 637 | // nsample=writeBuffer.size(); |
| 638 | // if(verbose_opt[0]) |
| 639 | // std::cout << "total number of samples written: " << nsample << std::endl; |
| 640 | // } |
| 641 | // else{//class_opt.size()!=0 |
| 642 | // assert(class_opt[0]); |
| 643 | // // if(class_opt[0]){ |
| 644 | // assert(threshold_opt.size()==1||threshold_opt.size()==class_opt.size()); |
| 645 | // // Jim classReader; |
| 646 | // // ImgWriterOgr ogrWriter; |
| 647 | // // VectorOgr ogrWriter; |
| 648 | // if(verbose_opt[0]>1){ |
| 649 | // std::cout << "reading position from sample dataset " << std::endl; |
| 650 | // std::cout << "class thresholds: " << std::endl; |
| 651 | // for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 652 | // if(threshold_opt.size()>1) |
| 653 | // std::cout << class_opt[iclass] << ": " << threshold_opt[iclass] << std::endl; |
| 654 | // else |
| 655 | // std::cout << class_opt[iclass] << ": " << threshold_opt[0] << std::endl; |
| 656 | // } |
| 657 | // } |
| 658 | // if(verbose_opt[0]>1) |
| 659 | // std::cout << "opening sample" << std::endl; |
| 660 | // // classReader.open(sample_opt[0],memory_opt[0]); |
| 661 | // vector<int> classBuffer(classReader.nrOfCol()); |
| 662 | // // vector<double> classBuffer(classReader.nrOfCol()); |
| 663 | // Vector2d<double> imgBuffer(nband,this->nrOfCol());//[band][col] |
| 664 | // // vector<double> imgBuffer(nband);//[band] |
| 665 | // vector<double> sample(2+nband);//x,y,band values |
| 666 | // Vector2d<double> writeBuffer; |
| 667 | // vector<int> writeBufferClass; |
| 668 | // vector<int> selectedClass; |
| 669 | // Vector2d<double> selectedBuffer; |
| 670 | // int irow=0; |
| 671 | // int icol=0; |
| 672 | // if(verbose_opt[0]>1) |
| 673 | // std::cout << "extracting sample from image..." << std::endl; |
| 674 | |
| 675 | // progress=0; |
| 676 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 677 | // for(irow=0;irow<classReader.nrOfRow();++irow){ |
| 678 | // if(irow%down_opt[0]) |
| 679 | // continue; |
| 680 | // classReader.readData(classBuffer,irow); |
| 681 | // double x=0;//geo x coordinate |
| 682 | // double y=0;//geo y coordinate |
| 683 | // double iimg=0;//image x-coordinate in img image |
| 684 | // double jimg=0;//image y-coordinate in img image |
| 685 | |
| 686 | // //find row in img |
| 687 | // classReader.image2geo(icol,irow,x,y); |
| 688 | // this->geo2image(x,y,iimg,jimg,ref2img); |
| 689 | // // ostringstream fs; |
| 690 | // // fs << "Error: geo2image failed to find row in image"; |
| 691 | // // throw(fs.str()); |
| 692 | // // } |
| 693 | // // this->geo2image(ivector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]],jvector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]],iimg,jimg); |
| 694 | // // iimg=ivector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]]; |
| 695 | // // jimg=jvector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]]; |
| 696 | // //nearest neighbour |
| 697 | // if(static_cast<int>(jimg)<0||static_cast<int>(jimg)>=this->nrOfRow()) |
| 698 | // continue; |
| 699 | // for(int iband=0;iband<nband;++iband){ |
| 700 | // int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 701 | // this->readData(imgBuffer[iband],static_cast<int>(jimg),theBand); |
| 702 | // } |
| 703 | |
| 704 | // for(icol=0;icol<classReader.nrOfCol();++icol){ |
| 705 | // if(icol%down_opt[0]) |
| 706 | // continue; |
| 707 | // int theClass=0; |
| 708 | // // double theClass=0; |
| 709 | // int processClass=-1; |
| 710 | // if(class_opt.empty()){//process every class |
| 711 | // if(classBuffer[icol]){ |
| 712 | // processClass=0; |
| 713 | // theClass=classBuffer[icol]; |
| 714 | // } |
| 715 | // } |
| 716 | // else{ |
| 717 | // for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 718 | // if(classBuffer[icol]==class_opt[iclass]){ |
| 719 | // processClass=iclass; |
| 720 | // theClass=class_opt[iclass]; |
| 721 | // } |
| 722 | // } |
| 723 | // } |
| 724 | // if(processClass>=0){ |
| 725 | // classReader.image2geo(icol,irow,x,y); |
| 726 | // sample[0]=x; |
| 727 | // sample[1]=y; |
| 728 | // if(verbose_opt[0]>1){ |
| 729 | // std::cout.precision(12); |
| 730 | // std::cout << theClass << " " << x << " " << y << std::endl; |
| 731 | // } |
| 732 | // //find col in img |
| 733 | // this->geo2image(x,y,iimg,jimg,ref2img); |
| 734 | // // ostringstream fs; |
| 735 | // // fs << "Error: geo2image failed to find col in img"; |
| 736 | // // throw(fs.str()); |
| 737 | // // } |
| 738 | // // this->geo2image(ivector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]],jvector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]],iimg,jimg); |
| 739 | // // //debug |
| 740 | // // if(!irow%100&&!icol%100) |
| 741 | // // std::cout << "x,y,iimg,jimg: " << x << ", " << y << ", " << iimg << ", " << jimg << std::endl; |
| 742 | // // iimg=ivector[irow/down_opt[0]*classReader.nrOfCol()/down_opt[0]+icol/down_opt[0]]; |
| 743 | // //nearest neighbour |
| 744 | // iimg=static_cast<int>(iimg); |
| 745 | // if(static_cast<int>(iimg)<0||static_cast<int>(iimg)>=this->nrOfCol()) |
| 746 | // continue; |
| 747 | // bool valid=true; |
| 748 | |
| 749 | // for(int iband=0;iband<nband;++iband){ |
| 750 | // int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 751 | // if(srcnodata_opt.size()&&theBand==bndnodata_opt[0]){ |
| 752 | // // vector<int>::const_iterator bndit=bndnodata_opt.begin(); |
| 753 | // for(int inodata=0;inodata<srcnodata_opt.size()&&valid;++inodata){ |
| 754 | // if(imgBuffer[iband][iimg]==srcnodata_opt[inodata]) |
| 755 | // valid=false; |
| 756 | // } |
| 757 | // } |
| 758 | // } |
| 759 | // if(valid){ |
| 760 | // for(int iband=0;iband<imgBuffer.size();++iband){ |
| 761 | // sample[iband+2]=imgBuffer[iband][iimg]; |
| 762 | // } |
| 763 | // float theThreshold=(threshold_opt.size()>1)?threshold_opt[processClass]:threshold_opt[0]; |
| 764 | // if(theThreshold>0){//percentual value |
| 765 | // double p=static_cast<double>(rand())/(RAND_MAX); |
| 766 | // p*=100.0; |
| 767 | // if(p>theThreshold) |
| 768 | // continue;//do not select for now, go to next column |
| 769 | // } |
| 770 | // // else if(nvalid.size()>processClass){//absolute value |
| 771 | // // if(nvalid[processClass]>=-theThreshold) |
| 772 | // // continue;//do not select any more pixels for this class, go to next column to search for other classes |
| 773 | // // } |
| 774 | // writeBuffer.push_back(sample); |
| 775 | // writeBufferClass.push_back(theClass); |
| 776 | // ++ntotalvalid; |
| 777 | // if(nvalid.count(theClass)) |
| 778 | // nvalid[theClass]+=1; |
| 779 | // else |
| 780 | // nvalid[theClass]=1; |
| 781 | // } |
| 782 | // else{ |
| 783 | // ++ntotalinvalid; |
| 784 | // if(ninvalid.count(theClass)) |
| 785 | // ninvalid[theClass]+=1; |
| 786 | // else |
| 787 | // ninvalid[theClass]=1; |
| 788 | // } |
| 789 | // }//processClass |
| 790 | // }//icol |
| 791 | // progress=static_cast<float>(irow+1.0)/classReader.nrOfRow(); |
| 792 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 793 | // }//irow |
| 794 | // if(writeBuffer.size()>0){ |
| 795 | // if(verbose_opt[0]>0){ |
| 796 | // map<int,unsigned long int>::const_iterator mapit=nvalid.begin(); |
| 797 | // for(mapit=nvalid.begin();mapit!=nvalid.end();++mapit) |
| 798 | // std::cout << "nvalid for class " << mapit->first << ": " << mapit->second << std::endl; |
| 799 | // std::cout << "creating image sample writer " << output_opt[0] << " with " << writeBuffer.size() << " samples (" << ntotalinvalid << " invalid)" << std::endl; |
| 800 | // } |
| 801 | // assert(ntotalvalid==writeBuffer.size()); |
| 802 | // if(verbose_opt[0]>0) |
| 803 | // std::cout << "creating image sample writer " << output_opt[0] << " collecting from " << writeBuffer.size() << " samples (" << ntotalinvalid << " invalid)" << std::endl; |
| 804 | // // ogrWriter.open(output_opt[0],ogrformat_opt[0]); |
| 805 | // if(ogrWriter.open(output_opt[0],ogrformat_opt[0])!=OGRERR_NONE){ |
| 806 | // ostringstream fs; |
| 807 | // fs << "open ogrWriter failed "; |
| 808 | // fs << "output name: " << output_opt[0] << ", "; |
| 809 | // fs << "format: "<< ogrformat_opt[0] << std::endl; |
| 810 | // throw(fs.str()); |
| 811 | // } |
| 812 | // if(ogrWriter.pushLayer(layer_opt[0], this->getProjectionRef(),wkbPoint,papszOptions)!=OGRERR_NONE){ |
| 813 | // ostringstream fs; |
| 814 | // fs << "push layer to ogrWriter with points failed, "; |
| 815 | // fs << "layer name: "<< layer_opt[0] << std::endl; |
| 816 | // throw(fs.str()); |
| 817 | // } |
| 818 | // // if(ogrWriter.open(output_opt[0],layer_opt,ogrformat_opt[0], wkbPoint, this->getProjectionRef(),papszOptions)!=OGRERR_NONE){ |
| 819 | // // ostringstream fs; |
| 820 | // // fs << "open ogrWriter with points failed "; |
| 821 | // // fs << "output name: " << output_opt[0] << ", "; |
| 822 | // // fs << "layer name: "<< layer_opt[0] << ", "; |
| 823 | // // fs << "format: "<< ogrformat_opt[0] << std::endl; |
| 824 | // // throw(fs.str()); |
| 825 | // // } |
| 826 | // // ostringstream slayer; |
| 827 | // // slayer << "training data"; |
| 828 | // // std::string layername=slayer.str(); |
| 829 | // // ogrWriter.createLayer(layername, this->getProjection(), wkbPoint, papszOptions); |
| 830 | // // std::string fieldname="fid";//number of the point |
| 831 | // // ogrWriter.createField(fieldname,OFTInteger); |
| 832 | // map<std::string,double> pointAttributes; |
| 833 | // ogrWriter.createField(label_opt[0],labelType); |
| 834 | // if(fid_opt.size()) |
| 835 | // ogrWriter.createField(fid_opt[0],OFTInteger64); |
| 836 | // for(int iband=0;iband<nband;++iband){ |
| 837 | // ogrWriter.createField(bandNames_opt[iband],fieldType); |
| 838 | // } |
| 839 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 840 | // progress=0; |
| 841 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 842 | |
| 843 | // map<int,short> classDone; |
| 844 | // Vector2d<double> writeBufferTmp; |
| 845 | // vector<int> writeBufferClassTmp; |
| 846 | |
| 847 | // if(threshold_opt[0]<0){//absolute threshold |
| 848 | // map<int,unsigned long int>::iterator mapit; |
| 849 | // map<int,unsigned long int> ncopied; |
| 850 | // for(mapit=nvalid.begin();mapit!=nvalid.end();++mapit){ |
| 851 | // ncopied[mapit->first]=0; |
| 852 | // if(!mapit->second) |
| 853 | // classDone[mapit->first]=1; |
| 854 | // } |
| 855 | // while(classDone.size()<nvalid.size()){ |
| 856 | // unsigned int index=rand()%writeBufferClass.size(); |
| 857 | // int theClass=writeBufferClass[index]; |
| 858 | // float theThreshold=threshold_opt[0]; |
| 859 | // if(threshold_opt.size()>1&&class_opt.size()) |
| 860 | // theThreshold=threshold_opt[classmap[theClass]]; |
| 861 | // theThreshold=-theThreshold; |
| 862 | // if(ncopied[theClass]<theThreshold){ |
| 863 | // writeBufferClassTmp.push_back(*(writeBufferClass.begin()+index)); |
| 864 | // writeBufferTmp.push_back(*(writeBuffer.begin()+index)); |
| 865 | // writeBufferClass.erase(writeBufferClass.begin()+index); |
| 866 | // writeBuffer.erase(writeBuffer.begin()+index); |
| 867 | // ++(ncopied[theClass]); |
| 868 | // } |
| 869 | // else |
| 870 | // classDone[theClass]=1; |
| 871 | // if(ncopied[theClass]>=nvalid[theClass]){ |
| 872 | // classDone[theClass]=1; |
| 873 | // } |
| 874 | // } |
| 875 | // writeBuffer=writeBufferTmp; |
| 876 | // writeBufferClass=writeBufferClassTmp; |
| 877 | // // while(classDone.size()<nvalid.size()){ |
| 878 | // // unsigned int index=rand()%writeBufferClass.size(); |
| 879 | // // int theClass=writeBufferClass[index]; |
| 880 | // // float theThreshold=threshold_opt[0]; |
| 881 | // // if(threshold_opt.size()>1&&class_opt.size()) |
| 882 | // // theThreshold=threshold_opt[classmap[theClass]]; |
| 883 | // // theThreshold=-theThreshold; |
| 884 | // // if(nvalid[theClass]>theThreshold){ |
| 885 | // // writeBufferClass.erase(writeBufferClass.begin()+index); |
| 886 | // // writeBuffer.erase(writeBuffer.begin()+index); |
| 887 | // // --(nvalid[theClass]); |
| 888 | // // } |
| 889 | // // else |
| 890 | // // classDone[theClass]=1; |
| 891 | // // } |
| 892 | // } |
| 893 | |
| 894 | // for(unsigned int isample=0;isample<writeBuffer.size();++isample){ |
| 895 | // pointAttributes[label_opt[0]]=writeBufferClass[isample]; |
| 896 | // if(fid_opt.size()) |
| 897 | // pointAttributes[fid_opt[0]]=isample; |
| 898 | // for(int iband=0;iband<writeBuffer[0].size()-2;++iband){ |
| 899 | // pointAttributes[bandNames_opt[iband]]=writeBuffer[isample][iband+2]; |
| 900 | // } |
| 901 | // // ogrWriter.addPoint(writeBuffer[isample][0],writeBuffer[isample][1],pointAttributes,fid_opt[0],isample); |
| 902 | // ogrWriter.addPoint(writeBuffer[isample][0],writeBuffer[isample][1],pointAttributes); |
| 903 | // progress=static_cast<float>(isample+1.0)/writeBuffer.size(); |
| 904 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 905 | // } |
| 906 | // } |
| 907 | // else{ |
| 908 | // std::cout << "No data found for any class " << std::endl; |
| 909 | // } |
| 910 | // // classReader.close(); |
| 911 | // nsample=writeBuffer.size(); |
| 912 | // if(verbose_opt[0]){ |
| 913 | // std::cout << "total number of samples written: " << nsample << std::endl; |
| 914 | // std::cout << "total number of valid samples: " << ntotalvalid << std::endl; |
| 915 | // std::cout << "total number of invalid samples: " << ntotalinvalid << std::endl; |
| 916 | // if(nvalid.size()==class_opt.size()){ |
| 917 | // for(int iclass=0;iclass<class_opt.size();++iclass) |
| 918 | // std::cout << "class " << class_opt[iclass] << " has " << nvalid[class_opt[iclass]] << " samples" << std::endl; |
| 919 | // } |
| 920 | // } |
| 921 | // } |
| 922 | // } |
| 923 | // else{//vector dataset |
| 924 | // cerr << "Error: vector sample not supported, consider using pkextractogr" << endl; |
| 925 | // }//else (vector) |
| 926 | // progress=1.0; |
| 927 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 928 | // // this->close(); |
| 929 | // } |
| 930 | // catch(string predefinedString){ |
| 931 | // std::cout << predefinedString << std::endl; |
| 932 | // throw; |
| 933 | // } |
| 934 | // } |