28 #include "imageclasses/ImgReaderGdal.h"
29 #include "imageclasses/ImgWriterOgr.h"
30 #include "base/Optionpk.h"
31 #include "algorithms/StatFactory.h"
34 #define PI 3.1415926535897932384626433832795
38 enum RULE_TYPE {point=0, mean=1, proportion=2, custom=3, min=4, max=5, mode=6, centroid=7, sum=8, median=9, stdev=10, percentile=11};
43 int main(
int argc,
char *argv[])
45 Optionpk<string> image_opt(
"i",
"input",
"Raster input dataset containing band information");
46 Optionpk<string> sample_opt(
"s",
"sample",
"OGR vector dataset with features to be extracted from input data. Output will contain features with input band information included. Sample image can also be GDAL raster dataset.");
47 Optionpk<string> layer_opt(
"ln",
"ln",
"Layer name(s) in sample (leave empty to select all)");
48 Optionpk<unsigned int> random_opt(
"rand",
"random",
"Create simple random sample of points. Provide number of points to generate");
49 Optionpk<double> grid_opt(
"grid",
"grid",
"Create systematic grid of points. Provide cell grid size (in projected units, e.g,. m)");
51 Optionpk<int> class_opt(
"c",
"class",
"Class(es) to extract from input sample image. Leave empty to extract all valid data pixels from sample dataset. Make sure to set classes if rule is set to mode or proportion");
52 Optionpk<float> threshold_opt(
"t",
"threshold",
"Probability threshold for selecting samples (randomly). Provide probability in percentage (>0) or absolute (<0). Use a single threshold for vector sample datasets. 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);
53 Optionpk<double> percentile_opt(
"perc",
"perc",
"Percentile value used for rule percentile",95);
54 Optionpk<string> ogrformat_opt(
"f",
"f",
"Output sample dataset format",
"SQLite");
55 Optionpk<string> ftype_opt(
"ft",
"ftype",
"Field type (only Real or Integer)",
"Real");
56 Optionpk<string> ltype_opt(
"lt",
"ltype",
"Label type: In16 or String",
"Integer");
57 Optionpk<bool> polygon_opt(
"polygon",
"polygon",
"Create OGRPolygon as geometry instead of OGRPoint.",
false);
58 Optionpk<int> band_opt(
"b",
"band",
"Band index(es) to extract (0 based). Leave empty to use all bands");
59 Optionpk<string> rule_opt(
"r",
"rule",
"Rule how to report image information per feature (only for vector sample). point (value at each point or at centroid if polygon), centroid, mean, stdev, median, proportion, min, max, mode, sum, percentile.",
"centroid");
60 Optionpk<double> srcnodata_opt(
"srcnodata",
"srcnodata",
"Invalid value(s) for input image");
61 Optionpk<int> bndnodata_opt(
"bndnodata",
"bndnodata",
"Band(s) in input image to check if pixel is valid (used for srcnodata)", 0);
62 Optionpk<float> polythreshold_opt(
"tp",
"thresholdPolygon",
"(absolute) threshold for selecting samples in each polygon");
63 Optionpk<string> test_opt(
"test",
"test",
"Test sample dataset (use this option in combination with threshold<100 to create a training (output) and test set");
64 Optionpk<string> fieldname_opt(
"bn",
"bname",
"For single band input data, this extra attribute name will correspond to the raster values. For multi-band input data, multiple attributes with this prefix will be added (e.g. b0, b1, b2, etc.)",
"b");
65 Optionpk<string> label_opt(
"cn",
"cname",
"Name of the class label in the output vector dataset",
"label");
66 Optionpk<short> geo_opt(
"geo",
"geo",
"Use geo coordinates (set to 0 to use image coordinates)", 1);
67 Optionpk<short> down_opt(
"down",
"down",
"Down sampling factor (for raster sample datasets only). Can be used to create grid points", 1);
68 Optionpk<short> buffer_opt(
"buf",
"buffer",
"Buffer for calculating statistics for point features ");
69 Optionpk<bool> disc_opt(
"circ",
"circular",
"Use a circular disc kernel buffer (for vector point sample datasets only, use in combination with buffer option)",
false);
70 Optionpk<short> verbose_opt(
"v",
"verbose",
"Verbose mode if > 0", 0,2);
72 bndnodata_opt.setHide(1);
73 srcnodata_opt.setHide(1);
74 polythreshold_opt.setHide(1);
75 percentile_opt.setHide(1);
77 fieldname_opt.setHide(1);
81 buffer_opt.setHide(1);
86 doProcess=image_opt.retrieveOption(argc,argv);
87 sample_opt.retrieveOption(argc,argv);
88 layer_opt.retrieveOption(argc,argv);
89 random_opt.retrieveOption(argc,argv);
90 grid_opt.retrieveOption(argc,argv);
91 output_opt.retrieveOption(argc,argv);
92 class_opt.retrieveOption(argc,argv);
93 threshold_opt.retrieveOption(argc,argv);
94 percentile_opt.retrieveOption(argc,argv);
95 ogrformat_opt.retrieveOption(argc,argv);
96 ftype_opt.retrieveOption(argc,argv);
97 ltype_opt.retrieveOption(argc,argv);
98 polygon_opt.retrieveOption(argc,argv);
99 band_opt.retrieveOption(argc,argv);
100 rule_opt.retrieveOption(argc,argv);
101 bndnodata_opt.retrieveOption(argc,argv);
102 srcnodata_opt.retrieveOption(argc,argv);
103 polythreshold_opt.retrieveOption(argc,argv);
104 test_opt.retrieveOption(argc,argv);
105 fieldname_opt.retrieveOption(argc,argv);
106 label_opt.retrieveOption(argc,argv);
107 geo_opt.retrieveOption(argc,argv);
108 down_opt.retrieveOption(argc,argv);
109 buffer_opt.retrieveOption(argc,argv);
110 disc_opt.retrieveOption(argc,argv);
113 verbose_opt.retrieveOption(argc,argv);
115 catch(
string predefinedString){
116 std::cout << predefinedString << std::endl;
121 cout <<
"Usage: pkextract -i input [-s sample | -rand number | -grid size] -o output" << endl;
123 std::cout <<
"short option -h shows basic options only, use long option --help to show all options" << std::endl;
127 std::map<std::string, rule::RULE_TYPE> ruleMap;
129 ruleMap[
"point"]=rule::point;
130 ruleMap[
"centroid"]=rule::centroid;
131 ruleMap[
"mean"]=rule::mean;
132 ruleMap[
"stdev"]=rule::stdev;
133 ruleMap[
"median"]=rule::median;
134 ruleMap[
"proportion"]=rule::proportion;
135 ruleMap[
"min"]=rule::min;
136 ruleMap[
"max"]=rule::max;
137 ruleMap[
"custom"]=rule::custom;
138 ruleMap[
"mode"]=rule::mode;
139 ruleMap[
"sum"]=rule::sum;
140 ruleMap[
"percentile"]=rule::percentile;
142 if(srcnodata_opt.size()){
143 while(srcnodata_opt.size()<bndnodata_opt.size())
144 srcnodata_opt.push_back(srcnodata_opt[0]);
145 while(bndnodata_opt.size()<srcnodata_opt.size())
146 bndnodata_opt.push_back(bndnodata_opt[0]);
150 std::cout << class_opt << std::endl;
152 stat.setNoDataValues(srcnodata_opt);
154 unsigned long int nsample=0;
155 unsigned long int ntotalvalid=0;
156 unsigned long int ntotalinvalid=0;
157 vector<unsigned long int> nvalid(class_opt.size());
158 vector<unsigned long int> ninvalid(class_opt.size());
159 for(
int it=0;it<nvalid.size();++it){
165 if(image_opt.empty()){
166 std::cerr <<
"No image dataset provided (use option -i). Use --help for help information";
169 if(output_opt.empty()){
170 std::cerr <<
"No output dataset provided (use option -o). Use --help for help information";
174 imgReader.open(image_opt[0]);
176 catch(std::string errorstring){
177 std::cout << errorstring << std::endl;
181 int nband=(band_opt.size()) ? band_opt.size() : imgReader.nrOfBand();
183 if(fieldname_opt.size()<nband){
184 std::string bandString=fieldname_opt[0];
185 fieldname_opt.clear();
186 fieldname_opt.resize(nband);
187 for(
int iband=0;iband<nband;++iband){
188 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
190 fs << bandString << theBand;
191 fieldname_opt[iband]=fs.str();
196 std::cout << fieldname_opt << std::endl;
199 std::cout <<
"Number of bands in input image: " << imgReader.nrOfBand() << std::endl;
201 OGRFieldType fieldType;
202 OGRFieldType labelType;
203 int ogr_typecount=11;
205 std::cout <<
"field and label types can be: ";
206 for(
int iType = 0; iType < ogr_typecount; ++iType){
208 std::cout <<
" " << OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType);
209 if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL
210 && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType),
211 ftype_opt[0].c_str()))
212 fieldType=(OGRFieldType) iType;
213 if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL
214 && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType),
215 ltype_opt[0].c_str()))
216 labelType=(OGRFieldType) iType;
224 std::cout << std::endl <<
"field type is: " << OGRFieldDefn::GetFieldTypeName(fieldType) << std::endl;
227 cerr <<
"field type " << OGRFieldDefn::GetFieldTypeName(fieldType) <<
" not supported" << std::endl;
237 std::cout << std::endl <<
"label type is: " << OGRFieldDefn::GetFieldTypeName(labelType) << std::endl;
240 cerr <<
"label type " << OGRFieldDefn::GetFieldTypeName(labelType) <<
" not supported" << std::endl;
245 const char* pszMessage;
246 void* pProgressArg=NULL;
247 GDALProgressFunc pfnProgress=GDALTermProgress;
251 bool sampleIsRaster=
false;
252 bool sampleIsVirtual=
false;
257 if(sample_opt.size()){
259 sampleReaderOgr.open(sample_opt[0]);
261 catch(
string errorString){
267 sampleWriterOgr.open(
"/vsimem/virtual",ogrformat_opt[0]);
269 catch(
string errorString){
270 cerr << errorString << endl;
272 char **papszOptions=NULL;
273 sampleWriterOgr.createLayer(
"points", imgReader.getProjection(), wkbPoint, papszOptions);
274 sampleIsVirtual=
true;
279 if(random_opt.size()){
282 double ulx,uly,lrx,lry;
283 imgReader.getBoundingBox(ulx,uly,lrx,lry);
284 for(
unsigned int ipoint=1;ipoint<=random_opt[0];++ipoint){
285 OGRFeature *pointFeature;
286 pointFeature=sampleWriterOgr.createFeature();
288 double theX=ulx+
static_cast<double>(rand())/(RAND_MAX)*(lrx-ulx);
289 double theY=uly-
static_cast<double>(rand())/(RAND_MAX)*(uly-lry);
292 pointFeature->SetGeometry( &pt );
293 if(sampleWriterOgr.createFeature(pointFeature) != OGRERR_NONE ){
294 cerr <<
"Failed to create feature in shapefile" << endl;
297 OGRFeature::DestroyFeature(pointFeature);
300 else if(grid_opt.size()){
303 double ulx,uly,lrx,lry;
304 imgReader.getBoundingBox(ulx,uly,lrx,lry);
305 unsigned int ipoint=0;
306 for(
double theY=uly-grid_opt[0]/2;theY>lry;theY-=grid_opt[0]){
307 for(
double theX=ulx+grid_opt[0]/2;theX<lrx;theX+=grid_opt[0]){
309 cout <<
"position: " << theX <<
" " << theY << endl;
310 OGRFeature *pointFeature;
311 pointFeature=sampleWriterOgr.createFeature();
315 pointFeature->SetGeometry( &pt );
316 if(sampleWriterOgr.createFeature(pointFeature) != OGRERR_NONE ){
317 cerr <<
"Failed to create feature in shapefile" << endl;
320 OGRFeature::DestroyFeature(pointFeature);
325 std::cerr <<
"No sample dataset provided (use option -s). Use --help for help information";
329 sampleWriterOgr.close();
330 sampleReaderOgr.open(
"/vsimem/virtual");
332 catch(
string errorString){
333 cerr << errorString << endl;
338 if(class_opt.empty()){
342 assert(sample_opt.size());
343 classReader.open(sample_opt[0]);
345 vector<double> classBuffer(classReader.nrOfCol());
347 vector<double> sample(2+nband);
350 vector<double> writeBufferClass;
351 vector<int> selectedClass;
357 std::cout <<
"extracting sample from image..." << std::endl;
359 pfnProgress(progress,pszMessage,pProgressArg);
360 for(irow=0;irow<classReader.nrOfRow();++irow){
364 classReader.readData(classBuffer,GDT_Float64,irow);
367 for(icol=0;icol<classReader.nrOfCol();++icol){
371 double theClass=classBuffer[icol];
392 classReader.image2geo(icol,irow,x,y);
395 if(verbose_opt[0]>1){
396 std::cout.precision(12);
397 std::cout << theClass <<
" " << x <<
" " << y << std::endl;
400 imgReader.geo2image(x,y,iimg,jimg);
402 jimg=
static_cast<int>(jimg);
403 iimg=
static_cast<int>(iimg);
404 if(static_cast<int>(iimg)<0||
static_cast<int>(iimg)>=imgReader.nrOfCol())
413 if(static_cast<int>(jimg)<0||
static_cast<int>(jimg)>=imgReader.nrOfRow())
418 if(static_cast<int>(jimg)!=
static_cast<int>(oldimgrow)){
419 assert(imgBuffer.size()==nband);
420 for(
int iband=0;iband<nband;++iband){
421 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
422 imgReader.readData(imgBuffer[iband],GDT_Float64,static_cast<int>(jimg),theBand);
423 assert(imgBuffer[iband].size()==imgReader.nrOfCol());
424 if(srcnodata_opt.size()){
425 vector<int>::const_iterator bndit=bndnodata_opt.begin();
426 vector<double>::const_iterator srcit=srcnodata_opt.begin();
427 while(bndit!=bndnodata_opt.end()&&srcit!=srcnodata_opt.end()){
428 if((*bndit==theBand)&&(*srcit==imgBuffer[iband][
static_cast<int>(iimg)])){
443 for(
int iband=0;iband<imgBuffer.size();++iband){
444 if(imgBuffer[iband].size()!=imgReader.nrOfCol()){
445 std::cout <<
"Error in band " << iband <<
": " << imgBuffer[iband].size() <<
"!=" << imgReader.nrOfCol() << std::endl;
446 assert(imgBuffer[iband].size()==imgReader.nrOfCol());
448 sample[iband+2]=imgBuffer[iband][
static_cast<int>(iimg)];
450 float theThreshold=(threshold_opt.size()>1)?threshold_opt[processClass]:threshold_opt[0];
452 double p=
static_cast<double>(rand())/(RAND_MAX);
457 else if(nvalid.size()>processClass){
458 if(nvalid[processClass]>=-theThreshold)
461 writeBuffer.push_back(sample);
462 writeBufferClass.push_back(theClass);
464 if(nvalid.size()>processClass)
465 ++(nvalid[processClass]);
469 if(ninvalid.size()>processClass)
470 ++(ninvalid[processClass]);
474 progress=
static_cast<float>(irow+1.0)/classReader.nrOfRow();
475 pfnProgress(progress,pszMessage,pProgressArg);
478 pfnProgress(progress,pszMessage,pProgressArg);
479 if(writeBuffer.size()>0){
480 assert(ntotalvalid==writeBuffer.size());
482 std::cout <<
"creating image sample writer " << output_opt[0] <<
" with " << writeBuffer.size() <<
" samples (" << ntotalinvalid <<
" invalid)" << std::endl;
483 ogrWriter.open(output_opt[0],ogrformat_opt[0]);
484 char **papszOptions=NULL;
485 ostringstream slayer;
486 slayer <<
"training data";
487 std::string layername=slayer.str();
488 ogrWriter.createLayer(layername, imgReader.getProjection(), wkbPoint, papszOptions);
489 std::string fieldname=
"fid";
490 ogrWriter.createField(fieldname,OFTInteger);
491 map<std::string,double> pointAttributes;
492 ogrWriter.createField(label_opt[0],labelType);
493 for(
int iband=0;iband<nband;++iband){
494 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
495 ogrWriter.createField(fieldname_opt[iband],fieldType);
497 std::cout <<
"writing sample to " << output_opt[0] <<
"..." << std::endl;
499 pfnProgress(progress,pszMessage,pProgressArg);
500 for(
int isample=0;isample<writeBuffer.size();++isample){
502 std::cout <<
"writing sample " << isample << std::endl;
503 pointAttributes[label_opt[0]]=writeBufferClass[isample];
504 for(
int iband=0;iband<writeBuffer[0].size()-2;++iband){
505 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
512 pointAttributes[fieldname_opt[iband]]=writeBuffer[isample][iband+2];
515 std::cout <<
"all bands written" << std::endl;
516 ogrWriter.addPoint(writeBuffer[isample][0],writeBuffer[isample][1],pointAttributes,fieldname,isample);
517 progress=
static_cast<float>(isample+1.0)/writeBuffer.size();
518 pfnProgress(progress,pszMessage,pProgressArg);
523 std::cout <<
"No data found for any class " << std::endl;
526 nsample=writeBuffer.size();
528 std::cout <<
"total number of samples written: " << nsample << std::endl;
531 assert(class_opt[0]);
533 assert(threshold_opt.size()==1||threshold_opt.size()==class_opt.size());
536 if(verbose_opt[0]>1){
537 std::cout <<
"reading position from sample dataset " << std::endl;
538 std::cout <<
"class thresholds: " << std::endl;
539 for(
int iclass=0;iclass<class_opt.size();++iclass){
540 if(threshold_opt.size()>1)
541 std::cout << class_opt[iclass] <<
": " << threshold_opt[iclass] << std::endl;
543 std::cout << class_opt[iclass] <<
": " << threshold_opt[0] << std::endl;
546 classReader.open(sample_opt[0]);
547 vector<int> classBuffer(classReader.nrOfCol());
550 vector<double> sample(2+nband);
552 vector<int> writeBufferClass;
554 vector<int> selectedClass;
560 std::cout <<
"extracting sample from image..." << std::endl;
562 pfnProgress(progress,pszMessage,pProgressArg);
563 for(irow=0;irow<classReader.nrOfRow();++irow){
566 classReader.readData(classBuffer,GDT_Int32,irow);
570 for(icol=0;icol<classReader.nrOfCol();++icol){
576 if(class_opt.empty()){
577 if(classBuffer[icol]){
579 theClass=classBuffer[icol];
583 for(
int iclass=0;iclass<class_opt.size();++iclass){
584 if(classBuffer[icol]==class_opt[iclass]){
586 theClass=class_opt[iclass];
593 classReader.image2geo(icol,irow,x,y);
596 if(verbose_opt[0]>1){
597 std::cout.precision(12);
598 std::cout << theClass <<
" " << x <<
" " << y << std::endl;
601 imgReader.geo2image(x,y,iimg,jimg);
603 jimg=
static_cast<int>(jimg);
604 iimg=
static_cast<int>(iimg);
605 if(static_cast<int>(iimg)<0||
static_cast<int>(iimg)>=imgReader.nrOfCol())
614 if(static_cast<int>(jimg)<0||
static_cast<int>(jimg)>=imgReader.nrOfRow())
619 if(static_cast<int>(jimg)!=
static_cast<int>(oldimgrow)){
620 assert(imgBuffer.size()==nband);
621 for(
int iband=0;iband<nband;++iband){
622 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
623 imgReader.readData(imgBuffer[iband],GDT_Float64,static_cast<int>(jimg),theBand);
624 assert(imgBuffer[iband].size()==imgReader.nrOfCol());
626 if(srcnodata_opt.size()){
627 vector<int>::const_iterator bndit=bndnodata_opt.begin();
628 vector<double>::const_iterator srcit=srcnodata_opt.begin();
629 while(bndit!=bndnodata_opt.end()&&srcit!=srcnodata_opt.end()){
630 if((*bndit==theBand)&&(*srcit==imgBuffer[iband][
static_cast<int>(iimg)])){
644 for(
int iband=0;iband<imgBuffer.size();++iband){
645 if(imgBuffer[iband].size()!=imgReader.nrOfCol()){
646 std::cout <<
"Error in band " << iband <<
": " << imgBuffer[iband].size() <<
"!=" << imgReader.nrOfCol() << std::endl;
647 assert(imgBuffer[iband].size()==imgReader.nrOfCol());
649 sample[iband+2]=imgBuffer[iband][
static_cast<int>(iimg)];
651 float theThreshold=(threshold_opt.size()>1)?threshold_opt[processClass]:threshold_opt[0];
653 double p=
static_cast<double>(rand())/(RAND_MAX);
658 else if(nvalid.size()>processClass){
659 if(nvalid[processClass]>=-theThreshold)
662 writeBuffer.push_back(sample);
664 writeBufferClass.push_back(theClass);
666 if(nvalid.size()>processClass)
667 ++(nvalid[processClass]);
671 if(ninvalid.size()>processClass)
672 ++(ninvalid[processClass]);
676 progress=
static_cast<float>(irow+1.0)/classReader.nrOfRow();
677 pfnProgress(progress,pszMessage,pProgressArg);
679 if(writeBuffer.size()>0){
680 assert(ntotalvalid==writeBuffer.size());
682 std::cout <<
"creating image sample writer " << output_opt[0] <<
" with " << writeBuffer.size() <<
" samples (" << ntotalinvalid <<
" invalid)" << std::endl;
683 ogrWriter.open(output_opt[0],ogrformat_opt[0]);
684 char **papszOptions=NULL;
685 ostringstream slayer;
686 slayer <<
"training data";
687 std::string layername=slayer.str();
688 ogrWriter.createLayer(layername, imgReader.getProjection(), wkbPoint, papszOptions);
689 std::string fieldname=
"fid";
690 ogrWriter.createField(fieldname,OFTInteger);
691 map<std::string,double> pointAttributes;
693 ogrWriter.createField(label_opt[0],labelType);
694 for(
int iband=0;iband<nband;++iband){
695 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
702 ogrWriter.createField(fieldname_opt[iband],fieldType);
704 pfnProgress(progress,pszMessage,pProgressArg);
705 std::cout <<
"writing sample to " << output_opt[0] <<
"..." << std::endl;
707 pfnProgress(progress,pszMessage,pProgressArg);
708 for(
int isample=0;isample<writeBuffer.size();++isample){
709 pointAttributes[label_opt[0]]=writeBufferClass[isample];
710 for(
int iband=0;iband<writeBuffer[0].size()-2;++iband){
711 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
718 pointAttributes[fieldname_opt[iband]]=writeBuffer[isample][iband+2];
720 ogrWriter.addPoint(writeBuffer[isample][0],writeBuffer[isample][1],pointAttributes,fieldname,isample);
721 progress=
static_cast<float>(isample+1.0)/writeBuffer.size();
722 pfnProgress(progress,pszMessage,pProgressArg);
727 std::cout <<
"No data found for any class " << std::endl;
730 nsample=writeBuffer.size();
732 std::cout <<
"total number of samples written: " << nsample << std::endl;
733 if(nvalid.size()==class_opt.size()){
734 for(
int iclass=0;iclass<class_opt.size();++iclass)
735 std::cout <<
"class " << class_opt[iclass] <<
" has " << nvalid[iclass] <<
" samples" << std::endl;
742 std::cout <<
"creating image sample writer " << output_opt[0] << std::endl;
745 ogrWriter.open(output_opt[0],ogrformat_opt[0]);
748 std::cout <<
"creating image test writer " << test_opt[0] << std::endl;
750 ogrTestWriter.open(test_opt[0],ogrformat_opt[0]);
752 catch(
string errorString){
753 cerr << errorString << endl;
757 int nlayerRead=sampleReaderOgr.getDataSource()->GetLayerCount();
760 std::cout <<
"number of layers: " << nlayerRead << endl;
762 for(
int ilayer=0;ilayer<nlayerRead;++ilayer){
763 OGRLayer *readLayer=sampleReaderOgr.getLayer(ilayer);
764 string currentLayername=readLayer->GetName();
766 if(find(layer_opt.begin(),layer_opt.end(),currentLayername)==layer_opt.end())
768 cout <<
"processing layer " << currentLayername << endl;
770 readLayer->ResetReading();
771 OGRLayer *writeLayer;
772 OGRLayer *writeTestLayer;
776 std::cout <<
"create polygons" << std::endl;
777 char **papszOptions=NULL;
778 writeLayer=ogrWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPolygon, papszOptions);
780 writeTestLayer=ogrTestWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPolygon, papszOptions);
784 std::cout <<
"create points in layer " << readLayer->GetName() << std::endl;
785 char **papszOptions=NULL;
787 writeLayer=ogrWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPoint, papszOptions);
789 char **papszOptions=NULL;
790 writeTestLayer=ogrTestWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPoint, papszOptions);
794 std::cout <<
"copy fields from layer " << ilayer << std::flush << std::endl;
795 ogrWriter.copyFields(sampleReaderOgr,ilayer,ilayerWrite);
799 std::cout <<
"copy fields test writer" << std::flush << std::endl;
800 ogrTestWriter.copyFields(sampleReaderOgr,ilayer,ilayerWrite);
809 if(class_opt.size()){
810 switch(ruleMap[rule_opt[0]]){
811 case(rule::proportion):{
812 for(
int iclass=0;iclass<class_opt.size();++iclass){
814 cs << class_opt[iclass];
815 ogrWriter.createField(cs.str(),fieldType,ilayerWrite);
821 ogrWriter.createField(label_opt[0],fieldType,ilayerWrite);
823 ogrTestWriter.createField(label_opt[0],fieldType,ilayerWrite);
828 for(
int iband=0;iband<nband;++iband){
829 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
831 fs << fieldname_opt[iband];
833 std::cout <<
"creating field " << fs.str() << std::endl;
835 ogrWriter.createField(fs.str(),fieldType,ilayerWrite);
837 ogrTestWriter.createField(fs.str(),fieldType,ilayerWrite);
840 OGRFeature *readFeature;
841 unsigned long int ifeature=0;
842 unsigned long int nfeature=sampleReaderOgr.getFeatureCount();
844 pfnProgress(progress,pszMessage,pProgressArg);
845 while( (readFeature = readLayer->GetNextFeature()) != NULL ){
846 bool validFeature=
false;
847 bool writeTest=
false;
849 std::cout <<
"reading feature " << readFeature->GetFID() << std::endl;
850 if(threshold_opt[0]>0){
851 double p=
static_cast<double>(rand())/(RAND_MAX);
853 if(p>threshold_opt[0]){
861 if(ntotalvalid>=-threshold_opt[0]){
869 std::cout <<
"processing feature " << readFeature->GetFID() << std::endl;
872 OGRGeometry *poGeometry;
873 poGeometry = readFeature->GetGeometryRef();
874 assert(poGeometry!=NULL);
876 if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint ){
878 if(!buffer_opt.size()){
879 switch(ruleMap[rule_opt[0]]){
881 case(rule::centroid):
882 buffer_opt.push_back(1);
885 buffer_opt.push_back(3);
890 std::cout <<
"boundary: " << buffer_opt[0] << std::endl;
892 OGRPolygon writePolygon;
893 OGRLinearRing writeRing;
894 OGRPoint writeCentroidPoint;
895 OGRFeature *writePolygonFeature;
896 OGRFeature *writeCentroidFeature;
898 OGRPoint *poPoint = (OGRPoint *) poGeometry;
899 writeCentroidPoint=*poPoint;
904 double i_centre,j_centre;
906 imgReader.geo2image(x,y,i_centre,j_centre);
912 j_centre=
static_cast<int>(j_centre);
913 i_centre=
static_cast<int>(i_centre);
915 double uli=i_centre-buffer_opt[0]/2;
916 double ulj=j_centre-buffer_opt[0]/2;
917 double lri=i_centre+buffer_opt[0]/2;
918 double lrj=j_centre+buffer_opt[0]/2;
925 ulj=
static_cast<int>(ulj);
926 uli=
static_cast<int>(uli);
927 lrj=
static_cast<int>(lrj);
928 lri=
static_cast<int>(lri);
930 if((polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point)||(ruleMap[rule_opt[0]]==rule::centroid)){
938 if(static_cast<int>(ulj)<0||
static_cast<int>(ulj)>=imgReader.nrOfRow())
941 if(static_cast<int>(uli)<0||
static_cast<int>(lri)>=imgReader.nrOfCol())
944 OGRPoint ulPoint,urPoint,llPoint,lrPoint;
951 double radius=buffer_opt[0]/2.0*sqrt(imgReader.getDeltaX()*imgReader.getDeltaY());
952 unsigned short nstep = 25;
953 for(
int i=0;i<nstep;++i){
955 aPoint.setX(x+radius*cos(2*PI*i/nstep));
956 aPoint.setY(y+radius*sin(2*PI*i/nstep));
957 writeRing.addPoint(&aPoint);
959 writePolygon.addRing(&writeRing);
960 writePolygon.closeRings();
965 imgReader.image2geo(uli,ulj,ulx,uly);
966 imgReader.image2geo(lri,lrj,lrx,lry);
967 ulPoint.setX(ulx-imgReader.getDeltaX()/2.0);
968 ulPoint.setY(uly+imgReader.getDeltaY()/2.0);
969 lrPoint.setX(lrx+imgReader.getDeltaX()/2.0);
970 lrPoint.setY(lry-imgReader.getDeltaY()/2.0);
971 urPoint.setX(lrx+imgReader.getDeltaX()/2.0);
972 urPoint.setY(uly+imgReader.getDeltaY()/2.0);
973 llPoint.setX(ulx-imgReader.getDeltaX()/2.0);
974 llPoint.setY(lry-imgReader.getDeltaY()/2.0);
976 writeRing.addPoint(&ulPoint);
977 writeRing.addPoint(&urPoint);
978 writeRing.addPoint(&lrPoint);
979 writeRing.addPoint(&llPoint);
980 writePolygon.addRing(&writeRing);
981 writePolygon.closeRings();
988 writePolygonFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
990 writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
992 else if(ruleMap[rule_opt[0]]!=rule::point){
994 writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
996 writeCentroidFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
999 vector<double> windowClassValues;
1001 if(class_opt.size()){
1002 windowClassValues.resize(class_opt.size());
1004 for(
int iclass=0;iclass<class_opt.size();++iclass)
1005 windowClassValues[iclass]=0;
1008 windowValues.resize(nband);
1009 vector< Vector2d<double> > readValues(nband);
1010 for(
int iband=0;iband<nband;++iband){
1011 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1014 assert(uli<imgReader.nrOfCol());
1016 assert(lri<imgReader.nrOfCol());
1018 assert(ulj<imgReader.nrOfRow());
1020 assert(lrj<imgReader.nrOfRow());
1021 imgReader.readDataBlock(readValues[iband],GDT_Float64,uli,lri,ulj,lrj,theBand);
1025 for(
int j=ulj;j<=lrj;++j){
1026 for(
int i=uli;i<=lri;++i){
1028 if(i<0||i>=imgReader.nrOfCol())
1030 if(j<0||j>=imgReader.nrOfRow())
1035 imgReader.image2geo(i,j,theX,theY);
1036 thePoint.setX(theX);
1037 thePoint.setY(theY);
1039 if(disc_opt[0]&&buffer_opt[0]>1){
1040 double radius=buffer_opt[0]/2.0*sqrt(imgReader.getDeltaX()*imgReader.getDeltaY());
1041 if((theX-x)*(theX-x)+(theY-y)*(theY-y)>radius*radius)
1046 if(srcnodata_opt.size()){
1047 for(
int vband=0;vband<bndnodata_opt.size();++vband){
1048 double value=((readValues[bndnodata_opt[vband]])[j-ulj])[i-uli];
1049 if(value==srcnodata_opt[vband]){
1064 OGRFeature *writePointFeature;
1065 if(!polygon_opt[0]){
1067 if(ruleMap[rule_opt[0]]==rule::point){
1069 writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1071 writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1072 if(verbose_opt[0]>1)
1073 std::cout <<
"copying fields from polygons " << std::endl;
1078 if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE)
1079 cerr <<
"writing feature failed" << std::endl;
1081 assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
1085 if(verbose_opt[0]>1)
1086 std::cout <<
"write feature has " << writePointFeature->GetFieldCount() <<
" fields" << std::endl;
1089 if(class_opt.size()){
1090 short value=((readValues[0])[j-ulj])[i-uli];
1091 for(
int iclass=0;iclass<class_opt.size();++iclass){
1092 if(value==class_opt[iclass])
1093 windowClassValues[iclass]+=1;
1097 for(
int iband=0;iband<nband;++iband){
1098 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1100 assert(j-ulj<readValues[iband].size());
1102 assert(i-uli<((readValues[iband])[j-ulj]).size());
1103 double value=((readValues[iband])[j-ulj])[i-uli];
1105 if(verbose_opt[0]>1)
1106 std::cout <<
": " << value << std::endl;
1107 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
1108 windowValues[iband].push_back(value);
1112 if(verbose_opt[0]>1)
1113 std::cout <<
"set field " << fieldname_opt[iband] <<
" to " << value << std::endl;
1114 switch( fieldType ){
1117 writePointFeature->SetField(fieldname_opt[iband].c_str(),value);
1120 writePointFeature->SetField(fieldname_opt[iband].c_str(),type2string<double>(value).c_str());
1123 std::string errorString=
"field type not supported";
1128 catch(std::string e){
1129 std::cout << e << std::endl;
1135 if(!polygon_opt[0]){
1136 if(ruleMap[rule_opt[0]]==rule::point){
1138 if(verbose_opt[0]>1)
1139 std::cout <<
"creating point feature" << std::endl;
1141 if(writeTestLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1142 std::string errorString=
"Failed to create feature in test ogr vector dataset";
1147 if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1148 std::string errorString=
"Failed to create feature in ogr vector dataset";
1153 OGRFeature::DestroyFeature( writePointFeature );
1156 std::cout <<
"ntotalvalid(2): " << ntotalvalid << std::endl;
1161 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
1165 cout <<
"no points found in window, continuing" << endl;
1175 if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE)
1176 cerr <<
"writing feature failed" << std::endl;
1178 writePolygonFeature->SetGeometry(&writePolygon);
1179 assert(wkbFlatten(writePolygonFeature->GetGeometryRef()->getGeometryType()) == wkbPolygon);
1181 if(verbose_opt[0]>1)
1182 std::cout <<
"copying new fields write polygon " << std::endl;
1183 if(verbose_opt[0]>1)
1184 std::cout <<
"write feature has " << writePolygonFeature->GetFieldCount() <<
" fields" << std::endl;
1189 if(verbose_opt[0]>1)
1190 std::cout <<
"copying fields from polygons " << std::endl;
1195 if(writeCentroidFeature->SetFrom(readFeature)!= OGRERR_NONE)
1196 cerr <<
"writing feature failed" << std::endl;
1197 assert(wkbFlatten(writeCentroidFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
1202 if(verbose_opt[0]>1)
1203 std::cout <<
"write feature has " << writeCentroidFeature->GetFieldCount() <<
" fields" << std::endl;
1205 if(class_opt.empty()){
1206 if(ruleMap[rule_opt[0]]==rule::point){
1208 std::cout <<
"number of points in window: " << nPointWindow << std::endl;
1209 for(
int index=0;index<windowValues.size();++index){
1211 if(windowValues[index].size()!=1){
1212 cerr <<
"Error: windowValues[index].size()=" << windowValues[index].size() << endl;
1213 assert(windowValues[index].size()==1);
1215 double theValue=windowValues[index].back();
1218 std::cout <<
"number of points in window: " << nPointWindow << std::endl;
1219 int theBand=(band_opt.size()) ? band_opt[index] : index;
1222 if(verbose_opt[0]>1)
1223 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1224 switch( fieldType ){
1228 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1230 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1234 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1236 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1239 std::string errorString=
"field type not supported";
1244 catch(std::string e){
1245 std::cout << e << std::endl;
1252 for(
int index=0;index<windowValues.size();++index){
1254 if(ruleMap[rule_opt[0]]==rule::mean)
1255 theValue=stat.mean(windowValues[index]);
1256 else if(ruleMap[rule_opt[0]]==rule::stdev)
1257 theValue=sqrt(stat.var(windowValues[index]));
1258 else if(ruleMap[rule_opt[0]]==rule::median)
1259 theValue=stat.median(windowValues[index]);
1260 else if(ruleMap[rule_opt[0]]==rule::percentile)
1261 theValue=stat.percentile(windowValues[index],windowValues[index].begin(),windowValues[index].end(),percentile_opt[0]);
1262 else if(ruleMap[rule_opt[0]]==rule::sum)
1263 theValue=stat.sum(windowValues[index]);
1264 else if(ruleMap[rule_opt[0]]==rule::max)
1265 theValue=stat.mymax(windowValues[index]);
1266 else if(ruleMap[rule_opt[0]]==rule::min)
1267 theValue=stat.mymin(windowValues[index]);
1268 else if(ruleMap[rule_opt[0]]==rule::centroid){
1270 std::cout <<
"number of points in polygon: " << nPointWindow << std::endl;
1271 assert(nPointWindow<=1);
1272 assert(nPointWindow==windowValues[index].size());
1273 theValue=windowValues[index].back();
1276 std::string errorString=
"rule not supported";
1279 if(verbose_opt[0]>1)
1280 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1281 switch( fieldType ){
1285 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1287 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1291 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1293 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1296 std::string errorString=
"field type not supported";
1301 catch(std::string e){
1302 std::cout << e << std::endl;
1309 if(ruleMap[rule_opt[0]]==rule::proportion){
1311 std::cout <<
"number of points in polygon: " << nPointWindow << std::endl;
1312 stat.normalize_pct(windowClassValues);
1313 for(
int index=0;index<windowClassValues.size();++index){
1314 double theValue=windowClassValues[index];
1316 fs << class_opt[index];
1318 writePolygonFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1320 writeCentroidFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1323 else if(ruleMap[rule_opt[0]]==rule::custom){
1324 assert(polygon_opt[0]);
1326 std::cout <<
"number of points in polygon: " << nPointWindow << std::endl;
1327 stat.normalize_pct(windowClassValues);
1328 assert(windowClassValues.size()==2);
1329 if(windowClassValues[0]>=75)
1330 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(11));
1331 else if(windowClassValues[1]>=75)
1332 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(12));
1333 else if(windowClassValues[0]>25&&windowClassValues[1]>25)
1334 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(13));
1337 std::cout <<
"No valid value in windowClassValues..." << std::endl;
1338 for(
int index=0;index<windowClassValues.size();++index){
1339 double theValue=windowClassValues[index];
1340 std::cout << theValue <<
" ";
1342 std::cout << std::endl;
1344 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(20));
1347 else if(ruleMap[rule_opt[0]]==rule::mode){
1350 std::cout <<
"number of points in window: " << nPointWindow << std::endl;
1352 int maxClass=stat.mymin(class_opt);
1353 vector<double>::iterator maxit;
1354 maxit=stat.mymax(windowClassValues,windowClassValues.begin(),windowClassValues.end());
1355 int maxIndex=distance(windowClassValues.begin(),maxit);
1356 maxClass=class_opt[maxIndex];
1357 if(verbose_opt[0]>0)
1358 std::cout <<
"maxClass: " << maxClass << std::endl;
1360 writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
1362 writeCentroidFeature->SetField(label_opt[0].c_str(),maxClass);
1366 if(verbose_opt[0]>1)
1367 std::cout <<
"creating polygon feature" << std::endl;
1369 if(writeTestLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1370 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1375 if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1376 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1380 OGRFeature::DestroyFeature( writePolygonFeature );
1383 std::cout <<
"ntotalvalid(1): " << ntotalvalid << std::endl;
1386 if(verbose_opt[0]>1)
1387 std::cout <<
"creating point feature in centroid" << std::endl;
1389 if(writeTestLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1390 std::string errorString=
"Failed to create point feature in ogr vector dataset";
1396 assert(validFeature);
1397 if(writeLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1398 std::string errorString=
"Failed to create point feature in ogr vector dataset";
1402 OGRFeature::DestroyFeature( writeCentroidFeature );
1405 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
1409 else if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){
1411 OGRPolygon readPolygon = *((OGRPolygon *) poGeometry);
1412 OGRPolygon writePolygon;
1413 OGRLinearRing writeRing;
1414 OGRPoint writeCentroidPoint;
1415 OGRFeature *writePolygonFeature;
1416 OGRFeature *writeCentroidFeature;
1418 readPolygon.closeRings();
1420 if(verbose_opt[0]>1)
1421 std::cout <<
"get point on polygon" << std::endl;
1422 if(ruleMap[rule_opt[0]]==rule::centroid)
1423 readPolygon.Centroid(&writeCentroidPoint);
1425 readPolygon.PointOnSurface(&writeCentroidPoint);
1427 double ulx,uly,lrx,lry;
1428 double uli,ulj,lri,lrj;
1429 if((polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point)||(ruleMap[rule_opt[0]]==rule::centroid)){
1430 ulx=writeCentroidPoint.getX();
1431 uly=writeCentroidPoint.getY();
1438 std::cout <<
"reading envelope for polygon " << ifeature << std::endl;
1439 OGREnvelope* psEnvelope=
new OGREnvelope();
1440 readPolygon.getEnvelope(psEnvelope);
1441 ulx=psEnvelope->MinX;
1442 uly=psEnvelope->MaxY;
1443 lrx=psEnvelope->MaxX;
1444 lry=psEnvelope->MinY;
1448 imgReader.geo2image(ulx,uly,uli,ulj);
1449 imgReader.geo2image(lrx,lry,lri,lrj);
1458 ulj=
static_cast<int>(ulj);
1459 uli=
static_cast<int>(uli);
1460 lrj=
static_cast<int>(lrj);
1461 lri=
static_cast<int>(lri);
1463 if(verbose_opt[0]>1)
1464 std::cout <<
"bounding box for polygon feature " << ifeature <<
": " << uli <<
" " << ulj <<
" " << lri <<
" " << lrj << std::endl;
1470 if(uli>=imgReader.nrOfCol())
1471 uli=imgReader.nrOfCol()-1;
1472 if(lri>=imgReader.nrOfCol())
1473 lri=imgReader.nrOfCol()-1;
1478 if(ulj>=imgReader.nrOfRow())
1479 ulj=imgReader.nrOfRow()-1;
1480 if(lrj>=imgReader.nrOfRow())
1481 lrj=imgReader.nrOfRow()-1;
1485 int nPointPolygon=0;
1489 writePolygonFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1491 writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1493 else if(ruleMap[rule_opt[0]]!=rule::point){
1495 writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1497 writeCentroidFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1501 vector<double> polyClassValues;
1503 if(class_opt.size()){
1505 polyClassValues.resize(class_opt.size());
1507 for(
int iclass=0;iclass<class_opt.size();++iclass)
1508 polyClassValues[iclass]=0;
1511 polyValues.resize(nband);
1512 vector< Vector2d<double> > readValues(nband);
1513 for(
int iband=0;iband<nband;++iband){
1514 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1517 assert(uli<imgReader.nrOfCol());
1519 assert(lri<imgReader.nrOfCol());
1521 assert(ulj<imgReader.nrOfRow());
1523 assert(lrj<imgReader.nrOfRow());
1524 imgReader.readDataBlock(readValues[iband],GDT_Float64,uli,lri,ulj,lrj,theBand);
1528 for(
int j=ulj;j<=lrj;++j){
1529 for(
int i=uli;i<=lri;++i){
1531 if(i<0||i>=imgReader.nrOfCol())
1533 if(j<0||j>=imgReader.nrOfRow())
1538 imgReader.image2geo(i,j,theX,theY);
1539 thePoint.setX(theX);
1540 thePoint.setY(theY);
1542 if(ruleMap[rule_opt[0]]!=rule::centroid&&!readPolygon.Contains(&thePoint))
1547 if(srcnodata_opt.size()){
1548 for(
int vband=0;vband<bndnodata_opt.size();++vband){
1549 double value=((readValues[bndnodata_opt[vband]])[j-ulj])[i-uli];
1550 if(value==srcnodata_opt[vband]){
1562 writeRing.addPoint(&thePoint);
1565 if(verbose_opt[0]>1)
1566 std::cout <<
"point is on surface:" << thePoint.getX() <<
"," << thePoint.getY() << std::endl;
1569 if(polythreshold_opt.size())
1570 if(nPointPolygon>polythreshold_opt[0])
1573 OGRFeature *writePointFeature;
1574 if(!polygon_opt[0]){
1576 if(ruleMap[rule_opt[0]]==rule::point){
1578 writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1580 writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1581 if(verbose_opt[0]>1)
1582 std::cout <<
"copying fields from polygons " << std::endl;
1583 if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE)
1584 cerr <<
"writing feature failed" << std::endl;
1585 if(verbose_opt[0]>1)
1586 std::cout <<
"set geometry as point " << std::endl;
1588 writePointFeature->SetGeometry(&thePoint);
1589 assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
1594 if(verbose_opt[0]>1)
1595 std::cout <<
"write feature has " << writePointFeature->GetFieldCount() <<
" fields" << std::endl;
1598 if(class_opt.size()){
1599 short value=((readValues[0])[j-ulj])[i-uli];
1600 for(
int iclass=0;iclass<class_opt.size();++iclass){
1601 if(value==class_opt[iclass])
1602 polyClassValues[iclass]+=1;
1606 for(
int iband=0;iband<nband;++iband){
1607 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1609 assert(j-ulj<readValues[iband].size());
1611 assert(i-uli<((readValues[iband])[j-ulj]).size());
1612 double value=((readValues[iband])[j-ulj])[i-uli];
1614 if(verbose_opt[0]>1)
1615 std::cout <<
": " << value << std::endl;
1616 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point)
1617 polyValues[iband].push_back(value);
1619 if(verbose_opt[0]>1)
1620 std::cout <<
"set field " << fieldname_opt[iband] <<
" to " << value << std::endl;
1621 switch( fieldType ){
1624 writePointFeature->SetField(fieldname_opt[iband].c_str(),value);
1627 writePointFeature->SetField(fieldname_opt[iband].c_str(),type2string<double>(value).c_str());
1636 if(!polygon_opt[0]){
1637 if(ruleMap[rule_opt[0]]==rule::point){
1639 if(verbose_opt[0]>1)
1640 std::cout <<
"creating point feature" << std::endl;
1642 if(writeTestLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1643 std::string errorString=
"Failed to create feature in test ogr vector dataset";
1648 if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1649 std::string errorString=
"Failed to create feature in ogr vector dataset";
1654 OGRFeature::DestroyFeature( writePointFeature );
1657 std::cout <<
"ntotalvalid(2): " << ntotalvalid << std::endl;
1662 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
1666 cout <<
"no points found in polygon, continuing" << endl;
1671 writePolygon.addRing(&writeRing);
1672 writePolygon.closeRings();
1677 if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE)
1678 cerr <<
"writing feature failed" << std::endl;
1679 if(verbose_opt[0]>1)
1680 std::cout <<
"copying new fields write polygon " << std::endl;
1681 if(verbose_opt[0]>1)
1682 std::cout <<
"write feature has " << writePolygonFeature->GetFieldCount() <<
" fields" << std::endl;
1687 if(verbose_opt[0]>1)
1688 std::cout <<
"copying fields from polygons " << std::endl;
1691 if(writeCentroidFeature->SetFrom(readFeature)!= OGRERR_NONE)
1692 cerr <<
"writing feature failed" << std::endl;
1694 writeCentroidFeature->SetGeometry(&writeCentroidPoint);
1695 assert(wkbFlatten(writeCentroidFeature->GetGeometryRef()->getGeometryType()) == wkbPoint );
1699 if(verbose_opt[0]>1)
1700 std::cout <<
"write feature has " << writeCentroidFeature->GetFieldCount() <<
" fields" << std::endl;
1702 if(class_opt.empty()){
1703 if(ruleMap[rule_opt[0]]==rule::point){
1705 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1706 for(
int index=0;index<polyValues.size();++index){
1707 assert(polyValues[index].size()==1);
1708 double theValue=polyValues[index].back();
1711 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1712 int theBand=(band_opt.size()) ? band_opt[index] : index;
1714 if(verbose_opt[0]>1)
1715 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1716 switch( fieldType ){
1720 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1722 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1726 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1728 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1731 std::string errorString=
"field type not supported";
1736 catch(std::string e){
1737 std::cout << e << std::endl;
1744 for(
int index=0;index<polyValues.size();++index){
1746 if(ruleMap[rule_opt[0]]==rule::mean)
1747 theValue=stat.mean(polyValues[index]);
1748 else if(ruleMap[rule_opt[0]]==rule::stdev)
1749 theValue=sqrt(stat.var(polyValues[index]));
1750 else if(ruleMap[rule_opt[0]]==rule::median)
1751 theValue=stat.median(polyValues[index]);
1752 else if(ruleMap[rule_opt[0]]==rule::percentile)
1753 theValue=stat.percentile(polyValues[index],polyValues[index].begin(),polyValues[index].end(),percentile_opt[0]);
1754 else if(ruleMap[rule_opt[0]]==rule::sum)
1755 theValue=stat.sum(polyValues[index]);
1756 else if(ruleMap[rule_opt[0]]==rule::max)
1757 theValue=stat.mymax(polyValues[index]);
1758 else if(ruleMap[rule_opt[0]]==rule::min)
1759 theValue=stat.mymin(polyValues[index]);
1760 else if(ruleMap[rule_opt[0]]==rule::centroid){
1762 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1763 assert(nPointPolygon<=1);
1764 assert(nPointPolygon==polyValues[index].size());
1765 theValue=polyValues[index].back();
1768 std::string errorString=
"rule not supported";
1771 if(verbose_opt[0]>1)
1772 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1773 switch( fieldType ){
1777 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1779 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1783 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1785 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1788 std::string errorString=
"field type not supported";
1793 catch(std::string e){
1794 std::cout << e << std::endl;
1801 if(ruleMap[rule_opt[0]]==rule::proportion){
1803 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1804 stat.normalize_pct(polyClassValues);
1805 for(
int index=0;index<polyClassValues.size();++index){
1806 double theValue=polyClassValues[index];
1808 fs << class_opt[index];
1810 writePolygonFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1812 writeCentroidFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1815 else if(ruleMap[rule_opt[0]]==rule::custom){
1816 assert(polygon_opt[0]);
1818 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1819 stat.normalize_pct(polyClassValues);
1820 assert(polyClassValues.size()==2);
1821 if(polyClassValues[0]>=75)
1822 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(11));
1823 else if(polyClassValues[1]>=75)
1824 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(12));
1825 else if(polyClassValues[0]>25&&polyClassValues[1]>25)
1826 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(13));
1829 std::cout <<
"No valid value in polyClassValues..." << std::endl;
1830 for(
int index=0;index<polyClassValues.size();++index){
1831 double theValue=polyClassValues[index];
1832 std::cout << theValue <<
" ";
1834 std::cout << std::endl;
1836 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(20));
1839 else if(ruleMap[rule_opt[0]]==rule::mode){
1842 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1844 int maxClass=stat.mymin(class_opt);
1845 vector<double>::iterator maxit;
1846 maxit=stat.mymax(polyClassValues,polyClassValues.begin(),polyClassValues.end());
1847 int maxIndex=distance(polyClassValues.begin(),maxit);
1848 maxClass=class_opt[maxIndex];
1849 if(verbose_opt[0]>0)
1850 std::cout <<
"maxClass: " << maxClass << std::endl;
1852 writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
1854 writeCentroidFeature->SetField(label_opt[0].c_str(),maxClass);
1858 if(verbose_opt[0]>1)
1859 std::cout <<
"creating polygon feature" << std::endl;
1861 if(writeTestLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1862 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1867 if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1868 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1872 OGRFeature::DestroyFeature( writePolygonFeature );
1875 std::cout <<
"ntotalvalid(1): " << ntotalvalid << std::endl;
1878 if(verbose_opt[0]>1)
1879 std::cout <<
"creating point feature in centroid" << std::endl;
1881 if(writeTestLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1882 std::string errorString=
"Failed to create point feature in ogr vector dataset";
1888 assert(validFeature);
1889 if(writeLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1890 std::string errorString=
"Failed to create point feature in ogr vector dataset";
1894 OGRFeature::DestroyFeature( writeCentroidFeature );
1897 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
1901 else if(wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon){
1902 OGRMultiPolygon readPolygon = *((OGRMultiPolygon *) poGeometry);
1903 OGRPolygon writePolygon;
1904 OGRLinearRing writeRing;
1905 OGRPoint writeCentroidPoint;
1906 OGRFeature *writePolygonFeature;
1907 OGRFeature *writeCentroidFeature;
1909 readPolygon.closeRings();
1911 if(verbose_opt[0]>1)
1912 std::cout <<
"get centroid point from polygon" << std::endl;
1914 readPolygon.Centroid(&writeCentroidPoint);
1916 double ulx,uly,lrx,lry;
1917 double uli,ulj,lri,lrj;
1918 if((polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point)||(ruleMap[rule_opt[0]]==rule::centroid)){
1919 ulx=writeCentroidPoint.getX();
1920 uly=writeCentroidPoint.getY();
1927 std::cout <<
"reading envelope for polygon " << ifeature << std::endl;
1928 OGREnvelope* psEnvelope=
new OGREnvelope();
1929 readPolygon.getEnvelope(psEnvelope);
1930 ulx=psEnvelope->MinX;
1931 uly=psEnvelope->MaxY;
1932 lrx=psEnvelope->MaxX;
1933 lry=psEnvelope->MinY;
1937 imgReader.geo2image(ulx,uly,uli,ulj);
1938 imgReader.geo2image(lrx,lry,lri,lrj);
1947 ulj=
static_cast<int>(ulj);
1948 uli=
static_cast<int>(uli);
1949 lrj=
static_cast<int>(lrj);
1950 lri=
static_cast<int>(lri);
1952 if(verbose_opt[0]>1)
1953 std::cout <<
"bounding box for multipologon feature " << ifeature <<
": " << uli <<
" " << ulj <<
" " << lri <<
" " << lrj << std::endl;
1959 if(uli>=imgReader.nrOfCol())
1960 uli=imgReader.nrOfCol()-1;
1961 if(lri>=imgReader.nrOfCol())
1962 lri=imgReader.nrOfCol()-1;
1967 if(ulj>=imgReader.nrOfRow())
1968 ulj=imgReader.nrOfRow()-1;
1969 if(lrj>=imgReader.nrOfRow())
1970 lrj=imgReader.nrOfRow()-1;
1974 int nPointPolygon=0;
1977 writePolygonFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1979 writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1981 else if(ruleMap[rule_opt[0]]!=rule::point){
1983 writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1985 writeCentroidFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1989 vector<double> polyClassValues;
1991 if(class_opt.size()){
1992 polyClassValues.resize(class_opt.size());
1994 for(
int iclass=0;iclass<class_opt.size();++iclass)
1995 polyClassValues[iclass]=0;
1998 polyValues.resize(nband);
1999 vector< Vector2d<double> > readValues(nband);
2000 for(
int iband=0;iband<nband;++iband){
2001 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
2004 assert(uli<imgReader.nrOfCol());
2006 assert(lri<imgReader.nrOfCol());
2008 assert(ulj<imgReader.nrOfRow());
2010 assert(lrj<imgReader.nrOfRow());
2011 imgReader.readDataBlock(readValues[iband],GDT_Float64,uli,lri,ulj,lrj,theBand);
2015 for(
int j=ulj;j<=lrj;++j){
2016 for(
int i=uli;i<=lri;++i){
2018 if(i<0||i>=imgReader.nrOfCol())
2020 if(j<0||j>=imgReader.nrOfRow())
2025 imgReader.image2geo(i,j,theX,theY);
2026 thePoint.setX(theX);
2027 thePoint.setY(theY);
2029 if(ruleMap[rule_opt[0]]!=rule::centroid&&!readPolygon.Contains(&thePoint))
2034 if(srcnodata_opt.size()){
2035 for(
int vband=0;vband<bndnodata_opt.size();++vband){
2036 double value=((readValues[bndnodata_opt[vband]])[j-ulj])[i-uli];
2037 if(value==srcnodata_opt[vband]){
2049 writeRing.addPoint(&thePoint);
2052 if(verbose_opt[0]>1)
2053 std::cout <<
"point is on surface:" << thePoint.getX() <<
"," << thePoint.getY() << std::endl;
2056 if(polythreshold_opt.size())
2057 if(nPointPolygon>polythreshold_opt[0])
2060 OGRFeature *writePointFeature;
2061 if(!polygon_opt[0]){
2063 if(ruleMap[rule_opt[0]]==rule::point){
2065 writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
2067 writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
2068 if(verbose_opt[0]>1)
2069 std::cout <<
"copying fields from polygons " << std::endl;
2072 if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE)
2073 cerr <<
"writing feature failed" << std::endl;
2075 writePointFeature->SetGeometry(&thePoint);
2076 assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
2080 if(verbose_opt[0]>1)
2081 std::cout <<
"write feature has " << writePointFeature->GetFieldCount() <<
" fields" << std::endl;
2084 if(class_opt.size()){
2085 short value=((readValues[0])[j-ulj])[i-uli];
2086 for(
int iclass=0;iclass<class_opt.size();++iclass){
2087 if(value==class_opt[iclass])
2088 polyClassValues[iclass]+=1;
2092 for(
int iband=0;iband<nband;++iband){
2095 assert(j-ulj<readValues[iband].size());
2097 assert(i-uli<((readValues[iband])[j-ulj]).size());
2098 double value=((readValues[iband])[j-ulj])[i-uli];
2100 if(verbose_opt[0]>1)
2101 std::cout <<
": " << value << std::endl;
2102 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point)
2103 polyValues[iband].push_back(value);
2105 if(verbose_opt[0]>1)
2106 std::cout <<
"set field " << fieldname_opt[iband] <<
" to " << value << std::endl;
2107 switch( fieldType ){
2110 writePointFeature->SetField(fieldname_opt[iband].c_str(),value);
2113 writePointFeature->SetField(fieldname_opt[iband].c_str(),type2string<double>(value).c_str());
2133 if(!polygon_opt[0]){
2134 if(ruleMap[rule_opt[0]]==rule::point){
2136 if(verbose_opt[0]>1)
2137 std::cout <<
"creating point feature" << std::endl;
2139 if(writeTestLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
2140 std::string errorString=
"Failed to create feature in ogr vector dataset";
2145 if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
2146 std::string errorString=
"Failed to create feature in ogr vector dataset";
2151 OGRFeature::DestroyFeature( writePointFeature );
2157 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
2162 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
2168 writePolygon.addRing(&writeRing);
2169 writePolygon.closeRings();
2174 if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE)
2175 cerr <<
"writing feature failed" << std::endl;
2176 assert(writePolygonFeature->GetGeometryRef()->getGeometryType()==wkbMultiPolygon);
2177 if(verbose_opt[0]>1)
2178 std::cout <<
"copying new fields write polygon " << std::endl;
2179 if(verbose_opt[0]>1)
2180 std::cout <<
"write feature has " << writePolygonFeature->GetFieldCount() <<
" fields" << std::endl;
2185 if(verbose_opt[0]>1)
2186 std::cout <<
"copying fields from polygons " << std::endl;
2189 if(writeCentroidFeature->SetFrom(readFeature)!= OGRERR_NONE)
2190 cerr <<
"writing feature failed" << std::endl;
2191 writeCentroidFeature->SetGeometry(&writeCentroidPoint);
2192 assert(wkbFlatten(writeCentroidFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
2197 if(verbose_opt[0]>1)
2198 std::cout <<
"write feature has " << writeCentroidFeature->GetFieldCount() <<
" fields" << std::endl;
2200 if(class_opt.empty()){
2201 if(ruleMap[rule_opt[0]]==rule::point){
2203 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2204 for(
int index=0;index<polyValues.size();++index){
2206 assert(polyValues[index].size()==1);
2207 double theValue=polyValues[index].back();
2209 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2210 int theBand=(band_opt.size()) ? band_opt[index] : index;
2212 if(verbose_opt[0]>1)
2213 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
2214 switch( fieldType ){
2218 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
2220 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
2224 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2226 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2229 std::string errorString=
"field type not supported";
2234 catch(std::string e){
2235 std::cout << e << std::endl;
2242 for(
int index=0;index<polyValues.size();++index){
2244 if(ruleMap[rule_opt[0]]==rule::mean)
2245 theValue=stat.mean(polyValues[index]);
2246 else if(ruleMap[rule_opt[0]]==rule::stdev)
2247 theValue=sqrt(stat.var(polyValues[index]));
2248 else if(ruleMap[rule_opt[0]]==rule::median)
2249 theValue=stat.median(polyValues[index]);
2250 else if(ruleMap[rule_opt[0]]==rule::percentile)
2251 theValue=stat.percentile(polyValues[index],polyValues[index].begin(),polyValues[index].end(),percentile_opt[0]);
2252 else if(ruleMap[rule_opt[0]]==rule::sum)
2253 theValue=stat.sum(polyValues[index]);
2254 else if(ruleMap[rule_opt[0]]==rule::max)
2255 theValue=stat.mymax(polyValues[index]);
2256 else if(ruleMap[rule_opt[0]]==rule::min)
2257 theValue=stat.mymin(polyValues[index]);
2258 else if(ruleMap[rule_opt[0]]==rule::centroid){
2260 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2261 assert(nPointPolygon<=1);
2262 assert(nPointPolygon==polyValues[index].size());
2263 theValue=polyValues[index].back();
2266 std::string errorString=
"rule not supported";
2269 if(verbose_opt[0]>1)
2270 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
2271 switch( fieldType ){
2275 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
2277 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
2281 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2283 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2286 std::string errorString=
"field type not supported";
2291 catch(std::string e){
2292 std::cout << e << std::endl;
2299 if(ruleMap[rule_opt[0]]==rule::proportion){
2301 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2302 stat.normalize_pct(polyClassValues);
2303 for(
int index=0;index<polyClassValues.size();++index){
2304 double theValue=polyClassValues[index];
2306 fs << class_opt[index];
2308 writePolygonFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
2310 writeCentroidFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
2313 else if(ruleMap[rule_opt[0]]==rule::custom){
2314 assert(polygon_opt[0]);
2316 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2317 stat.normalize_pct(polyClassValues);
2318 assert(polyClassValues.size()==2);
2319 if(polyClassValues[0]>=75)
2320 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(11));
2321 else if(polyClassValues[1]>=75)
2322 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(12));
2323 else if(polyClassValues[0]>25&&polyClassValues[1]>25)
2324 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(13));
2327 std::cout <<
"No valid value in polyClassValues..." << std::endl;
2328 for(
int index=0;index<polyClassValues.size();++index){
2329 double theValue=polyClassValues[index];
2330 std::cout << theValue <<
" ";
2332 std::cout << std::endl;
2334 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(20));
2337 else if(ruleMap[rule_opt[0]]==rule::mode){
2340 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2342 int maxClass=stat.mymin(class_opt);
2343 vector<double>::iterator maxit;
2344 maxit=stat.mymax(polyClassValues,polyClassValues.begin(),polyClassValues.end());
2345 int maxIndex=distance(polyClassValues.begin(),maxit);
2346 maxClass=class_opt[maxIndex];
2347 if(verbose_opt[0]>0)
2348 std::cout <<
"maxClass: " << maxClass << std::endl;
2350 writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
2352 writeCentroidFeature->SetField(label_opt[0].c_str(),maxClass);
2357 if(verbose_opt[0]>1)
2358 std::cout <<
"creating polygon feature" << std::endl;
2360 if(writeTestLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
2361 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
2366 if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
2367 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
2371 OGRFeature::DestroyFeature( writePolygonFeature );
2374 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
2377 if(verbose_opt[0]>1)
2378 std::cout <<
"creating point feature in centroid" << std::endl;
2380 if(writeTestLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
2381 std::string errorString=
"Failed to create point feature in ogr vector dataset";
2387 assert(validFeature);
2388 if(writeLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
2389 std::string errorString=
"Failed to create point feature in ogr vector dataset";
2393 OGRFeature::DestroyFeature( writeCentroidFeature );
2396 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
2402 test=poGeometry->getGeometryName();
2404 oss <<
"geometry " << test <<
" not supported";
2408 progress=
static_cast<float>(ifeature+1)/nfeature;
2409 pfnProgress(progress,pszMessage,pProgressArg);
2411 catch(std::string e){
2412 std::cout << e << std::endl;
2417 std::cout <<
"number of points read in polygon: " << npoint << std::endl;
2424 pfnProgress(progress,pszMessage,pProgressArg);
2429 ogrTestWriter.close();
2432 pfnProgress(progress,pszMessage,pProgressArg);