Debian Codegraph @

src/imageclasses/jlsml_lib.h

raw symbols SCIP
1 /**********************************************************************
2 jlsml_lib.h: classify raster image using SML
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 #pragma once
22 //#include <sstream>
23 //#includ <iostream>
24 #include <boost/serialization/map.hpp>
25 #include <boost/archive/binary_iarchive.hpp>
26 #include <boost/archive/binary_oarchive.hpp>
27 #include <boost/archive/text_iarchive.hpp>
28 #include <boost/archive/text_oarchive.hpp>
29 #include <boost/serialization/vector.hpp>
30
31 #include <vector>
32 #include <map>
33 #include <memory>
34 #include "Jim.h"
35 #include "VectorOgr.h"
36 #include "base/Optionjl.h"
37 #include "apps/AppFactory.h"
38
39 ///train and classify SML
40 template<typename T> void Jim::classifySML_t(Jim& imgWriter, JimList& referenceReader, app::AppFactory& app){
41 Optionjl<unsigned short> class_opt("c", "class", "Class(es) to extract from reference");
42 Optionjl<double> srcnodata_opt("srcnodata", "srcnodata", "Nodata value in source");
43 Optionjl<double> dstnodata_opt("dstnodata", "dstnodata", "Nodata value to put where image is masked as nodata",0);
44 Optionjl<short> verbose_opt("v", "verbose", "Verbose level",0,2);
45
46 bool doProcess;//stop process when program was invoked with help option (-h --help)
47 try{
48 doProcess=class_opt.retrieveOption(app);
49 srcnodata_opt.retrieveOption(app);
50 dstnodata_opt.retrieveOption(app);
51 verbose_opt.retrieveOption(app);
52
53 if(!doProcess){
54 std::cout << std::endl;
55 std::ostringstream helpStream;
56 helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
57 throw(helpStream.str());//help was invoked, stop processing
58 }
59
60 if(class_opt.empty()){
61 std::string errorString="Error: no classes provided, use option class to provide a list of classes in reference to train";
62 throw(errorString);
63 }
64
65 if(nrOfBand()>1){
66 std::string errorString="Error: only single band (multi-plane) datasets are supported, consider band2plane";
67 throw(errorString);
68 }
69
70 for(size_t iref=0;iref<referenceReader.size();++iref){
71 if(referenceReader.getImage(iref)->getDataType()!=GDT_Byte){
72 std::string errorstring="Error: data type of reference must be GDT_Byte";
73 throw(errorstring);
74 }
75 }
76
77 T* pin=static_cast<T*>(getDataPointer(0));
78
79 if(verbose_opt[0]>=1)
80 std::cout << "start SML training" << std::endl;
81
82 int nrow=nrOfRow();
83 int ncol=nrOfCol();
84
85 //umap: [unique band information]->[index,class1,class2,...] (occurrence is always updated in last node!!!)
86 std::map<std::vector<T>,std::vector<std::vector<size_t> > > umap;
87 std::vector<T> pixel(nrOfPlane());//pixel with plane information
88 std::vector<unsigned char> refpixel(referenceReader.size());//pixel with reference information
89
90 OGRSpatialReference thisSRS(getProjectionRef().c_str());
91 #if GDAL_VERSION_MAJOR > 2
92 thisSRS.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
93 #endif
94 OGRSpatialReference *thisSpatialRef=&thisSRS;
95 #if GDAL_VERSION_MAJOR > 2
96 thisSpatialRef->SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
97 #endif
98 //currently only a single SRS for all images in reference collection supported
99 OGRSpatialReference referenceSRS(referenceReader.getImage(0)->getProjectionRef().c_str());
100 #if GDAL_VERSION_MAJOR > 2
101 referenceSRS.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
102 #endif
103
104 OGRSpatialReference *referenceSpatialRef=&referenceSRS;
105 #if GDAL_VERSION_MAJOR > 2
106 referenceSpatialRef->SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
107 #endif
108 OGRCoordinateTransformation *img2ref=OGRCreateCoordinateTransformation(thisSpatialRef, referenceSpatialRef);
109
110 std::vector<unsigned char*> pref(referenceReader.size());
111 unsigned short theMax=0;
112 for(size_t iref=0;iref<referenceReader.size();++iref)
113 pref[iref]=static_cast<unsigned char*>(referenceReader.getImage(iref)->getDataPointer(0));
114 unsigned short nclass=class_opt.size();
115 if(nclass<2){
116 std::ostringstream errorStream;
117 errorStream << "Error: number of classes should be at least 2" << std::endl;
118 throw(errorStream.str());
119 }
120
121 if(verbose_opt[0]>=1){
122 std::cout << "number of classes: " << nclass << std::endl;
123 }
124
125 for(size_t y=0;y<nrow;++y){
126 for(size_t x=0;x<ncol;++x){
127 std::vector<size_t> posclass(1+nclass);
128 size_t index=y*ncol+x;
129 posclass[0]=static_cast<size_t>(index);
130 //read reference
131 bool validref=false;
132 for(size_t iref=0;iref<referenceReader.size();++iref){
133 size_t ncolref=referenceReader.getImage(iref)->nrOfCol();
134 size_t nrowref=referenceReader.getImage(iref)->nrOfRow();
135 double colReference=0;
136 double rowReference=0;
137 double geox=0;
138 double geoy=0;
139 image2geo(x,y,geox,geoy);
140 referenceReader.getImage(iref)->geo2image(geox,geoy,colReference,rowReference,img2ref);
141 if(rowReference>=0&&rowReference<referenceReader.getImage(iref)->nrOfRow()&&colReference>=0&&colReference<referenceReader.getImage(iref)->nrOfCol()){
142 size_t indexref=static_cast<size_t>(rowReference)*ncolref+static_cast<size_t>(colReference);
143 refpixel[iref]=static_cast<unsigned char>((pref[iref])[indexref]);
144 validref=true;
145 }
146 else{
147 std::cout << "Warning: could not find valid reference pixel for col, row " << x << ", " << y << std::endl;
148 std::cout << "colReference, rowReference: " << colReference << ", " << rowReference << std::endl;
149 std::cout << "geox, geoy: " << geox << ", " << geoy << std::endl;
150 continue;
151 }
152 }
153
154 if(!validref){
155 std::ostringstream errorStream;
156 errorStream << "Error: could not find valid reference";
157 throw(errorStream.str());
158 }
159
160 bool valid=true;
161 if(srcnodata_opt.size())
162 valid=false;
163 for(size_t z=0;z<nrOfPlane();++z){
164 pixel[z]=pin[index+z*ncol*nrow];
165 if(srcnodata_opt.size()){
166 //invalid iff pixel has no data in all planes
167 if(pixel[z]!=srcnodata_opt[0])
168 valid=true;
169 }
170 }
171 auto pit = umap.find(pixel);
172 for(size_t iclass=0;iclass<class_opt.size();++iclass){
173 size_t increment=0;
174 for(size_t iref=0;iref<referenceReader.size();++iref){
175 if(refpixel[iref]==class_opt[iclass])
176 ++increment;
177 }
178 if(pit!=umap.end()){
179 posclass[1+iclass]=((pit->second).back())[1+iclass]+increment;
180 }
181 else
182 posclass[1+iclass]=increment;
183 }
184 umap[pixel].push_back(posclass);
185 }
186 }
187
188 if(verbose_opt[0]>=1)
189 std::cout << "end of training" << std::endl;
190
191 if(verbose_opt[0]){
192 std::cout << "umap.size(): " << umap.size() << std::endl;
193 if(verbose_opt[0]>1){
194 for(auto mapit = umap.begin(); mapit != umap.end(); ++mapit){
195 for(size_t iclass=0;iclass<nclass;++iclass){
196 std::cout << static_cast<size_t>(((mapit->second.back())[0]))/ncol << " " << static_cast<size_t>(((mapit->second.back())[0]))%ncol << ": ";
197 double value=(mapit->second.back())[1+iclass];
198 std::cout << value << " " << "( " << class_opt[iclass] << ") ";
199 }
200 std::cout << std::endl;
201 }
202 }
203 }
204
205 imgWriter.open(ncol,nrow,nclass,GDT_Byte);
206 imgWriter.GDALSetNoDataValue(dstnodata_opt[0]);
207 imgWriter.setNoData(dstnodata_opt);
208 imgWriter.copyGeoTransform(*this);
209 imgWriter.setProjection(this->getProjection());
210 //initialize imgWriter with dstnodata_opt[0]
211 imgWriter.setData(dstnodata_opt[0]);
212
213 std::vector<unsigned char*> pout(nclass);
214 for(size_t iclass=0;iclass<nclass;++iclass){
215 pout[iclass]=static_cast<unsigned char*>(imgWriter.getDataPointer(iclass));
216 }
217
218 //loop through umap and assign pixel values based on occurrence
219 for(auto mapit = umap.begin(); mapit != umap.end(); ++mapit){
220 std::vector<double> fclass(nclass);
221 double maxValue=0;
222 for(size_t iclass=0;iclass<nclass;++iclass){
223 double value=((mapit->second).back())[1+iclass];
224 fclass[iclass]=value;
225 if(fclass[iclass]>maxValue)
226 maxValue=fclass[iclass];
227 }
228 double scale=(maxValue>0) ? 100.0/maxValue : 0;
229 for(size_t iclass=0;iclass<nclass;++iclass)
230 fclass[iclass]*=scale;
231 for(auto tupit = mapit->second.begin(); tupit != mapit->second.end(); ++tupit){
232 if(verbose_opt[0]>1)
233 std::cout << (*tupit)[0] << " = " << (*tupit)[0]/ncol << " " << (*tupit)[0]%ncol << ": ";
234 for(unsigned int iclass=0;iclass<nclass;++iclass){
235 if(static_cast<size_t>(((*tupit)[0]))/ncol<0){
236 std::ostringstream errorStream;
237 errorStream << "Error: not within rows: " << static_cast<size_t>(((*tupit)[0]))/ncol << std::endl;
238 throw(errorStream.str());
239 }
240 if(static_cast<size_t>(((*tupit)[0]))/ncol>nrow){
241 std::ostringstream errorStream;
242 errorStream << "Error: not within rows: " << static_cast<size_t>(((*tupit)[0]))/ncol << std::endl;
243 throw(errorStream.str());
244 }
245 if(static_cast<size_t>(((*tupit)[0]))%ncol<0){
246 std::ostringstream errorStream;
247 errorStream << "Error: not within cols: " << static_cast<size_t>(((*tupit)[0]))%ncol << std::endl;
248 throw(errorStream.str());
249 }
250 if(verbose_opt[0]>1)
251 std::cout << static_cast<unsigned short>(fclass[iclass]) << " ";
252 pout[iclass][(*tupit)[0]]=static_cast<unsigned char>(fclass[iclass]);
253 }
254 if(verbose_opt[0]>1)
255 std::cout << std::endl;
256 }
257 }
258 }
259 catch(std::string errorString){
260 std::cerr << errorString << std::endl;
261 throw;
262 }
263 }
264
265 ///train SML with information in 3D
266 //template<typename T> std::string Jim::trainSML_t(JimList& referenceReader, app::AppFactory& app){
267 template<typename T> void Jim::trainSML_t(JimList& referenceReader, app::AppFactory& app){
268 Optionjl<std::string> model_opt("model", "model", "Model filename to save trained classifier.");
269 Optionjl<unsigned short> class_opt("c", "class", "Class(es) to extract from reference. Leave empty to extract two classes only: 1 against rest",1);
270 Optionjl<double> srcnodata_opt("srcnodata", "srcnodata", "Nodata value in source",0);
271 Optionjl<short> verbose_opt("v", "verbose", "Verbose level",0,2);
272
273 bool doProcess;//stop process when program was invoked with help option (-h --help)
274 try{
275 doProcess=class_opt.retrieveOption(app);
276 model_opt.retrieveOption(app);
277 srcnodata_opt.retrieveOption(app);
278 verbose_opt.retrieveOption(app);
279
280 if(!doProcess){
281 std::cout << std::endl;
282 std::ostringstream helpStream;
283 helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
284 throw(helpStream.str());//help was invoked, stop processing
285 }
286
287 if(nrOfBand()>1){
288 std::string errorString="Error: only single band (multi-plane) datasets are supported, consider band2plane";
289 throw(errorString);
290 }
291
292 for(size_t iref=0;iref<referenceReader.size();++iref){
293 if(referenceReader.getImage(iref)->getDataType()!=GDT_Byte){
294 std::string errorstring="Error: data type of reference must be GDT_Byte";
295 throw(errorstring);
296 }
297 }
298
299 T* pin=static_cast<T*>(getDataPointer(0));
300
301 if(verbose_opt[0]>=1)
302 std::cout << "start SML training" << std::endl;
303
304 int nrow=nrOfRow();
305 int ncol=nrOfCol();
306
307 //umap: [unique band information]->[index,class1,class2,...] (occurrence is always updated in last node!!!)
308 std::map<std::vector<T>,std::vector<std::vector<size_t> > > umap;
309 std::vector<T> pixel(nrOfPlane());//pixel with plane information
310 std::vector<unsigned char> refpixel(referenceReader.size());//pixel with reference information
311
312 OGRSpatialReference thisSRS(getProjectionRef().c_str());
313 #if GDAL_VERSION_MAJOR > 2
314 thisSRS.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
315 #endif
316 OGRSpatialReference *thisSpatialRef=&thisSRS;
317 #if GDAL_VERSION_MAJOR > 2
318 thisSpatialRef->SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
319 #endif
320 //currently only a single SRS for all images in reference collection supported
321 OGRSpatialReference referenceSRS(referenceReader.getImage(0)->getProjectionRef().c_str());
322 #if GDAL_VERSION_MAJOR > 2
323 referenceSRS.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
324 #endif
325
326 OGRSpatialReference *referenceSpatialRef=&referenceSRS;
327 #if GDAL_VERSION_MAJOR > 2
328 referenceSpatialRef->SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
329 #endif
330 OGRCoordinateTransformation *img2ref=OGRCreateCoordinateTransformation(thisSpatialRef, referenceSpatialRef);
331
332 std::vector<unsigned char*> pref(referenceReader.size());
333 unsigned short theMax=0;
334 for(size_t iref=0;iref<referenceReader.size();++iref)
335 pref[iref]=static_cast<unsigned char*>(referenceReader.getImage(iref)->getDataPointer(0));
336 unsigned short nclass=class_opt.size()>1?class_opt.size():2;
337 if(nclass<2){
338 std::ostringstream errorStream;
339 errorStream << "Error: number of classes should be at least 2" << std::endl;
340 throw(errorStream.str());
341 }
342
343 if(verbose_opt[0]>=1){
344 std::cout << "number of classes: " << nclass << std::endl;
345 std::cout << "create vectors for training" << std::endl;
346 }
347
348 for(size_t y=0;y<nrow;++y){
349 for(size_t x=0;x<ncol;++x){
350 std::vector<size_t> posclass(1+nclass);
351 size_t index=y*ncol+x;
352 posclass[0]=static_cast<size_t>(index);
353 //read reference
354 bool validref=false;
355 for(size_t iref=0;iref<referenceReader.size();++iref){
356 size_t ncolref=referenceReader.getImage(iref)->nrOfCol();
357 size_t nrowref=referenceReader.getImage(iref)->nrOfRow();
358 double colReference=0;
359 double rowReference=0;
360 double geox=0;
361 double geoy=0;
362 image2geo(x,y,geox,geoy);
363 referenceReader.getImage(iref)->geo2image(geox,geoy,colReference,rowReference,img2ref);
364 if(rowReference>=0&&rowReference<referenceReader.getImage(iref)->nrOfRow()&&colReference>=0&&colReference<referenceReader.getImage(iref)->nrOfCol()){
365 size_t indexref=static_cast<size_t>(rowReference)*ncolref+static_cast<size_t>(colReference);
366 refpixel[iref]=static_cast<unsigned char>((pref[iref])[indexref]);
367 validref=true;
368 }
369 else{
370 std::cout << "Warning: could not find valid reference pixel for col, row " << x << ", " << y << std::endl;
371 std::cout << "colReference, rowReference: " << colReference << ", " << rowReference << std::endl;
372 std::cout << "geox, geoy: " << geox << ", " << geoy << std::endl;
373 continue;
374 }
375 }
376 if(!validref){
377 std::ostringstream errorStream;
378 errorStream << "Error: could not find valid reference";
379 throw(errorStream.str());
380 }
381
382 bool valid=true;
383 if(srcnodata_opt.size())
384 valid=false;
385 for(size_t z=0;z<nrOfPlane();++z){
386 pixel[z]=pin[index+z*ncol*nrow];
387 if(srcnodata_opt.size()){
388 //invalid iff pixel has no data in all planes
389 if(pixel[z]!=srcnodata_opt[0])
390 valid=true;
391 }
392 }
393 auto pit = umap.find(pixel);
394 for(size_t iclass=0;iclass<class_opt.size();++iclass){
395 size_t increment=0;
396 for(size_t iref=0;iref<referenceReader.size();++iref){
397 if(refpixel[iref]==class_opt[iclass])
398 ++increment;
399 }
400 if(pit!=umap.end()){
401 posclass[1+iclass]=((pit->second).back())[1+iclass]+increment;
402 }
403 else
404 posclass[1+iclass]=increment;
405 }
406 umap[pixel].push_back(posclass);
407 }
408 }
409 //todo: scale from 0 to 100
410 for(auto mapit = umap.begin(); mapit != umap.end(); ++mapit){
411 std::vector<double> fclass(nclass);
412 double maxValue=0;
413 for(size_t iclass=0;iclass<nclass;++iclass){
414 double value=((mapit->second).back())[1+iclass];
415 fclass[iclass]=value;
416 if(fclass[iclass]>maxValue)
417 maxValue=fclass[iclass];
418 }
419 double scale=(maxValue>0) ? 100.0/maxValue : 0;
420 for(size_t iclass=0;iclass<nclass;++iclass)
421 ((mapit->second).back())[1+iclass]*=scale;
422 }
423 if(verbose_opt[0]>=1)
424 std::cout << "end of training, write to output" << std::endl;
425 /* std::ostringstream outputStream; */
426 /* boost::archive::text_oarchive oarch(outputStream); */
427 std::ofstream ofs(model_opt[0], std::ios::binary);
428 boost::archive::binary_oarchive oarch(ofs);
429 try{
430 oarch & umap;
431 }
432 catch (const boost::archive::archive_exception &e) {
433 if (e.code != boost::archive::archive_exception::output_stream_error) {
434 std::cerr << "Error: could not write model 0" << std::endl;
435 throw;
436 }
437 else{
438 std::cerr << "Error: could not write model 1" << std::endl;
439 throw;
440 }
441 }
442 /* oarch << umap; */
443
444 if(verbose_opt[0]){
445 std::cout << "umap.size(): " << umap.size() << std::endl;
446 if(verbose_opt[0]>1){
447 for(auto mapit = umap.begin(); mapit != umap.end(); ++mapit){
448 for(size_t iclass=0;iclass<nclass;++iclass){
449 std::cout << static_cast<size_t>(((mapit->second.back())[0]))/ncol << " " << static_cast<size_t>(((mapit->second.back())[0]))%ncol << ": ";
450 double value=(mapit->second.back())[1+iclass];
451 std::cout << value << " ";
452 }
453 std::cout << std::endl;
454 }
455 }
456 }
457 //return(outputStream.str());
458 }
459 catch(std::string errorString){
460 std::cerr << errorString << std::endl;
461 throw;
462 }
463 }
464
465 ///classify 3D raster dataset with SML
466 template<typename T> void Jim::classifySML_t(Jim& imgWriter, app::AppFactory& app){
467 Optionjl<std::string> model_opt("model", "model", "Model filename to save trained classifier.");
468 Optionjl<double> srcnodata_opt("srcnodata", "srcnodata", "Nodata value in source",0);
469 Optionjl<double> dstnodata_opt("dstnodata", "dstnodata", "Nodata value to put where image is masked as nodata", 0);
470 Optionjl<short> verbose_opt("v", "verbose", "Verbose level",0,2);
471
472 bool doProcess;//stop process when program was invoked with help option (-h --help)
473 try{
474 doProcess=model_opt.retrieveOption(app);
475 srcnodata_opt.retrieveOption(app);
476 dstnodata_opt.retrieveOption(app);
477 verbose_opt.retrieveOption(app);
478
479 if(!doProcess){
480 std::cout << std::endl;
481 std::ostringstream helpStream;
482 helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
483 throw(helpStream.str());//help was invoked, stop processing
484 }
485
486 if(model_opt.empty()){
487 std::string errorString="Error: model option to write model not set";
488 throw(errorString);
489 }
490
491 if(nrOfBand()>1){
492 std::string errorString="Error: only single band (multi-plane) datasets are supported, consider band2plane";
493 throw(errorString);
494 }
495
496 T* pin=static_cast<T*>(getDataPointer(0));
497
498 if(verbose_opt[0]>=1)
499 std::cout << "start SML classification" << std::endl;
500
501 //umap: [unique band information]->[index,class1,class2,...] (occurrence is always updated in last node!!!)
502 std::map<std::vector<T>,std::vector<std::vector<size_t> > > umap;
503 std::vector<T> pixel(nrOfPlane());//pixel with plane information
504
505 std::ifstream ifs(model_opt[0],std::ios::binary);
506 umap.clear();
507 boost::archive::binary_iarchive iarch(ifs);
508 /* boost::archive::text_iarchive iarch(ifs); */
509 /* iarch >> umap; */
510 try{
511 iarch & umap;
512 }
513 catch (const boost::archive::archive_exception &e) {
514 if (e.code != boost::archive::archive_exception::input_stream_error) {
515 std::cerr << "Error: could not read model 0" << std::endl;
516 throw;
517 }
518 else{
519 std::cerr << "Error: could not read model 1" << std::endl;
520 throw;
521 }
522 }
523
524 int nclass=(umap.begin()->second).back().size()-1;
525
526 int nrow=nrOfRow();
527 int ncol=nrOfCol();
528 if(this->isInit()){
529 if(verbose_opt[0]>=1)
530 std::cout << "We are in initialize" << std::endl;
531 imgWriter.open(ncol,nrow,nclass,GDT_Byte);
532 imgWriter.GDALSetNoDataValue(dstnodata_opt[0]);
533 imgWriter.setNoData(dstnodata_opt);
534 imgWriter.copyGeoTransform(*this);
535 imgWriter.setProjection(this->getProjection());
536 //initialize imgWriter with dstnodata_opt[0]
537 imgWriter.setData(dstnodata_opt[0]);
538 }
539 std::vector<unsigned char*> pout(nclass);
540 for(size_t iclass=0;iclass<nclass;++iclass)
541 pout[iclass]=static_cast<unsigned char*>(imgWriter.getDataPointer(iclass));
542
543 bool valid=true;
544 if(srcnodata_opt.size())
545 valid=false;
546 for(size_t index=0;index<nrow*ncol;++index){
547 for(size_t z=0;z<nrOfPlane();++z){
548 pixel[z]=pin[index+z*ncol*nrow];
549 if(srcnodata_opt.size()){
550 //invalid iff pixel has no data in all planes
551 if(pixel[z]!=srcnodata_opt[0])
552 valid=true;
553 }
554 }
555 if(!valid){
556 for(size_t iclass=0;iclass<nclass;++iclass)
557 pout[iclass][index]=static_cast<unsigned char>(dstnodata_opt[0]);
558 continue;
559 }
560 auto pit = umap.find(pixel);
561 if(pit==umap.end()){
562 for(size_t iclass=0;iclass<nclass;++iclass)
563 pout[iclass][index]=static_cast<unsigned char>(dstnodata_opt[0]);
564 continue;
565 }
566 for(size_t iclass=0;iclass<nclass;++iclass)
567 pout[iclass][index]=(pit->second.back())[1+iclass];
568 }
569 }
570 catch(std::string errorString){
571 std::cerr << errorString << std::endl;
572 throw;
573 }
574 }
575
576 ///classify 3D raster dataset with SML
577 /* template<typename T> void Jim::classifySML_t(Jim& imgWriter, app::AppFactory& app){ */
578 /* Optionjl<std::string> model_opt("model", "model", "Model filename to save trained classifier."); */
579 /* Optionjl<unsigned short> class_opt("c", "class", "Class(es) to extract from reference. Leave empty to extract two classes only: 1 against rest",1); */
580 /* Optionjl<double> srcnodata_opt("srcnodata", "srcnodata", "Nodata value in source",0); */
581 /* Optionjl<double> dstnodata_opt("dstnodata", "dstnodata", "Nodata value to put where image is masked as nodata", 0); */
582 /* Optionjl<short> verbose_opt("v", "verbose", "Verbose level",0,2); */
583
584 /* bool doProcess;//stop process when program was invoked with help option (-h --help) */
585 /* try{ */
586 /* doProcess=model_opt.retrieveOption(app); */
587 /* class_opt.retrieveOption(app); */
588 /* srcnodata_opt.retrieveOption(app); */
589 /* dstnodata_opt.retrieveOption(app); */
590 /* verbose_opt.retrieveOption(app); */
591
592 /* if(!doProcess){ */
593 /* std::cout << std::endl; */
594 /* std::ostringstream helpStream; */
595 /* helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; */
596 /* throw(helpStream.str());//help was invoked, stop processing */
597 /* } */
598
599 /* if(model_opt.empty()){ */
600 /* std::string errorString="Error: model option to write model not set"; */
601 /* throw(errorString); */
602 /* } */
603
604 /* if(nrOfBand()>1){ */
605 /* std::string errorString="Error: only single band (multi-plane) datasets are supported, consider band2plane"; */
606 /* throw(errorString); */
607 /* } */
608
609 /* if(verbose_opt[0]>=1) */
610 /* std::cout << "start SML classification" << std::endl; */
611
612 /* //umap: [unique band information]->[index,class1,class2,...] (occurrence is always updated in last node!!!) */
613 /* std::map<std::vector<T>,std::vector<std::vector<size_t> > > umap; */
614
615 /* std::ifstream ifs(model_opt[0]); */
616 /* umap.clear(); */
617 /* boost::archive::text_iarchive iarch(ifs); */
618 /* iarch >> umap; */
619
620 /* int nclass=(umap.begin()->second).back().size()-1; */
621
622 /* int nrow=nrOfRow(); */
623 /* int ncol=nrOfCol(); */
624 /* if(this->isInit()){ */
625 /* if(verbose_opt[0]>=1) */
626 /* std::cout << "We are in initialize" << std::endl; */
627 /* imgWriter.open(ncol,nrow,nclass,GDT_Byte); */
628 /* imgWriter.GDALSetNoDataValue(dstnodata_opt[0]); */
629 /* imgWriter.setNoData(dstnodata_opt); */
630 /* imgWriter.copyGeoTransform(*this); */
631 /* imgWriter.setProjection(this->getProjection()); */
632 /* //initialize imgWriter with dstnodata_opt[0] */
633 /* imgWriter.setData(dstnodata_opt[0]); */
634 /* } */
635 /* std::vector<unsigned char*> pout(nclass); */
636 /* for(size_t iclass=0;iclass<nclass;++iclass){ */
637 /* pout[iclass]=static_cast<unsigned char*>(imgWriter.getDataPointer(iclass)); */
638 /* } */
639
640 /* //loop through umap and assign pixel values based on occurrence */
641 /* for(auto mapit = umap.begin(); mapit != umap.end(); ++mapit){ */
642 /* std::vector<double> fclass(nclass); */
643 /* double maxValue=0; */
644 /* for(size_t iclass=0;iclass<nclass;++iclass){ */
645 /* double value=((mapit->second).back())[1+iclass]; */
646 /* fclass[iclass]=value; */
647 /* if(fclass[iclass]>maxValue) */
648 /* maxValue=fclass[iclass]; */
649 /* } */
650 /* double scale=(maxValue>0) ? 100.0/maxValue : 0; */
651 /* for(size_t iclass=0;iclass<nclass;++iclass) */
652 /* fclass[iclass]*=scale; */
653 /* for(auto tupit = mapit->second.begin(); tupit != mapit->second.end(); ++tupit){ */
654 /* if(verbose_opt[0]>1) */
655 /* std::cout << (*tupit)[0] << " = " << (*tupit)[0]/ncol << " " << (*tupit)[0]%ncol << ": "; */
656 /* for(unsigned int iclass=0;iclass<nclass;++iclass){ */
657 /* if(static_cast<size_t>(((*tupit)[0]))/ncol<0){ */
658 /* std::ostringstream errorStream; */
659 /* errorStream << "Error: not within rows: " << static_cast<size_t>(((*tupit)[0]))/ncol << std::endl; */
660 /* throw(errorStream.str()); */
661 /* } */
662 /* if(static_cast<size_t>(((*tupit)[0]))/ncol>nrow){ */
663 /* std::ostringstream errorStream; */
664 /* errorStream << "Error: not within rows: " << static_cast<size_t>(((*tupit)[0]))/ncol << std::endl; */
665 /* throw(errorStream.str()); */
666 /* } */
667 /* if(static_cast<size_t>(((*tupit)[0]))%ncol<0){ */
668 /* std::ostringstream errorStream; */
669 /* errorStream << "Error: not within cols: " << static_cast<size_t>(((*tupit)[0]))%ncol << std::endl; */
670 /* throw(errorStream.str()); */
671 /* } */
672 /* if(verbose_opt[0]>1) */
673 /* std::cout << static_cast<unsigned short>(fclass[iclass]) << " "; */
674 /* pout[iclass][(*tupit)[0]]=static_cast<unsigned char>(fclass[iclass]); */
675 /* } */
676 /* if(verbose_opt[0]>1) */
677 /* std::cout << std::endl; */
678 /* } */
679 /* } */
680 /* } */
681 /* catch(std::string errorString){ */
682 /* std::cerr << errorString << std::endl; */
683 /* throw; */
684 /* } */
685 /* } */
686
687 ///train SML in 2D with information in bands
688 template<typename T> std::string Jim::trainSML2d_t(JimList& referenceReader, app::AppFactory& app){
689 /* Optionjl<std::string> model_opt("model", "model", "Model filename to save trained classifier."); */
690 Optionjl<unsigned int> band_opt("b", "band", "Band index (starting from 0, either use band option or use start to end)");
691 Optionjl<unsigned int> bstart_opt("sband", "startband", "Start band sequence number");
692 Optionjl<unsigned int> bend_opt("eband", "endband", "End band sequence number");
693 Optionjl<unsigned short> class_opt("c", "class", "Class(es) to extract from reference. Leave empty to extract two classes only: 1 against rest",1);
694 Optionjl<double> srcnodata_opt("srcnodata", "srcnodata", "Nodata value in source",0);
695 Optionjl<short> verbose_opt("v", "verbose", "Verbose level",0,2);
696
697 bool doProcess;//stop process when program was invoked with help option (-h --help)
698 try{
699 /* doProcess=model_opt.retrieveOption(app); */
700 doProcess=band_opt.retrieveOption(app);
701 bstart_opt.retrieveOption(app);
702 bend_opt.retrieveOption(app);
703 class_opt.retrieveOption(app);
704 srcnodata_opt.retrieveOption(app);
705 verbose_opt.retrieveOption(app);
706 // memory_opt.retrieveOption(app);
707
708 if(!doProcess){
709 std::cout << std::endl;
710 std::ostringstream helpStream;
711 helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
712 throw(helpStream.str());//help was invoked, stop processing
713 }
714
715 /* if(model_opt.empty()){ */
716 /* std::string errorString="Error: model option to write model not set"; */
717 /* throw(errorString); */
718 /* } */
719
720 if(verbose_opt[0]>=1)
721 std::cout << "start SML" << std::endl;
722
723 if(verbose_opt[0]>=1)
724 std::cout << "configure band options" << std::endl;
725
726 //convert start and end band options to vector of band indexes
727 if(bstart_opt.size()){
728 if(bend_opt.size()!=bstart_opt.size()){
729 std::string errorstring="Error: options for start and end band indexes must be provided as pairs, missing end band";
730 throw(errorstring);
731 }
732 band_opt.clear();
733 for(unsigned int ipair=0;ipair<bstart_opt.size();++ipair){
734 if(bend_opt[ipair]<=bstart_opt[ipair]){
735 std::string errorstring="Error: index for end band must be smaller then start band";
736 throw(errorstring);
737 }
738 for(unsigned int iband=bstart_opt[ipair];iband<=bend_opt[ipair];++iband)
739 band_opt.push_back(iband);
740 }
741 }
742
743 if(verbose_opt[0]>=1)
744 std::cout << "sort bands" << std::endl;
745
746 //sort bands
747 if(band_opt.size())
748 std::sort(band_opt.begin(),band_opt.end());
749 else{
750 unsigned int iband=0;
751 while(band_opt.size()<nrOfBand())
752 band_opt.push_back(iband++);
753 }
754
755 int nrow=nrOfRow();
756 int ncol=nrOfCol();
757 int nclass=class_opt.size()>1?class_opt.size():2;
758
759 if(verbose_opt[0]>=1)
760 std::cout << "create vectors for training" << std::endl;
761
762 //umap: [unique band information]->[x,y,class1,class2,...]
763 std::map<std::vector<T>,std::vector<std::vector<unsigned int> > > umap;
764 std::vector<T> pixel(band_opt.size());//pixel with band information
765 std::vector<T> refpixel(referenceReader.size());//pixel with reference information
766
767 OGRSpatialReference thisSRS(getProjectionRef().c_str());
768 #if GDAL_VERSION_MAJOR > 2
769 thisSRS.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
770 #endif
771 OGRSpatialReference *thisSpatialRef=&thisSRS;
772 #if GDAL_VERSION_MAJOR > 2
773 thisSpatialRef->SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
774 #endif
775 //currently only a single SRS for all images in reference collection supported
776 OGRSpatialReference referenceSRS(referenceReader.getImage(0)->getProjectionRef().c_str());
777 #if GDAL_VERSION_MAJOR > 2
778 referenceSRS.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
779 #endif
780
781 OGRSpatialReference *referenceSpatialRef=&referenceSRS;
782 #if GDAL_VERSION_MAJOR > 2
783 referenceSpatialRef->SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
784 #endif
785 OGRCoordinateTransformation *img2ref=OGRCreateCoordinateTransformation(thisSpatialRef, referenceSpatialRef);
786
787 std::vector<double> oldRowReference(referenceReader.size());
788 for(int ireference=0;ireference<referenceReader.size();++ireference)
789 oldRowReference[ireference]=-1;
790 if(verbose_opt[0]>=1)
791 std::cout << "here we go" << std::endl;
792 Vector2d<T> classInput(referenceReader.size());
793 for(int y=0;y<nrOfRow();++y){
794 Vector2d<T> lineInput(band_opt.size(),nrOfCol());
795 for(unsigned int iband=0;iband<band_opt.size();++iband)
796 readData(lineInput[iband],y,band_opt[iband]);
797 for(int x=0;x<this->nrOfCol();++x){
798 std::vector<unsigned int> posclass(2+nclass);
799 posclass[0]=static_cast<unsigned int>(x);
800 posclass[1]=static_cast<unsigned int>(y);
801 //read reference
802 for(size_t iref=0;iref<referenceReader.size();++iref){
803 double colReference=0;
804 double rowReference=0;
805 double geox=0;
806 double geoy=0;
807 image2geo(x,y,geox,geoy);
808 referenceReader.getImage(iref)->geo2image(geox,geoy,colReference,rowReference,img2ref);
809 rowReference=static_cast<int>(rowReference);
810 colReference=static_cast<int>(colReference);
811 if(rowReference>=0&&rowReference<referenceReader.getImage(iref)->nrOfRow()&&colReference>=0&&colReference<referenceReader.getImage(iref)->nrOfCol()){
812 if(static_cast<int>(rowReference)!=static_cast<int>(oldRowReference[iref])){
813 referenceReader.getImage(iref)->readData(classInput[iref],static_cast<unsigned int>(rowReference));
814 oldRowReference[iref]=rowReference;
815 }
816 refpixel[iref]=classInput[iref][colReference];
817 }
818 else
819 continue;
820 }
821
822 pixel=lineInput.selectCol(x);
823 bool valid=false;
824 for(size_t iband=0;iband<pixel.size();++iband){
825 if(pixel[iband]!=srcnodata_opt[0]){
826 valid=true;
827 break;
828 }
829 }
830 if(!valid)
831 continue;
832 for(size_t refband=0;refband<referenceReader.size();++refband){
833 bool notFound=true;
834 for(size_t iclass=0;iclass<class_opt.size();++iclass){
835 if(refpixel[refband]==class_opt[iclass]){
836 ++(posclass[2+iclass]);
837 notFound=false;
838 break;
839 }
840 }
841 if(notFound){
842 if(class_opt.size()<2)
843 ++(posclass[2+1]);
844 }
845 }
846 umap[pixel].push_back(posclass);
847 }
848 }
849 if(verbose_opt[0]>=1)
850 std::cout << "write to output" << std::endl;
851 std::ostringstream outputStream;
852 boost::archive::text_oarchive oarch(outputStream);
853 /* std::ofstream ofs(model_opt[0], std::ios::binary); */
854 /* boost::archive::binary_oarchive oarch(ofs); */
855 oarch << umap;
856
857 if(verbose_opt[0]){
858 std::cout << "umap.size(): " << umap.size() << std::endl;
859 auto umapit = umap.begin();
860 for(auto tupit = umapit->second.begin(); tupit != umapit->second.end(); ++tupit){
861 for(size_t iclass=0;iclass<nclass;++iclass){
862 double value=(*tupit)[2+iclass];
863 std::cout << value << " ";
864 }
865 std::cout << std::endl;
866 }
867 }
868 return(outputStream.str());
869 }
870 catch(std::string errorString){
871 std::cerr << errorString << std::endl;
872 throw;
873 }
874 }
875 ///classify raster dataset with SML
876 /* template<typename T> void Jim::classifySML(Jim& imgWriter, app::AppFactory& app){ */
877 /* Optionjl<std::string> model_opt("model", "model", "Model filename to save trained classifier."); */
878 /* Optionjl<unsigned int> band_opt("b", "band", "Band index (starting from 0, either use band option or use start to end)"); */
879 /* Optionjl<unsigned int> bstart_opt("sband", "startband", "Start band sequence number"); */
880 /* Optionjl<unsigned int> bend_opt("eband", "endband", "End band sequence number"); */
881 /* Optionjl<unsigned short> class_opt("c", "class", "Class(es) to extract from reference. Leave empty to extract two classes only: 1 against rest",1); */
882 /* Optionjl<std::string> extent_opt("e", "extent", "Only classify within extent from polygons in vector file"); */
883 /* Optionjl<std::string> eoption_opt("eo","eo", "special extent options controlling rasterization: ATTRIBUTE|CHUNKYSIZE|ALL_TOUCHED|BURN_VALUE_FROM|MERGE_ALG, e.g., -eo ATTRIBUTE=fieldname"); */
884 /* Optionjl<std::string> mask_opt("m", "mask", "Only classify within specified mask. For raster mask, set nodata values with the option msknodata."); */
885 /* Optionjl<short> msknodata_opt("msknodata", "msknodata", "Mask value(s) not to consider for classification. Values will be taken over in classification image.", 0); */
886 /* Optionjl<double> srcnodata_opt("srcnodata", "srcnodata", "Nodata value in source",0); */
887 /* Optionjl<double> dstnodata_opt("dstnodata", "dstnodata", "Nodata value to put where image is masked as nodata", 0); */
888 /* Optionjl<short> verbose_opt("v", "verbose", "Verbose level",0,2); */
889
890 /* extent_opt.setHide(1); */
891 /* eoption_opt.setHide(1); */
892
893 /* bool doProcess;//stop process when program was invoked with help option (-h --help) */
894 /* try{ */
895 /* doProcess=model_opt.retrieveOption(app); */
896 /* band_opt.retrieveOption(app); */
897 /* bstart_opt.retrieveOption(app); */
898 /* bend_opt.retrieveOption(app); */
899 /* class_opt.retrieveOption(app); */
900 /* model_opt.retrieveOption(app); */
901 /* extent_opt.retrieveOption(app); */
902 /* eoption_opt.retrieveOption(app); */
903 /* mask_opt.retrieveOption(app); */
904 /* msknodata_opt.retrieveOption(app); */
905 /* srcnodata_opt.retrieveOption(app); */
906 /* dstnodata_opt.retrieveOption(app); */
907 /* verbose_opt.retrieveOption(app); */
908
909 /* if(!doProcess){ */
910 /* std::cout << std::endl; */
911 /* std::ostringstream helpStream; */
912 /* helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; */
913 /* throw(helpStream.str());//help was invoked, stop processing */
914 /* } */
915
916 /* if(model_opt.empty()){ */
917 /* std::string errorString="Error: model option to write model not set"; */
918 /* throw(errorString); */
919 /* } */
920
921 /* VectorOgr extentReader; */
922 /* Jim maskReader; */
923
924 /* double ulx=0; */
925 /* double uly=0; */
926 /* double lrx=0; */
927 /* double lry=0; */
928
929
930 /* if(verbose_opt[0]>=1) */
931 /* std::cout << "start SML" << std::endl; */
932
933 /* if(extent_opt.size()){ */
934 /* if(verbose_opt[0]>=1) */
935 /* std::cout << "We are in extent" << std::endl; */
936 /* if(mask_opt.size()){ */
937 /* std::string errorString="Error: can only either mask or extent, not both"; */
938 /* throw(errorString); */
939 /* } */
940 /* extentReader.open(extent_opt[0]); */
941 /* extentReader.getExtent(ulx,uly,lrx,lry); */
942 /* maskReader.open(this->nrOfCol(),this->nrOfRow(),1,GDT_Float64); */
943 /* double gt[6]; */
944 /* this->getGeoTransform(gt); */
945 /* maskReader.setGeoTransform(gt); */
946 /* maskReader.setProjection(this->getProjection()); */
947 /* maskReader.rasterizeBuf(extentReader,dstnodata_opt[0],eoption_opt); */
948 /* maskReader.GDALSetNoDataValue(dstnodata_opt[0]); */
949 /* extentReader.close(); */
950 /* } */
951
952 /* if(verbose_opt[0]>=1) */
953 /* std::cout << "configure band options" << std::endl; */
954
955 /* //convert start and end band options to vector of band indexes */
956 /* if(bstart_opt.size()){ */
957 /* if(bend_opt.size()!=bstart_opt.size()){ */
958 /* std::string errorstring="Error: options for start and end band indexes must be provided as pairs, missing end band"; */
959 /* throw(errorstring); */
960 /* } */
961 /* band_opt.clear(); */
962 /* for(unsigned int ipair=0;ipair<bstart_opt.size();++ipair){ */
963 /* if(bend_opt[ipair]<=bstart_opt[ipair]){ */
964 /* std::string errorstring="Error: index for end band must be smaller then start band"; */
965 /* throw(errorstring); */
966 /* } */
967 /* for(unsigned int iband=bstart_opt[ipair];iband<=bend_opt[ipair];++iband) */
968 /* band_opt.push_back(iband); */
969 /* } */
970 /* } */
971
972
973 /* //sort bands */
974 /* if(band_opt.size()) */
975 /* std::sort(band_opt.begin(),band_opt.end()); */
976 /* else{ */
977 /* unsigned int iband=0; */
978 /* while(band_opt.size()<nrOfBand()) */
979 /* band_opt.push_back(iband++); */
980 /* } */
981
982 /* //umap: [unique band information]->[x,y,class1,class2,...] */
983 /* std::map<std::vector<T>,std::vector<std::vector<unsigned int> > > umap; */
984 /* std::vector<short> lineMask; */
985
986 /* std::ifstream ifs(model_opt[0]); */
987 /* umap.clear(); */
988 /* boost::archive::text_iarchive iarch(ifs); */
989 /* iarch >> umap; */
990
991 /* int nclass=0; */
992 /* auto umapit = umap.begin(); */
993 /* for(auto tupit = umapit->second.begin(); tupit != umapit->second.end(); ++tupit){ */
994 /* nclass=tupit->size()-2; */
995 /* if(verbose_opt[0]) */
996 /* std::cout << "number of classes: " << nclass; */
997 /* for(size_t iclass=0;iclass<nclass;++iclass){ */
998 /* double value=(*tupit)[2+iclass]; */
999 /* if(verbose_opt[0]) */
1000 /* std::cout << value << " "; */
1001 /* } */
1002 /* if(verbose_opt[0]) */
1003 /* std::cout << std::endl; */
1004 /* } */
1005
1006 /* int nrow=nrOfRow(); */
1007 /* int ncol=nrOfCol(); */
1008 /* if(this->isInit()){ */
1009 /* if(verbose_opt[0]>=1) */
1010 /* std::cout << "We are in initialize" << std::endl; */
1011 /* imgWriter.open(ncol,nrow,nclass,GDT_Byte); */
1012 /* imgWriter.GDALSetNoDataValue(dstnodata_opt[0]); */
1013 /* imgWriter.setNoData(dstnodata_opt); */
1014 /* imgWriter.copyGeoTransform(*this); */
1015 /* imgWriter.setProjection(this->getProjection()); */
1016 /* } */
1017 /* //initialize imgWriter with dstnodata_opt[0] */
1018 /* std::vector<unsigned short> vnodata(imgWriter.nrOfCol()); */
1019 /* for(size_t icol=0;icol<imgWriter.nrOfCol();++icol) */
1020 /* vnodata[icol]=static_cast<unsigned short>(dstnodata_opt[0]); */
1021 /* for(size_t iband=0;iband<imgWriter.nrOfBand();++iband){ */
1022 /* for(size_t irow=0;irow<imgWriter.nrOfRow();++irow) */
1023 /* imgWriter.writeData(vnodata,irow,iband); */
1024 /* } */
1025
1026 /* if(mask_opt.size()){ */
1027 /* if(verbose_opt[0]>=1) */
1028 /* std::cout << "opening mask image file " << mask_opt[0] << std::endl; */
1029 /* maskReader.open(mask_opt[0]); */
1030 /* } */
1031
1032 /* double oldRowMask=-1;//keep track of row mask to optimize number of line readings */
1033 /* for(int y=0;y<nrOfRow();++y){ */
1034 /* Vector2d<T> lineInput(band_opt.size(),nrOfCol()); */
1035 /* for(unsigned int iband=0;iband<band_opt.size();++iband) */
1036 /* readData(lineInput[iband],y,band_opt[iband]); */
1037 /* for(int x=0;x<this->nrOfCol();++x){ */
1038 /* bool doClassify=true; */
1039 /* double geox=0; */
1040 /* double geoy=0; */
1041 /* imgWriter.image2geo(x,y,geox,geoy); */
1042 /* if(maskReader.isInit()){ */
1043 /* bool masked=false; */
1044 /* //read mask */
1045 /* double colMask=0; */
1046 /* double rowMask=0; */
1047 /* maskReader.geo2image(geox,geoy,colMask,rowMask); */
1048 /* colMask=static_cast<int>(colMask); */
1049 /* rowMask=static_cast<int>(rowMask); */
1050 /* if(rowMask>=0&&rowMask<maskReader.nrOfRow()&&colMask>=0&&colMask<maskReader.nrOfCol()){ */
1051 /* if(static_cast<int>(rowMask)!=static_cast<int>(oldRowMask)){ */
1052 /* maskReader.readData(lineMask,static_cast<unsigned int>(rowMask)); */
1053 /* oldRowMask=rowMask; */
1054 /* } */
1055 /* short theMask=0; */
1056 /* for(short ivalue=0;ivalue<msknodata_opt.size();++ivalue){ */
1057 /* if(lineMask[colMask]==msknodata_opt[ivalue]){ */
1058 /* theMask=lineMask[colMask]; */
1059 /* masked=true; */
1060 /* break; */
1061 /* } */
1062 /* } */
1063 /* if(masked){ */
1064 /* double prob=dstnodata_opt[0]; */
1065 /* for(unsigned int iclass=0;iclass<nclass;++iclass) */
1066 /* imgWriter.writeData(static_cast<unsigned short>(prob),x,y,iclass); */
1067 /* continue; */
1068 /* } */
1069 /* } */
1070 /* } */
1071 /* } */
1072 /* } */
1073 /* //loop through umap and assign pixel values based on occurrence */
1074 /* for(auto mapit = umap.begin(); mapit != umap.end(); ++mapit){ */
1075 /* std::vector<double> fclass(nclass); */
1076 /* double maxValue=0; */
1077 /* for(auto tupit = mapit->second.begin(); tupit != mapit->second.end(); ++tupit){ */
1078 /* for(size_t iclass=0;iclass<nclass;++iclass){ */
1079 /* double value=(*tupit)[2+iclass]; */
1080 /* fclass[iclass]+=value; */
1081 /* if(fclass[iclass]>maxValue) */
1082 /* maxValue=fclass[iclass]; */
1083 /* } */
1084 /* } */
1085 /* double scale=255.0/maxValue; */
1086 /* for(size_t iclass=0;iclass<nclass;++iclass) */
1087 /* fclass[iclass]*=scale; */
1088 /* for(auto tupit = mapit->second.begin(); tupit != mapit->second.end(); ++tupit){ */
1089 /* for(unsigned int iclass=0;iclass<nclass;++iclass) */
1090 /* imgWriter.writeData(static_cast<unsigned short>(fclass[iclass]),(*tupit)[0],(*tupit)[1],iclass); */
1091 /* } */
1092 /* } */
1093 /* if(mask_opt.size()) */
1094 /* maskReader.close(); */
1095 /* } */
1096 /* catch(std::string errorString){ */
1097 /* std::cerr << errorString << std::endl; */
1098 /* throw; */
1099 /* } */
1100 /* } */
1101
1102 /* ///classify raster dataset with SML */
1103 /* template<typename T> CPLErr Jim::classifySML(JimList& referenceReader, Jim& imgWriter, app::AppFactory& app){ */
1104 /* Optionjl<unsigned int> band_opt("b", "band", "Band index (starting from 0, either use band option or use start to end)"); */
1105 /* Optionjl<unsigned int> bstart_opt("sband", "startband", "Start band sequence number"); */
1106 /* Optionjl<unsigned int> bend_opt("eband", "endband", "End band sequence number"); */
1107 /* Optionjl<std::string> model_opt("model", "model", "Model filename to save trained classifier."); */
1108 /* Optionjl<unsigned short> class_opt("c", "class", "Class(es) to extract from reference. Leave empty to extract two classes only: 1 against rest",1); */
1109 /* Optionjl<std::string> extent_opt("e", "extent", "Only classify within extent from polygons in vector file"); */
1110 /* Optionjl<std::string> eoption_opt("eo","eo", "special extent options controlling rasterization: ATTRIBUTE|CHUNKYSIZE|ALL_TOUCHED|BURN_VALUE_FROM|MERGE_ALG, e.g., -eo ATTRIBUTE=fieldname"); */
1111 /* Optionjl<std::string> mask_opt("m", "mask", "Only classify within specified mask. For raster mask, set nodata values with the option msknodata."); */
1112 /* Optionjl<short> msknodata_opt("msknodata", "msknodata", "Mask value(s) not to consider for classification. Values will be taken over in classification image.", 0); */
1113 /* Optionjl<double> srcnodata_opt("srcnodata", "srcnodata", "Nodata value in source",0); */
1114 /* Optionjl<double> dstnodata_opt("dstnodata", "dstnodata", "Nodata value to put where image is masked as nodata", 0); */
1115 /* /\* Optionjl<double> nodata_opt("nodata", "nodata", "Nodata value to put where image is masked as nodata", 0); *\/ */
1116 /* Optionjl<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","GDT_Byte"); */
1117 /* Optionjl<short> verbose_opt("v", "verbose", "Verbose level",0,2); */
1118
1119 /* extent_opt.setHide(1); */
1120 /* eoption_opt.setHide(1); */
1121
1122 /* bool doProcess;//stop process when program was invoked with help option (-h --help) */
1123 /* try{ */
1124 /* doProcess=band_opt.retrieveOption(app); */
1125 /* bstart_opt.retrieveOption(app); */
1126 /* bend_opt.retrieveOption(app); */
1127 /* class_opt.retrieveOption(app); */
1128 /* model_opt.retrieveOption(app); */
1129 /* extent_opt.retrieveOption(app); */
1130 /* eoption_opt.retrieveOption(app); */
1131 /* mask_opt.retrieveOption(app); */
1132 /* msknodata_opt.retrieveOption(app); */
1133 /* srcnodata_opt.retrieveOption(app); */
1134 /* dstnodata_opt.retrieveOption(app); */
1135 /* otype_opt.retrieveOption(app); */
1136 /* verbose_opt.retrieveOption(app); */
1137 /* // memory_opt.retrieveOption(app); */
1138
1139 /* if(!doProcess){ */
1140 /* std::cout << std::endl; */
1141 /* std::ostringstream helpStream; */
1142 /* helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; */
1143 /* throw(helpStream.str());//help was invoked, stop processing */
1144 /* } */
1145
1146 /* VectorOgr extentReader; */
1147 /* Jim maskReader; */
1148
1149 /* double ulx=0; */
1150 /* double uly=0; */
1151 /* double lrx=0; */
1152 /* double lry=0; */
1153
1154
1155 /* if(verbose_opt[0]>=1) */
1156 /* std::cout << "start SML" << std::endl; */
1157
1158 /* if(extent_opt.size()){ */
1159 /* if(verbose_opt[0]>=1) */
1160 /* std::cout << "We are in extent" << std::endl; */
1161 /* if(mask_opt.size()){ */
1162 /* std::string errorString="Error: can only either mask or extent, not both"; */
1163 /* throw(errorString); */
1164 /* } */
1165 /* extentReader.open(extent_opt[0]); */
1166 /* // readLayer = extentReader.getDataSource()->GetLayer(0); */
1167 /* extentReader.getExtent(ulx,uly,lrx,lry); */
1168 /* maskReader.open(this->nrOfCol(),this->nrOfRow(),1,GDT_Float64); */
1169 /* double gt[6]; */
1170 /* this->getGeoTransform(gt); */
1171 /* maskReader.setGeoTransform(gt); */
1172 /* maskReader.setProjection(this->getProjection()); */
1173 /* // vector<double> burnValues(1,1);//burn value is 1 (single band) */
1174 /* // maskReader.rasterizeBuf(extentReader); */
1175 /* maskReader.rasterizeBuf(extentReader,dstnodata_opt[0],eoption_opt); */
1176 /* maskReader.GDALSetNoDataValue(dstnodata_opt[0]); */
1177 /* extentReader.close(); */
1178 /* } */
1179
1180 /* if(verbose_opt[0]>=1) */
1181 /* std::cout << "configure band options" << std::endl; */
1182
1183 /* //convert start and end band options to vector of band indexes */
1184 /* if(bstart_opt.size()){ */
1185 /* if(bend_opt.size()!=bstart_opt.size()){ */
1186 /* std::string errorstring="Error: options for start and end band indexes must be provided as pairs, missing end band"; */
1187 /* throw(errorstring); */
1188 /* } */
1189 /* band_opt.clear(); */
1190 /* for(unsigned int ipair=0;ipair<bstart_opt.size();++ipair){ */
1191 /* if(bend_opt[ipair]<=bstart_opt[ipair]){ */
1192 /* std::string errorstring="Error: index for end band must be smaller then start band"; */
1193 /* throw(errorstring); */
1194 /* } */
1195 /* for(unsigned int iband=bstart_opt[ipair];iband<=bend_opt[ipair];++iband) */
1196 /* band_opt.push_back(iband); */
1197 /* } */
1198 /* } */
1199
1200
1201 /* //sort bands */
1202 /* if(band_opt.size()) */
1203 /* std::sort(band_opt.begin(),band_opt.end()); */
1204 /* else{ */
1205 /* unsigned int iband=0; */
1206 /* while(band_opt.size()<nrOfBand()) */
1207 /* band_opt.push_back(iband++); */
1208 /* } */
1209
1210 /* int nrow=nrOfRow(); */
1211 /* int ncol=nrOfCol(); */
1212 /* int nclass=class_opt.size()>1?class_opt.size():2; */
1213 /* if(this->isInit()){ */
1214 /* if(verbose_opt[0]>=1) */
1215 /* std::cout << "We are in initialize" << std::endl; */
1216 /* GDALDataType theType=string2GDAL(otype_opt[0]); */
1217 /* if(theType==GDT_Unknown) */
1218 /* std::cout << "Warning: unknown output pixel type: " << otype_opt[0] << ", using input type as default" << std::endl; */
1219 /* imgWriter.open(ncol,nrow,nclass,GDT_Byte); */
1220 /* imgWriter.GDALSetNoDataValue(dstnodata_opt[0]); */
1221 /* imgWriter.setNoData(dstnodata_opt); */
1222 /* imgWriter.copyGeoTransform(*this); */
1223 /* imgWriter.setProjection(this->getProjection()); */
1224 /* // if(colorTable_opt.size()) */
1225 /* // imgWriter.setColorTable(colorTable_opt[0],0); */
1226 /* } */
1227 /* //initialize imgWriter with dstnodata_opt[0] */
1228 /* std::vector<unsigned short> vnodata(imgWriter.nrOfCol()); */
1229 /* for(size_t icol=0;icol<imgWriter.nrOfCol();++icol) */
1230 /* vnodata[icol]=static_cast<unsigned short>(dstnodata_opt[0]); */
1231 /* for(size_t iband=0;iband<imgWriter.nrOfBand();++iband){ */
1232 /* for(size_t irow=0;irow<imgWriter.nrOfRow();++irow) */
1233 /* imgWriter.writeData(vnodata,irow,iband); */
1234 /* } */
1235
1236 /* if(mask_opt.size()){ */
1237 /* if(verbose_opt[0]>=1) */
1238 /* std::cout << "opening mask image file " << mask_opt[0] << std::endl; */
1239 /* maskReader.open(mask_opt[0]); */
1240 /* } */
1241
1242 /* //umap: [unique band information]->[x,y,class1,class2,...] */
1243 /* std::map<std::vector<T>,std::vector<std::vector<unsigned int> > > umap; */
1244 /* std::vector<short> lineMask; */
1245 /* std::vector<T> pixel(band_opt.size());//pixel with band information */
1246 /* std::vector<T> refpixel(referenceReader.size());//pixel with reference information */
1247
1248 /* OGRSpatialReference thisSRS(getProjectionRef().c_str()); */
1249 /* OGRSpatialReference *thisSpatialRef=&thisSRS; */
1250 /* //currently only a single SRS for all images in reference collection supported */
1251 /* OGRSpatialReference referenceSRS(referenceReader.getImage(0)->getProjectionRef().c_str()); */
1252
1253 /* OGRSpatialReference *referenceSpatialRef=&referenceSRS; */
1254 /* OGRCoordinateTransformation *img2ref=OGRCreateCoordinateTransformation(thisSpatialRef, referenceSpatialRef); */
1255
1256 /* double oldRowMask=-1;//keep track of row mask to optimize number of line readings */
1257 /* std::vector<double> oldRowReference(referenceReader.size()); */
1258 /* for(int ireference=0;ireference<referenceReader.size();++ireference) */
1259 /* oldRowReference[ireference]=-1; */
1260 /* for(int y=0;y<nrOfRow();++y){ */
1261 /* Vector2d<T> lineInput(band_opt.size(),nrOfCol()); */
1262 /* for(unsigned int iband=0;iband<band_opt.size();++iband) */
1263 /* readData(lineInput[iband],y,band_opt[iband]); */
1264 /* /\* Vector2d<T> classInput(referenceReader.size(),nrOfCol()); *\/ */
1265 /* Vector2d<T> classInput(referenceReader.size()); */
1266 /* /\* for(unsigned int iband=0;iband<cband_opt.size();++iband) *\/ */
1267 /* /\* readData(classInput[iband],y,cband_opt[iband]); *\/ */
1268 /* for(int x=0;x<this->nrOfCol();++x){ */
1269 /* std::vector<unsigned int> posclass(2+nclass); */
1270 /* posclass[0]=static_cast<unsigned int>(x); */
1271 /* posclass[1]=static_cast<unsigned int>(y); */
1272 /* bool doClassify=true; */
1273 /* double geox=0; */
1274 /* double geoy=0; */
1275 /* imgWriter.image2geo(x,y,geox,geoy); */
1276 /* if(maskReader.isInit()){ */
1277 /* bool masked=false; */
1278 /* //read mask */
1279 /* double colMask=0; */
1280 /* double rowMask=0; */
1281 /* maskReader.geo2image(geox,geoy,colMask,rowMask); */
1282 /* colMask=static_cast<int>(colMask); */
1283 /* rowMask=static_cast<int>(rowMask); */
1284 /* if(rowMask>=0&&rowMask<maskReader.nrOfRow()&&colMask>=0&&colMask<maskReader.nrOfCol()){ */
1285 /* if(static_cast<int>(rowMask)!=static_cast<int>(oldRowMask)){ */
1286 /* maskReader.readData(lineMask,static_cast<unsigned int>(rowMask)); */
1287 /* oldRowMask=rowMask; */
1288 /* } */
1289 /* short theMask=0; */
1290 /* for(short ivalue=0;ivalue<msknodata_opt.size();++ivalue){ */
1291 /* if(lineMask[colMask]==msknodata_opt[ivalue]){ */
1292 /* theMask=lineMask[colMask]; */
1293 /* masked=true; */
1294 /* break; */
1295 /* } */
1296 /* } */
1297 /* if(masked){ */
1298 /* double prob=dstnodata_opt[0]; */
1299 /* for(unsigned int iclass=0;iclass<nclass;++iclass) */
1300 /* imgWriter.writeData(static_cast<unsigned short>(prob),x,y,iclass); */
1301 /* continue; */
1302 /* } */
1303 /* } */
1304 /* } */
1305
1306 /* //read reference */
1307 /* for(size_t iref=0;iref<referenceReader.size();++iref){ */
1308 /* double colReference=0; */
1309 /* double rowReference=0; */
1310 /* referenceReader.getImage(iref)->geo2image(geox,geoy,colReference,rowReference,img2ref); */
1311 /* rowReference=static_cast<int>(rowReference); */
1312 /* colReference=static_cast<int>(colReference); */
1313 /* if(rowReference>=0&&rowReference<referenceReader.getImage(iref)->nrOfRow()&&colReference>=0&&colReference<referenceReader.getImage(iref)->nrOfCol()){ */
1314 /* if(static_cast<int>(rowReference)!=static_cast<int>(oldRowReference[iref])){ */
1315 /* referenceReader.getImage(iref)->readData(classInput[iref],static_cast<unsigned int>(rowReference)); */
1316 /* oldRowReference[iref]=rowReference; */
1317 /* } */
1318 /* refpixel[iref]=classInput[iref][colReference]; */
1319 /* } */
1320 /* else */
1321 /* continue; */
1322 /* } */
1323
1324 /* pixel=lineInput.selectCol(x); */
1325 /* bool valid=false; */
1326 /* for(size_t iband=0;iband<pixel.size();++iband){ */
1327 /* if(pixel[iband]!=srcnodata_opt[0]){ */
1328 /* valid=true; */
1329 /* break; */
1330 /* } */
1331 /* } */
1332 /* if(!valid) */
1333 /* continue; */
1334 /* for(size_t refband=0;refband<referenceReader.size();++refband){ */
1335 /* bool notFound=true; */
1336 /* for(size_t iclass=0;iclass<class_opt.size();++iclass){ */
1337 /* if(refpixel[refband]==class_opt[iclass]){ */
1338 /* ++(posclass[2+iclass]); */
1339 /* notFound=false; */
1340 /* break; */
1341 /* } */
1342 /* } */
1343 /* if(notFound){ */
1344 /* if(class_opt.size()<2) */
1345 /* ++(posclass[2+1]); */
1346 /* } */
1347 /* } */
1348 /* umap[pixel].push_back(posclass); */
1349 /* } */
1350 /* } */
1351 /* double absMaxValue=0; */
1352
1353 /* /\*todo: not tested yet... *\/ */
1354 /* if(model_opt.size()){ */
1355 /* std::ofstream ofs(model_opt[0], std::ios::binary); */
1356 /* /\* std::ofstream ofs(model_opt[0]); *\/ */
1357 /* boost::archive::binary_oarchive oarch(ofs); */
1358 /* /\* boost::archive::text_oarchive oarch(ofs); *\/ */
1359 /* oarch << umap; */
1360
1361 /* if(verbose_opt[0]){ */
1362 /* std::cout << "umap.size(): " << umap.size() << std::endl; */
1363 /* auto umapit = umap.begin(); */
1364 /* for(auto tupit = umapit->second.begin(); tupit != umapit->second.end(); ++tupit){ */
1365 /* for(size_t iclass=0;iclass<nclass;++iclass){ */
1366 /* double value=(*tupit)[2+iclass]; */
1367 /* std::cout << value << " "; */
1368 /* } */
1369 /* std::cout << std::endl; */
1370 /* } */
1371 /* } */
1372 /* } */
1373 /* if(model_opt.size()){ */
1374 /* //input stream must be in new scope in order output stream to be closed correctly */
1375 /* /\* std::map<std::vector<T>,std::vector<std::vector<unsigned int> > > newmap; *\/ */
1376 /* std::ifstream ifs(model_opt[0], std::ios::binary); */
1377 /* /\* std::ifstream ifs(model_opt[0]); *\/ */
1378 /* umap.clear(); */
1379 /* boost::archive::binary_iarchive iarch(ifs); */
1380 /* /\* boost::archive::text_iarchive iarch(ifs); *\/ */
1381 /* iarch >> umap; */
1382 /* /\* iarch >> newmap; *\/ */
1383
1384 /* if(verbose_opt[0]){ */
1385 /* /\* std::cout << "newmap.size(): " << newmap.size() << std::endl; *\/ */
1386 /* auto umapit = umap.begin(); */
1387 /* /\* for(auto tupit = newmapit->second.begin(); tupit != newmapit->second.end(); ++tupit){ *\/ */
1388 /* for(auto tupit = umapit->second.begin(); tupit != umapit->second.end(); ++tupit){ */
1389 /* for(size_t iclass=0;iclass<nclass;++iclass){ */
1390 /* double value=(*tupit)[2+iclass]; */
1391 /* std::cout << value << " "; */
1392 /* } */
1393 /* std::cout << std::endl; */
1394 /* } */
1395 /* } */
1396 /* } */
1397 /* //loop through umap and assign pixel values based on occurrence */
1398 /* for(auto mapit = umap.begin(); mapit != umap.end(); ++mapit){ */
1399 /* std::vector<double> fclass(nclass); */
1400 /* double maxValue=0; */
1401 /* for(auto tupit = mapit->second.begin(); tupit != mapit->second.end(); ++tupit){ */
1402 /* for(size_t iclass=0;iclass<nclass;++iclass){ */
1403 /* double value=(*tupit)[2+iclass]; */
1404 /* fclass[iclass]+=value; */
1405 /* if(fclass[iclass]>maxValue) */
1406 /* maxValue=fclass[iclass]; */
1407 /* } */
1408 /* } */
1409 /* double scale=255.0/maxValue; */
1410 /* for(size_t iclass=0;iclass<nclass;++iclass) */
1411 /* fclass[iclass]*=scale; */
1412 /* for(auto tupit = mapit->second.begin(); tupit != mapit->second.end(); ++tupit){ */
1413 /* for(unsigned int iclass=0;iclass<nclass;++iclass) */
1414 /* imgWriter.writeData(static_cast<unsigned short>(fclass[iclass]),(*tupit)[0],(*tupit)[1],iclass); */
1415 /* if(mask_opt.size()) */
1416 /* maskReader.close(); */
1417 /* } */
1418 /* } */
1419 /* } */
1420 /* catch(std::string errorString){ */
1421 /* std::cerr << errorString << std::endl; */
1422 /* return(CE_Failure); */
1423 /* } */
1424 /* } */
1425 ///classify raster dataset with SML
1426 ///this Jim contains input bands and reference with binary class information)
1427 /**
1428 * @param imgWriter output classified raster dataset
1429 * @param app application specific option arguments
1430 * @return CE_None if successful, CE_Failure if failed
1431 **/
1432 /* template<typename T> CPLErr Jim::classifySML(Jim& imgWriter, app::AppFactory& app){ */
1433 /* Optionjl<unsigned int> band_opt("b", "band", "Band index (starting from 0, either use band option or use start to end)"); */
1434 /* Optionjl<unsigned int> bstart_opt("sband", "startband", "Start band sequence number"); */
1435 /* Optionjl<unsigned int> bend_opt("eband", "endband", "End band sequence number"); */
1436 /* Optionjl<unsigned int> cband_opt("cb", "cband", "band index(es) used for the reference class information", 0); */
1437 /* Optionjl<unsigned short> class_opt("c", "class", "Class(es) to extract from reference. Leave empty to extract two classes only: 1 against rest",1); */
1438 /* Optionjl<std::string> extent_opt("e", "extent", "Only classify within extent from polygons in vector file"); */
1439 /* Optionjl<std::string> eoption_opt("eo","eo", "special extent options controlling rasterization: ATTRIBUTE|CHUNKYSIZE|ALL_TOUCHED|BURN_VALUE_FROM|MERGE_ALG, e.g., -eo ATTRIBUTE=fieldname"); */
1440 /* Optionjl<std::string> mask_opt("m", "mask", "Only classify within specified mask. For raster mask, set nodata values with the option msknodata."); */
1441 /* Optionjl<short> msknodata_opt("msknodata", "msknodata", "Mask value(s) not to consider for classification. Values will be taken over in classification image.", 0); */
1442 /* Optionjl<double> nodata_opt("nodata", "nodata", "Nodata value to put where image is masked as nodata", 0); */
1443 /* Optionjl<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","GDT_Byte"); */
1444 /* Optionjl<short> verbose_opt("v", "verbose", "Verbose level",0,2); */
1445
1446 /* extent_opt.setHide(1); */
1447 /* eoption_opt.setHide(1); */
1448
1449 /* bool doProcess;//stop process when program was invoked with help option (-h --help) */
1450 /* try{ */
1451 /* doProcess=band_opt.retrieveOption(app); */
1452 /* bstart_opt.retrieveOption(app); */
1453 /* bend_opt.retrieveOption(app); */
1454 /* cband_opt.retrieveOption(app); */
1455 /* class_opt.retrieveOption(app); */
1456 /* extent_opt.retrieveOption(app); */
1457 /* eoption_opt.retrieveOption(app); */
1458 /* mask_opt.retrieveOption(app); */
1459 /* msknodata_opt.retrieveOption(app); */
1460 /* nodata_opt.retrieveOption(app); */
1461 /* otype_opt.retrieveOption(app); */
1462 /* verbose_opt.retrieveOption(app); */
1463 /* // memory_opt.retrieveOption(app); */
1464
1465 /* if(!doProcess){ */
1466 /* std::cout << std::endl; */
1467 /* std::ostringstream helpStream; */
1468 /* helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; */
1469 /* throw(helpStream.str());//help was invoked, stop processing */
1470 /* } */
1471
1472 /* VectorOgr extentReader; */
1473 /* Jim maskReader; */
1474
1475 /* double ulx=0; */
1476 /* double uly=0; */
1477 /* double lrx=0; */
1478 /* double lry=0; */
1479
1480 /* if(extent_opt.size()){ */
1481 /* if(mask_opt.size()){ */
1482 /* std::string errorString="Error: can only either mask or extent, not both"; */
1483 /* throw(errorString); */
1484 /* } */
1485 /* extentReader.open(extent_opt[0]); */
1486 /* // readLayer = extentReader.getDataSource()->GetLayer(0); */
1487 /* extentReader.getExtent(ulx,uly,lrx,lry); */
1488 /* maskReader.open(this->nrOfCol(),this->nrOfRow(),1,GDT_Float64); */
1489 /* double gt[6]; */
1490 /* this->getGeoTransform(gt); */
1491 /* maskReader.setGeoTransform(gt); */
1492 /* maskReader.setProjection(this->getProjection()); */
1493 /* // vector<double> burnValues(1,1);//burn value is 1 (single band) */
1494 /* // maskReader.rasterizeBuf(extentReader); */
1495 /* maskReader.rasterizeBuf(extentReader,nodata_opt[0],eoption_opt); */
1496 /* maskReader.GDALSetNoDataValue(nodata_opt[0]); */
1497 /* extentReader.close(); */
1498 /* } */
1499
1500 /* //convert start and end band options to vector of band indexes */
1501 /* if(bstart_opt.size()){ */
1502 /* if(bend_opt.size()!=bstart_opt.size()){ */
1503 /* std::string errorstring="Error: options for start and end band indexes must be provided as pairs, missing end band"; */
1504 /* throw(errorstring); */
1505 /* } */
1506 /* band_opt.clear(); */
1507 /* for(unsigned int ipair=0;ipair<bstart_opt.size();++ipair){ */
1508 /* if(bend_opt[ipair]<=bstart_opt[ipair]){ */
1509 /* std::string errorstring="Error: index for end band must be smaller then start band"; */
1510 /* throw(errorstring); */
1511 /* } */
1512 /* for(unsigned int iband=bstart_opt[ipair];iband<=bend_opt[ipair];++iband) */
1513 /* band_opt.push_back(iband); */
1514 /* } */
1515 /* } */
1516
1517 /* //sort bands */
1518 /* if(band_opt.size()) */
1519 /* std::sort(band_opt.begin(),band_opt.end()); */
1520 /* if(cband_opt.size()) */
1521 /* std::sort(cband_opt.begin(),cband_opt.end()); */
1522
1523 /* int nrow=nrOfRow(); */
1524 /* int ncol=nrOfCol(); */
1525 /* int nclass=class_opt.size()>1?class_opt.size():2; */
1526 /* if(this->isInit()){ */
1527 /* GDALDataType theType=string2GDAL(otype_opt[0]); */
1528 /* if(theType==GDT_Unknown) */
1529 /* std::cout << "Warning: unknown output pixel type: " << otype_opt[0] << ", using input type as default" << std::endl; */
1530 /* imgWriter.open(ncol,nrow,nclass,GDT_Byte); */
1531 /* imgWriter.GDALSetNoDataValue(nodata_opt[0]); */
1532 /* imgWriter.setNoData(nodata_opt); */
1533 /* imgWriter.copyGeoTransform(*this); */
1534 /* imgWriter.setProjection(this->getProjection()); */
1535 /* // if(colorTable_opt.size()) */
1536 /* // imgWriter.setColorTable(colorTable_opt[0],0); */
1537 /* } */
1538
1539 /* if(mask_opt.size()){ */
1540 /* if(verbose_opt[0]>=1) */
1541 /* std::cout << "opening mask image file " << mask_opt[0] << std::endl; */
1542 /* maskReader.open(mask_opt[0]); */
1543 /* } */
1544
1545 /* //umap: [unique band information]->[x,y,class1,class2,...] */
1546 /* std::map<std::vector<T>,std::vector<std::vector<unsigned int> > > umap; */
1547 /* std::vector<short> lineMask; */
1548 /* std::vector<T> pixel(band_opt.size());//pixel with band information */
1549 /* std::vector<T> refpixel(cband_opt.size());//pixel with reference information */
1550
1551 /* double oldRowMask=-1;//keep track of row mask to optimize number of line readings */
1552 /* for(int y=0;y<nrOfRow();++y){ */
1553 /* Vector2d<T> lineInput(band_opt.size(),nrOfCol()); */
1554 /* for(unsigned int iband=0;iband<band_opt.size();++iband) */
1555 /* readData(lineInput[iband],y,band_opt[iband]); */
1556 /* /\* Vector2d<T> classInput(cband_opt.size(),nrOfCol()); *\/ */
1557 /* Vector2d<T> classInput(cband_opt.size()); */
1558 /* for(unsigned int iband=0;iband<cband_opt.size();++iband) */
1559 /* readData(classInput[iband],y,cband_opt[iband]); */
1560 /* for(int x=0;x<this->nrOfCol();++x){ */
1561 /* std::vector<unsigned int> posclass(2+nclass); */
1562 /* posclass[0]=static_cast<unsigned int>(x); */
1563 /* posclass[1]=static_cast<unsigned int>(y); */
1564 /* bool doClassify=true; */
1565 /* double geox=0; */
1566 /* double geoy=0; */
1567 /* if(maskReader.isInit()){ */
1568 /* bool masked=false; */
1569 /* //read mask */
1570 /* double colMask=0; */
1571 /* double rowMask=0; */
1572 /* imgWriter.image2geo(x,y,geox,geoy); */
1573 /* maskReader.geo2image(geox,geoy,colMask,rowMask); */
1574 /* colMask=static_cast<int>(colMask); */
1575 /* rowMask=static_cast<int>(rowMask); */
1576 /* if(rowMask>=0&&rowMask<maskReader.nrOfRow()&&colMask>=0&&colMask<maskReader.nrOfCol()){ */
1577 /* if(static_cast<int>(rowMask)!=static_cast<int>(oldRowMask)){ */
1578 /* maskReader.readData(lineMask,static_cast<unsigned int>(rowMask)); */
1579 /* oldRowMask=rowMask; */
1580 /* } */
1581 /* short theMask=0; */
1582 /* for(short ivalue=0;ivalue<msknodata_opt.size();++ivalue){ */
1583 /* if(lineMask[colMask]==msknodata_opt[ivalue]){ */
1584 /* theMask=lineMask[colMask]; */
1585 /* masked=true; */
1586 /* break; */
1587 /* } */
1588 /* } */
1589 /* if(masked){ */
1590 /* double prob=nodata_opt[0]; */
1591 /* for(unsigned int iclass=0;iclass<nclass;++iclass) */
1592 /* imgWriter.writeData(static_cast<unsigned short>(prob),x,y,iclass); */
1593 /* continue; */
1594 /* } */
1595 /* } */
1596 /* } */
1597 /* pixel=lineInput.selectCol(x); */
1598 /* refpixel=classInput.selectCol(x); */
1599 /* for(size_t refband=0;refband<cband_opt.size();++refband){ */
1600 /* bool notFound=true; */
1601 /* for(size_t iclass=0;iclass<class_opt.size();++iclass){ */
1602 /* if(refpixel[refband]==class_opt[iclass]){ */
1603 /* ++(posclass[2+iclass]); */
1604 /* notFound=false; */
1605 /* break; */
1606 /* } */
1607 /* } */
1608 /* if(notFound){ */
1609 /* if(class_opt.size()<2) */
1610 /* ++(posclass[2+1]); */
1611 /* } */
1612 /* } */
1613 /* umap[pixel].push_back(posclass); */
1614 /* } */
1615 /* } */
1616 /* //loop through umap and assign pixel values based on occurrence */
1617 /* for(auto mapit = umap.begin(); mapit != umap.end(); ++mapit){ */
1618 /* std::vector<double> fclass(nclass); */
1619 /* double maxValue=0; */
1620 /* for(auto tupit = mapit->second.begin(); tupit != mapit->second.end(); ++tupit){ */
1621 /* for(size_t iclass=0;iclass<nclass;++iclass){ */
1622 /* double value=(*tupit)[2+iclass]; */
1623 /* fclass[iclass]+=value; */
1624 /* if(fclass[iclass]>maxValue) */
1625 /* maxValue=fclass[iclass]; */
1626 /* } */
1627 /* } */
1628 /* double scale=255.0/maxValue; */
1629 /* for(size_t iclass=0;iclass<nclass;++iclass) */
1630 /* fclass[iclass]*=scale; */
1631 /* for(auto tupit = mapit->second.begin(); tupit != mapit->second.end(); ++tupit){ */
1632 /* for(unsigned int iclass=0;iclass<nclass;++iclass) */
1633 /* imgWriter.writeData(static_cast<unsigned short>(fclass[iclass]),(*tupit)[0],(*tupit)[1],iclass); */
1634 /* if(mask_opt.size()) */
1635 /* maskReader.close(); */
1636 /* } */
1637 /* } */
1638 /* } */
1639 /* catch(std::string errorString){ */
1640 /* std::cerr << errorString << std::endl; */
1641 /* return(CE_Failure); */
1642 /* } */
1643 /* } */
1644
1645 /* template<typename T> std::shared_ptr<Jim> Jim::classifySML(JimList& referenceReader, app::AppFactory& app){ */
1646 /* try{ */
1647 /* std::shared_ptr<Jim> imgWriter=createImg(); */
1648 /* classifySML<T>(referenceReader,*imgWriter, app); */
1649 /* return(imgWriter); */
1650 /* } */
1651 /* catch(std::string helpString){ */
1652 /* std::cerr << helpString << std::endl; */
1653 /* return(0); */
1654 /* } */
1655 /* } */
1656
1657 /**
1658 * @param app application specific option arguments
1659 * @return output classified raster dataset
1660 **/
1661 template<typename T> std::shared_ptr<Jim> Jim::classifySML_t(app::AppFactory& app){
1662 try{
1663 std::shared_ptr<Jim> imgWriter=createImg();
1664 classifySML_t<T>(*imgWriter, app);
1665 return(imgWriter);
1666 }
1667 catch(std::string helpString){
1668 std::cerr << helpString << std::endl;
1669 throw;
1670 }
1671 }