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
102 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};
107 int main(
int argc,
char *argv[])
109 Optionpk<string> image_opt(
"i",
"input",
"Raster input dataset containing band information");
110 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.");
111 Optionpk<string> layer_opt(
"ln",
"ln",
"Layer name(s) in sample (leave empty to select all)");
112 Optionpk<unsigned int> random_opt(
"rand",
"random",
"Create simple random sample of points. Provide number of points to generate");
113 Optionpk<double> grid_opt(
"grid",
"grid",
"Create systematic grid of points. Provide cell grid size (in projected units, e.g,. m)");
115 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");
116 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);
117 Optionpk<double> percentile_opt(
"perc",
"perc",
"Percentile value used for rule percentile",95);
118 Optionpk<string> ogrformat_opt(
"f",
"f",
"Output sample dataset format",
"SQLite");
119 Optionpk<string> ftype_opt(
"ft",
"ftype",
"Field type (only Real or Integer)",
"Real");
120 Optionpk<string> ltype_opt(
"lt",
"ltype",
"Label type: In16 or String",
"Integer");
121 Optionpk<bool> polygon_opt(
"polygon",
"polygon",
"Create OGRPolygon as geometry instead of OGRPoint.",
false);
122 Optionpk<int> band_opt(
"b",
"band",
"Band index(es) to extract (0 based). Leave empty to use all bands");
123 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");
124 Optionpk<double> srcnodata_opt(
"srcnodata",
"srcnodata",
"Invalid value(s) for input image");
125 Optionpk<int> bndnodata_opt(
"bndnodata",
"bndnodata",
"Band(s) in input image to check if pixel is valid (used for srcnodata)", 0);
126 Optionpk<float> polythreshold_opt(
"tp",
"thresholdPolygon",
"(absolute) threshold for selecting samples in each polygon");
127 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");
128 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");
129 Optionpk<string> label_opt(
"cn",
"cname",
"Name of the class label in the output vector dataset",
"label");
130 Optionpk<short> geo_opt(
"geo",
"geo",
"Use geo coordinates (set to 0 to use image coordinates)", 1);
131 Optionpk<short> down_opt(
"down",
"down",
"Down sampling factor (for raster sample datasets only). Can be used to create grid points", 1);
132 Optionpk<short> buffer_opt(
"buf",
"buffer",
"Buffer for calculating statistics for point features ");
133 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);
134 Optionpk<short> verbose_opt(
"v",
"verbose",
"Verbose mode if > 0", 0,2);
136 bndnodata_opt.setHide(1);
137 srcnodata_opt.setHide(1);
138 polythreshold_opt.setHide(1);
139 percentile_opt.setHide(1);
141 fieldname_opt.setHide(1);
142 label_opt.setHide(1);
145 buffer_opt.setHide(1);
150 doProcess=image_opt.retrieveOption(argc,argv);
151 sample_opt.retrieveOption(argc,argv);
152 layer_opt.retrieveOption(argc,argv);
153 random_opt.retrieveOption(argc,argv);
154 grid_opt.retrieveOption(argc,argv);
155 output_opt.retrieveOption(argc,argv);
156 class_opt.retrieveOption(argc,argv);
157 threshold_opt.retrieveOption(argc,argv);
158 percentile_opt.retrieveOption(argc,argv);
159 ogrformat_opt.retrieveOption(argc,argv);
160 ftype_opt.retrieveOption(argc,argv);
161 ltype_opt.retrieveOption(argc,argv);
162 polygon_opt.retrieveOption(argc,argv);
163 band_opt.retrieveOption(argc,argv);
164 rule_opt.retrieveOption(argc,argv);
165 bndnodata_opt.retrieveOption(argc,argv);
166 srcnodata_opt.retrieveOption(argc,argv);
167 polythreshold_opt.retrieveOption(argc,argv);
168 test_opt.retrieveOption(argc,argv);
169 fieldname_opt.retrieveOption(argc,argv);
170 label_opt.retrieveOption(argc,argv);
171 geo_opt.retrieveOption(argc,argv);
172 down_opt.retrieveOption(argc,argv);
173 buffer_opt.retrieveOption(argc,argv);
174 disc_opt.retrieveOption(argc,argv);
177 verbose_opt.retrieveOption(argc,argv);
179 catch(
string predefinedString){
180 std::cout << predefinedString << std::endl;
185 cout <<
"Usage: pkextract -i input [-s sample | -rand number | -grid size] -o output" << endl;
187 std::cout <<
"short option -h shows basic options only, use long option --help to show all options" << std::endl;
191 std::map<std::string, rule::RULE_TYPE> ruleMap;
193 ruleMap[
"point"]=rule::point;
194 ruleMap[
"centroid"]=rule::centroid;
195 ruleMap[
"mean"]=rule::mean;
196 ruleMap[
"stdev"]=rule::stdev;
197 ruleMap[
"median"]=rule::median;
198 ruleMap[
"proportion"]=rule::proportion;
199 ruleMap[
"min"]=rule::min;
200 ruleMap[
"max"]=rule::max;
201 ruleMap[
"custom"]=rule::custom;
202 ruleMap[
"mode"]=rule::mode;
203 ruleMap[
"sum"]=rule::sum;
204 ruleMap[
"percentile"]=rule::percentile;
206 if(srcnodata_opt.size()){
207 while(srcnodata_opt.size()<bndnodata_opt.size())
208 srcnodata_opt.push_back(srcnodata_opt[0]);
209 while(bndnodata_opt.size()<srcnodata_opt.size())
210 bndnodata_opt.push_back(bndnodata_opt[0]);
214 std::cout << class_opt << std::endl;
216 stat.setNoDataValues(srcnodata_opt);
218 unsigned long int nsample=0;
219 unsigned long int ntotalvalid=0;
220 unsigned long int ntotalinvalid=0;
221 vector<unsigned long int> nvalid(class_opt.size());
222 vector<unsigned long int> ninvalid(class_opt.size());
223 for(
int it=0;it<nvalid.size();++it){
229 if(image_opt.empty()){
230 std::cerr <<
"No image dataset provided (use option -i). Use --help for help information";
233 if(output_opt.empty()){
234 std::cerr <<
"No output dataset provided (use option -o). Use --help for help information";
238 imgReader.open(image_opt[0]);
240 catch(std::string errorstring){
241 std::cout << errorstring << std::endl;
245 int nband=(band_opt.size()) ? band_opt.size() : imgReader.nrOfBand();
247 if(fieldname_opt.size()<nband){
248 std::string bandString=fieldname_opt[0];
249 fieldname_opt.clear();
250 fieldname_opt.resize(nband);
251 for(
int iband=0;iband<nband;++iband){
252 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
254 fs << bandString << theBand;
255 fieldname_opt[iband]=fs.str();
260 std::cout << fieldname_opt << std::endl;
263 std::cout <<
"Number of bands in input image: " << imgReader.nrOfBand() << std::endl;
265 OGRFieldType fieldType;
266 OGRFieldType labelType;
267 int ogr_typecount=11;
269 std::cout <<
"field and label types can be: ";
270 for(
int iType = 0; iType < ogr_typecount; ++iType){
272 std::cout <<
" " << OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType);
273 if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL
274 && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType),
275 ftype_opt[0].c_str()))
276 fieldType=(OGRFieldType) iType;
277 if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL
278 && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType),
279 ltype_opt[0].c_str()))
280 labelType=(OGRFieldType) iType;
288 std::cout << std::endl <<
"field type is: " << OGRFieldDefn::GetFieldTypeName(fieldType) << std::endl;
291 cerr <<
"field type " << OGRFieldDefn::GetFieldTypeName(fieldType) <<
" not supported" << std::endl;
301 std::cout << std::endl <<
"label type is: " << OGRFieldDefn::GetFieldTypeName(labelType) << std::endl;
304 cerr <<
"label type " << OGRFieldDefn::GetFieldTypeName(labelType) <<
" not supported" << std::endl;
309 const char* pszMessage;
310 void* pProgressArg=NULL;
311 GDALProgressFunc pfnProgress=GDALTermProgress;
315 bool sampleIsRaster=
false;
316 bool sampleIsVirtual=
false;
321 if(sample_opt.size()){
323 sampleReaderOgr.open(sample_opt[0]);
325 catch(
string errorString){
331 sampleWriterOgr.open(
"/vsimem/virtual",ogrformat_opt[0]);
333 catch(
string errorString){
334 cerr << errorString << endl;
336 char **papszOptions=NULL;
337 sampleWriterOgr.createLayer(
"points", imgReader.getProjection(), wkbPoint, papszOptions);
338 sampleIsVirtual=
true;
343 if(random_opt.size()){
346 double ulx,uly,lrx,lry;
347 imgReader.getBoundingBox(ulx,uly,lrx,lry);
348 for(
unsigned int ipoint=1;ipoint<=random_opt[0];++ipoint){
349 OGRFeature *pointFeature;
350 pointFeature=sampleWriterOgr.createFeature();
352 double theX=ulx+
static_cast<double>(rand())/(RAND_MAX)*(lrx-ulx);
353 double theY=uly-
static_cast<double>(rand())/(RAND_MAX)*(uly-lry);
356 pointFeature->SetGeometry( &pt );
357 if(sampleWriterOgr.createFeature(pointFeature) != OGRERR_NONE ){
358 cerr <<
"Failed to create feature in shapefile" << endl;
361 OGRFeature::DestroyFeature(pointFeature);
364 else if(grid_opt.size()){
367 double ulx,uly,lrx,lry;
368 imgReader.getBoundingBox(ulx,uly,lrx,lry);
369 unsigned int ipoint=0;
370 for(
double theY=uly-grid_opt[0]/2;theY>lry;theY-=grid_opt[0]){
371 for(
double theX=ulx+grid_opt[0]/2;theX<lrx;theX+=grid_opt[0]){
373 cout <<
"position: " << theX <<
" " << theY << endl;
374 OGRFeature *pointFeature;
375 pointFeature=sampleWriterOgr.createFeature();
379 pointFeature->SetGeometry( &pt );
380 if(sampleWriterOgr.createFeature(pointFeature) != OGRERR_NONE ){
381 cerr <<
"Failed to create feature in shapefile" << endl;
384 OGRFeature::DestroyFeature(pointFeature);
389 std::cerr <<
"No sample dataset provided (use option -s). Use --help for help information";
393 sampleWriterOgr.close();
394 sampleReaderOgr.open(
"/vsimem/virtual");
396 catch(
string errorString){
397 cerr << errorString << endl;
402 if(class_opt.empty()){
406 assert(sample_opt.size());
407 classReader.open(sample_opt[0]);
409 vector<double> classBuffer(classReader.nrOfCol());
411 vector<double> sample(2+nband);
414 vector<double> writeBufferClass;
415 vector<int> selectedClass;
421 std::cout <<
"extracting sample from image..." << std::endl;
423 pfnProgress(progress,pszMessage,pProgressArg);
424 for(irow=0;irow<classReader.nrOfRow();++irow){
428 classReader.readData(classBuffer,GDT_Float64,irow);
431 for(icol=0;icol<classReader.nrOfCol();++icol){
435 double theClass=classBuffer[icol];
456 classReader.image2geo(icol,irow,x,y);
459 if(verbose_opt[0]>1){
460 std::cout.precision(12);
461 std::cout << theClass <<
" " << x <<
" " << y << std::endl;
464 imgReader.geo2image(x,y,iimg,jimg);
466 jimg=
static_cast<int>(jimg);
467 iimg=
static_cast<int>(iimg);
468 if(static_cast<int>(iimg)<0||
static_cast<int>(iimg)>=imgReader.nrOfCol())
477 if(static_cast<int>(jimg)<0||
static_cast<int>(jimg)>=imgReader.nrOfRow())
482 if(static_cast<int>(jimg)!=
static_cast<int>(oldimgrow)){
483 assert(imgBuffer.size()==nband);
484 for(
int iband=0;iband<nband;++iband){
485 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
486 imgReader.readData(imgBuffer[iband],GDT_Float64,static_cast<int>(jimg),theBand);
487 assert(imgBuffer[iband].size()==imgReader.nrOfCol());
488 if(srcnodata_opt.size()){
489 vector<int>::const_iterator bndit=bndnodata_opt.begin();
490 vector<double>::const_iterator srcit=srcnodata_opt.begin();
491 while(bndit!=bndnodata_opt.end()&&srcit!=srcnodata_opt.end()){
492 if((*bndit==theBand)&&(*srcit==imgBuffer[iband][
static_cast<int>(iimg)])){
507 for(
int iband=0;iband<imgBuffer.size();++iband){
508 if(imgBuffer[iband].size()!=imgReader.nrOfCol()){
509 std::cout <<
"Error in band " << iband <<
": " << imgBuffer[iband].size() <<
"!=" << imgReader.nrOfCol() << std::endl;
510 assert(imgBuffer[iband].size()==imgReader.nrOfCol());
512 sample[iband+2]=imgBuffer[iband][
static_cast<int>(iimg)];
514 float theThreshold=(threshold_opt.size()>1)?threshold_opt[processClass]:threshold_opt[0];
516 double p=
static_cast<double>(rand())/(RAND_MAX);
521 else if(nvalid.size()>processClass){
522 if(nvalid[processClass]>=-theThreshold)
525 writeBuffer.push_back(sample);
526 writeBufferClass.push_back(theClass);
528 if(nvalid.size()>processClass)
529 ++(nvalid[processClass]);
533 if(ninvalid.size()>processClass)
534 ++(ninvalid[processClass]);
538 progress=
static_cast<float>(irow+1.0)/classReader.nrOfRow();
539 pfnProgress(progress,pszMessage,pProgressArg);
542 pfnProgress(progress,pszMessage,pProgressArg);
543 if(writeBuffer.size()>0){
544 assert(ntotalvalid==writeBuffer.size());
546 std::cout <<
"creating image sample writer " << output_opt[0] <<
" with " << writeBuffer.size() <<
" samples (" << ntotalinvalid <<
" invalid)" << std::endl;
547 ogrWriter.open(output_opt[0],ogrformat_opt[0]);
548 char **papszOptions=NULL;
549 ostringstream slayer;
550 slayer <<
"training data";
551 std::string layername=slayer.str();
552 ogrWriter.createLayer(layername, imgReader.getProjection(), wkbPoint, papszOptions);
553 std::string fieldname=
"fid";
554 ogrWriter.createField(fieldname,OFTInteger);
555 map<std::string,double> pointAttributes;
556 ogrWriter.createField(label_opt[0],labelType);
557 for(
int iband=0;iband<nband;++iband){
558 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
559 ogrWriter.createField(fieldname_opt[iband],fieldType);
561 std::cout <<
"writing sample to " << output_opt[0] <<
"..." << std::endl;
563 pfnProgress(progress,pszMessage,pProgressArg);
564 for(
int isample=0;isample<writeBuffer.size();++isample){
566 std::cout <<
"writing sample " << isample << std::endl;
567 pointAttributes[label_opt[0]]=writeBufferClass[isample];
568 for(
int iband=0;iband<writeBuffer[0].size()-2;++iband){
569 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
576 pointAttributes[fieldname_opt[iband]]=writeBuffer[isample][iband+2];
579 std::cout <<
"all bands written" << std::endl;
580 ogrWriter.addPoint(writeBuffer[isample][0],writeBuffer[isample][1],pointAttributes,fieldname,isample);
581 progress=
static_cast<float>(isample+1.0)/writeBuffer.size();
582 pfnProgress(progress,pszMessage,pProgressArg);
587 std::cout <<
"No data found for any class " << std::endl;
590 nsample=writeBuffer.size();
592 std::cout <<
"total number of samples written: " << nsample << std::endl;
595 assert(class_opt[0]);
597 assert(threshold_opt.size()==1||threshold_opt.size()==class_opt.size());
600 if(verbose_opt[0]>1){
601 std::cout <<
"reading position from sample dataset " << std::endl;
602 std::cout <<
"class thresholds: " << std::endl;
603 for(
int iclass=0;iclass<class_opt.size();++iclass){
604 if(threshold_opt.size()>1)
605 std::cout << class_opt[iclass] <<
": " << threshold_opt[iclass] << std::endl;
607 std::cout << class_opt[iclass] <<
": " << threshold_opt[0] << std::endl;
610 classReader.open(sample_opt[0]);
611 vector<int> classBuffer(classReader.nrOfCol());
614 vector<double> sample(2+nband);
616 vector<int> writeBufferClass;
618 vector<int> selectedClass;
624 std::cout <<
"extracting sample from image..." << std::endl;
626 pfnProgress(progress,pszMessage,pProgressArg);
627 for(irow=0;irow<classReader.nrOfRow();++irow){
630 classReader.readData(classBuffer,GDT_Int32,irow);
634 for(icol=0;icol<classReader.nrOfCol();++icol){
640 if(class_opt.empty()){
641 if(classBuffer[icol]){
643 theClass=classBuffer[icol];
647 for(
int iclass=0;iclass<class_opt.size();++iclass){
648 if(classBuffer[icol]==class_opt[iclass]){
650 theClass=class_opt[iclass];
657 classReader.image2geo(icol,irow,x,y);
660 if(verbose_opt[0]>1){
661 std::cout.precision(12);
662 std::cout << theClass <<
" " << x <<
" " << y << std::endl;
665 imgReader.geo2image(x,y,iimg,jimg);
667 jimg=
static_cast<int>(jimg);
668 iimg=
static_cast<int>(iimg);
669 if(static_cast<int>(iimg)<0||
static_cast<int>(iimg)>=imgReader.nrOfCol())
678 if(static_cast<int>(jimg)<0||
static_cast<int>(jimg)>=imgReader.nrOfRow())
683 if(static_cast<int>(jimg)!=
static_cast<int>(oldimgrow)){
684 assert(imgBuffer.size()==nband);
685 for(
int iband=0;iband<nband;++iband){
686 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
687 imgReader.readData(imgBuffer[iband],GDT_Float64,static_cast<int>(jimg),theBand);
688 assert(imgBuffer[iband].size()==imgReader.nrOfCol());
690 if(srcnodata_opt.size()){
691 vector<int>::const_iterator bndit=bndnodata_opt.begin();
692 vector<double>::const_iterator srcit=srcnodata_opt.begin();
693 while(bndit!=bndnodata_opt.end()&&srcit!=srcnodata_opt.end()){
694 if((*bndit==theBand)&&(*srcit==imgBuffer[iband][
static_cast<int>(iimg)])){
708 for(
int iband=0;iband<imgBuffer.size();++iband){
709 if(imgBuffer[iband].size()!=imgReader.nrOfCol()){
710 std::cout <<
"Error in band " << iband <<
": " << imgBuffer[iband].size() <<
"!=" << imgReader.nrOfCol() << std::endl;
711 assert(imgBuffer[iband].size()==imgReader.nrOfCol());
713 sample[iband+2]=imgBuffer[iband][
static_cast<int>(iimg)];
715 float theThreshold=(threshold_opt.size()>1)?threshold_opt[processClass]:threshold_opt[0];
717 double p=
static_cast<double>(rand())/(RAND_MAX);
722 else if(nvalid.size()>processClass){
723 if(nvalid[processClass]>=-theThreshold)
726 writeBuffer.push_back(sample);
728 writeBufferClass.push_back(theClass);
730 if(nvalid.size()>processClass)
731 ++(nvalid[processClass]);
735 if(ninvalid.size()>processClass)
736 ++(ninvalid[processClass]);
740 progress=
static_cast<float>(irow+1.0)/classReader.nrOfRow();
741 pfnProgress(progress,pszMessage,pProgressArg);
743 if(writeBuffer.size()>0){
744 assert(ntotalvalid==writeBuffer.size());
746 std::cout <<
"creating image sample writer " << output_opt[0] <<
" with " << writeBuffer.size() <<
" samples (" << ntotalinvalid <<
" invalid)" << std::endl;
747 ogrWriter.open(output_opt[0],ogrformat_opt[0]);
748 char **papszOptions=NULL;
749 ostringstream slayer;
750 slayer <<
"training data";
751 std::string layername=slayer.str();
752 ogrWriter.createLayer(layername, imgReader.getProjection(), wkbPoint, papszOptions);
753 std::string fieldname=
"fid";
754 ogrWriter.createField(fieldname,OFTInteger);
755 map<std::string,double> pointAttributes;
757 ogrWriter.createField(label_opt[0],labelType);
758 for(
int iband=0;iband<nband;++iband){
759 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
766 ogrWriter.createField(fieldname_opt[iband],fieldType);
768 pfnProgress(progress,pszMessage,pProgressArg);
769 std::cout <<
"writing sample to " << output_opt[0] <<
"..." << std::endl;
771 pfnProgress(progress,pszMessage,pProgressArg);
772 for(
int isample=0;isample<writeBuffer.size();++isample){
773 pointAttributes[label_opt[0]]=writeBufferClass[isample];
774 for(
int iband=0;iband<writeBuffer[0].size()-2;++iband){
775 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
782 pointAttributes[fieldname_opt[iband]]=writeBuffer[isample][iband+2];
784 ogrWriter.addPoint(writeBuffer[isample][0],writeBuffer[isample][1],pointAttributes,fieldname,isample);
785 progress=
static_cast<float>(isample+1.0)/writeBuffer.size();
786 pfnProgress(progress,pszMessage,pProgressArg);
791 std::cout <<
"No data found for any class " << std::endl;
794 nsample=writeBuffer.size();
796 std::cout <<
"total number of samples written: " << nsample << std::endl;
797 if(nvalid.size()==class_opt.size()){
798 for(
int iclass=0;iclass<class_opt.size();++iclass)
799 std::cout <<
"class " << class_opt[iclass] <<
" has " << nvalid[iclass] <<
" samples" << std::endl;
806 std::cout <<
"creating image sample writer " << output_opt[0] << std::endl;
809 ogrWriter.open(output_opt[0],ogrformat_opt[0]);
812 std::cout <<
"creating image test writer " << test_opt[0] << std::endl;
814 ogrTestWriter.open(test_opt[0],ogrformat_opt[0]);
816 catch(
string errorString){
817 cerr << errorString << endl;
821 int nlayerRead=sampleReaderOgr.getDataSource()->GetLayerCount();
824 std::cout <<
"number of layers: " << nlayerRead << endl;
826 for(
int ilayer=0;ilayer<nlayerRead;++ilayer){
827 OGRLayer *readLayer=sampleReaderOgr.getLayer(ilayer);
828 string currentLayername=readLayer->GetName();
830 if(find(layer_opt.begin(),layer_opt.end(),currentLayername)==layer_opt.end())
832 cout <<
"processing layer " << currentLayername << endl;
834 readLayer->ResetReading();
835 OGRLayer *writeLayer;
836 OGRLayer *writeTestLayer;
840 std::cout <<
"create polygons" << std::endl;
841 char **papszOptions=NULL;
842 writeLayer=ogrWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPolygon, papszOptions);
844 writeTestLayer=ogrTestWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPolygon, papszOptions);
848 std::cout <<
"create points in layer " << readLayer->GetName() << std::endl;
849 char **papszOptions=NULL;
851 writeLayer=ogrWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPoint, papszOptions);
853 char **papszOptions=NULL;
854 writeTestLayer=ogrTestWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPoint, papszOptions);
858 std::cout <<
"copy fields from layer " << ilayer << std::flush << std::endl;
859 ogrWriter.copyFields(sampleReaderOgr,ilayer,ilayerWrite);
863 std::cout <<
"copy fields test writer" << std::flush << std::endl;
864 ogrTestWriter.copyFields(sampleReaderOgr,ilayer,ilayerWrite);
873 if(class_opt.size()){
874 switch(ruleMap[rule_opt[0]]){
875 case(rule::proportion):{
876 for(
int iclass=0;iclass<class_opt.size();++iclass){
878 cs << class_opt[iclass];
879 ogrWriter.createField(cs.str(),fieldType,ilayerWrite);
885 ogrWriter.createField(label_opt[0],fieldType,ilayerWrite);
887 ogrTestWriter.createField(label_opt[0],fieldType,ilayerWrite);
892 for(
int iband=0;iband<nband;++iband){
893 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
895 fs << fieldname_opt[iband];
897 std::cout <<
"creating field " << fs.str() << std::endl;
899 ogrWriter.createField(fs.str(),fieldType,ilayerWrite);
901 ogrTestWriter.createField(fs.str(),fieldType,ilayerWrite);
904 OGRFeature *readFeature;
905 unsigned long int ifeature=0;
906 unsigned long int nfeature=sampleReaderOgr.getFeatureCount();
908 pfnProgress(progress,pszMessage,pProgressArg);
909 while( (readFeature = readLayer->GetNextFeature()) != NULL ){
910 bool validFeature=
false;
911 bool writeTest=
false;
913 std::cout <<
"reading feature " << readFeature->GetFID() << std::endl;
914 if(threshold_opt[0]>0){
915 double p=
static_cast<double>(rand())/(RAND_MAX);
917 if(p>threshold_opt[0]){
925 if(ntotalvalid>=-threshold_opt[0]){
933 std::cout <<
"processing feature " << readFeature->GetFID() << std::endl;
936 OGRGeometry *poGeometry;
937 poGeometry = readFeature->GetGeometryRef();
938 assert(poGeometry!=NULL);
940 if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint ){
942 if(!buffer_opt.size()){
943 switch(ruleMap[rule_opt[0]]){
945 case(rule::centroid):
946 buffer_opt.push_back(1);
949 buffer_opt.push_back(3);
954 std::cout <<
"boundary: " << buffer_opt[0] << std::endl;
956 OGRPolygon writePolygon;
957 OGRLinearRing writeRing;
958 OGRPoint writeCentroidPoint;
959 OGRFeature *writePolygonFeature;
960 OGRFeature *writeCentroidFeature;
962 OGRPoint *poPoint = (OGRPoint *) poGeometry;
963 writeCentroidPoint=*poPoint;
968 double i_centre,j_centre;
970 imgReader.geo2image(x,y,i_centre,j_centre);
976 j_centre=
static_cast<int>(j_centre);
977 i_centre=
static_cast<int>(i_centre);
979 double uli=i_centre-buffer_opt[0]/2;
980 double ulj=j_centre-buffer_opt[0]/2;
981 double lri=i_centre+buffer_opt[0]/2;
982 double lrj=j_centre+buffer_opt[0]/2;
985 ulj=
static_cast<int>(ulj);
986 uli=
static_cast<int>(uli);
987 lrj=
static_cast<int>(lrj);
988 lri=
static_cast<int>(lri);
998 if(static_cast<int>(ulj)<0||
static_cast<int>(ulj)>=imgReader.nrOfRow())
1001 if(static_cast<int>(uli)<0||
static_cast<int>(lri)>=imgReader.nrOfCol())
1004 OGRPoint ulPoint,urPoint,llPoint,lrPoint;
1011 double radius=buffer_opt[0]/2.0*sqrt(imgReader.getDeltaX()*imgReader.getDeltaY());
1012 unsigned short nstep = 25;
1013 for(
int i=0;i<nstep;++i){
1015 aPoint.setX(x+imgReader.getDeltaX()/2.0+radius*cos(2*PI*i/nstep));
1016 aPoint.setY(y-imgReader.getDeltaY()/2.0+radius*sin(2*PI*i/nstep));
1017 writeRing.addPoint(&aPoint);
1019 writePolygon.addRing(&writeRing);
1020 writePolygon.closeRings();
1025 imgReader.image2geo(uli,ulj,ulx,uly);
1026 imgReader.image2geo(lri,lrj,lrx,lry);
1027 ulPoint.setX(ulx-imgReader.getDeltaX()/2.0);
1028 ulPoint.setY(uly+imgReader.getDeltaY()/2.0);
1029 lrPoint.setX(lrx+imgReader.getDeltaX()/2.0);
1030 lrPoint.setY(lry-imgReader.getDeltaY()/2.0);
1031 urPoint.setX(lrx+imgReader.getDeltaX()/2.0);
1032 urPoint.setY(uly+imgReader.getDeltaY()/2.0);
1033 llPoint.setX(ulx-imgReader.getDeltaX()/2.0);
1034 llPoint.setY(lry-imgReader.getDeltaY()/2.0);
1036 writeRing.addPoint(&ulPoint);
1037 writeRing.addPoint(&urPoint);
1038 writeRing.addPoint(&lrPoint);
1039 writeRing.addPoint(&llPoint);
1040 writePolygon.addRing(&writeRing);
1041 writePolygon.closeRings();
1045 if((polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point)||(ruleMap[rule_opt[0]]==rule::centroid)){
1055 writePolygonFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1057 writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1059 else if(ruleMap[rule_opt[0]]!=rule::point){
1061 writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1063 writeCentroidFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1066 vector<double> windowClassValues;
1068 if(class_opt.size()){
1069 windowClassValues.resize(class_opt.size());
1071 for(
int iclass=0;iclass<class_opt.size();++iclass)
1072 windowClassValues[iclass]=0;
1075 windowValues.resize(nband);
1076 vector< Vector2d<double> > readValues(nband);
1077 for(
int iband=0;iband<nband;++iband){
1078 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1081 assert(uli<imgReader.nrOfCol());
1083 assert(lri<imgReader.nrOfCol());
1085 assert(ulj<imgReader.nrOfRow());
1087 assert(lrj<imgReader.nrOfRow());
1088 imgReader.readDataBlock(readValues[iband],GDT_Float64,uli,lri,ulj,lrj,theBand);
1092 for(
int j=ulj;j<=lrj;++j){
1093 for(
int i=uli;i<=lri;++i){
1095 if(i<0||i>=imgReader.nrOfCol())
1097 if(j<0||j>=imgReader.nrOfRow())
1102 imgReader.image2geo(i,j,theX,theY);
1103 thePoint.setX(theX);
1104 thePoint.setY(theY);
1106 if(disc_opt[0]&&buffer_opt[0]>1){
1107 double radius=buffer_opt[0]/2.0*sqrt(imgReader.getDeltaX()*imgReader.getDeltaY());
1108 if((theX-x)*(theX-x)+(theY-y)*(theY-y)>radius*radius)
1113 if(srcnodata_opt.size()){
1114 for(
int vband=0;vband<bndnodata_opt.size();++vband){
1115 double value=((readValues[bndnodata_opt[vband]])[j-ulj])[i-uli];
1116 if(value==srcnodata_opt[vband]){
1131 OGRFeature *writePointFeature;
1132 if(!polygon_opt[0]){
1134 if(ruleMap[rule_opt[0]]==rule::point){
1136 writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1138 writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1139 if(verbose_opt[0]>1)
1140 std::cout <<
"copying fields from polygons " << std::endl;
1145 if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE)
1146 cerr <<
"writing feature failed" << std::endl;
1148 assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
1152 if(verbose_opt[0]>1)
1153 std::cout <<
"write feature has " << writePointFeature->GetFieldCount() <<
" fields" << std::endl;
1156 if(class_opt.size()){
1157 short value=((readValues[0])[j-ulj])[i-uli];
1158 for(
int iclass=0;iclass<class_opt.size();++iclass){
1159 if(value==class_opt[iclass])
1160 windowClassValues[iclass]+=1;
1164 for(
int iband=0;iband<nband;++iband){
1165 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1167 assert(j-ulj<readValues[iband].size());
1169 assert(i-uli<((readValues[iband])[j-ulj]).size());
1170 double value=((readValues[iband])[j-ulj])[i-uli];
1172 if(verbose_opt[0]>1)
1173 std::cout <<
": " << value << std::endl;
1174 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
1175 windowValues[iband].push_back(value);
1179 if(verbose_opt[0]>1)
1180 std::cout <<
"set field " << fieldname_opt[iband] <<
" to " << value << std::endl;
1181 switch( fieldType ){
1184 writePointFeature->SetField(fieldname_opt[iband].c_str(),value);
1187 writePointFeature->SetField(fieldname_opt[iband].c_str(),type2string<double>(value).c_str());
1190 std::string errorString=
"field type not supported";
1195 catch(std::string e){
1196 std::cout << e << std::endl;
1202 if(!polygon_opt[0]){
1203 if(ruleMap[rule_opt[0]]==rule::point){
1205 if(verbose_opt[0]>1)
1206 std::cout <<
"creating point feature" << std::endl;
1208 if(writeTestLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1209 std::string errorString=
"Failed to create feature in test ogr vector dataset";
1214 if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1215 std::string errorString=
"Failed to create feature in ogr vector dataset";
1220 OGRFeature::DestroyFeature( writePointFeature );
1223 std::cout <<
"ntotalvalid(2): " << ntotalvalid << std::endl;
1228 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
1232 cout <<
"no points found in window, continuing" << endl;
1242 if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE)
1243 cerr <<
"writing feature failed" << std::endl;
1245 writePolygonFeature->SetGeometry(&writePolygon);
1246 assert(wkbFlatten(writePolygonFeature->GetGeometryRef()->getGeometryType()) == wkbPolygon);
1248 if(verbose_opt[0]>1)
1249 std::cout <<
"copying new fields write polygon " << std::endl;
1250 if(verbose_opt[0]>1)
1251 std::cout <<
"write feature has " << writePolygonFeature->GetFieldCount() <<
" fields" << std::endl;
1256 if(verbose_opt[0]>1)
1257 std::cout <<
"copying fields from polygons " << std::endl;
1262 if(writeCentroidFeature->SetFrom(readFeature)!= OGRERR_NONE)
1263 cerr <<
"writing feature failed" << std::endl;
1264 assert(wkbFlatten(writeCentroidFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
1269 if(verbose_opt[0]>1)
1270 std::cout <<
"write feature has " << writeCentroidFeature->GetFieldCount() <<
" fields" << std::endl;
1272 if(class_opt.empty()){
1273 if(ruleMap[rule_opt[0]]==rule::point){
1275 std::cout <<
"number of points in window: " << nPointWindow << std::endl;
1276 for(
int index=0;index<windowValues.size();++index){
1278 if(windowValues[index].size()!=1){
1279 cerr <<
"Error: windowValues[index].size()=" << windowValues[index].size() << endl;
1280 assert(windowValues[index].size()==1);
1282 double theValue=windowValues[index].back();
1285 std::cout <<
"number of points in window: " << nPointWindow << std::endl;
1286 int theBand=(band_opt.size()) ? band_opt[index] : index;
1289 if(verbose_opt[0]>1)
1290 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1291 switch( fieldType ){
1295 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1297 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1301 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1303 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1306 std::string errorString=
"field type not supported";
1311 catch(std::string e){
1312 std::cout << e << std::endl;
1319 for(
int index=0;index<windowValues.size();++index){
1321 if(ruleMap[rule_opt[0]]==rule::mean)
1322 theValue=stat.mean(windowValues[index]);
1323 else if(ruleMap[rule_opt[0]]==rule::stdev)
1324 theValue=sqrt(stat.var(windowValues[index]));
1325 else if(ruleMap[rule_opt[0]]==rule::median)
1326 theValue=stat.median(windowValues[index]);
1327 else if(ruleMap[rule_opt[0]]==rule::percentile)
1328 theValue=stat.percentile(windowValues[index],windowValues[index].begin(),windowValues[index].end(),percentile_opt[0]);
1329 else if(ruleMap[rule_opt[0]]==rule::sum)
1330 theValue=stat.sum(windowValues[index]);
1331 else if(ruleMap[rule_opt[0]]==rule::max)
1332 theValue=stat.mymax(windowValues[index]);
1333 else if(ruleMap[rule_opt[0]]==rule::min)
1334 theValue=stat.mymin(windowValues[index]);
1335 else if(ruleMap[rule_opt[0]]==rule::centroid){
1337 std::cout <<
"number of points in polygon: " << nPointWindow << std::endl;
1338 assert(nPointWindow<=1);
1339 assert(nPointWindow==windowValues[index].size());
1340 theValue=windowValues[index].back();
1343 std::string errorString=
"rule not supported";
1346 if(verbose_opt[0]>1)
1347 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1348 switch( fieldType ){
1352 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1354 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1358 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1360 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1363 std::string errorString=
"field type not supported";
1368 catch(std::string e){
1369 std::cout << e << std::endl;
1376 if(ruleMap[rule_opt[0]]==rule::proportion){
1378 std::cout <<
"number of points in polygon: " << nPointWindow << std::endl;
1379 stat.normalize_pct(windowClassValues);
1380 for(
int index=0;index<windowClassValues.size();++index){
1381 double theValue=windowClassValues[index];
1383 fs << class_opt[index];
1385 writePolygonFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1387 writeCentroidFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1390 else if(ruleMap[rule_opt[0]]==rule::custom){
1391 assert(polygon_opt[0]);
1393 std::cout <<
"number of points in polygon: " << nPointWindow << std::endl;
1394 stat.normalize_pct(windowClassValues);
1395 assert(windowClassValues.size()==2);
1396 if(windowClassValues[0]>=75)
1397 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(11));
1398 else if(windowClassValues[1]>=75)
1399 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(12));
1400 else if(windowClassValues[0]>25&&windowClassValues[1]>25)
1401 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(13));
1404 std::cout <<
"No valid value in windowClassValues..." << std::endl;
1405 for(
int index=0;index<windowClassValues.size();++index){
1406 double theValue=windowClassValues[index];
1407 std::cout << theValue <<
" ";
1409 std::cout << std::endl;
1411 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(20));
1414 else if(ruleMap[rule_opt[0]]==rule::mode){
1417 std::cout <<
"number of points in window: " << nPointWindow << std::endl;
1419 int maxClass=stat.mymin(class_opt);
1420 vector<double>::iterator maxit;
1421 maxit=stat.mymax(windowClassValues,windowClassValues.begin(),windowClassValues.end());
1422 int maxIndex=distance(windowClassValues.begin(),maxit);
1423 maxClass=class_opt[maxIndex];
1424 if(verbose_opt[0]>0)
1425 std::cout <<
"maxClass: " << maxClass << std::endl;
1427 writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
1429 writeCentroidFeature->SetField(label_opt[0].c_str(),maxClass);
1433 if(verbose_opt[0]>1)
1434 std::cout <<
"creating polygon feature" << std::endl;
1436 if(writeTestLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1437 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1442 if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1443 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1447 OGRFeature::DestroyFeature( writePolygonFeature );
1450 std::cout <<
"ntotalvalid(1): " << ntotalvalid << std::endl;
1453 if(verbose_opt[0]>1)
1454 std::cout <<
"creating point feature in centroid" << std::endl;
1456 if(writeTestLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1457 std::string errorString=
"Failed to create point feature in ogr vector dataset";
1463 assert(validFeature);
1464 if(writeLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1465 std::string errorString=
"Failed to create point feature in ogr vector dataset";
1469 OGRFeature::DestroyFeature( writeCentroidFeature );
1472 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
1476 else if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){
1478 OGRPolygon readPolygon = *((OGRPolygon *) poGeometry);
1479 OGRPolygon writePolygon;
1480 OGRLinearRing writeRing;
1481 OGRPoint writeCentroidPoint;
1482 OGRFeature *writePolygonFeature;
1483 OGRFeature *writeCentroidFeature;
1485 readPolygon.closeRings();
1487 if(verbose_opt[0]>1)
1488 std::cout <<
"get point on polygon" << std::endl;
1489 if(ruleMap[rule_opt[0]]==rule::centroid)
1490 readPolygon.Centroid(&writeCentroidPoint);
1492 readPolygon.PointOnSurface(&writeCentroidPoint);
1494 double ulx,uly,lrx,lry;
1495 double uli,ulj,lri,lrj;
1496 if((polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point)||(ruleMap[rule_opt[0]]==rule::centroid)){
1497 ulx=writeCentroidPoint.getX();
1498 uly=writeCentroidPoint.getY();
1505 std::cout <<
"reading envelope for polygon " << ifeature << std::endl;
1506 OGREnvelope* psEnvelope=
new OGREnvelope();
1507 readPolygon.getEnvelope(psEnvelope);
1508 ulx=psEnvelope->MinX;
1509 uly=psEnvelope->MaxY;
1510 lrx=psEnvelope->MaxX;
1511 lry=psEnvelope->MinY;
1515 imgReader.geo2image(ulx,uly,uli,ulj);
1516 imgReader.geo2image(lrx,lry,lri,lrj);
1525 ulj=
static_cast<int>(ulj);
1526 uli=
static_cast<int>(uli);
1527 lrj=
static_cast<int>(lrj);
1528 lri=
static_cast<int>(lri);
1530 if(verbose_opt[0]>1)
1531 std::cout <<
"bounding box for polygon feature " << ifeature <<
": " << uli <<
" " << ulj <<
" " << lri <<
" " << lrj << std::endl;
1537 if(uli>=imgReader.nrOfCol())
1538 uli=imgReader.nrOfCol()-1;
1539 if(lri>=imgReader.nrOfCol())
1540 lri=imgReader.nrOfCol()-1;
1545 if(ulj>=imgReader.nrOfRow())
1546 ulj=imgReader.nrOfRow()-1;
1547 if(lrj>=imgReader.nrOfRow())
1548 lrj=imgReader.nrOfRow()-1;
1552 int nPointPolygon=0;
1556 writePolygonFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1558 writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1560 else if(ruleMap[rule_opt[0]]!=rule::point){
1562 writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1564 writeCentroidFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1568 vector<double> polyClassValues;
1570 if(class_opt.size()){
1572 polyClassValues.resize(class_opt.size());
1574 for(
int iclass=0;iclass<class_opt.size();++iclass)
1575 polyClassValues[iclass]=0;
1578 polyValues.resize(nband);
1579 vector< Vector2d<double> > readValues(nband);
1580 for(
int iband=0;iband<nband;++iband){
1581 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1584 assert(uli<imgReader.nrOfCol());
1586 assert(lri<imgReader.nrOfCol());
1588 assert(ulj<imgReader.nrOfRow());
1590 assert(lrj<imgReader.nrOfRow());
1591 imgReader.readDataBlock(readValues[iband],GDT_Float64,uli,lri,ulj,lrj,theBand);
1595 for(
int j=ulj;j<=lrj;++j){
1596 for(
int i=uli;i<=lri;++i){
1598 if(i<0||i>=imgReader.nrOfCol())
1600 if(j<0||j>=imgReader.nrOfRow())
1605 imgReader.image2geo(i,j,theX,theY);
1606 thePoint.setX(theX);
1607 thePoint.setY(theY);
1609 if(ruleMap[rule_opt[0]]!=rule::centroid&&!readPolygon.Contains(&thePoint))
1614 if(srcnodata_opt.size()){
1615 for(
int vband=0;vband<bndnodata_opt.size();++vband){
1616 double value=((readValues[bndnodata_opt[vband]])[j-ulj])[i-uli];
1617 if(value==srcnodata_opt[vband]){
1629 writeRing.addPoint(&thePoint);
1632 if(verbose_opt[0]>1)
1633 std::cout <<
"point is on surface:" << thePoint.getX() <<
"," << thePoint.getY() << std::endl;
1636 if(polythreshold_opt.size())
1637 if(nPointPolygon>polythreshold_opt[0])
1640 OGRFeature *writePointFeature;
1641 if(!polygon_opt[0]){
1643 if(ruleMap[rule_opt[0]]==rule::point){
1645 writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1647 writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1648 if(verbose_opt[0]>1)
1649 std::cout <<
"copying fields from polygons " << std::endl;
1650 if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE)
1651 cerr <<
"writing feature failed" << std::endl;
1652 if(verbose_opt[0]>1)
1653 std::cout <<
"set geometry as point " << std::endl;
1655 writePointFeature->SetGeometry(&thePoint);
1656 assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
1661 if(verbose_opt[0]>1)
1662 std::cout <<
"write feature has " << writePointFeature->GetFieldCount() <<
" fields" << std::endl;
1665 if(class_opt.size()){
1666 short value=((readValues[0])[j-ulj])[i-uli];
1667 for(
int iclass=0;iclass<class_opt.size();++iclass){
1668 if(value==class_opt[iclass])
1669 polyClassValues[iclass]+=1;
1673 for(
int iband=0;iband<nband;++iband){
1674 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1676 assert(j-ulj<readValues[iband].size());
1678 assert(i-uli<((readValues[iband])[j-ulj]).size());
1679 double value=((readValues[iband])[j-ulj])[i-uli];
1681 if(verbose_opt[0]>1)
1682 std::cout <<
": " << value << std::endl;
1683 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point)
1684 polyValues[iband].push_back(value);
1686 if(verbose_opt[0]>1)
1687 std::cout <<
"set field " << fieldname_opt[iband] <<
" to " << value << std::endl;
1688 switch( fieldType ){
1691 writePointFeature->SetField(fieldname_opt[iband].c_str(),value);
1694 writePointFeature->SetField(fieldname_opt[iband].c_str(),type2string<double>(value).c_str());
1703 if(!polygon_opt[0]){
1704 if(ruleMap[rule_opt[0]]==rule::point){
1706 if(verbose_opt[0]>1)
1707 std::cout <<
"creating point feature" << std::endl;
1709 if(writeTestLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1710 std::string errorString=
"Failed to create feature in test ogr vector dataset";
1715 if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1716 std::string errorString=
"Failed to create feature in ogr vector dataset";
1721 OGRFeature::DestroyFeature( writePointFeature );
1724 std::cout <<
"ntotalvalid(2): " << ntotalvalid << std::endl;
1729 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
1733 cout <<
"no points found in polygon, continuing" << endl;
1738 writePolygon.addRing(&writeRing);
1739 writePolygon.closeRings();
1744 if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE)
1745 cerr <<
"writing feature failed" << std::endl;
1746 if(verbose_opt[0]>1)
1747 std::cout <<
"copying new fields write polygon " << std::endl;
1748 if(verbose_opt[0]>1)
1749 std::cout <<
"write feature has " << writePolygonFeature->GetFieldCount() <<
" fields" << std::endl;
1754 if(verbose_opt[0]>1)
1755 std::cout <<
"copying fields from polygons " << std::endl;
1758 if(writeCentroidFeature->SetFrom(readFeature)!= OGRERR_NONE)
1759 cerr <<
"writing feature failed" << std::endl;
1761 writeCentroidFeature->SetGeometry(&writeCentroidPoint);
1762 assert(wkbFlatten(writeCentroidFeature->GetGeometryRef()->getGeometryType()) == wkbPoint );
1766 if(verbose_opt[0]>1)
1767 std::cout <<
"write feature has " << writeCentroidFeature->GetFieldCount() <<
" fields" << std::endl;
1769 if(class_opt.empty()){
1770 if(ruleMap[rule_opt[0]]==rule::point){
1772 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1773 for(
int index=0;index<polyValues.size();++index){
1774 assert(polyValues[index].size()==1);
1775 double theValue=polyValues[index].back();
1778 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1779 int theBand=(band_opt.size()) ? band_opt[index] : index;
1781 if(verbose_opt[0]>1)
1782 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1783 switch( fieldType ){
1787 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1789 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1793 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1795 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1798 std::string errorString=
"field type not supported";
1803 catch(std::string e){
1804 std::cout << e << std::endl;
1811 for(
int index=0;index<polyValues.size();++index){
1813 if(ruleMap[rule_opt[0]]==rule::mean)
1814 theValue=stat.mean(polyValues[index]);
1815 else if(ruleMap[rule_opt[0]]==rule::stdev)
1816 theValue=sqrt(stat.var(polyValues[index]));
1817 else if(ruleMap[rule_opt[0]]==rule::median)
1818 theValue=stat.median(polyValues[index]);
1819 else if(ruleMap[rule_opt[0]]==rule::percentile)
1820 theValue=stat.percentile(polyValues[index],polyValues[index].begin(),polyValues[index].end(),percentile_opt[0]);
1821 else if(ruleMap[rule_opt[0]]==rule::sum)
1822 theValue=stat.sum(polyValues[index]);
1823 else if(ruleMap[rule_opt[0]]==rule::max)
1824 theValue=stat.mymax(polyValues[index]);
1825 else if(ruleMap[rule_opt[0]]==rule::min)
1826 theValue=stat.mymin(polyValues[index]);
1827 else if(ruleMap[rule_opt[0]]==rule::centroid){
1829 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1830 assert(nPointPolygon<=1);
1831 assert(nPointPolygon==polyValues[index].size());
1832 theValue=polyValues[index].back();
1835 std::string errorString=
"rule not supported";
1838 if(verbose_opt[0]>1)
1839 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1840 switch( fieldType ){
1844 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1846 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1850 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1852 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1855 std::string errorString=
"field type not supported";
1860 catch(std::string e){
1861 std::cout << e << std::endl;
1868 if(ruleMap[rule_opt[0]]==rule::proportion){
1870 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1871 stat.normalize_pct(polyClassValues);
1872 for(
int index=0;index<polyClassValues.size();++index){
1873 double theValue=polyClassValues[index];
1875 fs << class_opt[index];
1877 writePolygonFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1879 writeCentroidFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1882 else if(ruleMap[rule_opt[0]]==rule::custom){
1883 assert(polygon_opt[0]);
1885 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1886 stat.normalize_pct(polyClassValues);
1887 assert(polyClassValues.size()==2);
1888 if(polyClassValues[0]>=75)
1889 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(11));
1890 else if(polyClassValues[1]>=75)
1891 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(12));
1892 else if(polyClassValues[0]>25&&polyClassValues[1]>25)
1893 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(13));
1896 std::cout <<
"No valid value in polyClassValues..." << std::endl;
1897 for(
int index=0;index<polyClassValues.size();++index){
1898 double theValue=polyClassValues[index];
1899 std::cout << theValue <<
" ";
1901 std::cout << std::endl;
1903 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(20));
1906 else if(ruleMap[rule_opt[0]]==rule::mode){
1909 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1911 int maxClass=stat.mymin(class_opt);
1912 vector<double>::iterator maxit;
1913 maxit=stat.mymax(polyClassValues,polyClassValues.begin(),polyClassValues.end());
1914 int maxIndex=distance(polyClassValues.begin(),maxit);
1915 maxClass=class_opt[maxIndex];
1916 if(verbose_opt[0]>0)
1917 std::cout <<
"maxClass: " << maxClass << std::endl;
1919 writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
1921 writeCentroidFeature->SetField(label_opt[0].c_str(),maxClass);
1925 if(verbose_opt[0]>1)
1926 std::cout <<
"creating polygon feature" << std::endl;
1928 if(writeTestLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1929 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1934 if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1935 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1939 OGRFeature::DestroyFeature( writePolygonFeature );
1942 std::cout <<
"ntotalvalid(1): " << ntotalvalid << std::endl;
1945 if(verbose_opt[0]>1)
1946 std::cout <<
"creating point feature in centroid" << std::endl;
1948 if(writeTestLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1949 std::string errorString=
"Failed to create point feature in ogr vector dataset";
1955 assert(validFeature);
1956 if(writeLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1957 std::string errorString=
"Failed to create point feature in ogr vector dataset";
1961 OGRFeature::DestroyFeature( writeCentroidFeature );
1964 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
1968 else if(wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon){
1969 OGRMultiPolygon readPolygon = *((OGRMultiPolygon *) poGeometry);
1970 OGRPolygon writePolygon;
1971 OGRLinearRing writeRing;
1972 OGRPoint writeCentroidPoint;
1973 OGRFeature *writePolygonFeature;
1974 OGRFeature *writeCentroidFeature;
1976 readPolygon.closeRings();
1978 if(verbose_opt[0]>1)
1979 std::cout <<
"get centroid point from polygon" << std::endl;
1981 readPolygon.Centroid(&writeCentroidPoint);
1983 double ulx,uly,lrx,lry;
1984 double uli,ulj,lri,lrj;
1985 if((polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point)||(ruleMap[rule_opt[0]]==rule::centroid)){
1986 ulx=writeCentroidPoint.getX();
1987 uly=writeCentroidPoint.getY();
1994 std::cout <<
"reading envelope for polygon " << ifeature << std::endl;
1995 OGREnvelope* psEnvelope=
new OGREnvelope();
1996 readPolygon.getEnvelope(psEnvelope);
1997 ulx=psEnvelope->MinX;
1998 uly=psEnvelope->MaxY;
1999 lrx=psEnvelope->MaxX;
2000 lry=psEnvelope->MinY;
2004 imgReader.geo2image(ulx,uly,uli,ulj);
2005 imgReader.geo2image(lrx,lry,lri,lrj);
2014 ulj=
static_cast<int>(ulj);
2015 uli=
static_cast<int>(uli);
2016 lrj=
static_cast<int>(lrj);
2017 lri=
static_cast<int>(lri);
2019 if(verbose_opt[0]>1)
2020 std::cout <<
"bounding box for multipologon feature " << ifeature <<
": " << uli <<
" " << ulj <<
" " << lri <<
" " << lrj << std::endl;
2026 if(uli>=imgReader.nrOfCol())
2027 uli=imgReader.nrOfCol()-1;
2028 if(lri>=imgReader.nrOfCol())
2029 lri=imgReader.nrOfCol()-1;
2034 if(ulj>=imgReader.nrOfRow())
2035 ulj=imgReader.nrOfRow()-1;
2036 if(lrj>=imgReader.nrOfRow())
2037 lrj=imgReader.nrOfRow()-1;
2041 int nPointPolygon=0;
2044 writePolygonFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
2046 writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
2048 else if(ruleMap[rule_opt[0]]!=rule::point){
2050 writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
2052 writeCentroidFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
2056 vector<double> polyClassValues;
2058 if(class_opt.size()){
2059 polyClassValues.resize(class_opt.size());
2061 for(
int iclass=0;iclass<class_opt.size();++iclass)
2062 polyClassValues[iclass]=0;
2065 polyValues.resize(nband);
2066 vector< Vector2d<double> > readValues(nband);
2067 for(
int iband=0;iband<nband;++iband){
2068 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
2071 assert(uli<imgReader.nrOfCol());
2073 assert(lri<imgReader.nrOfCol());
2075 assert(ulj<imgReader.nrOfRow());
2077 assert(lrj<imgReader.nrOfRow());
2078 imgReader.readDataBlock(readValues[iband],GDT_Float64,uli,lri,ulj,lrj,theBand);
2082 for(
int j=ulj;j<=lrj;++j){
2083 for(
int i=uli;i<=lri;++i){
2085 if(i<0||i>=imgReader.nrOfCol())
2087 if(j<0||j>=imgReader.nrOfRow())
2092 imgReader.image2geo(i,j,theX,theY);
2093 thePoint.setX(theX);
2094 thePoint.setY(theY);
2096 if(ruleMap[rule_opt[0]]!=rule::centroid&&!readPolygon.Contains(&thePoint))
2101 if(srcnodata_opt.size()){
2102 for(
int vband=0;vband<bndnodata_opt.size();++vband){
2103 double value=((readValues[bndnodata_opt[vband]])[j-ulj])[i-uli];
2104 if(value==srcnodata_opt[vband]){
2116 writeRing.addPoint(&thePoint);
2119 if(verbose_opt[0]>1)
2120 std::cout <<
"point is on surface:" << thePoint.getX() <<
"," << thePoint.getY() << std::endl;
2123 if(polythreshold_opt.size())
2124 if(nPointPolygon>polythreshold_opt[0])
2127 OGRFeature *writePointFeature;
2128 if(!polygon_opt[0]){
2130 if(ruleMap[rule_opt[0]]==rule::point){
2132 writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
2134 writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
2135 if(verbose_opt[0]>1)
2136 std::cout <<
"copying fields from polygons " << std::endl;
2139 if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE)
2140 cerr <<
"writing feature failed" << std::endl;
2142 writePointFeature->SetGeometry(&thePoint);
2143 assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
2147 if(verbose_opt[0]>1)
2148 std::cout <<
"write feature has " << writePointFeature->GetFieldCount() <<
" fields" << std::endl;
2151 if(class_opt.size()){
2152 short value=((readValues[0])[j-ulj])[i-uli];
2153 for(
int iclass=0;iclass<class_opt.size();++iclass){
2154 if(value==class_opt[iclass])
2155 polyClassValues[iclass]+=1;
2159 for(
int iband=0;iband<nband;++iband){
2162 assert(j-ulj<readValues[iband].size());
2164 assert(i-uli<((readValues[iband])[j-ulj]).size());
2165 double value=((readValues[iband])[j-ulj])[i-uli];
2167 if(verbose_opt[0]>1)
2168 std::cout <<
": " << value << std::endl;
2169 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point)
2170 polyValues[iband].push_back(value);
2172 if(verbose_opt[0]>1)
2173 std::cout <<
"set field " << fieldname_opt[iband] <<
" to " << value << std::endl;
2174 switch( fieldType ){
2177 writePointFeature->SetField(fieldname_opt[iband].c_str(),value);
2180 writePointFeature->SetField(fieldname_opt[iband].c_str(),type2string<double>(value).c_str());
2200 if(!polygon_opt[0]){
2201 if(ruleMap[rule_opt[0]]==rule::point){
2203 if(verbose_opt[0]>1)
2204 std::cout <<
"creating point feature" << std::endl;
2206 if(writeTestLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
2207 std::string errorString=
"Failed to create feature in ogr vector dataset";
2212 if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
2213 std::string errorString=
"Failed to create feature in ogr vector dataset";
2218 OGRFeature::DestroyFeature( writePointFeature );
2224 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
2229 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
2235 writePolygon.addRing(&writeRing);
2236 writePolygon.closeRings();
2241 if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE)
2242 cerr <<
"writing feature failed" << std::endl;
2243 assert(writePolygonFeature->GetGeometryRef()->getGeometryType()==wkbMultiPolygon);
2244 if(verbose_opt[0]>1)
2245 std::cout <<
"copying new fields write polygon " << std::endl;
2246 if(verbose_opt[0]>1)
2247 std::cout <<
"write feature has " << writePolygonFeature->GetFieldCount() <<
" fields" << std::endl;
2252 if(verbose_opt[0]>1)
2253 std::cout <<
"copying fields from polygons " << std::endl;
2256 if(writeCentroidFeature->SetFrom(readFeature)!= OGRERR_NONE)
2257 cerr <<
"writing feature failed" << std::endl;
2258 writeCentroidFeature->SetGeometry(&writeCentroidPoint);
2259 assert(wkbFlatten(writeCentroidFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
2264 if(verbose_opt[0]>1)
2265 std::cout <<
"write feature has " << writeCentroidFeature->GetFieldCount() <<
" fields" << std::endl;
2267 if(class_opt.empty()){
2268 if(ruleMap[rule_opt[0]]==rule::point){
2270 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2271 for(
int index=0;index<polyValues.size();++index){
2273 assert(polyValues[index].size()==1);
2274 double theValue=polyValues[index].back();
2276 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2277 int theBand=(band_opt.size()) ? band_opt[index] : index;
2279 if(verbose_opt[0]>1)
2280 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
2281 switch( fieldType ){
2285 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
2287 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
2291 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2293 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2296 std::string errorString=
"field type not supported";
2301 catch(std::string e){
2302 std::cout << e << std::endl;
2309 for(
int index=0;index<polyValues.size();++index){
2311 if(ruleMap[rule_opt[0]]==rule::mean)
2312 theValue=stat.mean(polyValues[index]);
2313 else if(ruleMap[rule_opt[0]]==rule::stdev)
2314 theValue=sqrt(stat.var(polyValues[index]));
2315 else if(ruleMap[rule_opt[0]]==rule::median)
2316 theValue=stat.median(polyValues[index]);
2317 else if(ruleMap[rule_opt[0]]==rule::percentile)
2318 theValue=stat.percentile(polyValues[index],polyValues[index].begin(),polyValues[index].end(),percentile_opt[0]);
2319 else if(ruleMap[rule_opt[0]]==rule::sum)
2320 theValue=stat.sum(polyValues[index]);
2321 else if(ruleMap[rule_opt[0]]==rule::max)
2322 theValue=stat.mymax(polyValues[index]);
2323 else if(ruleMap[rule_opt[0]]==rule::min)
2324 theValue=stat.mymin(polyValues[index]);
2325 else if(ruleMap[rule_opt[0]]==rule::centroid){
2327 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2328 assert(nPointPolygon<=1);
2329 assert(nPointPolygon==polyValues[index].size());
2330 theValue=polyValues[index].back();
2333 std::string errorString=
"rule not supported";
2336 if(verbose_opt[0]>1)
2337 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
2338 switch( fieldType ){
2342 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
2344 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
2348 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2350 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2353 std::string errorString=
"field type not supported";
2358 catch(std::string e){
2359 std::cout << e << std::endl;
2366 if(ruleMap[rule_opt[0]]==rule::proportion){
2368 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2369 stat.normalize_pct(polyClassValues);
2370 for(
int index=0;index<polyClassValues.size();++index){
2371 double theValue=polyClassValues[index];
2373 fs << class_opt[index];
2375 writePolygonFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
2377 writeCentroidFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
2380 else if(ruleMap[rule_opt[0]]==rule::custom){
2381 assert(polygon_opt[0]);
2383 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2384 stat.normalize_pct(polyClassValues);
2385 assert(polyClassValues.size()==2);
2386 if(polyClassValues[0]>=75)
2387 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(11));
2388 else if(polyClassValues[1]>=75)
2389 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(12));
2390 else if(polyClassValues[0]>25&&polyClassValues[1]>25)
2391 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(13));
2394 std::cout <<
"No valid value in polyClassValues..." << std::endl;
2395 for(
int index=0;index<polyClassValues.size();++index){
2396 double theValue=polyClassValues[index];
2397 std::cout << theValue <<
" ";
2399 std::cout << std::endl;
2401 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(20));
2404 else if(ruleMap[rule_opt[0]]==rule::mode){
2407 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2409 int maxClass=stat.mymin(class_opt);
2410 vector<double>::iterator maxit;
2411 maxit=stat.mymax(polyClassValues,polyClassValues.begin(),polyClassValues.end());
2412 int maxIndex=distance(polyClassValues.begin(),maxit);
2413 maxClass=class_opt[maxIndex];
2414 if(verbose_opt[0]>0)
2415 std::cout <<
"maxClass: " << maxClass << std::endl;
2417 writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
2419 writeCentroidFeature->SetField(label_opt[0].c_str(),maxClass);
2424 if(verbose_opt[0]>1)
2425 std::cout <<
"creating polygon feature" << std::endl;
2427 if(writeTestLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
2428 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
2433 if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
2434 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
2438 OGRFeature::DestroyFeature( writePolygonFeature );
2441 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
2444 if(verbose_opt[0]>1)
2445 std::cout <<
"creating point feature in centroid" << std::endl;
2447 if(writeTestLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
2448 std::string errorString=
"Failed to create point feature in ogr vector dataset";
2454 assert(validFeature);
2455 if(writeLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
2456 std::string errorString=
"Failed to create point feature in ogr vector dataset";
2460 OGRFeature::DestroyFeature( writeCentroidFeature );
2463 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
2469 test=poGeometry->getGeometryName();
2471 oss <<
"geometry " << test <<
" not supported";
2475 progress=
static_cast<float>(ifeature+1)/nfeature;
2476 pfnProgress(progress,pszMessage,pProgressArg);
2478 catch(std::string e){
2479 std::cout << e << std::endl;
2484 std::cout <<
"number of points read in polygon: " << npoint << std::endl;
2491 pfnProgress(progress,pszMessage,pProgressArg);
2496 ogrTestWriter.close();
2499 pfnProgress(progress,pszMessage,pProgressArg);