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();
902 std::cout <<
"number of layers: " << nlayerRead << endl;
904 for(
int ilayer=0;ilayer<nlayerRead;++ilayer){
905 OGRLayer *readLayer=sampleReaderOgr.getLayer(ilayer);
906 string currentLayername=readLayer->GetName();
908 if(find(layer_opt.begin(),layer_opt.end(),currentLayername)==layer_opt.end())
910 cout <<
"processing layer " << currentLayername << endl;
912 readLayer->ResetReading();
913 OGRLayer *writeLayer;
914 OGRLayer *writeTestLayer;
918 std::cout <<
"create polygons" << std::endl;
919 char **papszOptions=NULL;
920 writeLayer=ogrWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPolygon, papszOptions);
922 writeTestLayer=ogrTestWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPolygon, papszOptions);
926 std::cout <<
"create points in layer " << readLayer->GetName() << std::endl;
927 char **papszOptions=NULL;
929 writeLayer=ogrWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPoint, papszOptions);
931 char **papszOptions=NULL;
932 writeTestLayer=ogrTestWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPoint, papszOptions);
936 std::cout <<
"copy fields from layer " << ilayer << std::flush << std::endl;
937 ogrWriter.copyFields(sampleReaderOgr,ilayer,ilayerWrite);
941 std::cout <<
"copy fields test writer" << std::flush << std::endl;
942 ogrTestWriter.copyFields(sampleReaderOgr,ilayer,ilayerWrite);
951 if(class_opt.size()){
952 switch(ruleMap[rule_opt[0]]){
953 case(rule::proportion):
955 for(
int iclass=0;iclass<class_opt.size();++iclass){
957 cs << class_opt[iclass];
958 ogrWriter.createField(cs.str(),fieldType,ilayerWrite);
964 ogrWriter.createField(label_opt[0],fieldType,ilayerWrite);
966 ogrTestWriter.createField(label_opt[0],fieldType,ilayerWrite);
971 for(
int iband=0;iband<nband;++iband){
972 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
974 fs << fieldname_opt[iband];
976 std::cout <<
"creating field " << fs.str() << std::endl;
978 ogrWriter.createField(fs.str(),fieldType,ilayerWrite);
980 ogrTestWriter.createField(fs.str(),fieldType,ilayerWrite);
983 OGRFeature *readFeature;
984 unsigned long int ifeature=0;
985 unsigned long int nfeature=sampleReaderOgr.getFeatureCount();
987 pfnProgress(progress,pszMessage,pProgressArg);
988 while( (readFeature = readLayer->GetNextFeature()) != NULL ){
989 bool validFeature=
false;
990 bool writeTest=
false;
992 std::cout <<
"reading feature " << readFeature->GetFID() << std::endl;
993 if(threshold_opt[0]>0){
994 double p=
static_cast<double>(rand())/(RAND_MAX);
996 if(p>threshold_opt[0]){
1004 if(ntotalvalid>=-threshold_opt[0]){
1011 if(verbose_opt[0]>0)
1012 std::cout <<
"processing feature " << readFeature->GetFID() << std::endl;
1015 OGRGeometry *poGeometry;
1016 poGeometry = readFeature->GetGeometryRef();
1017 assert(poGeometry!=NULL);
1019 if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint ){
1021 if(!buffer_opt.size()){
1022 switch(ruleMap[rule_opt[0]]){
1024 case(rule::centroid):
1025 buffer_opt.push_back(1);
1028 buffer_opt.push_back(3);
1032 if(verbose_opt[0]>1)
1033 std::cout <<
"boundary: " << buffer_opt[0] << std::endl;
1035 OGRPolygon writePolygon;
1036 OGRLinearRing writeRing;
1037 OGRPoint writeCentroidPoint;
1038 OGRFeature *writePolygonFeature;
1039 OGRFeature *writeCentroidFeature;
1041 OGRPoint *poPoint = (OGRPoint *) poGeometry;
1042 writeCentroidPoint=*poPoint;
1047 double i_centre,j_centre;
1049 imgReader.geo2image(x,y,i_centre,j_centre);
1055 j_centre=
static_cast<int>(j_centre);
1056 i_centre=
static_cast<int>(i_centre);
1058 double uli=i_centre-buffer_opt[0]/2;
1059 double ulj=j_centre-buffer_opt[0]/2;
1060 double lri=i_centre+buffer_opt[0]/2;
1061 double lrj=j_centre+buffer_opt[0]/2;
1064 ulj=
static_cast<int>(ulj);
1065 uli=
static_cast<int>(uli);
1066 lrj=
static_cast<int>(lrj);
1067 lri=
static_cast<int>(lri);
1077 if(static_cast<int>(ulj)<0||
static_cast<int>(ulj)>=imgReader.nrOfRow())
1080 if(static_cast<int>(uli)<0||
static_cast<int>(lri)>=imgReader.nrOfCol())
1083 OGRPoint ulPoint,urPoint,llPoint,lrPoint;
1090 double radius=buffer_opt[0]/2.0*sqrt(imgReader.getDeltaX()*imgReader.getDeltaY());
1091 unsigned short nstep = 25;
1092 for(
int i=0;i<nstep;++i){
1094 aPoint.setX(x+imgReader.getDeltaX()/2.0+radius*cos(2*PI*i/nstep));
1095 aPoint.setY(y-imgReader.getDeltaY()/2.0+radius*sin(2*PI*i/nstep));
1096 writeRing.addPoint(&aPoint);
1098 writePolygon.addRing(&writeRing);
1099 writePolygon.closeRings();
1104 imgReader.image2geo(uli,ulj,ulx,uly);
1105 imgReader.image2geo(lri,lrj,lrx,lry);
1106 ulPoint.setX(ulx-imgReader.getDeltaX()/2.0);
1107 ulPoint.setY(uly+imgReader.getDeltaY()/2.0);
1108 lrPoint.setX(lrx+imgReader.getDeltaX()/2.0);
1109 lrPoint.setY(lry-imgReader.getDeltaY()/2.0);
1110 urPoint.setX(lrx+imgReader.getDeltaX()/2.0);
1111 urPoint.setY(uly+imgReader.getDeltaY()/2.0);
1112 llPoint.setX(ulx-imgReader.getDeltaX()/2.0);
1113 llPoint.setY(lry-imgReader.getDeltaY()/2.0);
1115 writeRing.addPoint(&ulPoint);
1116 writeRing.addPoint(&urPoint);
1117 writeRing.addPoint(&lrPoint);
1118 writeRing.addPoint(&llPoint);
1119 writePolygon.addRing(&writeRing);
1120 writePolygon.closeRings();
1124 if((polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point)||(ruleMap[rule_opt[0]]==rule::centroid)){
1134 writePolygonFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1136 writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1138 else if(ruleMap[rule_opt[0]]!=rule::point){
1140 writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1142 writeCentroidFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1145 vector<double> windowClassValues;
1147 if(class_opt.size()){
1148 windowClassValues.resize(class_opt.size());
1150 for(
int iclass=0;iclass<class_opt.size();++iclass)
1151 windowClassValues[iclass]=0;
1154 windowValues.resize(nband);
1155 vector< Vector2d<double> > readValues(nband);
1156 for(
int iband=0;iband<nband;++iband){
1157 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1158 imgReader.readDataBlock(readValues[iband],GDT_Float64,uli,lri,ulj,lrj,theBand);
1162 for(
int j=ulj;j<=lrj;++j){
1163 for(
int i=uli;i<=lri;++i){
1165 if(i<0||i>=imgReader.nrOfCol())
1167 if(j<0||j>=imgReader.nrOfRow())
1172 imgReader.image2geo(i,j,theX,theY);
1173 thePoint.setX(theX);
1174 thePoint.setY(theY);
1176 if(disc_opt[0]&&buffer_opt[0]>1){
1177 double radius=buffer_opt[0]/2.0*sqrt(imgReader.getDeltaX()*imgReader.getDeltaY());
1178 if((theX-x)*(theX-x)+(theY-y)*(theY-y)>radius*radius)
1183 if(srcnodata_opt.size()){
1184 for(
int vband=0;vband<bndnodata_opt.size();++vband){
1185 double value=((readValues[bndnodata_opt[vband]])[j-ulj])[i-uli];
1186 if(value==srcnodata_opt[vband]){
1201 OGRFeature *writePointFeature;
1202 if(!polygon_opt[0]){
1204 if(ruleMap[rule_opt[0]]==rule::point){
1206 writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1208 writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1209 if(verbose_opt[0]>1)
1210 std::cout <<
"copying fields from polygons " << std::endl;
1215 if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE)
1216 cerr <<
"writing feature failed" << std::endl;
1218 assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
1222 if(verbose_opt[0]>1)
1223 std::cout <<
"write feature has " << writePointFeature->GetFieldCount() <<
" fields" << std::endl;
1226 if(class_opt.size()){
1227 short value=((readValues[0])[j-ulj])[i-uli];
1228 for(
int iclass=0;iclass<class_opt.size();++iclass){
1229 if(value==class_opt[iclass])
1230 windowClassValues[iclass]+=1;
1234 for(
int iband=0;iband<nband;++iband){
1235 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1237 assert(j-ulj<readValues[iband].size());
1239 assert(i-uli<((readValues[iband])[j-ulj]).size());
1240 double value=((readValues[iband])[j-ulj])[i-uli];
1242 if(verbose_opt[0]>1)
1243 std::cout <<
": " << value << std::endl;
1244 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
1245 windowValues[iband].push_back(value);
1249 if(verbose_opt[0]>1)
1250 std::cout <<
"set field " << fieldname_opt[iband] <<
" to " << value << std::endl;
1251 switch( fieldType ){
1254 writePointFeature->SetField(fieldname_opt[iband].c_str(),value);
1257 writePointFeature->SetField(fieldname_opt[iband].c_str(),type2string<double>(value).c_str());
1260 std::string errorString=
"field type not supported";
1265 catch(std::string e){
1266 std::cout << e << std::endl;
1272 if(!polygon_opt[0]){
1273 if(ruleMap[rule_opt[0]]==rule::point){
1275 if(verbose_opt[0]>1)
1276 std::cout <<
"creating point feature" << std::endl;
1278 if(writeTestLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1279 std::string errorString=
"Failed to create feature in test ogr vector dataset";
1284 if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1285 std::string errorString=
"Failed to create feature in ogr vector dataset";
1290 OGRFeature::DestroyFeature( writePointFeature );
1293 std::cout <<
"ntotalvalid(2): " << ntotalvalid << std::endl;
1298 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
1302 cout <<
"no points found in window, continuing" << endl;
1312 if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE)
1313 cerr <<
"writing feature failed" << std::endl;
1315 writePolygonFeature->SetGeometry(&writePolygon);
1316 assert(wkbFlatten(writePolygonFeature->GetGeometryRef()->getGeometryType()) == wkbPolygon);
1318 if(verbose_opt[0]>1)
1319 std::cout <<
"copying new fields write polygon " << std::endl;
1320 if(verbose_opt[0]>1)
1321 std::cout <<
"write feature has " << writePolygonFeature->GetFieldCount() <<
" fields" << std::endl;
1326 if(verbose_opt[0]>1)
1327 std::cout <<
"copying fields from polygons " << std::endl;
1332 if(writeCentroidFeature->SetFrom(readFeature)!= OGRERR_NONE)
1333 cerr <<
"writing feature failed" << std::endl;
1334 assert(wkbFlatten(writeCentroidFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
1339 if(verbose_opt[0]>1)
1340 std::cout <<
"write feature has " << writeCentroidFeature->GetFieldCount() <<
" fields" << std::endl;
1342 if(class_opt.empty()){
1343 if(ruleMap[rule_opt[0]]==rule::point){
1345 std::cout <<
"number of points in window: " << nPointWindow << std::endl;
1346 for(
int index=0;index<windowValues.size();++index){
1348 if(windowValues[index].size()!=1){
1349 cerr <<
"Error: windowValues[index].size()=" << windowValues[index].size() << endl;
1350 assert(windowValues[index].size()==1);
1352 double theValue=windowValues[index].back();
1355 std::cout <<
"number of points in window: " << nPointWindow << std::endl;
1356 int theBand=(band_opt.size()) ? band_opt[index] : index;
1359 if(verbose_opt[0]>1)
1360 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1361 switch( fieldType ){
1365 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1367 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1371 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1373 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1376 std::string errorString=
"field type not supported";
1381 catch(std::string e){
1382 std::cout << e << std::endl;
1389 for(
int index=0;index<windowValues.size();++index){
1391 if(ruleMap[rule_opt[0]]==rule::mean)
1392 theValue=stat.mean(windowValues[index]);
1393 else if(ruleMap[rule_opt[0]]==rule::stdev)
1394 theValue=sqrt(stat.var(windowValues[index]));
1395 else if(ruleMap[rule_opt[0]]==rule::median)
1396 theValue=stat.median(windowValues[index]);
1397 else if(ruleMap[rule_opt[0]]==rule::percentile)
1398 theValue=stat.percentile(windowValues[index],windowValues[index].begin(),windowValues[index].end(),percentile_opt[0]);
1399 else if(ruleMap[rule_opt[0]]==rule::sum)
1400 theValue=stat.sum(windowValues[index]);
1401 else if(ruleMap[rule_opt[0]]==rule::max)
1402 theValue=stat.mymax(windowValues[index]);
1403 else if(ruleMap[rule_opt[0]]==rule::min)
1404 theValue=stat.mymin(windowValues[index]);
1405 else if(ruleMap[rule_opt[0]]==rule::centroid){
1407 std::cout <<
"number of points in polygon: " << nPointWindow << std::endl;
1408 assert(nPointWindow<=1);
1409 assert(nPointWindow==windowValues[index].size());
1410 theValue=windowValues[index].back();
1413 std::string errorString=
"rule not supported";
1416 if(verbose_opt[0]>1)
1417 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1418 switch( fieldType ){
1422 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1424 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1428 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1430 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1433 std::string errorString=
"field type not supported";
1438 catch(std::string e){
1439 std::cout << e << std::endl;
1446 if(ruleMap[rule_opt[0]]==rule::proportion||ruleMap[rule_opt[0]]==rule::count){
1448 std::cout <<
"number of points in polygon: " << nPointWindow << std::endl;
1449 if(ruleMap[rule_opt[0]]==rule::proportion)
1450 stat.normalize_pct(windowClassValues);
1451 for(
int index=0;index<windowClassValues.size();++index){
1452 double theValue=windowClassValues[index];
1454 fs << class_opt[index];
1456 writePolygonFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1458 writeCentroidFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1461 else if(ruleMap[rule_opt[0]]==rule::custom){
1462 assert(polygon_opt[0]);
1464 std::cout <<
"number of points in polygon: " << nPointWindow << std::endl;
1465 stat.normalize_pct(windowClassValues);
1466 assert(windowClassValues.size()==2);
1467 if(windowClassValues[0]>=75)
1468 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(11));
1469 else if(windowClassValues[1]>=75)
1470 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(12));
1471 else if(windowClassValues[0]>25&&windowClassValues[1]>25)
1472 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(13));
1475 std::cout <<
"No valid value in windowClassValues..." << std::endl;
1476 for(
int index=0;index<windowClassValues.size();++index){
1477 double theValue=windowClassValues[index];
1478 std::cout << theValue <<
" ";
1480 std::cout << std::endl;
1482 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(20));
1485 else if(ruleMap[rule_opt[0]]==rule::mode){
1488 std::cout <<
"number of points in window: " << nPointWindow << std::endl;
1490 int maxClass=stat.mymin(class_opt);
1491 vector<double>::iterator maxit;
1492 maxit=stat.mymax(windowClassValues,windowClassValues.begin(),windowClassValues.end());
1493 int maxIndex=distance(windowClassValues.begin(),maxit);
1494 maxClass=class_opt[maxIndex];
1495 if(verbose_opt[0]>0)
1496 std::cout <<
"maxClass: " << maxClass << std::endl;
1498 writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
1500 writeCentroidFeature->SetField(label_opt[0].c_str(),maxClass);
1504 if(verbose_opt[0]>1)
1505 std::cout <<
"creating polygon feature" << std::endl;
1507 if(writeTestLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1508 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1513 if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1514 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1518 OGRFeature::DestroyFeature( writePolygonFeature );
1521 std::cout <<
"ntotalvalid(1): " << ntotalvalid << std::endl;
1524 if(verbose_opt[0]>1)
1525 std::cout <<
"creating point feature in centroid" << std::endl;
1527 if(writeTestLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1528 std::string errorString=
"Failed to create point feature in ogr vector dataset";
1534 assert(validFeature);
1535 if(writeLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1536 std::string errorString=
"Failed to create point feature in ogr vector dataset";
1540 OGRFeature::DestroyFeature( writeCentroidFeature );
1543 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
1547 else if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){
1549 OGRPolygon readPolygon = *((OGRPolygon *) poGeometry);
1550 OGRPolygon writePolygon;
1551 OGRLinearRing writeRing;
1552 OGRPoint writeCentroidPoint;
1553 OGRFeature *writePolygonFeature;
1554 OGRFeature *writeCentroidFeature;
1556 readPolygon.closeRings();
1558 if(verbose_opt[0]>1)
1559 std::cout <<
"get point on polygon" << std::endl;
1560 if(ruleMap[rule_opt[0]]==rule::centroid)
1561 readPolygon.Centroid(&writeCentroidPoint);
1562 else if(readPolygon.PointOnSurface(&writeCentroidPoint)!=OGRERR_NONE){
1564 readPolygon.Centroid(&writeCentroidPoint);
1566 double ulx,uly,lrx,lry;
1567 double uli,ulj,lri,lrj;
1568 if((polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point)||(ruleMap[rule_opt[0]]==rule::centroid)){
1569 ulx=writeCentroidPoint.getX();
1570 uly=writeCentroidPoint.getY();
1577 std::cout <<
"reading envelope for polygon " << ifeature << std::endl;
1578 OGREnvelope* psEnvelope=
new OGREnvelope();
1579 readPolygon.getEnvelope(psEnvelope);
1580 ulx=psEnvelope->MinX;
1581 uly=psEnvelope->MaxY;
1582 lrx=psEnvelope->MaxX;
1583 lry=psEnvelope->MinY;
1587 imgReader.geo2image(ulx,uly,uli,ulj);
1588 imgReader.geo2image(lrx,lry,lri,lrj);
1597 ulj=
static_cast<int>(ulj);
1598 uli=
static_cast<int>(uli);
1599 lrj=
static_cast<int>(lrj);
1600 lri=
static_cast<int>(lri);
1602 if(verbose_opt[0]>1)
1603 std::cout <<
"bounding box for polygon feature " << ifeature <<
": " << uli <<
" " << ulj <<
" " << lri <<
" " << lrj << std::endl;
1609 if(uli>=imgReader.nrOfCol())
1610 uli=imgReader.nrOfCol()-1;
1611 if(lri>=imgReader.nrOfCol())
1612 lri=imgReader.nrOfCol()-1;
1617 if(ulj>=imgReader.nrOfRow())
1618 ulj=imgReader.nrOfRow()-1;
1619 if(lrj>=imgReader.nrOfRow())
1620 lrj=imgReader.nrOfRow()-1;
1624 int nPointPolygon=0;
1628 writePolygonFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1630 writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1632 else if(ruleMap[rule_opt[0]]!=rule::point){
1634 writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1636 writeCentroidFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1640 vector<double> polyClassValues;
1642 if(class_opt.size()){
1644 polyClassValues.resize(class_opt.size());
1646 for(
int iclass=0;iclass<class_opt.size();++iclass)
1647 polyClassValues[iclass]=0;
1650 polyValues.resize(nband);
1651 vector< Vector2d<double> > readValues(nband);
1652 for(
int iband=0;iband<nband;++iband){
1653 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1654 imgReader.readDataBlock(readValues[iband],GDT_Float64,uli,lri,ulj,lrj,theBand);
1658 for(
int j=ulj;j<=lrj;++j){
1659 for(
int i=uli;i<=lri;++i){
1661 if(i<0||i>=imgReader.nrOfCol())
1663 if(j<0||j>=imgReader.nrOfRow())
1668 imgReader.image2geo(i,j,theX,theY);
1669 thePoint.setX(theX);
1670 thePoint.setY(theY);
1671 if(ruleMap[rule_opt[0]]!=rule::centroid&&!readPolygon.Contains(&thePoint))
1676 if(srcnodata_opt.size()){
1677 for(
int vband=0;vband<bndnodata_opt.size();++vband){
1678 double value=((readValues[bndnodata_opt[vband]])[j-ulj])[i-uli];
1679 if(value==srcnodata_opt[vband]){
1691 writeRing.addPoint(&thePoint);
1694 if(verbose_opt[0]>1)
1695 std::cout <<
"point is on surface:" << thePoint.getX() <<
"," << thePoint.getY() << std::endl;
1698 if(polythreshold_opt.size())
1699 if(nPointPolygon>polythreshold_opt[0])
1702 OGRFeature *writePointFeature;
1703 if(!polygon_opt[0]){
1705 if(ruleMap[rule_opt[0]]==rule::point){
1707 writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1709 writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1710 if(verbose_opt[0]>1)
1711 std::cout <<
"copying fields from polygons " << std::endl;
1712 if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE)
1713 cerr <<
"writing feature failed" << std::endl;
1714 if(verbose_opt[0]>1)
1715 std::cout <<
"set geometry as point " << std::endl;
1717 writePointFeature->SetGeometry(&thePoint);
1718 assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
1722 if(verbose_opt[0]>1)
1723 std::cout <<
"write feature has " << writePointFeature->GetFieldCount() <<
" fields" << std::endl;
1726 if(class_opt.size()){
1727 short value=((readValues[0])[j-ulj])[i-uli];
1728 for(
int iclass=0;iclass<class_opt.size();++iclass){
1729 if(value==class_opt[iclass])
1730 polyClassValues[iclass]+=1;
1734 for(
int iband=0;iband<nband;++iband){
1735 double value=((readValues[iband])[j-ulj])[i-uli];
1736 if(verbose_opt[0]>1)
1737 std::cout <<
": " << value << std::endl;
1738 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point)
1739 polyValues[iband].push_back(value);
1741 if(verbose_opt[0]>1)
1742 std::cout <<
"set field " << fieldname_opt[iband] <<
" to " << value << std::endl;
1743 switch( fieldType ){
1746 writePointFeature->SetField(fieldname_opt[iband].c_str(),value);
1749 writePointFeature->SetField(fieldname_opt[iband].c_str(),type2string<double>(value).c_str());
1758 if(!polygon_opt[0]){
1759 if(ruleMap[rule_opt[0]]==rule::point){
1761 if(verbose_opt[0]>1)
1762 std::cout <<
"creating point feature" << std::endl;
1764 if(writeTestLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1765 std::string errorString=
"Failed to create feature in test ogr vector dataset";
1770 if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1771 std::string errorString=
"Failed to create feature in ogr vector dataset";
1776 OGRFeature::DestroyFeature( writePointFeature );
1779 std::cout <<
"ntotalvalid(2): " << ntotalvalid << std::endl;
1784 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
1788 cout <<
"no points found in polygon, continuing" << endl;
1793 writePolygon.addRing(&writeRing);
1794 writePolygon.closeRings();
1799 if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE)
1800 cerr <<
"writing feature failed" << std::endl;
1801 if(verbose_opt[0]>1)
1802 std::cout <<
"copying new fields write polygon " << std::endl;
1803 if(verbose_opt[0]>1)
1804 std::cout <<
"write feature has " << writePolygonFeature->GetFieldCount() <<
" fields" << std::endl;
1809 if(verbose_opt[0]>1)
1810 std::cout <<
"copying fields from polygons " << std::endl;
1811 if(writeCentroidFeature->SetFrom(readFeature)!= OGRERR_NONE)
1812 cerr <<
"writing feature failed" << std::endl;
1813 writeCentroidFeature->SetGeometry(&writeCentroidPoint);
1814 assert(wkbFlatten(writeCentroidFeature->GetGeometryRef()->getGeometryType()) == wkbPoint );
1815 if(verbose_opt[0]>1)
1816 std::cout <<
"write feature has " << writeCentroidFeature->GetFieldCount() <<
" fields" << std::endl;
1818 if(class_opt.empty()){
1819 if(ruleMap[rule_opt[0]]==rule::point){
1821 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1822 for(
int index=0;index<polyValues.size();++index){
1823 assert(polyValues[index].size()==1);
1824 double theValue=polyValues[index].back();
1827 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1828 int theBand=(band_opt.size()) ? band_opt[index] : index;
1830 if(verbose_opt[0]>1)
1831 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1832 switch( fieldType ){
1836 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1838 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1842 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1844 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1847 std::string errorString=
"field type not supported";
1852 catch(std::string e){
1853 std::cout << e << std::endl;
1860 for(
int index=0;index<polyValues.size();++index){
1862 if(ruleMap[rule_opt[0]]==rule::mean)
1863 theValue=stat.mean(polyValues[index]);
1864 else if(ruleMap[rule_opt[0]]==rule::stdev)
1865 theValue=sqrt(stat.var(polyValues[index]));
1866 else if(ruleMap[rule_opt[0]]==rule::median)
1867 theValue=stat.median(polyValues[index]);
1868 else if(ruleMap[rule_opt[0]]==rule::percentile)
1869 theValue=stat.percentile(polyValues[index],polyValues[index].begin(),polyValues[index].end(),percentile_opt[0]);
1870 else if(ruleMap[rule_opt[0]]==rule::sum)
1871 theValue=stat.sum(polyValues[index]);
1872 else if(ruleMap[rule_opt[0]]==rule::max)
1873 theValue=stat.mymax(polyValues[index]);
1874 else if(ruleMap[rule_opt[0]]==rule::min)
1875 theValue=stat.mymin(polyValues[index]);
1876 else if(ruleMap[rule_opt[0]]==rule::centroid){
1878 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1879 assert(nPointPolygon<=1);
1880 assert(nPointPolygon==polyValues[index].size());
1881 theValue=polyValues[index].back();
1884 std::string errorString=
"rule not supported";
1887 if(verbose_opt[0]>1)
1888 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1889 switch( fieldType ){
1893 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1895 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1899 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1901 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1904 std::string errorString=
"field type not supported";
1909 catch(std::string e){
1910 std::cout << e << std::endl;
1917 if(ruleMap[rule_opt[0]]==rule::proportion||ruleMap[rule_opt[0]]==rule::count){
1919 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1920 if(ruleMap[rule_opt[0]]==rule::proportion)
1921 stat.normalize_pct(polyClassValues);
1922 for(
int index=0;index<polyClassValues.size();++index){
1923 double theValue=polyClassValues[index];
1925 fs << class_opt[index];
1927 writePolygonFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1929 writeCentroidFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1932 else if(ruleMap[rule_opt[0]]==rule::custom){
1933 assert(polygon_opt[0]);
1935 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1936 stat.normalize_pct(polyClassValues);
1937 assert(polyClassValues.size()==2);
1938 if(polyClassValues[0]>=75)
1939 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(11));
1940 else if(polyClassValues[1]>=75)
1941 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(12));
1942 else if(polyClassValues[0]>25&&polyClassValues[1]>25)
1943 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(13));
1946 std::cout <<
"No valid value in polyClassValues..." << std::endl;
1947 for(
int index=0;index<polyClassValues.size();++index){
1948 double theValue=polyClassValues[index];
1949 std::cout << theValue <<
" ";
1951 std::cout << std::endl;
1953 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(20));
1956 else if(ruleMap[rule_opt[0]]==rule::mode){
1959 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1961 int maxClass=stat.mymin(class_opt);
1962 vector<double>::iterator maxit;
1963 maxit=stat.mymax(polyClassValues,polyClassValues.begin(),polyClassValues.end());
1964 int maxIndex=distance(polyClassValues.begin(),maxit);
1965 maxClass=class_opt[maxIndex];
1966 if(verbose_opt[0]>0)
1967 std::cout <<
"maxClass: " << maxClass << std::endl;
1969 writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
1971 writeCentroidFeature->SetField(label_opt[0].c_str(),maxClass);
1975 if(verbose_opt[0]>1)
1976 std::cout <<
"creating polygon feature" << std::endl;
1978 if(writeTestLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1979 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1984 if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1985 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1989 OGRFeature::DestroyFeature( writePolygonFeature );
1992 std::cout <<
"ntotalvalid(1): " << ntotalvalid << std::endl;
1995 if(verbose_opt[0]>1)
1996 std::cout <<
"creating point feature in centroid" << std::endl;
1998 if(writeTestLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1999 std::string errorString=
"Failed to create point feature in ogr vector dataset";
2005 assert(validFeature);
2006 if(writeLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
2007 std::string errorString=
"Failed to create point feature in ogr vector dataset";
2011 OGRFeature::DestroyFeature( writeCentroidFeature );
2014 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
2018 else if(wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon){
2019 OGRMultiPolygon readPolygon = *((OGRMultiPolygon *) poGeometry);
2020 OGRPolygon writePolygon;
2021 OGRLinearRing writeRing;
2022 OGRPoint writeCentroidPoint;
2023 OGRFeature *writePolygonFeature;
2024 OGRFeature *writeCentroidFeature;
2026 readPolygon.closeRings();
2028 if(verbose_opt[0]>1)
2029 std::cout <<
"get centroid point from polygon" << std::endl;
2030 readPolygon.Centroid(&writeCentroidPoint);
2032 double ulx,uly,lrx,lry;
2033 double uli,ulj,lri,lrj;
2034 if((polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point)||(ruleMap[rule_opt[0]]==rule::centroid)){
2035 ulx=writeCentroidPoint.getX();
2036 uly=writeCentroidPoint.getY();
2043 std::cout <<
"reading envelope for polygon " << ifeature << std::endl;
2044 OGREnvelope* psEnvelope=
new OGREnvelope();
2045 readPolygon.getEnvelope(psEnvelope);
2046 ulx=psEnvelope->MinX;
2047 uly=psEnvelope->MaxY;
2048 lrx=psEnvelope->MaxX;
2049 lry=psEnvelope->MinY;
2053 imgReader.geo2image(ulx,uly,uli,ulj);
2054 imgReader.geo2image(lrx,lry,lri,lrj);
2063 ulj=
static_cast<int>(ulj);
2064 uli=
static_cast<int>(uli);
2065 lrj=
static_cast<int>(lrj);
2066 lri=
static_cast<int>(lri);
2068 if(verbose_opt[0]>1)
2069 std::cout <<
"bounding box for multipologon feature " << ifeature <<
": " << uli <<
" " << ulj <<
" " << lri <<
" " << lrj << std::endl;
2075 if(uli>=imgReader.nrOfCol())
2076 uli=imgReader.nrOfCol()-1;
2077 if(lri>=imgReader.nrOfCol())
2078 lri=imgReader.nrOfCol()-1;
2083 if(ulj>=imgReader.nrOfRow())
2084 ulj=imgReader.nrOfRow()-1;
2085 if(lrj>=imgReader.nrOfRow())
2086 lrj=imgReader.nrOfRow()-1;
2090 int nPointPolygon=0;
2093 writePolygonFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
2095 writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
2097 else if(ruleMap[rule_opt[0]]!=rule::point){
2099 writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
2101 writeCentroidFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
2105 vector<double> polyClassValues;
2107 if(class_opt.size()){
2108 polyClassValues.resize(class_opt.size());
2110 for(
int iclass=0;iclass<class_opt.size();++iclass)
2111 polyClassValues[iclass]=0;
2114 polyValues.resize(nband);
2115 vector< Vector2d<double> > readValues(nband);
2116 for(
int iband=0;iband<nband;++iband){
2117 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
2118 imgReader.readDataBlock(readValues[iband],GDT_Float64,uli,lri,ulj,lrj,theBand);
2122 for(
int j=ulj;j<=lrj;++j){
2123 for(
int i=uli;i<=lri;++i){
2125 if(i<0||i>=imgReader.nrOfCol())
2127 if(j<0||j>=imgReader.nrOfRow())
2132 imgReader.image2geo(i,j,theX,theY);
2133 thePoint.setX(theX);
2134 thePoint.setY(theY);
2136 if(ruleMap[rule_opt[0]]!=rule::centroid&&!readPolygon.Contains(&thePoint))
2141 if(srcnodata_opt.size()){
2142 for(
int vband=0;vband<bndnodata_opt.size();++vband){
2143 double value=((readValues[bndnodata_opt[vband]])[j-ulj])[i-uli];
2144 if(value==srcnodata_opt[vband]){
2156 writeRing.addPoint(&thePoint);
2159 if(verbose_opt[0]>1)
2160 std::cout <<
"point is on surface:" << thePoint.getX() <<
"," << thePoint.getY() << std::endl;
2163 if(polythreshold_opt.size())
2164 if(nPointPolygon>polythreshold_opt[0])
2167 OGRFeature *writePointFeature;
2168 if(!polygon_opt[0]){
2170 if(ruleMap[rule_opt[0]]==rule::point){
2172 writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
2174 writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
2175 if(verbose_opt[0]>1)
2176 std::cout <<
"copying fields from polygons " << std::endl;
2177 if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE)
2178 cerr <<
"writing feature failed" << std::endl;
2179 if(verbose_opt[0]>1)
2180 std::cout <<
"set geometry as point " << std::endl;
2182 writePointFeature->SetGeometry(&thePoint);
2183 assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
2187 if(verbose_opt[0]>1)
2188 std::cout <<
"write feature has " << writePointFeature->GetFieldCount() <<
" fields" << std::endl;
2191 if(class_opt.size()){
2192 short value=((readValues[0])[j-ulj])[i-uli];
2193 for(
int iclass=0;iclass<class_opt.size();++iclass){
2194 if(value==class_opt[iclass])
2195 polyClassValues[iclass]+=1;
2199 for(
int iband=0;iband<nband;++iband){
2200 double value=((readValues[iband])[j-ulj])[i-uli];
2201 if(verbose_opt[0]>1)
2202 std::cout <<
": " << value << std::endl;
2203 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point)
2204 polyValues[iband].push_back(value);
2206 if(verbose_opt[0]>1)
2207 std::cout <<
"set field " << fieldname_opt[iband] <<
" to " << value << std::endl;
2208 switch( fieldType ){
2211 writePointFeature->SetField(fieldname_opt[iband].c_str(),value);
2214 writePointFeature->SetField(fieldname_opt[iband].c_str(),type2string<double>(value).c_str());
2223 if(!polygon_opt[0]){
2224 if(ruleMap[rule_opt[0]]==rule::point){
2226 if(verbose_opt[0]>1)
2227 std::cout <<
"creating point feature" << std::endl;
2229 if(writeTestLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
2230 std::string errorString=
"Failed to create feature in ogr vector dataset";
2235 if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
2236 std::string errorString=
"Failed to create feature in ogr vector dataset";
2241 OGRFeature::DestroyFeature( writePointFeature );
2247 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
2252 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
2258 writePolygon.addRing(&writeRing);
2259 writePolygon.closeRings();
2264 if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE)
2265 cerr <<
"writing feature failed" << std::endl;
2266 assert(writePolygonFeature->GetGeometryRef()->getGeometryType()==wkbMultiPolygon);
2267 if(verbose_opt[0]>1)
2268 std::cout <<
"copying new fields write polygon " << std::endl;
2269 if(verbose_opt[0]>1)
2270 std::cout <<
"write feature has " << writePolygonFeature->GetFieldCount() <<
" fields" << std::endl;
2275 if(verbose_opt[0]>1)
2276 std::cout <<
"copying fields from polygons " << std::endl;
2277 if(writeCentroidFeature->SetFrom(readFeature)!= OGRERR_NONE)
2278 cerr <<
"writing feature failed" << std::endl;
2279 writeCentroidFeature->SetGeometry(&writeCentroidPoint);
2280 assert(wkbFlatten(writeCentroidFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
2281 if(verbose_opt[0]>1)
2282 std::cout <<
"write feature has " << writeCentroidFeature->GetFieldCount() <<
" fields" << std::endl;
2284 if(class_opt.empty()){
2285 if(ruleMap[rule_opt[0]]==rule::point){
2287 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2288 for(
int index=0;index<polyValues.size();++index){
2290 assert(polyValues[index].size()==1);
2291 double theValue=polyValues[index].back();
2293 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2294 int theBand=(band_opt.size()) ? band_opt[index] : index;
2296 if(verbose_opt[0]>1)
2297 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
2298 switch( fieldType ){
2302 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
2304 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
2308 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2310 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2313 std::string errorString=
"field type not supported";
2318 catch(std::string e){
2319 std::cout << e << std::endl;
2326 for(
int index=0;index<polyValues.size();++index){
2328 if(ruleMap[rule_opt[0]]==rule::mean)
2329 theValue=stat.mean(polyValues[index]);
2330 else if(ruleMap[rule_opt[0]]==rule::stdev)
2331 theValue=sqrt(stat.var(polyValues[index]));
2332 else if(ruleMap[rule_opt[0]]==rule::median)
2333 theValue=stat.median(polyValues[index]);
2334 else if(ruleMap[rule_opt[0]]==rule::percentile)
2335 theValue=stat.percentile(polyValues[index],polyValues[index].begin(),polyValues[index].end(),percentile_opt[0]);
2336 else if(ruleMap[rule_opt[0]]==rule::sum)
2337 theValue=stat.sum(polyValues[index]);
2338 else if(ruleMap[rule_opt[0]]==rule::max)
2339 theValue=stat.mymax(polyValues[index]);
2340 else if(ruleMap[rule_opt[0]]==rule::min)
2341 theValue=stat.mymin(polyValues[index]);
2342 else if(ruleMap[rule_opt[0]]==rule::centroid){
2344 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2345 assert(nPointPolygon<=1);
2346 assert(nPointPolygon==polyValues[index].size());
2347 theValue=polyValues[index].back();
2350 std::string errorString=
"rule not supported";
2353 if(verbose_opt[0]>1)
2354 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
2355 switch( fieldType ){
2359 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
2361 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
2365 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2367 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2370 std::string errorString=
"field type not supported";
2375 catch(std::string e){
2376 std::cout << e << std::endl;
2383 if(ruleMap[rule_opt[0]]==rule::proportion||ruleMap[rule_opt[0]]==rule::count){
2385 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2386 if(ruleMap[rule_opt[0]]==rule::proportion)
2387 stat.normalize_pct(polyClassValues);
2388 for(
int index=0;index<polyClassValues.size();++index){
2389 double theValue=polyClassValues[index];
2391 fs << class_opt[index];
2393 writePolygonFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
2395 writeCentroidFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
2398 else if(ruleMap[rule_opt[0]]==rule::custom){
2399 assert(polygon_opt[0]);
2401 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2402 stat.normalize_pct(polyClassValues);
2403 assert(polyClassValues.size()==2);
2404 if(polyClassValues[0]>=75)
2405 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(11));
2406 else if(polyClassValues[1]>=75)
2407 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(12));
2408 else if(polyClassValues[0]>25&&polyClassValues[1]>25)
2409 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(13));
2412 std::cout <<
"No valid value in polyClassValues..." << std::endl;
2413 for(
int index=0;index<polyClassValues.size();++index){
2414 double theValue=polyClassValues[index];
2415 std::cout << theValue <<
" ";
2417 std::cout << std::endl;
2419 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(20));
2422 else if(ruleMap[rule_opt[0]]==rule::mode){
2425 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2427 int maxClass=stat.mymin(class_opt);
2428 vector<double>::iterator maxit;
2429 maxit=stat.mymax(polyClassValues,polyClassValues.begin(),polyClassValues.end());
2430 int maxIndex=distance(polyClassValues.begin(),maxit);
2431 maxClass=class_opt[maxIndex];
2432 if(verbose_opt[0]>0)
2433 std::cout <<
"maxClass: " << maxClass << std::endl;
2435 writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
2437 writeCentroidFeature->SetField(label_opt[0].c_str(),maxClass);
2442 if(verbose_opt[0]>1)
2443 std::cout <<
"creating polygon feature" << std::endl;
2445 if(writeTestLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
2446 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
2451 if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
2452 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
2456 OGRFeature::DestroyFeature( writePolygonFeature );
2459 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
2462 if(verbose_opt[0]>1)
2463 std::cout <<
"creating point feature in centroid" << std::endl;
2465 if(writeTestLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
2466 std::string errorString=
"Failed to create point feature in ogr vector dataset";
2472 assert(validFeature);
2473 if(writeLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
2474 std::string errorString=
"Failed to create point feature in ogr vector dataset";
2478 OGRFeature::DestroyFeature( writeCentroidFeature );
2481 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
2487 test=poGeometry->getGeometryName();
2489 oss <<
"geometry " << test <<
" not supported";
2493 progress=
static_cast<float>(ifeature+1)/nfeature;
2494 pfnProgress(progress,pszMessage,pProgressArg);
2496 catch(std::string e){
2497 std::cout << e << std::endl;
2502 std::cout <<
"number of points read in polygon: " << npoint << std::endl;
2509 pfnProgress(progress,pszMessage,pProgressArg);
2512 sampleReaderOgr.close();
2515 ogrTestWriter.close();
2518 pfnProgress(progress,pszMessage,pProgressArg);