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
123 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, count=12};
128 int main(
int argc,
char *argv[])
130 Optionpk<string> image_opt(
"i",
"input",
"Raster input dataset containing band information");
131 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.");
132 Optionpk<string> layer_opt(
"ln",
"ln",
"Layer name(s) in sample (leave empty to select all)");
133 Optionpk<unsigned int> random_opt(
"rand",
"random",
"Create simple random sample of points. Provide number of points to generate");
134 Optionpk<double> grid_opt(
"grid",
"grid",
"Create systematic grid of points. Provide cell grid size (in projected units, e.g,. m)");
136 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, proportion or count");
137 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);
138 Optionpk<double> percentile_opt(
"perc",
"perc",
"Percentile value used for rule percentile",95);
139 Optionpk<string> ogrformat_opt(
"f",
"f",
"Output sample dataset format",
"SQLite");
140 Optionpk<string> ftype_opt(
"ft",
"ftype",
"Field type (only Real or Integer)",
"Real");
141 Optionpk<string> ltype_opt(
"lt",
"ltype",
"Label type: In16 or String",
"Integer");
142 Optionpk<bool> polygon_opt(
"polygon",
"polygon",
"Create OGRPolygon as geometry instead of OGRPoint.",
false);
143 Optionpk<int> band_opt(
"b",
"band",
"Band index(es) to extract (0 based). Leave empty to use all bands");
146 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, count, min, max, mode, sum, percentile.",
"centroid");
147 Optionpk<double> srcnodata_opt(
"srcnodata",
"srcnodata",
"Invalid value(s) for input image");
148 Optionpk<int> bndnodata_opt(
"bndnodata",
"bndnodata",
"Band(s) in input image to check if pixel is valid (used for srcnodata)", 0);
149 Optionpk<float> polythreshold_opt(
"tp",
"thresholdPolygon",
"(absolute) threshold for selecting samples in each polygon");
150 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");
151 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");
152 Optionpk<string> label_opt(
"cn",
"cname",
"Name of the class label in the output vector dataset",
"label");
153 Optionpk<short> geo_opt(
"geo",
"geo",
"Use geo coordinates (set to 0 to use image coordinates)", 1);
154 Optionpk<short> down_opt(
"down",
"down",
"Down sampling factor (for raster sample datasets only). Can be used to create grid points", 1);
155 Optionpk<short> buffer_opt(
"buf",
"buffer",
"Buffer for calculating statistics for point features ");
156 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);
157 Optionpk<short> verbose_opt(
"v",
"verbose",
"Verbose mode if > 0", 0,2);
159 bstart_opt.setHide(1);
161 bndnodata_opt.setHide(1);
162 srcnodata_opt.setHide(1);
163 polythreshold_opt.setHide(1);
164 percentile_opt.setHide(1);
166 fieldname_opt.setHide(1);
167 label_opt.setHide(1);
170 buffer_opt.setHide(1);
175 doProcess=image_opt.retrieveOption(argc,argv);
176 sample_opt.retrieveOption(argc,argv);
177 layer_opt.retrieveOption(argc,argv);
178 random_opt.retrieveOption(argc,argv);
179 grid_opt.retrieveOption(argc,argv);
180 output_opt.retrieveOption(argc,argv);
181 class_opt.retrieveOption(argc,argv);
182 threshold_opt.retrieveOption(argc,argv);
183 percentile_opt.retrieveOption(argc,argv);
184 ogrformat_opt.retrieveOption(argc,argv);
185 ftype_opt.retrieveOption(argc,argv);
186 ltype_opt.retrieveOption(argc,argv);
187 polygon_opt.retrieveOption(argc,argv);
188 band_opt.retrieveOption(argc,argv);
189 bstart_opt.retrieveOption(argc,argv);
190 bend_opt.retrieveOption(argc,argv);
191 rule_opt.retrieveOption(argc,argv);
192 bndnodata_opt.retrieveOption(argc,argv);
193 srcnodata_opt.retrieveOption(argc,argv);
194 polythreshold_opt.retrieveOption(argc,argv);
195 test_opt.retrieveOption(argc,argv);
196 fieldname_opt.retrieveOption(argc,argv);
197 label_opt.retrieveOption(argc,argv);
198 geo_opt.retrieveOption(argc,argv);
199 down_opt.retrieveOption(argc,argv);
200 buffer_opt.retrieveOption(argc,argv);
201 disc_opt.retrieveOption(argc,argv);
204 verbose_opt.retrieveOption(argc,argv);
206 catch(
string predefinedString){
207 std::cout << predefinedString << std::endl;
212 cout <<
"Usage: pkextract -i input [-s sample | -rand number | -grid size] -o output" << endl;
214 std::cout <<
"short option -h shows basic options only, use long option --help to show all options" << std::endl;
218 std::map<std::string, rule::RULE_TYPE> ruleMap;
220 ruleMap[
"point"]=rule::point;
221 ruleMap[
"centroid"]=rule::centroid;
222 ruleMap[
"mean"]=rule::mean;
223 ruleMap[
"stdev"]=rule::stdev;
224 ruleMap[
"median"]=rule::median;
225 ruleMap[
"proportion"]=rule::proportion;
226 ruleMap[
"count"]=rule::count;
227 ruleMap[
"min"]=rule::min;
228 ruleMap[
"max"]=rule::max;
229 ruleMap[
"custom"]=rule::custom;
230 ruleMap[
"mode"]=rule::mode;
231 ruleMap[
"sum"]=rule::sum;
232 ruleMap[
"percentile"]=rule::percentile;
234 if(srcnodata_opt.size()){
235 while(srcnodata_opt.size()<bndnodata_opt.size())
236 srcnodata_opt.push_back(srcnodata_opt[0]);
237 while(bndnodata_opt.size()<srcnodata_opt.size())
238 bndnodata_opt.push_back(bndnodata_opt[0]);
242 std::cout << class_opt << std::endl;
244 stat.setNoDataValues(srcnodata_opt);
246 unsigned long int nsample=0;
247 unsigned long int ntotalvalid=0;
248 unsigned long int ntotalinvalid=0;
249 vector<unsigned long int> nvalid(class_opt.size());
250 vector<unsigned long int> ninvalid(class_opt.size());
251 for(
int it=0;it<nvalid.size();++it){
257 if(image_opt.empty()){
258 std::cerr <<
"No image dataset provided (use option -i). Use --help for help information";
261 if(output_opt.empty()){
262 std::cerr <<
"No output dataset provided (use option -o). Use --help for help information";
266 imgReader.open(image_opt[0]);
268 catch(std::string errorstring){
269 std::cout << errorstring << std::endl;
275 if(bstart_opt.size()){
276 if(bend_opt.size()!=bstart_opt.size()){
277 string errorstring=
"Error: options for start and end band indexes must be provided as pairs, missing end band";
281 for(
int ipair=0;ipair<bstart_opt.size();++ipair){
282 if(bend_opt[ipair]<=bstart_opt[ipair]){
283 string errorstring=
"Error: index for end band must be smaller then start band";
286 for(
int iband=bstart_opt[ipair];iband<=bend_opt[ipair];++iband)
287 band_opt.push_back(iband);
292 cerr << error << std::endl;
296 int nband=(band_opt.size()) ? band_opt.size() : imgReader.nrOfBand();
298 if(fieldname_opt.size()<nband){
299 std::string bandString=fieldname_opt[0];
300 fieldname_opt.clear();
301 fieldname_opt.resize(nband);
302 for(
int iband=0;iband<nband;++iband){
303 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
305 fs << bandString << theBand;
306 fieldname_opt[iband]=fs.str();
311 std::cout << fieldname_opt << std::endl;
314 std::cout <<
"Number of bands in input image: " << imgReader.nrOfBand() << std::endl;
316 OGRFieldType fieldType;
317 OGRFieldType labelType;
318 int ogr_typecount=11;
320 std::cout <<
"field and label types can be: ";
321 for(
int iType = 0; iType < ogr_typecount; ++iType){
323 std::cout <<
" " << OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType);
324 if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL
325 && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType),
326 ftype_opt[0].c_str()))
327 fieldType=(OGRFieldType) iType;
328 if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL
329 && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType),
330 ltype_opt[0].c_str()))
331 labelType=(OGRFieldType) iType;
339 std::cout << std::endl <<
"field type is: " << OGRFieldDefn::GetFieldTypeName(fieldType) << std::endl;
342 cerr <<
"field type " << OGRFieldDefn::GetFieldTypeName(fieldType) <<
" not supported" << std::endl;
352 std::cout << std::endl <<
"label type is: " << OGRFieldDefn::GetFieldTypeName(labelType) << std::endl;
355 cerr <<
"label type " << OGRFieldDefn::GetFieldTypeName(labelType) <<
" not supported" << std::endl;
360 const char* pszMessage;
361 void* pProgressArg=NULL;
362 GDALProgressFunc pfnProgress=GDALTermProgress;
366 bool sampleIsRaster=
false;
367 bool sampleIsVirtual=
false;
372 if(sample_opt.size()){
374 sampleReaderOgr.open(sample_opt[0]);
376 catch(
string errorString){
382 sampleWriterOgr.open(
"/vsimem/virtual",ogrformat_opt[0]);
384 catch(
string errorString){
385 cerr << errorString << endl;
387 char **papszOptions=NULL;
388 sampleWriterOgr.createLayer(
"points", imgReader.getProjection(), wkbPoint, papszOptions);
389 sampleIsVirtual=
true;
394 if(random_opt.size()){
397 double ulx,uly,lrx,lry;
398 imgReader.getBoundingBox(ulx,uly,lrx,lry);
399 for(
unsigned int ipoint=1;ipoint<=random_opt[0];++ipoint){
400 OGRFeature *pointFeature;
401 pointFeature=sampleWriterOgr.createFeature();
403 double theX=ulx+
static_cast<double>(rand())/(RAND_MAX)*(lrx-ulx);
404 double theY=uly-
static_cast<double>(rand())/(RAND_MAX)*(uly-lry);
407 pointFeature->SetGeometry( &pt );
408 if(sampleWriterOgr.createFeature(pointFeature) != OGRERR_NONE ){
409 cerr <<
"Failed to create feature in shapefile" << endl;
412 OGRFeature::DestroyFeature(pointFeature);
415 else if(grid_opt.size()){
418 double ulx,uly,lrx,lry;
419 imgReader.getBoundingBox(ulx,uly,lrx,lry);
420 unsigned int ipoint=0;
421 for(
double theY=uly-grid_opt[0]/2;theY>lry;theY-=grid_opt[0]){
422 for(
double theX=ulx+grid_opt[0]/2;theX<lrx;theX+=grid_opt[0]){
424 cout <<
"position: " << theX <<
" " << theY << endl;
425 OGRFeature *pointFeature;
426 pointFeature=sampleWriterOgr.createFeature();
430 pointFeature->SetGeometry( &pt );
431 if(sampleWriterOgr.createFeature(pointFeature) != OGRERR_NONE ){
432 cerr <<
"Failed to create feature in shapefile" << endl;
435 OGRFeature::DestroyFeature(pointFeature);
440 std::cerr <<
"No sample dataset provided (use option -s). Use --help for help information";
444 sampleWriterOgr.close();
445 sampleReaderOgr.open(
"/vsimem/virtual");
447 catch(
string errorString){
448 cerr << errorString << endl;
453 if(class_opt.empty()){
457 assert(sample_opt.size());
458 classReader.open(sample_opt[0]);
460 vector<double> classBuffer(classReader.nrOfCol());
462 vector<double> sample(2+nband);
465 vector<double> writeBufferClass;
466 vector<int> selectedClass;
472 std::cout <<
"extracting sample from image..." << std::endl;
474 pfnProgress(progress,pszMessage,pProgressArg);
475 for(irow=0;irow<classReader.nrOfRow();++irow){
479 classReader.readData(classBuffer,GDT_Float64,irow);
482 for(icol=0;icol<classReader.nrOfCol();++icol){
486 double theClass=classBuffer[icol];
507 classReader.image2geo(icol,irow,x,y);
510 if(verbose_opt[0]>1){
511 std::cout.precision(12);
512 std::cout << theClass <<
" " << x <<
" " << y << std::endl;
515 imgReader.geo2image(x,y,iimg,jimg);
517 jimg=
static_cast<int>(jimg);
518 iimg=
static_cast<int>(iimg);
519 if(static_cast<int>(iimg)<0||
static_cast<int>(iimg)>=imgReader.nrOfCol())
528 if(static_cast<int>(jimg)<0||
static_cast<int>(jimg)>=imgReader.nrOfRow())
533 if(static_cast<int>(jimg)!=
static_cast<int>(oldimgrow)){
534 assert(imgBuffer.size()==nband);
535 for(
int iband=0;iband<nband;++iband){
536 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
537 imgReader.readData(imgBuffer[iband],GDT_Float64,static_cast<int>(jimg),theBand);
538 assert(imgBuffer[iband].size()==imgReader.nrOfCol());
539 if(srcnodata_opt.size()){
540 vector<int>::const_iterator bndit=bndnodata_opt.begin();
541 vector<double>::const_iterator srcit=srcnodata_opt.begin();
542 while(bndit!=bndnodata_opt.end()&&srcit!=srcnodata_opt.end()){
543 if((*bndit==theBand)&&(*srcit==imgBuffer[iband][
static_cast<int>(iimg)])){
558 for(
int iband=0;iband<imgBuffer.size();++iband){
559 if(imgBuffer[iband].size()!=imgReader.nrOfCol()){
560 std::cout <<
"Error in band " << iband <<
": " << imgBuffer[iband].size() <<
"!=" << imgReader.nrOfCol() << std::endl;
561 assert(imgBuffer[iband].size()==imgReader.nrOfCol());
563 sample[iband+2]=imgBuffer[iband][
static_cast<int>(iimg)];
565 float theThreshold=(threshold_opt.size()>1)?threshold_opt[processClass]:threshold_opt[0];
567 double p=
static_cast<double>(rand())/(RAND_MAX);
572 else if(nvalid.size()>processClass){
573 if(nvalid[processClass]>=-theThreshold)
576 writeBuffer.push_back(sample);
577 writeBufferClass.push_back(theClass);
579 if(nvalid.size()>processClass)
580 ++(nvalid[processClass]);
584 if(ninvalid.size()>processClass)
585 ++(ninvalid[processClass]);
589 progress=
static_cast<float>(irow+1.0)/classReader.nrOfRow();
590 pfnProgress(progress,pszMessage,pProgressArg);
593 pfnProgress(progress,pszMessage,pProgressArg);
594 if(writeBuffer.size()>0){
595 assert(ntotalvalid==writeBuffer.size());
597 std::cout <<
"creating image sample writer " << output_opt[0] <<
" with " << writeBuffer.size() <<
" samples (" << ntotalinvalid <<
" invalid)" << std::endl;
598 ogrWriter.open(output_opt[0],ogrformat_opt[0]);
599 char **papszOptions=NULL;
600 ostringstream slayer;
601 slayer <<
"training data";
602 std::string layername=slayer.str();
603 ogrWriter.createLayer(layername, imgReader.getProjection(), wkbPoint, papszOptions);
604 std::string fieldname=
"fid";
605 ogrWriter.createField(fieldname,OFTInteger);
606 map<std::string,double> pointAttributes;
607 ogrWriter.createField(label_opt[0],labelType);
608 for(
int iband=0;iband<nband;++iband){
609 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
610 ogrWriter.createField(fieldname_opt[iband],fieldType);
612 std::cout <<
"writing sample to " << output_opt[0] <<
"..." << std::endl;
614 pfnProgress(progress,pszMessage,pProgressArg);
615 for(
int isample=0;isample<writeBuffer.size();++isample){
617 std::cout <<
"writing sample " << isample << std::endl;
618 pointAttributes[label_opt[0]]=writeBufferClass[isample];
619 for(
int iband=0;iband<writeBuffer[0].size()-2;++iband){
620 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
627 pointAttributes[fieldname_opt[iband]]=writeBuffer[isample][iband+2];
630 std::cout <<
"all bands written" << std::endl;
631 ogrWriter.addPoint(writeBuffer[isample][0],writeBuffer[isample][1],pointAttributes,fieldname,isample);
632 progress=
static_cast<float>(isample+1.0)/writeBuffer.size();
633 pfnProgress(progress,pszMessage,pProgressArg);
638 std::cout <<
"No data found for any class " << std::endl;
641 nsample=writeBuffer.size();
643 std::cout <<
"total number of samples written: " << nsample << std::endl;
646 assert(class_opt[0]);
648 assert(threshold_opt.size()==1||threshold_opt.size()==class_opt.size());
651 if(verbose_opt[0]>1){
652 std::cout <<
"reading position from sample dataset " << std::endl;
653 std::cout <<
"class thresholds: " << std::endl;
654 for(
int iclass=0;iclass<class_opt.size();++iclass){
655 if(threshold_opt.size()>1)
656 std::cout << class_opt[iclass] <<
": " << threshold_opt[iclass] << std::endl;
658 std::cout << class_opt[iclass] <<
": " << threshold_opt[0] << std::endl;
661 classReader.open(sample_opt[0]);
662 vector<int> classBuffer(classReader.nrOfCol());
665 vector<double> sample(2+nband);
667 vector<int> writeBufferClass;
669 vector<int> selectedClass;
675 std::cout <<
"extracting sample from image..." << std::endl;
677 pfnProgress(progress,pszMessage,pProgressArg);
678 for(irow=0;irow<classReader.nrOfRow();++irow){
681 classReader.readData(classBuffer,GDT_Int32,irow);
685 for(icol=0;icol<classReader.nrOfCol();++icol){
691 if(class_opt.empty()){
692 if(classBuffer[icol]){
694 theClass=classBuffer[icol];
698 for(
int iclass=0;iclass<class_opt.size();++iclass){
699 if(classBuffer[icol]==class_opt[iclass]){
701 theClass=class_opt[iclass];
708 classReader.image2geo(icol,irow,x,y);
711 if(verbose_opt[0]>1){
712 std::cout.precision(12);
713 std::cout << theClass <<
" " << x <<
" " << y << std::endl;
716 imgReader.geo2image(x,y,iimg,jimg);
718 jimg=
static_cast<int>(jimg);
719 iimg=
static_cast<int>(iimg);
720 if(static_cast<int>(iimg)<0||
static_cast<int>(iimg)>=imgReader.nrOfCol())
729 if(static_cast<int>(jimg)<0||
static_cast<int>(jimg)>=imgReader.nrOfRow())
734 if(static_cast<int>(jimg)!=
static_cast<int>(oldimgrow)){
735 assert(imgBuffer.size()==nband);
736 for(
int iband=0;iband<nband;++iband){
737 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
738 imgReader.readData(imgBuffer[iband],GDT_Float64,static_cast<int>(jimg),theBand);
739 assert(imgBuffer[iband].size()==imgReader.nrOfCol());
741 if(srcnodata_opt.size()){
742 vector<int>::const_iterator bndit=bndnodata_opt.begin();
743 vector<double>::const_iterator srcit=srcnodata_opt.begin();
744 while(bndit!=bndnodata_opt.end()&&srcit!=srcnodata_opt.end()){
745 if((*bndit==theBand)&&(*srcit==imgBuffer[iband][
static_cast<int>(iimg)])){
759 for(
int iband=0;iband<imgBuffer.size();++iband){
760 if(imgBuffer[iband].size()!=imgReader.nrOfCol()){
761 std::cout <<
"Error in band " << iband <<
": " << imgBuffer[iband].size() <<
"!=" << imgReader.nrOfCol() << std::endl;
762 assert(imgBuffer[iband].size()==imgReader.nrOfCol());
764 sample[iband+2]=imgBuffer[iband][
static_cast<int>(iimg)];
766 float theThreshold=(threshold_opt.size()>1)?threshold_opt[processClass]:threshold_opt[0];
768 double p=
static_cast<double>(rand())/(RAND_MAX);
773 else if(nvalid.size()>processClass){
774 if(nvalid[processClass]>=-theThreshold)
777 writeBuffer.push_back(sample);
779 writeBufferClass.push_back(theClass);
781 if(nvalid.size()>processClass)
782 ++(nvalid[processClass]);
786 if(ninvalid.size()>processClass)
787 ++(ninvalid[processClass]);
791 progress=
static_cast<float>(irow+1.0)/classReader.nrOfRow();
792 pfnProgress(progress,pszMessage,pProgressArg);
794 if(writeBuffer.size()>0){
795 assert(ntotalvalid==writeBuffer.size());
797 std::cout <<
"creating image sample writer " << output_opt[0] <<
" with " << writeBuffer.size() <<
" samples (" << ntotalinvalid <<
" invalid)" << std::endl;
798 ogrWriter.open(output_opt[0],ogrformat_opt[0]);
799 char **papszOptions=NULL;
800 ostringstream slayer;
801 slayer <<
"training data";
802 std::string layername=slayer.str();
803 ogrWriter.createLayer(layername, imgReader.getProjection(), wkbPoint, papszOptions);
804 std::string fieldname=
"fid";
805 ogrWriter.createField(fieldname,OFTInteger);
806 map<std::string,double> pointAttributes;
808 ogrWriter.createField(label_opt[0],labelType);
809 for(
int iband=0;iband<nband;++iband){
810 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
817 ogrWriter.createField(fieldname_opt[iband],fieldType);
819 pfnProgress(progress,pszMessage,pProgressArg);
820 std::cout <<
"writing sample to " << output_opt[0] <<
"..." << std::endl;
822 pfnProgress(progress,pszMessage,pProgressArg);
823 for(
int isample=0;isample<writeBuffer.size();++isample){
824 pointAttributes[label_opt[0]]=writeBufferClass[isample];
825 for(
int iband=0;iband<writeBuffer[0].size()-2;++iband){
826 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
833 pointAttributes[fieldname_opt[iband]]=writeBuffer[isample][iband+2];
835 ogrWriter.addPoint(writeBuffer[isample][0],writeBuffer[isample][1],pointAttributes,fieldname,isample);
836 progress=
static_cast<float>(isample+1.0)/writeBuffer.size();
837 pfnProgress(progress,pszMessage,pProgressArg);
842 std::cout <<
"No data found for any class " << std::endl;
845 nsample=writeBuffer.size();
847 std::cout <<
"total number of samples written: " << nsample << std::endl;
848 if(nvalid.size()==class_opt.size()){
849 for(
int iclass=0;iclass<class_opt.size();++iclass)
850 std::cout <<
"class " << class_opt[iclass] <<
" has " << nvalid[iclass] <<
" samples" << std::endl;
857 std::cout <<
"creating image sample writer " << output_opt[0] << std::endl;
861 ogrWriter.open(output_opt[0],ogrformat_opt[0]);
864 std::cout <<
"creating image test writer " << test_opt[0] << std::endl;
865 ogrTestWriter.open(test_opt[0],ogrformat_opt[0]);
869 switch(ruleMap[rule_opt[0]]){
870 case(rule::proportion):
874 if(class_opt.empty()){
880 imgReader.getMinMax(minValue,maxValue,theBand);
881 int nclass=maxValue-minValue+1;
882 if(nclass<0&&nclass<256){
883 string errorString=
"Could not automatically define classes, please set class option";
886 for(
int iclass=minValue;iclass<=maxValue;++iclass)
887 class_opt.push_back(iclass);
893 catch(
string errorString){
894 cerr << errorString << endl;
899 int nlayerRead=sampleReaderOgr.getDataSource()->GetLayerCount();
901 unsigned long int ntotalvalid=0;
904 std::cout <<
"number of layers: " << nlayerRead << endl;
906 for(
int ilayer=0;ilayer<nlayerRead;++ilayer){
907 OGRLayer *readLayer=sampleReaderOgr.getLayer(ilayer);
908 string currentLayername=readLayer->GetName();
909 int layerIndex=ilayer;
910 if(layer_opt.size()){
911 vector<string>::const_iterator it=find(layer_opt.begin(),layer_opt.end(),currentLayername);
912 if(it==layer_opt.end())
915 layerIndex=it-layer_opt.begin();
917 float theThreshold=(threshold_opt.size()==layer_opt.size())? threshold_opt[layerIndex]: threshold_opt[0];
918 cout <<
"processing layer " << currentLayername << endl;
920 readLayer->ResetReading();
921 OGRLayer *writeLayer;
922 OGRLayer *writeTestLayer;
926 std::cout <<
"create polygons" << std::endl;
927 char **papszOptions=NULL;
928 writeLayer=ogrWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPolygon, papszOptions);
930 writeTestLayer=ogrTestWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPolygon, papszOptions);
934 std::cout <<
"create points in layer " << readLayer->GetName() << std::endl;
935 char **papszOptions=NULL;
937 writeLayer=ogrWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPoint, papszOptions);
939 char **papszOptions=NULL;
940 writeTestLayer=ogrTestWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPoint, papszOptions);
944 std::cout <<
"copy fields from layer " << ilayer << std::flush << std::endl;
945 ogrWriter.copyFields(sampleReaderOgr,ilayer,ilayerWrite);
949 std::cout <<
"copy fields test writer" << std::flush << std::endl;
950 ogrTestWriter.copyFields(sampleReaderOgr,ilayer,ilayerWrite);
959 if(class_opt.size()){
960 switch(ruleMap[rule_opt[0]]){
961 case(rule::proportion):
963 for(
int iclass=0;iclass<class_opt.size();++iclass){
965 cs << class_opt[iclass];
966 ogrWriter.createField(cs.str(),fieldType,ilayerWrite);
972 ogrWriter.createField(label_opt[0],fieldType,ilayerWrite);
974 ogrTestWriter.createField(label_opt[0],fieldType,ilayerWrite);
979 for(
int iband=0;iband<nband;++iband){
980 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
982 fs << fieldname_opt[iband];
984 std::cout <<
"creating field " << fs.str() << std::endl;
986 ogrWriter.createField(fs.str(),fieldType,ilayerWrite);
988 ogrTestWriter.createField(fs.str(),fieldType,ilayerWrite);
991 OGRFeature *readFeature;
992 unsigned long int ifeature=0;
993 unsigned long int nfeatureLayer=sampleReaderOgr.getFeatureCount(ilayer);
994 unsigned long int ntotalvalidLayer=0;
996 pfnProgress(progress,pszMessage,pProgressArg);
997 while( (readFeature = readLayer->GetNextFeature()) != NULL ){
998 bool validFeature=
false;
999 bool writeTest=
false;
1000 if(verbose_opt[0]>0)
1001 std::cout <<
"reading feature " << readFeature->GetFID() << std::endl;
1005 double p=
static_cast<double>(rand())/(RAND_MAX);
1015 if(threshold_opt.size()==layer_opt.size()){
1016 if(ntotalvalidLayer>=-theThreshold){
1024 if(ntotalvalid>=-theThreshold){
1032 if(verbose_opt[0]>0)
1033 std::cout <<
"processing feature " << readFeature->GetFID() << std::endl;
1036 OGRGeometry *poGeometry;
1037 poGeometry = readFeature->GetGeometryRef();
1038 assert(poGeometry!=NULL);
1040 if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint ){
1042 if(!buffer_opt.size()){
1043 switch(ruleMap[rule_opt[0]]){
1045 case(rule::centroid):
1046 buffer_opt.push_back(1);
1049 buffer_opt.push_back(3);
1053 if(verbose_opt[0]>1)
1054 std::cout <<
"boundary: " << buffer_opt[0] << std::endl;
1056 OGRPolygon writePolygon;
1057 OGRLinearRing writeRing;
1058 OGRPoint writeCentroidPoint;
1059 OGRFeature *writePolygonFeature;
1060 OGRFeature *writeCentroidFeature;
1062 OGRPoint *poPoint = (OGRPoint *) poGeometry;
1063 writeCentroidPoint=*poPoint;
1068 double i_centre,j_centre;
1070 imgReader.geo2image(x,y,i_centre,j_centre);
1076 j_centre=
static_cast<int>(j_centre);
1077 i_centre=
static_cast<int>(i_centre);
1079 double uli=i_centre-buffer_opt[0]/2;
1080 double ulj=j_centre-buffer_opt[0]/2;
1081 double lri=i_centre+buffer_opt[0]/2;
1082 double lrj=j_centre+buffer_opt[0]/2;
1085 ulj=
static_cast<int>(ulj);
1086 uli=
static_cast<int>(uli);
1087 lrj=
static_cast<int>(lrj);
1088 lri=
static_cast<int>(lri);
1098 if(static_cast<int>(ulj)<0||
static_cast<int>(ulj)>=imgReader.nrOfRow())
1101 if(static_cast<int>(uli)<0||
static_cast<int>(lri)>=imgReader.nrOfCol())
1104 OGRPoint ulPoint,urPoint,llPoint,lrPoint;
1111 double radius=buffer_opt[0]/2.0*sqrt(imgReader.getDeltaX()*imgReader.getDeltaY());
1112 unsigned short nstep = 25;
1113 for(
int i=0;i<nstep;++i){
1115 aPoint.setX(x+imgReader.getDeltaX()/2.0+radius*cos(2*PI*i/nstep));
1116 aPoint.setY(y-imgReader.getDeltaY()/2.0+radius*sin(2*PI*i/nstep));
1117 writeRing.addPoint(&aPoint);
1119 writePolygon.addRing(&writeRing);
1120 writePolygon.closeRings();
1125 imgReader.image2geo(uli,ulj,ulx,uly);
1126 imgReader.image2geo(lri,lrj,lrx,lry);
1127 ulPoint.setX(ulx-imgReader.getDeltaX()/2.0);
1128 ulPoint.setY(uly+imgReader.getDeltaY()/2.0);
1129 lrPoint.setX(lrx+imgReader.getDeltaX()/2.0);
1130 lrPoint.setY(lry-imgReader.getDeltaY()/2.0);
1131 urPoint.setX(lrx+imgReader.getDeltaX()/2.0);
1132 urPoint.setY(uly+imgReader.getDeltaY()/2.0);
1133 llPoint.setX(ulx-imgReader.getDeltaX()/2.0);
1134 llPoint.setY(lry-imgReader.getDeltaY()/2.0);
1136 writeRing.addPoint(&ulPoint);
1137 writeRing.addPoint(&urPoint);
1138 writeRing.addPoint(&lrPoint);
1139 writeRing.addPoint(&llPoint);
1140 writePolygon.addRing(&writeRing);
1141 writePolygon.closeRings();
1145 if((polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point)||(ruleMap[rule_opt[0]]==rule::centroid)){
1155 writePolygonFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1157 writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1159 else if(ruleMap[rule_opt[0]]!=rule::point){
1161 writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1163 writeCentroidFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1166 vector<double> windowClassValues;
1168 if(class_opt.size()){
1169 windowClassValues.resize(class_opt.size());
1171 for(
int iclass=0;iclass<class_opt.size();++iclass)
1172 windowClassValues[iclass]=0;
1175 windowValues.resize(nband);
1176 vector< Vector2d<double> > readValues(nband);
1177 for(
int iband=0;iband<nband;++iband){
1178 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1179 imgReader.readDataBlock(readValues[iband],GDT_Float64,uli,lri,ulj,lrj,theBand);
1183 for(
int j=ulj;j<=lrj;++j){
1184 for(
int i=uli;i<=lri;++i){
1186 if(i<0||i>=imgReader.nrOfCol())
1188 if(j<0||j>=imgReader.nrOfRow())
1193 imgReader.image2geo(i,j,theX,theY);
1194 thePoint.setX(theX);
1195 thePoint.setY(theY);
1197 if(disc_opt[0]&&buffer_opt[0]>1){
1198 double radius=buffer_opt[0]/2.0*sqrt(imgReader.getDeltaX()*imgReader.getDeltaY());
1199 if((theX-x)*(theX-x)+(theY-y)*(theY-y)>radius*radius)
1204 if(srcnodata_opt.size()){
1205 for(
int vband=0;vband<bndnodata_opt.size();++vband){
1206 double value=((readValues[bndnodata_opt[vband]])[j-ulj])[i-uli];
1207 if(value==srcnodata_opt[vband]){
1222 OGRFeature *writePointFeature;
1223 if(!polygon_opt[0]){
1225 if(ruleMap[rule_opt[0]]==rule::point){
1227 writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1229 writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1230 if(verbose_opt[0]>1)
1231 std::cout <<
"copying fields from polygons " << std::endl;
1236 if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE)
1237 cerr <<
"writing feature failed" << std::endl;
1239 assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
1243 if(verbose_opt[0]>1)
1244 std::cout <<
"write feature has " << writePointFeature->GetFieldCount() <<
" fields" << std::endl;
1247 if(class_opt.size()){
1248 short value=((readValues[0])[j-ulj])[i-uli];
1249 for(
int iclass=0;iclass<class_opt.size();++iclass){
1250 if(value==class_opt[iclass])
1251 windowClassValues[iclass]+=1;
1255 for(
int iband=0;iband<nband;++iband){
1256 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1258 assert(j-ulj<readValues[iband].size());
1260 assert(i-uli<((readValues[iband])[j-ulj]).size());
1261 double value=((readValues[iband])[j-ulj])[i-uli];
1263 if(verbose_opt[0]>1)
1264 std::cout <<
": " << value << std::endl;
1265 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
1266 windowValues[iband].push_back(value);
1270 if(verbose_opt[0]>1)
1271 std::cout <<
"set field " << fieldname_opt[iband] <<
" to " << value << std::endl;
1272 switch( fieldType ){
1275 writePointFeature->SetField(fieldname_opt[iband].c_str(),value);
1278 writePointFeature->SetField(fieldname_opt[iband].c_str(),type2string<double>(value).c_str());
1281 std::string errorString=
"field type not supported";
1286 catch(std::string e){
1287 std::cout << e << std::endl;
1293 if(!polygon_opt[0]){
1294 if(ruleMap[rule_opt[0]]==rule::point){
1296 if(verbose_opt[0]>1)
1297 std::cout <<
"creating point feature" << std::endl;
1299 if(writeTestLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1300 std::string errorString=
"Failed to create feature in test ogr vector dataset";
1305 if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1306 std::string errorString=
"Failed to create feature in ogr vector dataset";
1311 OGRFeature::DestroyFeature( writePointFeature );
1318 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
1322 cout <<
"no points found in window, continuing" << endl;
1332 if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE)
1333 cerr <<
"writing feature failed" << std::endl;
1335 writePolygonFeature->SetGeometry(&writePolygon);
1336 assert(wkbFlatten(writePolygonFeature->GetGeometryRef()->getGeometryType()) == wkbPolygon);
1338 if(verbose_opt[0]>1)
1339 std::cout <<
"copying new fields write polygon " << std::endl;
1340 if(verbose_opt[0]>1)
1341 std::cout <<
"write feature has " << writePolygonFeature->GetFieldCount() <<
" fields" << std::endl;
1346 if(verbose_opt[0]>1)
1347 std::cout <<
"copying fields from polygons " << std::endl;
1352 if(writeCentroidFeature->SetFrom(readFeature)!= OGRERR_NONE)
1353 cerr <<
"writing feature failed" << std::endl;
1354 assert(wkbFlatten(writeCentroidFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
1359 if(verbose_opt[0]>1)
1360 std::cout <<
"write feature has " << writeCentroidFeature->GetFieldCount() <<
" fields" << std::endl;
1362 if(class_opt.empty()){
1363 if(ruleMap[rule_opt[0]]==rule::point){
1365 std::cout <<
"number of points in window: " << nPointWindow << std::endl;
1366 for(
int index=0;index<windowValues.size();++index){
1368 if(windowValues[index].size()!=1){
1369 cerr <<
"Error: windowValues[index].size()=" << windowValues[index].size() << endl;
1370 assert(windowValues[index].size()==1);
1372 double theValue=windowValues[index].back();
1375 std::cout <<
"number of points in window: " << nPointWindow << std::endl;
1376 int theBand=(band_opt.size()) ? band_opt[index] : index;
1379 if(verbose_opt[0]>1)
1380 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1381 switch( fieldType ){
1385 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1387 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1391 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1393 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1396 std::string errorString=
"field type not supported";
1401 catch(std::string e){
1402 std::cout << e << std::endl;
1409 for(
int index=0;index<windowValues.size();++index){
1411 if(ruleMap[rule_opt[0]]==rule::mean)
1412 theValue=stat.mean(windowValues[index]);
1413 else if(ruleMap[rule_opt[0]]==rule::stdev)
1414 theValue=sqrt(stat.var(windowValues[index]));
1415 else if(ruleMap[rule_opt[0]]==rule::median)
1416 theValue=stat.median(windowValues[index]);
1417 else if(ruleMap[rule_opt[0]]==rule::percentile)
1418 theValue=stat.percentile(windowValues[index],windowValues[index].begin(),windowValues[index].end(),percentile_opt[0]);
1419 else if(ruleMap[rule_opt[0]]==rule::sum)
1420 theValue=stat.sum(windowValues[index]);
1421 else if(ruleMap[rule_opt[0]]==rule::max)
1422 theValue=stat.mymax(windowValues[index]);
1423 else if(ruleMap[rule_opt[0]]==rule::min)
1424 theValue=stat.mymin(windowValues[index]);
1425 else if(ruleMap[rule_opt[0]]==rule::centroid){
1427 std::cout <<
"number of points in polygon: " << nPointWindow << std::endl;
1428 assert(nPointWindow<=1);
1429 assert(nPointWindow==windowValues[index].size());
1430 theValue=windowValues[index].back();
1433 std::string errorString=
"rule not supported";
1436 if(verbose_opt[0]>1)
1437 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1438 switch( fieldType ){
1442 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1444 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1448 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1450 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1453 std::string errorString=
"field type not supported";
1458 catch(std::string e){
1459 std::cout << e << std::endl;
1466 if(ruleMap[rule_opt[0]]==rule::proportion||ruleMap[rule_opt[0]]==rule::count){
1468 std::cout <<
"number of points in polygon: " << nPointWindow << std::endl;
1469 if(ruleMap[rule_opt[0]]==rule::proportion)
1470 stat.normalize_pct(windowClassValues);
1471 for(
int index=0;index<windowClassValues.size();++index){
1472 double theValue=windowClassValues[index];
1474 fs << class_opt[index];
1476 writePolygonFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1478 writeCentroidFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1481 else if(ruleMap[rule_opt[0]]==rule::custom){
1482 assert(polygon_opt[0]);
1484 std::cout <<
"number of points in polygon: " << nPointWindow << std::endl;
1485 stat.normalize_pct(windowClassValues);
1486 assert(windowClassValues.size()==2);
1487 if(windowClassValues[0]>=75)
1488 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(11));
1489 else if(windowClassValues[1]>=75)
1490 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(12));
1491 else if(windowClassValues[0]>25&&windowClassValues[1]>25)
1492 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(13));
1495 std::cout <<
"No valid value in windowClassValues..." << std::endl;
1496 for(
int index=0;index<windowClassValues.size();++index){
1497 double theValue=windowClassValues[index];
1498 std::cout << theValue <<
" ";
1500 std::cout << std::endl;
1502 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(20));
1505 else if(ruleMap[rule_opt[0]]==rule::mode){
1508 std::cout <<
"number of points in window: " << nPointWindow << std::endl;
1510 int maxClass=stat.mymin(class_opt);
1511 vector<double>::iterator maxit;
1512 maxit=stat.mymax(windowClassValues,windowClassValues.begin(),windowClassValues.end());
1513 int maxIndex=distance(windowClassValues.begin(),maxit);
1514 maxClass=class_opt[maxIndex];
1515 if(verbose_opt[0]>0)
1516 std::cout <<
"maxClass: " << maxClass << std::endl;
1518 writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
1520 writeCentroidFeature->SetField(label_opt[0].c_str(),maxClass);
1524 if(verbose_opt[0]>1)
1525 std::cout <<
"creating polygon feature" << std::endl;
1527 if(writeTestLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1528 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1533 if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1534 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1538 OGRFeature::DestroyFeature( writePolygonFeature );
1543 if(verbose_opt[0]>1)
1544 std::cout <<
"creating point feature in centroid" << std::endl;
1546 if(writeTestLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1547 std::string errorString=
"Failed to create point feature in ogr vector dataset";
1553 assert(validFeature);
1554 if(writeLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1555 std::string errorString=
"Failed to create point feature in ogr vector dataset";
1559 OGRFeature::DestroyFeature( writeCentroidFeature );
1565 else if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){
1567 OGRPolygon readPolygon = *((OGRPolygon *) poGeometry);
1568 OGRPolygon writePolygon;
1569 OGRLinearRing writeRing;
1570 OGRPoint writeCentroidPoint;
1571 OGRFeature *writePolygonFeature;
1572 OGRFeature *writeCentroidFeature;
1574 readPolygon.closeRings();
1576 if(verbose_opt[0]>1)
1577 std::cout <<
"get point on polygon" << std::endl;
1578 if(ruleMap[rule_opt[0]]==rule::centroid)
1579 readPolygon.Centroid(&writeCentroidPoint);
1580 else if(readPolygon.PointOnSurface(&writeCentroidPoint)!=OGRERR_NONE){
1582 readPolygon.Centroid(&writeCentroidPoint);
1584 double ulx,uly,lrx,lry;
1585 double uli,ulj,lri,lrj;
1586 if((polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point)||(ruleMap[rule_opt[0]]==rule::centroid)){
1587 ulx=writeCentroidPoint.getX();
1588 uly=writeCentroidPoint.getY();
1595 std::cout <<
"reading envelope for polygon " << ifeature << std::endl;
1596 OGREnvelope* psEnvelope=
new OGREnvelope();
1597 readPolygon.getEnvelope(psEnvelope);
1598 ulx=psEnvelope->MinX;
1599 uly=psEnvelope->MaxY;
1600 lrx=psEnvelope->MaxX;
1601 lry=psEnvelope->MinY;
1605 imgReader.geo2image(ulx,uly,uli,ulj);
1606 imgReader.geo2image(lrx,lry,lri,lrj);
1615 ulj=
static_cast<int>(ulj);
1616 uli=
static_cast<int>(uli);
1617 lrj=
static_cast<int>(lrj);
1618 lri=
static_cast<int>(lri);
1620 if(verbose_opt[0]>1)
1621 std::cout <<
"bounding box for polygon feature " << ifeature <<
": " << uli <<
" " << ulj <<
" " << lri <<
" " << lrj << std::endl;
1627 if(uli>=imgReader.nrOfCol())
1628 uli=imgReader.nrOfCol()-1;
1629 if(lri>=imgReader.nrOfCol())
1630 lri=imgReader.nrOfCol()-1;
1635 if(ulj>=imgReader.nrOfRow())
1636 ulj=imgReader.nrOfRow()-1;
1637 if(lrj>=imgReader.nrOfRow())
1638 lrj=imgReader.nrOfRow()-1;
1642 int nPointPolygon=0;
1646 writePolygonFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1648 writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1650 else if(ruleMap[rule_opt[0]]!=rule::point){
1652 writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1654 writeCentroidFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1658 vector<double> polyClassValues;
1660 if(class_opt.size()){
1662 polyClassValues.resize(class_opt.size());
1664 for(
int iclass=0;iclass<class_opt.size();++iclass)
1665 polyClassValues[iclass]=0;
1668 polyValues.resize(nband);
1669 vector< Vector2d<double> > readValues(nband);
1670 for(
int iband=0;iband<nband;++iband){
1671 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1672 imgReader.readDataBlock(readValues[iband],GDT_Float64,uli,lri,ulj,lrj,theBand);
1676 for(
int j=ulj;j<=lrj;++j){
1677 for(
int i=uli;i<=lri;++i){
1679 if(i<0||i>=imgReader.nrOfCol())
1681 if(j<0||j>=imgReader.nrOfRow())
1686 imgReader.image2geo(i,j,theX,theY);
1687 thePoint.setX(theX);
1688 thePoint.setY(theY);
1689 if(ruleMap[rule_opt[0]]!=rule::centroid&&!readPolygon.Contains(&thePoint))
1694 if(srcnodata_opt.size()){
1695 for(
int vband=0;vband<bndnodata_opt.size();++vband){
1696 double value=((readValues[bndnodata_opt[vband]])[j-ulj])[i-uli];
1697 if(value==srcnodata_opt[vband]){
1709 writeRing.addPoint(&thePoint);
1712 if(verbose_opt[0]>1)
1713 std::cout <<
"point is on surface:" << thePoint.getX() <<
"," << thePoint.getY() << std::endl;
1716 if(polythreshold_opt.size())
1717 if(nPointPolygon>polythreshold_opt[0])
1720 OGRFeature *writePointFeature;
1721 if(!polygon_opt[0]){
1723 if(ruleMap[rule_opt[0]]==rule::point){
1725 writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1727 writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1728 if(verbose_opt[0]>1)
1729 std::cout <<
"copying fields from polygons " << std::endl;
1730 if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE)
1731 cerr <<
"writing feature failed" << std::endl;
1732 if(verbose_opt[0]>1)
1733 std::cout <<
"set geometry as point " << std::endl;
1735 writePointFeature->SetGeometry(&thePoint);
1736 assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
1740 if(verbose_opt[0]>1)
1741 std::cout <<
"write feature has " << writePointFeature->GetFieldCount() <<
" fields" << std::endl;
1744 if(class_opt.size()){
1745 short value=((readValues[0])[j-ulj])[i-uli];
1746 for(
int iclass=0;iclass<class_opt.size();++iclass){
1747 if(value==class_opt[iclass])
1748 polyClassValues[iclass]+=1;
1752 for(
int iband=0;iband<nband;++iband){
1753 double value=((readValues[iband])[j-ulj])[i-uli];
1754 if(verbose_opt[0]>1)
1755 std::cout <<
": " << value << std::endl;
1756 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point)
1757 polyValues[iband].push_back(value);
1759 if(verbose_opt[0]>1)
1760 std::cout <<
"set field " << fieldname_opt[iband] <<
" to " << value << std::endl;
1761 switch( fieldType ){
1764 writePointFeature->SetField(fieldname_opt[iband].c_str(),value);
1767 writePointFeature->SetField(fieldname_opt[iband].c_str(),type2string<double>(value).c_str());
1776 if(!polygon_opt[0]){
1777 if(ruleMap[rule_opt[0]]==rule::point){
1779 if(verbose_opt[0]>1)
1780 std::cout <<
"creating point feature" << std::endl;
1782 if(writeTestLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1783 std::string errorString=
"Failed to create feature in test ogr vector dataset";
1788 if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1789 std::string errorString=
"Failed to create feature in ogr vector dataset";
1794 OGRFeature::DestroyFeature( writePointFeature );
1801 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
1805 cout <<
"no points found in polygon, continuing" << endl;
1810 writePolygon.addRing(&writeRing);
1811 writePolygon.closeRings();
1816 if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE)
1817 cerr <<
"writing feature failed" << std::endl;
1818 if(verbose_opt[0]>1)
1819 std::cout <<
"copying new fields write polygon " << std::endl;
1820 if(verbose_opt[0]>1)
1821 std::cout <<
"write feature has " << writePolygonFeature->GetFieldCount() <<
" fields" << std::endl;
1826 if(verbose_opt[0]>1)
1827 std::cout <<
"copying fields from polygons " << std::endl;
1828 if(writeCentroidFeature->SetFrom(readFeature)!= OGRERR_NONE)
1829 cerr <<
"writing feature failed" << std::endl;
1830 writeCentroidFeature->SetGeometry(&writeCentroidPoint);
1831 assert(wkbFlatten(writeCentroidFeature->GetGeometryRef()->getGeometryType()) == wkbPoint );
1832 if(verbose_opt[0]>1)
1833 std::cout <<
"write feature has " << writeCentroidFeature->GetFieldCount() <<
" fields" << std::endl;
1835 if(class_opt.empty()){
1836 if(ruleMap[rule_opt[0]]==rule::point){
1838 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1839 for(
int index=0;index<polyValues.size();++index){
1840 assert(polyValues[index].size()==1);
1841 double theValue=polyValues[index].back();
1844 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1845 int theBand=(band_opt.size()) ? band_opt[index] : index;
1847 if(verbose_opt[0]>1)
1848 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1849 switch( fieldType ){
1853 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1855 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1859 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1861 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1864 std::string errorString=
"field type not supported";
1869 catch(std::string e){
1870 std::cout << e << std::endl;
1877 for(
int index=0;index<polyValues.size();++index){
1879 if(ruleMap[rule_opt[0]]==rule::mean)
1880 theValue=stat.mean(polyValues[index]);
1881 else if(ruleMap[rule_opt[0]]==rule::stdev)
1882 theValue=sqrt(stat.var(polyValues[index]));
1883 else if(ruleMap[rule_opt[0]]==rule::median)
1884 theValue=stat.median(polyValues[index]);
1885 else if(ruleMap[rule_opt[0]]==rule::percentile)
1886 theValue=stat.percentile(polyValues[index],polyValues[index].begin(),polyValues[index].end(),percentile_opt[0]);
1887 else if(ruleMap[rule_opt[0]]==rule::sum)
1888 theValue=stat.sum(polyValues[index]);
1889 else if(ruleMap[rule_opt[0]]==rule::max)
1890 theValue=stat.mymax(polyValues[index]);
1891 else if(ruleMap[rule_opt[0]]==rule::min)
1892 theValue=stat.mymin(polyValues[index]);
1893 else if(ruleMap[rule_opt[0]]==rule::centroid){
1895 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1896 assert(nPointPolygon<=1);
1897 assert(nPointPolygon==polyValues[index].size());
1898 theValue=polyValues[index].back();
1901 std::string errorString=
"rule not supported";
1904 if(verbose_opt[0]>1)
1905 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1906 switch( fieldType ){
1910 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1912 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1916 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1918 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1921 std::string errorString=
"field type not supported";
1926 catch(std::string e){
1927 std::cout << e << std::endl;
1934 if(ruleMap[rule_opt[0]]==rule::proportion||ruleMap[rule_opt[0]]==rule::count){
1936 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1937 if(ruleMap[rule_opt[0]]==rule::proportion)
1938 stat.normalize_pct(polyClassValues);
1939 for(
int index=0;index<polyClassValues.size();++index){
1940 double theValue=polyClassValues[index];
1942 fs << class_opt[index];
1944 writePolygonFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1946 writeCentroidFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1949 else if(ruleMap[rule_opt[0]]==rule::custom){
1950 assert(polygon_opt[0]);
1952 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1953 stat.normalize_pct(polyClassValues);
1954 assert(polyClassValues.size()==2);
1955 if(polyClassValues[0]>=75)
1956 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(11));
1957 else if(polyClassValues[1]>=75)
1958 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(12));
1959 else if(polyClassValues[0]>25&&polyClassValues[1]>25)
1960 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(13));
1963 std::cout <<
"No valid value in polyClassValues..." << std::endl;
1964 for(
int index=0;index<polyClassValues.size();++index){
1965 double theValue=polyClassValues[index];
1966 std::cout << theValue <<
" ";
1968 std::cout << std::endl;
1970 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(20));
1973 else if(ruleMap[rule_opt[0]]==rule::mode){
1976 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1978 int maxClass=stat.mymin(class_opt);
1979 vector<double>::iterator maxit;
1980 maxit=stat.mymax(polyClassValues,polyClassValues.begin(),polyClassValues.end());
1981 int maxIndex=distance(polyClassValues.begin(),maxit);
1982 maxClass=class_opt[maxIndex];
1983 if(verbose_opt[0]>0)
1984 std::cout <<
"maxClass: " << maxClass << std::endl;
1986 writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
1988 writeCentroidFeature->SetField(label_opt[0].c_str(),maxClass);
1992 if(verbose_opt[0]>1)
1993 std::cout <<
"creating polygon feature" << std::endl;
1995 if(writeTestLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1996 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
2001 if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
2002 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
2006 OGRFeature::DestroyFeature( writePolygonFeature );
2011 if(verbose_opt[0]>1)
2012 std::cout <<
"creating point feature in centroid" << std::endl;
2014 if(writeTestLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
2015 std::string errorString=
"Failed to create point feature in ogr vector dataset";
2021 assert(validFeature);
2022 if(writeLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
2023 std::string errorString=
"Failed to create point feature in ogr vector dataset";
2027 OGRFeature::DestroyFeature( writeCentroidFeature );
2033 else if(wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon){
2034 OGRMultiPolygon readPolygon = *((OGRMultiPolygon *) poGeometry);
2035 OGRPolygon writePolygon;
2036 OGRLinearRing writeRing;
2037 OGRPoint writeCentroidPoint;
2038 OGRFeature *writePolygonFeature;
2039 OGRFeature *writeCentroidFeature;
2041 readPolygon.closeRings();
2043 if(verbose_opt[0]>1)
2044 std::cout <<
"get centroid point from polygon" << std::endl;
2045 readPolygon.Centroid(&writeCentroidPoint);
2047 double ulx,uly,lrx,lry;
2048 double uli,ulj,lri,lrj;
2049 if((polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point)||(ruleMap[rule_opt[0]]==rule::centroid)){
2050 ulx=writeCentroidPoint.getX();
2051 uly=writeCentroidPoint.getY();
2058 std::cout <<
"reading envelope for polygon " << ifeature << std::endl;
2059 OGREnvelope* psEnvelope=
new OGREnvelope();
2060 readPolygon.getEnvelope(psEnvelope);
2061 ulx=psEnvelope->MinX;
2062 uly=psEnvelope->MaxY;
2063 lrx=psEnvelope->MaxX;
2064 lry=psEnvelope->MinY;
2068 imgReader.geo2image(ulx,uly,uli,ulj);
2069 imgReader.geo2image(lrx,lry,lri,lrj);
2078 ulj=
static_cast<int>(ulj);
2079 uli=
static_cast<int>(uli);
2080 lrj=
static_cast<int>(lrj);
2081 lri=
static_cast<int>(lri);
2083 if(verbose_opt[0]>1)
2084 std::cout <<
"bounding box for multipologon feature " << ifeature <<
": " << uli <<
" " << ulj <<
" " << lri <<
" " << lrj << std::endl;
2090 if(uli>=imgReader.nrOfCol())
2091 uli=imgReader.nrOfCol()-1;
2092 if(lri>=imgReader.nrOfCol())
2093 lri=imgReader.nrOfCol()-1;
2098 if(ulj>=imgReader.nrOfRow())
2099 ulj=imgReader.nrOfRow()-1;
2100 if(lrj>=imgReader.nrOfRow())
2101 lrj=imgReader.nrOfRow()-1;
2105 int nPointPolygon=0;
2108 writePolygonFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
2110 writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
2112 else if(ruleMap[rule_opt[0]]!=rule::point){
2114 writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
2116 writeCentroidFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
2120 vector<double> polyClassValues;
2122 if(class_opt.size()){
2123 polyClassValues.resize(class_opt.size());
2125 for(
int iclass=0;iclass<class_opt.size();++iclass)
2126 polyClassValues[iclass]=0;
2129 polyValues.resize(nband);
2130 vector< Vector2d<double> > readValues(nband);
2131 for(
int iband=0;iband<nband;++iband){
2132 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
2133 imgReader.readDataBlock(readValues[iband],GDT_Float64,uli,lri,ulj,lrj,theBand);
2137 for(
int j=ulj;j<=lrj;++j){
2138 for(
int i=uli;i<=lri;++i){
2140 if(i<0||i>=imgReader.nrOfCol())
2142 if(j<0||j>=imgReader.nrOfRow())
2147 imgReader.image2geo(i,j,theX,theY);
2148 thePoint.setX(theX);
2149 thePoint.setY(theY);
2151 if(ruleMap[rule_opt[0]]!=rule::centroid&&!readPolygon.Contains(&thePoint))
2156 if(srcnodata_opt.size()){
2157 for(
int vband=0;vband<bndnodata_opt.size();++vband){
2158 double value=((readValues[bndnodata_opt[vband]])[j-ulj])[i-uli];
2159 if(value==srcnodata_opt[vband]){
2171 writeRing.addPoint(&thePoint);
2174 if(verbose_opt[0]>1)
2175 std::cout <<
"point is on surface:" << thePoint.getX() <<
"," << thePoint.getY() << std::endl;
2178 if(polythreshold_opt.size())
2179 if(nPointPolygon>polythreshold_opt[0])
2182 OGRFeature *writePointFeature;
2183 if(!polygon_opt[0]){
2185 if(ruleMap[rule_opt[0]]==rule::point){
2187 writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
2189 writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
2190 if(verbose_opt[0]>1)
2191 std::cout <<
"copying fields from polygons " << std::endl;
2192 if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE)
2193 cerr <<
"writing feature failed" << std::endl;
2194 if(verbose_opt[0]>1)
2195 std::cout <<
"set geometry as point " << std::endl;
2197 writePointFeature->SetGeometry(&thePoint);
2198 assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
2202 if(verbose_opt[0]>1)
2203 std::cout <<
"write feature has " << writePointFeature->GetFieldCount() <<
" fields" << std::endl;
2206 if(class_opt.size()){
2207 short value=((readValues[0])[j-ulj])[i-uli];
2208 for(
int iclass=0;iclass<class_opt.size();++iclass){
2209 if(value==class_opt[iclass])
2210 polyClassValues[iclass]+=1;
2214 for(
int iband=0;iband<nband;++iband){
2215 double value=((readValues[iband])[j-ulj])[i-uli];
2216 if(verbose_opt[0]>1)
2217 std::cout <<
": " << value << std::endl;
2218 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point)
2219 polyValues[iband].push_back(value);
2221 if(verbose_opt[0]>1)
2222 std::cout <<
"set field " << fieldname_opt[iband] <<
" to " << value << std::endl;
2223 switch( fieldType ){
2226 writePointFeature->SetField(fieldname_opt[iband].c_str(),value);
2229 writePointFeature->SetField(fieldname_opt[iband].c_str(),type2string<double>(value).c_str());
2238 if(!polygon_opt[0]){
2239 if(ruleMap[rule_opt[0]]==rule::point){
2241 if(verbose_opt[0]>1)
2242 std::cout <<
"creating point feature" << std::endl;
2244 if(writeTestLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
2245 std::string errorString=
"Failed to create feature in ogr vector dataset";
2250 if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
2251 std::string errorString=
"Failed to create feature in ogr vector dataset";
2256 OGRFeature::DestroyFeature( writePointFeature );
2266 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
2272 writePolygon.addRing(&writeRing);
2273 writePolygon.closeRings();
2278 if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE)
2279 cerr <<
"writing feature failed" << std::endl;
2280 assert(writePolygonFeature->GetGeometryRef()->getGeometryType()==wkbMultiPolygon);
2281 if(verbose_opt[0]>1)
2282 std::cout <<
"copying new fields write polygon " << std::endl;
2283 if(verbose_opt[0]>1)
2284 std::cout <<
"write feature has " << writePolygonFeature->GetFieldCount() <<
" fields" << std::endl;
2289 if(verbose_opt[0]>1)
2290 std::cout <<
"copying fields from polygons " << std::endl;
2291 if(writeCentroidFeature->SetFrom(readFeature)!= OGRERR_NONE)
2292 cerr <<
"writing feature failed" << std::endl;
2293 writeCentroidFeature->SetGeometry(&writeCentroidPoint);
2294 assert(wkbFlatten(writeCentroidFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
2295 if(verbose_opt[0]>1)
2296 std::cout <<
"write feature has " << writeCentroidFeature->GetFieldCount() <<
" fields" << std::endl;
2298 if(class_opt.empty()){
2299 if(ruleMap[rule_opt[0]]==rule::point){
2301 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2302 for(
int index=0;index<polyValues.size();++index){
2304 assert(polyValues[index].size()==1);
2305 double theValue=polyValues[index].back();
2307 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2308 int theBand=(band_opt.size()) ? band_opt[index] : index;
2310 if(verbose_opt[0]>1)
2311 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
2312 switch( fieldType ){
2316 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
2318 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
2322 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2324 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2327 std::string errorString=
"field type not supported";
2332 catch(std::string e){
2333 std::cout << e << std::endl;
2340 for(
int index=0;index<polyValues.size();++index){
2342 if(ruleMap[rule_opt[0]]==rule::mean)
2343 theValue=stat.mean(polyValues[index]);
2344 else if(ruleMap[rule_opt[0]]==rule::stdev)
2345 theValue=sqrt(stat.var(polyValues[index]));
2346 else if(ruleMap[rule_opt[0]]==rule::median)
2347 theValue=stat.median(polyValues[index]);
2348 else if(ruleMap[rule_opt[0]]==rule::percentile)
2349 theValue=stat.percentile(polyValues[index],polyValues[index].begin(),polyValues[index].end(),percentile_opt[0]);
2350 else if(ruleMap[rule_opt[0]]==rule::sum)
2351 theValue=stat.sum(polyValues[index]);
2352 else if(ruleMap[rule_opt[0]]==rule::max)
2353 theValue=stat.mymax(polyValues[index]);
2354 else if(ruleMap[rule_opt[0]]==rule::min)
2355 theValue=stat.mymin(polyValues[index]);
2356 else if(ruleMap[rule_opt[0]]==rule::centroid){
2358 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2359 assert(nPointPolygon<=1);
2360 assert(nPointPolygon==polyValues[index].size());
2361 theValue=polyValues[index].back();
2364 std::string errorString=
"rule not supported";
2367 if(verbose_opt[0]>1)
2368 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
2369 switch( fieldType ){
2373 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
2375 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
2379 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2381 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2384 std::string errorString=
"field type not supported";
2389 catch(std::string e){
2390 std::cout << e << std::endl;
2397 if(ruleMap[rule_opt[0]]==rule::proportion||ruleMap[rule_opt[0]]==rule::count){
2399 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2400 if(ruleMap[rule_opt[0]]==rule::proportion)
2401 stat.normalize_pct(polyClassValues);
2402 for(
int index=0;index<polyClassValues.size();++index){
2403 double theValue=polyClassValues[index];
2405 fs << class_opt[index];
2407 writePolygonFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
2409 writeCentroidFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
2412 else if(ruleMap[rule_opt[0]]==rule::custom){
2413 assert(polygon_opt[0]);
2415 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2416 stat.normalize_pct(polyClassValues);
2417 assert(polyClassValues.size()==2);
2418 if(polyClassValues[0]>=75)
2419 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(11));
2420 else if(polyClassValues[1]>=75)
2421 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(12));
2422 else if(polyClassValues[0]>25&&polyClassValues[1]>25)
2423 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(13));
2426 std::cout <<
"No valid value in polyClassValues..." << std::endl;
2427 for(
int index=0;index<polyClassValues.size();++index){
2428 double theValue=polyClassValues[index];
2429 std::cout << theValue <<
" ";
2431 std::cout << std::endl;
2433 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(20));
2436 else if(ruleMap[rule_opt[0]]==rule::mode){
2439 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2441 int maxClass=stat.mymin(class_opt);
2442 vector<double>::iterator maxit;
2443 maxit=stat.mymax(polyClassValues,polyClassValues.begin(),polyClassValues.end());
2444 int maxIndex=distance(polyClassValues.begin(),maxit);
2445 maxClass=class_opt[maxIndex];
2446 if(verbose_opt[0]>0)
2447 std::cout <<
"maxClass: " << maxClass << std::endl;
2449 writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
2451 writeCentroidFeature->SetField(label_opt[0].c_str(),maxClass);
2456 if(verbose_opt[0]>1)
2457 std::cout <<
"creating polygon feature" << std::endl;
2459 if(writeTestLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
2460 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
2465 if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
2466 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
2470 OGRFeature::DestroyFeature( writePolygonFeature );
2475 if(verbose_opt[0]>1)
2476 std::cout <<
"creating point feature in centroid" << std::endl;
2478 if(writeTestLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
2479 std::string errorString=
"Failed to create point feature in ogr vector dataset";
2485 assert(validFeature);
2486 if(writeLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
2487 std::string errorString=
"Failed to create point feature in ogr vector dataset";
2491 OGRFeature::DestroyFeature( writeCentroidFeature );
2499 test=poGeometry->getGeometryName();
2501 oss <<
"geometry " << test <<
" not supported";
2506 if(threshold_opt.size()==layer_opt.size())
2507 progress=(100.0/theThreshold)*
static_cast<float>(ntotalvalidLayer)/nfeatureLayer;
2509 progress=
static_cast<float>(ntotalvalidLayer)/nfeatureLayer;
2512 progress=
static_cast<float>(ifeature+1)/(-theThreshold);
2513 pfnProgress(progress,pszMessage,pProgressArg);
2515 catch(std::string e){
2516 std::cout << e << std::endl;
2521 std::cout <<
"number of points read in polygon: " << npoint << std::endl;
2528 pfnProgress(progress,pszMessage,pProgressArg);
2531 sampleReaderOgr.close();
2534 ogrTestWriter.close();
2537 pfnProgress(progress,pszMessage,pProgressArg);