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
120 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};
125 int main(
int argc,
char *argv[])
127 Optionpk<string> image_opt(
"i",
"input",
"Raster input dataset containing band information");
128 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.");
129 Optionpk<string> layer_opt(
"ln",
"ln",
"Layer name(s) in sample (leave empty to select all)");
130 Optionpk<unsigned int> random_opt(
"rand",
"random",
"Create simple random sample of points. Provide number of points to generate");
131 Optionpk<double> grid_opt(
"grid",
"grid",
"Create systematic grid of points. Provide cell grid size (in projected units, e.g,. m)");
133 Optionpk<int> class_opt(
"c",
"class",
"Class(es) to extract from input sample image. Leave empty to extract all valid data pixels from sample dataset. Make sure to set classes if rule is set to mode or proportion");
134 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);
135 Optionpk<double> percentile_opt(
"perc",
"perc",
"Percentile value used for rule percentile",95);
136 Optionpk<string> ogrformat_opt(
"f",
"f",
"Output sample dataset format",
"SQLite");
137 Optionpk<string> ftype_opt(
"ft",
"ftype",
"Field type (only Real or Integer)",
"Real");
138 Optionpk<string> ltype_opt(
"lt",
"ltype",
"Label type: In16 or String",
"Integer");
139 Optionpk<bool> polygon_opt(
"polygon",
"polygon",
"Create OGRPolygon as geometry instead of OGRPoint.",
false);
140 Optionpk<int> band_opt(
"b",
"band",
"Band index(es) to extract (0 based). Leave empty to use all bands");
141 Optionpk<string> rule_opt(
"r",
"rule",
"Rule how to report image information per feature (only for vector sample). point (value at each point or at centroid if polygon), centroid, mean, stdev, median, proportion, min, max, mode, sum, percentile.",
"centroid");
142 Optionpk<double> srcnodata_opt(
"srcnodata",
"srcnodata",
"Invalid value(s) for input image");
143 Optionpk<int> bndnodata_opt(
"bndnodata",
"bndnodata",
"Band(s) in input image to check if pixel is valid (used for srcnodata)", 0);
144 Optionpk<float> polythreshold_opt(
"tp",
"thresholdPolygon",
"(absolute) threshold for selecting samples in each polygon");
145 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");
146 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");
147 Optionpk<string> label_opt(
"cn",
"cname",
"Name of the class label in the output vector dataset",
"label");
148 Optionpk<short> geo_opt(
"geo",
"geo",
"Use geo coordinates (set to 0 to use image coordinates)", 1);
149 Optionpk<short> down_opt(
"down",
"down",
"Down sampling factor (for raster sample datasets only). Can be used to create grid points", 1);
150 Optionpk<short> buffer_opt(
"buf",
"buffer",
"Buffer for calculating statistics for point features ");
151 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);
152 Optionpk<short> verbose_opt(
"v",
"verbose",
"Verbose mode if > 0", 0,2);
154 bndnodata_opt.setHide(1);
155 srcnodata_opt.setHide(1);
156 polythreshold_opt.setHide(1);
157 percentile_opt.setHide(1);
159 fieldname_opt.setHide(1);
160 label_opt.setHide(1);
163 buffer_opt.setHide(1);
168 doProcess=image_opt.retrieveOption(argc,argv);
169 sample_opt.retrieveOption(argc,argv);
170 layer_opt.retrieveOption(argc,argv);
171 random_opt.retrieveOption(argc,argv);
172 grid_opt.retrieveOption(argc,argv);
173 output_opt.retrieveOption(argc,argv);
174 class_opt.retrieveOption(argc,argv);
175 threshold_opt.retrieveOption(argc,argv);
176 percentile_opt.retrieveOption(argc,argv);
177 ogrformat_opt.retrieveOption(argc,argv);
178 ftype_opt.retrieveOption(argc,argv);
179 ltype_opt.retrieveOption(argc,argv);
180 polygon_opt.retrieveOption(argc,argv);
181 band_opt.retrieveOption(argc,argv);
182 rule_opt.retrieveOption(argc,argv);
183 bndnodata_opt.retrieveOption(argc,argv);
184 srcnodata_opt.retrieveOption(argc,argv);
185 polythreshold_opt.retrieveOption(argc,argv);
186 test_opt.retrieveOption(argc,argv);
187 fieldname_opt.retrieveOption(argc,argv);
188 label_opt.retrieveOption(argc,argv);
189 geo_opt.retrieveOption(argc,argv);
190 down_opt.retrieveOption(argc,argv);
191 buffer_opt.retrieveOption(argc,argv);
192 disc_opt.retrieveOption(argc,argv);
195 verbose_opt.retrieveOption(argc,argv);
197 catch(
string predefinedString){
198 std::cout << predefinedString << std::endl;
203 cout <<
"Usage: pkextract -i input [-s sample | -rand number | -grid size] -o output" << endl;
205 std::cout <<
"short option -h shows basic options only, use long option --help to show all options" << std::endl;
209 std::map<std::string, rule::RULE_TYPE> ruleMap;
211 ruleMap[
"point"]=rule::point;
212 ruleMap[
"centroid"]=rule::centroid;
213 ruleMap[
"mean"]=rule::mean;
214 ruleMap[
"stdev"]=rule::stdev;
215 ruleMap[
"median"]=rule::median;
216 ruleMap[
"proportion"]=rule::proportion;
217 ruleMap[
"min"]=rule::min;
218 ruleMap[
"max"]=rule::max;
219 ruleMap[
"custom"]=rule::custom;
220 ruleMap[
"mode"]=rule::mode;
221 ruleMap[
"sum"]=rule::sum;
222 ruleMap[
"percentile"]=rule::percentile;
224 if(srcnodata_opt.size()){
225 while(srcnodata_opt.size()<bndnodata_opt.size())
226 srcnodata_opt.push_back(srcnodata_opt[0]);
227 while(bndnodata_opt.size()<srcnodata_opt.size())
228 bndnodata_opt.push_back(bndnodata_opt[0]);
232 std::cout << class_opt << std::endl;
234 stat.setNoDataValues(srcnodata_opt);
236 unsigned long int nsample=0;
237 unsigned long int ntotalvalid=0;
238 unsigned long int ntotalinvalid=0;
239 vector<unsigned long int> nvalid(class_opt.size());
240 vector<unsigned long int> ninvalid(class_opt.size());
241 for(
int it=0;it<nvalid.size();++it){
247 if(image_opt.empty()){
248 std::cerr <<
"No image dataset provided (use option -i). Use --help for help information";
251 if(output_opt.empty()){
252 std::cerr <<
"No output dataset provided (use option -o). Use --help for help information";
256 imgReader.open(image_opt[0]);
258 catch(std::string errorstring){
259 std::cout << errorstring << std::endl;
263 int nband=(band_opt.size()) ? band_opt.size() : imgReader.nrOfBand();
265 if(fieldname_opt.size()<nband){
266 std::string bandString=fieldname_opt[0];
267 fieldname_opt.clear();
268 fieldname_opt.resize(nband);
269 for(
int iband=0;iband<nband;++iband){
270 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
272 fs << bandString << theBand;
273 fieldname_opt[iband]=fs.str();
278 std::cout << fieldname_opt << std::endl;
281 std::cout <<
"Number of bands in input image: " << imgReader.nrOfBand() << std::endl;
283 OGRFieldType fieldType;
284 OGRFieldType labelType;
285 int ogr_typecount=11;
287 std::cout <<
"field and label types can be: ";
288 for(
int iType = 0; iType < ogr_typecount; ++iType){
290 std::cout <<
" " << OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType);
291 if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL
292 && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType),
293 ftype_opt[0].c_str()))
294 fieldType=(OGRFieldType) iType;
295 if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL
296 && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType),
297 ltype_opt[0].c_str()))
298 labelType=(OGRFieldType) iType;
306 std::cout << std::endl <<
"field type is: " << OGRFieldDefn::GetFieldTypeName(fieldType) << std::endl;
309 cerr <<
"field type " << OGRFieldDefn::GetFieldTypeName(fieldType) <<
" not supported" << std::endl;
319 std::cout << std::endl <<
"label type is: " << OGRFieldDefn::GetFieldTypeName(labelType) << std::endl;
322 cerr <<
"label type " << OGRFieldDefn::GetFieldTypeName(labelType) <<
" not supported" << std::endl;
327 const char* pszMessage;
328 void* pProgressArg=NULL;
329 GDALProgressFunc pfnProgress=GDALTermProgress;
333 bool sampleIsRaster=
false;
334 bool sampleIsVirtual=
false;
339 if(sample_opt.size()){
341 sampleReaderOgr.open(sample_opt[0]);
343 catch(
string errorString){
349 sampleWriterOgr.open(
"/vsimem/virtual",ogrformat_opt[0]);
351 catch(
string errorString){
352 cerr << errorString << endl;
354 char **papszOptions=NULL;
355 sampleWriterOgr.createLayer(
"points", imgReader.getProjection(), wkbPoint, papszOptions);
356 sampleIsVirtual=
true;
361 if(random_opt.size()){
364 double ulx,uly,lrx,lry;
365 imgReader.getBoundingBox(ulx,uly,lrx,lry);
366 for(
unsigned int ipoint=1;ipoint<=random_opt[0];++ipoint){
367 OGRFeature *pointFeature;
368 pointFeature=sampleWriterOgr.createFeature();
370 double theX=ulx+
static_cast<double>(rand())/(RAND_MAX)*(lrx-ulx);
371 double theY=uly-
static_cast<double>(rand())/(RAND_MAX)*(uly-lry);
374 pointFeature->SetGeometry( &pt );
375 if(sampleWriterOgr.createFeature(pointFeature) != OGRERR_NONE ){
376 cerr <<
"Failed to create feature in shapefile" << endl;
379 OGRFeature::DestroyFeature(pointFeature);
382 else if(grid_opt.size()){
385 double ulx,uly,lrx,lry;
386 imgReader.getBoundingBox(ulx,uly,lrx,lry);
387 unsigned int ipoint=0;
388 for(
double theY=uly-grid_opt[0]/2;theY>lry;theY-=grid_opt[0]){
389 for(
double theX=ulx+grid_opt[0]/2;theX<lrx;theX+=grid_opt[0]){
391 cout <<
"position: " << theX <<
" " << theY << endl;
392 OGRFeature *pointFeature;
393 pointFeature=sampleWriterOgr.createFeature();
397 pointFeature->SetGeometry( &pt );
398 if(sampleWriterOgr.createFeature(pointFeature) != OGRERR_NONE ){
399 cerr <<
"Failed to create feature in shapefile" << endl;
402 OGRFeature::DestroyFeature(pointFeature);
407 std::cerr <<
"No sample dataset provided (use option -s). Use --help for help information";
411 sampleWriterOgr.close();
412 sampleReaderOgr.open(
"/vsimem/virtual");
414 catch(
string errorString){
415 cerr << errorString << endl;
420 if(class_opt.empty()){
424 assert(sample_opt.size());
425 classReader.open(sample_opt[0]);
427 vector<double> classBuffer(classReader.nrOfCol());
429 vector<double> sample(2+nband);
432 vector<double> writeBufferClass;
433 vector<int> selectedClass;
439 std::cout <<
"extracting sample from image..." << std::endl;
441 pfnProgress(progress,pszMessage,pProgressArg);
442 for(irow=0;irow<classReader.nrOfRow();++irow){
446 classReader.readData(classBuffer,GDT_Float64,irow);
449 for(icol=0;icol<classReader.nrOfCol();++icol){
453 double theClass=classBuffer[icol];
474 classReader.image2geo(icol,irow,x,y);
477 if(verbose_opt[0]>1){
478 std::cout.precision(12);
479 std::cout << theClass <<
" " << x <<
" " << y << std::endl;
482 imgReader.geo2image(x,y,iimg,jimg);
484 jimg=
static_cast<int>(jimg);
485 iimg=
static_cast<int>(iimg);
486 if(static_cast<int>(iimg)<0||
static_cast<int>(iimg)>=imgReader.nrOfCol())
495 if(static_cast<int>(jimg)<0||
static_cast<int>(jimg)>=imgReader.nrOfRow())
500 if(static_cast<int>(jimg)!=
static_cast<int>(oldimgrow)){
501 assert(imgBuffer.size()==nband);
502 for(
int iband=0;iband<nband;++iband){
503 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
504 imgReader.readData(imgBuffer[iband],GDT_Float64,static_cast<int>(jimg),theBand);
505 assert(imgBuffer[iband].size()==imgReader.nrOfCol());
506 if(srcnodata_opt.size()){
507 vector<int>::const_iterator bndit=bndnodata_opt.begin();
508 vector<double>::const_iterator srcit=srcnodata_opt.begin();
509 while(bndit!=bndnodata_opt.end()&&srcit!=srcnodata_opt.end()){
510 if((*bndit==theBand)&&(*srcit==imgBuffer[iband][
static_cast<int>(iimg)])){
525 for(
int iband=0;iband<imgBuffer.size();++iband){
526 if(imgBuffer[iband].size()!=imgReader.nrOfCol()){
527 std::cout <<
"Error in band " << iband <<
": " << imgBuffer[iband].size() <<
"!=" << imgReader.nrOfCol() << std::endl;
528 assert(imgBuffer[iband].size()==imgReader.nrOfCol());
530 sample[iband+2]=imgBuffer[iband][
static_cast<int>(iimg)];
532 float theThreshold=(threshold_opt.size()>1)?threshold_opt[processClass]:threshold_opt[0];
534 double p=
static_cast<double>(rand())/(RAND_MAX);
539 else if(nvalid.size()>processClass){
540 if(nvalid[processClass]>=-theThreshold)
543 writeBuffer.push_back(sample);
544 writeBufferClass.push_back(theClass);
546 if(nvalid.size()>processClass)
547 ++(nvalid[processClass]);
551 if(ninvalid.size()>processClass)
552 ++(ninvalid[processClass]);
556 progress=
static_cast<float>(irow+1.0)/classReader.nrOfRow();
557 pfnProgress(progress,pszMessage,pProgressArg);
560 pfnProgress(progress,pszMessage,pProgressArg);
561 if(writeBuffer.size()>0){
562 assert(ntotalvalid==writeBuffer.size());
564 std::cout <<
"creating image sample writer " << output_opt[0] <<
" with " << writeBuffer.size() <<
" samples (" << ntotalinvalid <<
" invalid)" << std::endl;
565 ogrWriter.open(output_opt[0],ogrformat_opt[0]);
566 char **papszOptions=NULL;
567 ostringstream slayer;
568 slayer <<
"training data";
569 std::string layername=slayer.str();
570 ogrWriter.createLayer(layername, imgReader.getProjection(), wkbPoint, papszOptions);
571 std::string fieldname=
"fid";
572 ogrWriter.createField(fieldname,OFTInteger);
573 map<std::string,double> pointAttributes;
574 ogrWriter.createField(label_opt[0],labelType);
575 for(
int iband=0;iband<nband;++iband){
576 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
577 ogrWriter.createField(fieldname_opt[iband],fieldType);
579 std::cout <<
"writing sample to " << output_opt[0] <<
"..." << std::endl;
581 pfnProgress(progress,pszMessage,pProgressArg);
582 for(
int isample=0;isample<writeBuffer.size();++isample){
584 std::cout <<
"writing sample " << isample << std::endl;
585 pointAttributes[label_opt[0]]=writeBufferClass[isample];
586 for(
int iband=0;iband<writeBuffer[0].size()-2;++iband){
587 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
594 pointAttributes[fieldname_opt[iband]]=writeBuffer[isample][iband+2];
597 std::cout <<
"all bands written" << std::endl;
598 ogrWriter.addPoint(writeBuffer[isample][0],writeBuffer[isample][1],pointAttributes,fieldname,isample);
599 progress=
static_cast<float>(isample+1.0)/writeBuffer.size();
600 pfnProgress(progress,pszMessage,pProgressArg);
605 std::cout <<
"No data found for any class " << std::endl;
608 nsample=writeBuffer.size();
610 std::cout <<
"total number of samples written: " << nsample << std::endl;
613 assert(class_opt[0]);
615 assert(threshold_opt.size()==1||threshold_opt.size()==class_opt.size());
618 if(verbose_opt[0]>1){
619 std::cout <<
"reading position from sample dataset " << std::endl;
620 std::cout <<
"class thresholds: " << std::endl;
621 for(
int iclass=0;iclass<class_opt.size();++iclass){
622 if(threshold_opt.size()>1)
623 std::cout << class_opt[iclass] <<
": " << threshold_opt[iclass] << std::endl;
625 std::cout << class_opt[iclass] <<
": " << threshold_opt[0] << std::endl;
628 classReader.open(sample_opt[0]);
629 vector<int> classBuffer(classReader.nrOfCol());
632 vector<double> sample(2+nband);
634 vector<int> writeBufferClass;
636 vector<int> selectedClass;
642 std::cout <<
"extracting sample from image..." << std::endl;
644 pfnProgress(progress,pszMessage,pProgressArg);
645 for(irow=0;irow<classReader.nrOfRow();++irow){
648 classReader.readData(classBuffer,GDT_Int32,irow);
652 for(icol=0;icol<classReader.nrOfCol();++icol){
658 if(class_opt.empty()){
659 if(classBuffer[icol]){
661 theClass=classBuffer[icol];
665 for(
int iclass=0;iclass<class_opt.size();++iclass){
666 if(classBuffer[icol]==class_opt[iclass]){
668 theClass=class_opt[iclass];
675 classReader.image2geo(icol,irow,x,y);
678 if(verbose_opt[0]>1){
679 std::cout.precision(12);
680 std::cout << theClass <<
" " << x <<
" " << y << std::endl;
683 imgReader.geo2image(x,y,iimg,jimg);
685 jimg=
static_cast<int>(jimg);
686 iimg=
static_cast<int>(iimg);
687 if(static_cast<int>(iimg)<0||
static_cast<int>(iimg)>=imgReader.nrOfCol())
696 if(static_cast<int>(jimg)<0||
static_cast<int>(jimg)>=imgReader.nrOfRow())
701 if(static_cast<int>(jimg)!=
static_cast<int>(oldimgrow)){
702 assert(imgBuffer.size()==nband);
703 for(
int iband=0;iband<nband;++iband){
704 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
705 imgReader.readData(imgBuffer[iband],GDT_Float64,static_cast<int>(jimg),theBand);
706 assert(imgBuffer[iband].size()==imgReader.nrOfCol());
708 if(srcnodata_opt.size()){
709 vector<int>::const_iterator bndit=bndnodata_opt.begin();
710 vector<double>::const_iterator srcit=srcnodata_opt.begin();
711 while(bndit!=bndnodata_opt.end()&&srcit!=srcnodata_opt.end()){
712 if((*bndit==theBand)&&(*srcit==imgBuffer[iband][
static_cast<int>(iimg)])){
726 for(
int iband=0;iband<imgBuffer.size();++iband){
727 if(imgBuffer[iband].size()!=imgReader.nrOfCol()){
728 std::cout <<
"Error in band " << iband <<
": " << imgBuffer[iband].size() <<
"!=" << imgReader.nrOfCol() << std::endl;
729 assert(imgBuffer[iband].size()==imgReader.nrOfCol());
731 sample[iband+2]=imgBuffer[iband][
static_cast<int>(iimg)];
733 float theThreshold=(threshold_opt.size()>1)?threshold_opt[processClass]:threshold_opt[0];
735 double p=
static_cast<double>(rand())/(RAND_MAX);
740 else if(nvalid.size()>processClass){
741 if(nvalid[processClass]>=-theThreshold)
744 writeBuffer.push_back(sample);
746 writeBufferClass.push_back(theClass);
748 if(nvalid.size()>processClass)
749 ++(nvalid[processClass]);
753 if(ninvalid.size()>processClass)
754 ++(ninvalid[processClass]);
758 progress=
static_cast<float>(irow+1.0)/classReader.nrOfRow();
759 pfnProgress(progress,pszMessage,pProgressArg);
761 if(writeBuffer.size()>0){
762 assert(ntotalvalid==writeBuffer.size());
764 std::cout <<
"creating image sample writer " << output_opt[0] <<
" with " << writeBuffer.size() <<
" samples (" << ntotalinvalid <<
" invalid)" << std::endl;
765 ogrWriter.open(output_opt[0],ogrformat_opt[0]);
766 char **papszOptions=NULL;
767 ostringstream slayer;
768 slayer <<
"training data";
769 std::string layername=slayer.str();
770 ogrWriter.createLayer(layername, imgReader.getProjection(), wkbPoint, papszOptions);
771 std::string fieldname=
"fid";
772 ogrWriter.createField(fieldname,OFTInteger);
773 map<std::string,double> pointAttributes;
775 ogrWriter.createField(label_opt[0],labelType);
776 for(
int iband=0;iband<nband;++iband){
777 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
784 ogrWriter.createField(fieldname_opt[iband],fieldType);
786 pfnProgress(progress,pszMessage,pProgressArg);
787 std::cout <<
"writing sample to " << output_opt[0] <<
"..." << std::endl;
789 pfnProgress(progress,pszMessage,pProgressArg);
790 for(
int isample=0;isample<writeBuffer.size();++isample){
791 pointAttributes[label_opt[0]]=writeBufferClass[isample];
792 for(
int iband=0;iband<writeBuffer[0].size()-2;++iband){
793 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
800 pointAttributes[fieldname_opt[iband]]=writeBuffer[isample][iband+2];
802 ogrWriter.addPoint(writeBuffer[isample][0],writeBuffer[isample][1],pointAttributes,fieldname,isample);
803 progress=
static_cast<float>(isample+1.0)/writeBuffer.size();
804 pfnProgress(progress,pszMessage,pProgressArg);
809 std::cout <<
"No data found for any class " << std::endl;
812 nsample=writeBuffer.size();
814 std::cout <<
"total number of samples written: " << nsample << std::endl;
815 if(nvalid.size()==class_opt.size()){
816 for(
int iclass=0;iclass<class_opt.size();++iclass)
817 std::cout <<
"class " << class_opt[iclass] <<
" has " << nvalid[iclass] <<
" samples" << std::endl;
824 std::cout <<
"creating image sample writer " << output_opt[0] << std::endl;
827 ogrWriter.open(output_opt[0],ogrformat_opt[0]);
830 std::cout <<
"creating image test writer " << test_opt[0] << std::endl;
832 ogrTestWriter.open(test_opt[0],ogrformat_opt[0]);
834 catch(
string errorString){
835 cerr << errorString << endl;
839 int nlayerRead=sampleReaderOgr.getDataSource()->GetLayerCount();
842 std::cout <<
"number of layers: " << nlayerRead << endl;
844 for(
int ilayer=0;ilayer<nlayerRead;++ilayer){
845 OGRLayer *readLayer=sampleReaderOgr.getLayer(ilayer);
846 string currentLayername=readLayer->GetName();
848 if(find(layer_opt.begin(),layer_opt.end(),currentLayername)==layer_opt.end())
850 cout <<
"processing layer " << currentLayername << endl;
852 readLayer->ResetReading();
853 OGRLayer *writeLayer;
854 OGRLayer *writeTestLayer;
858 std::cout <<
"create polygons" << std::endl;
859 char **papszOptions=NULL;
860 writeLayer=ogrWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPolygon, papszOptions);
862 writeTestLayer=ogrTestWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPolygon, papszOptions);
866 std::cout <<
"create points in layer " << readLayer->GetName() << std::endl;
867 char **papszOptions=NULL;
869 writeLayer=ogrWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPoint, papszOptions);
871 char **papszOptions=NULL;
872 writeTestLayer=ogrTestWriter.createLayer(readLayer->GetName(), imgReader.getProjection(), wkbPoint, papszOptions);
876 std::cout <<
"copy fields from layer " << ilayer << std::flush << std::endl;
877 ogrWriter.copyFields(sampleReaderOgr,ilayer,ilayerWrite);
881 std::cout <<
"copy fields test writer" << std::flush << std::endl;
882 ogrTestWriter.copyFields(sampleReaderOgr,ilayer,ilayerWrite);
891 if(class_opt.size()){
892 switch(ruleMap[rule_opt[0]]){
893 case(rule::proportion):{
894 for(
int iclass=0;iclass<class_opt.size();++iclass){
896 cs << class_opt[iclass];
897 ogrWriter.createField(cs.str(),fieldType,ilayerWrite);
903 ogrWriter.createField(label_opt[0],fieldType,ilayerWrite);
905 ogrTestWriter.createField(label_opt[0],fieldType,ilayerWrite);
910 for(
int iband=0;iband<nband;++iband){
911 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
913 fs << fieldname_opt[iband];
915 std::cout <<
"creating field " << fs.str() << std::endl;
917 ogrWriter.createField(fs.str(),fieldType,ilayerWrite);
919 ogrTestWriter.createField(fs.str(),fieldType,ilayerWrite);
922 OGRFeature *readFeature;
923 unsigned long int ifeature=0;
924 unsigned long int nfeature=sampleReaderOgr.getFeatureCount();
926 pfnProgress(progress,pszMessage,pProgressArg);
927 while( (readFeature = readLayer->GetNextFeature()) != NULL ){
928 bool validFeature=
false;
929 bool writeTest=
false;
931 std::cout <<
"reading feature " << readFeature->GetFID() << std::endl;
932 if(threshold_opt[0]>0){
933 double p=
static_cast<double>(rand())/(RAND_MAX);
935 if(p>threshold_opt[0]){
943 if(ntotalvalid>=-threshold_opt[0]){
951 std::cout <<
"processing feature " << readFeature->GetFID() << std::endl;
954 OGRGeometry *poGeometry;
955 poGeometry = readFeature->GetGeometryRef();
956 assert(poGeometry!=NULL);
958 if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint ){
960 if(!buffer_opt.size()){
961 switch(ruleMap[rule_opt[0]]){
963 case(rule::centroid):
964 buffer_opt.push_back(1);
967 buffer_opt.push_back(3);
972 std::cout <<
"boundary: " << buffer_opt[0] << std::endl;
974 OGRPolygon writePolygon;
975 OGRLinearRing writeRing;
976 OGRPoint writeCentroidPoint;
977 OGRFeature *writePolygonFeature;
978 OGRFeature *writeCentroidFeature;
980 OGRPoint *poPoint = (OGRPoint *) poGeometry;
981 writeCentroidPoint=*poPoint;
986 double i_centre,j_centre;
988 imgReader.geo2image(x,y,i_centre,j_centre);
994 j_centre=
static_cast<int>(j_centre);
995 i_centre=
static_cast<int>(i_centre);
997 double uli=i_centre-buffer_opt[0]/2;
998 double ulj=j_centre-buffer_opt[0]/2;
999 double lri=i_centre+buffer_opt[0]/2;
1000 double lrj=j_centre+buffer_opt[0]/2;
1003 ulj=
static_cast<int>(ulj);
1004 uli=
static_cast<int>(uli);
1005 lrj=
static_cast<int>(lrj);
1006 lri=
static_cast<int>(lri);
1016 if(static_cast<int>(ulj)<0||
static_cast<int>(ulj)>=imgReader.nrOfRow())
1019 if(static_cast<int>(uli)<0||
static_cast<int>(lri)>=imgReader.nrOfCol())
1022 OGRPoint ulPoint,urPoint,llPoint,lrPoint;
1029 double radius=buffer_opt[0]/2.0*sqrt(imgReader.getDeltaX()*imgReader.getDeltaY());
1030 unsigned short nstep = 25;
1031 for(
int i=0;i<nstep;++i){
1033 aPoint.setX(x+imgReader.getDeltaX()/2.0+radius*cos(2*PI*i/nstep));
1034 aPoint.setY(y-imgReader.getDeltaY()/2.0+radius*sin(2*PI*i/nstep));
1035 writeRing.addPoint(&aPoint);
1037 writePolygon.addRing(&writeRing);
1038 writePolygon.closeRings();
1043 imgReader.image2geo(uli,ulj,ulx,uly);
1044 imgReader.image2geo(lri,lrj,lrx,lry);
1045 ulPoint.setX(ulx-imgReader.getDeltaX()/2.0);
1046 ulPoint.setY(uly+imgReader.getDeltaY()/2.0);
1047 lrPoint.setX(lrx+imgReader.getDeltaX()/2.0);
1048 lrPoint.setY(lry-imgReader.getDeltaY()/2.0);
1049 urPoint.setX(lrx+imgReader.getDeltaX()/2.0);
1050 urPoint.setY(uly+imgReader.getDeltaY()/2.0);
1051 llPoint.setX(ulx-imgReader.getDeltaX()/2.0);
1052 llPoint.setY(lry-imgReader.getDeltaY()/2.0);
1054 writeRing.addPoint(&ulPoint);
1055 writeRing.addPoint(&urPoint);
1056 writeRing.addPoint(&lrPoint);
1057 writeRing.addPoint(&llPoint);
1058 writePolygon.addRing(&writeRing);
1059 writePolygon.closeRings();
1063 if((polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point)||(ruleMap[rule_opt[0]]==rule::centroid)){
1073 writePolygonFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1075 writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1077 else if(ruleMap[rule_opt[0]]!=rule::point){
1079 writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1081 writeCentroidFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1084 vector<double> windowClassValues;
1086 if(class_opt.size()){
1087 windowClassValues.resize(class_opt.size());
1089 for(
int iclass=0;iclass<class_opt.size();++iclass)
1090 windowClassValues[iclass]=0;
1093 windowValues.resize(nband);
1094 vector< Vector2d<double> > readValues(nband);
1095 for(
int iband=0;iband<nband;++iband){
1096 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1097 imgReader.readDataBlock(readValues[iband],GDT_Float64,uli,lri,ulj,lrj,theBand);
1101 for(
int j=ulj;j<=lrj;++j){
1102 for(
int i=uli;i<=lri;++i){
1104 if(i<0||i>=imgReader.nrOfCol())
1106 if(j<0||j>=imgReader.nrOfRow())
1111 imgReader.image2geo(i,j,theX,theY);
1112 thePoint.setX(theX);
1113 thePoint.setY(theY);
1115 if(disc_opt[0]&&buffer_opt[0]>1){
1116 double radius=buffer_opt[0]/2.0*sqrt(imgReader.getDeltaX()*imgReader.getDeltaY());
1117 if((theX-x)*(theX-x)+(theY-y)*(theY-y)>radius*radius)
1122 if(srcnodata_opt.size()){
1123 for(
int vband=0;vband<bndnodata_opt.size();++vband){
1124 double value=((readValues[bndnodata_opt[vband]])[j-ulj])[i-uli];
1125 if(value==srcnodata_opt[vband]){
1140 OGRFeature *writePointFeature;
1141 if(!polygon_opt[0]){
1143 if(ruleMap[rule_opt[0]]==rule::point){
1145 writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1147 writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1148 if(verbose_opt[0]>1)
1149 std::cout <<
"copying fields from polygons " << std::endl;
1154 if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE)
1155 cerr <<
"writing feature failed" << std::endl;
1157 assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
1161 if(verbose_opt[0]>1)
1162 std::cout <<
"write feature has " << writePointFeature->GetFieldCount() <<
" fields" << std::endl;
1165 if(class_opt.size()){
1166 short value=((readValues[0])[j-ulj])[i-uli];
1167 for(
int iclass=0;iclass<class_opt.size();++iclass){
1168 if(value==class_opt[iclass])
1169 windowClassValues[iclass]+=1;
1173 for(
int iband=0;iband<nband;++iband){
1174 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1176 assert(j-ulj<readValues[iband].size());
1178 assert(i-uli<((readValues[iband])[j-ulj]).size());
1179 double value=((readValues[iband])[j-ulj])[i-uli];
1181 if(verbose_opt[0]>1)
1182 std::cout <<
": " << value << std::endl;
1183 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
1184 windowValues[iband].push_back(value);
1188 if(verbose_opt[0]>1)
1189 std::cout <<
"set field " << fieldname_opt[iband] <<
" to " << value << std::endl;
1190 switch( fieldType ){
1193 writePointFeature->SetField(fieldname_opt[iband].c_str(),value);
1196 writePointFeature->SetField(fieldname_opt[iband].c_str(),type2string<double>(value).c_str());
1199 std::string errorString=
"field type not supported";
1204 catch(std::string e){
1205 std::cout << e << std::endl;
1211 if(!polygon_opt[0]){
1212 if(ruleMap[rule_opt[0]]==rule::point){
1214 if(verbose_opt[0]>1)
1215 std::cout <<
"creating point feature" << std::endl;
1217 if(writeTestLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1218 std::string errorString=
"Failed to create feature in test ogr vector dataset";
1223 if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1224 std::string errorString=
"Failed to create feature in ogr vector dataset";
1229 OGRFeature::DestroyFeature( writePointFeature );
1232 std::cout <<
"ntotalvalid(2): " << ntotalvalid << std::endl;
1237 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
1241 cout <<
"no points found in window, continuing" << endl;
1251 if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE)
1252 cerr <<
"writing feature failed" << std::endl;
1254 writePolygonFeature->SetGeometry(&writePolygon);
1255 assert(wkbFlatten(writePolygonFeature->GetGeometryRef()->getGeometryType()) == wkbPolygon);
1257 if(verbose_opt[0]>1)
1258 std::cout <<
"copying new fields write polygon " << std::endl;
1259 if(verbose_opt[0]>1)
1260 std::cout <<
"write feature has " << writePolygonFeature->GetFieldCount() <<
" fields" << std::endl;
1265 if(verbose_opt[0]>1)
1266 std::cout <<
"copying fields from polygons " << std::endl;
1271 if(writeCentroidFeature->SetFrom(readFeature)!= OGRERR_NONE)
1272 cerr <<
"writing feature failed" << std::endl;
1273 assert(wkbFlatten(writeCentroidFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
1278 if(verbose_opt[0]>1)
1279 std::cout <<
"write feature has " << writeCentroidFeature->GetFieldCount() <<
" fields" << std::endl;
1281 if(class_opt.empty()){
1282 if(ruleMap[rule_opt[0]]==rule::point){
1284 std::cout <<
"number of points in window: " << nPointWindow << std::endl;
1285 for(
int index=0;index<windowValues.size();++index){
1287 if(windowValues[index].size()!=1){
1288 cerr <<
"Error: windowValues[index].size()=" << windowValues[index].size() << endl;
1289 assert(windowValues[index].size()==1);
1291 double theValue=windowValues[index].back();
1294 std::cout <<
"number of points in window: " << nPointWindow << std::endl;
1295 int theBand=(band_opt.size()) ? band_opt[index] : index;
1298 if(verbose_opt[0]>1)
1299 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1300 switch( fieldType ){
1304 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1306 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1310 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1312 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1315 std::string errorString=
"field type not supported";
1320 catch(std::string e){
1321 std::cout << e << std::endl;
1328 for(
int index=0;index<windowValues.size();++index){
1330 if(ruleMap[rule_opt[0]]==rule::mean)
1331 theValue=stat.mean(windowValues[index]);
1332 else if(ruleMap[rule_opt[0]]==rule::stdev)
1333 theValue=sqrt(stat.var(windowValues[index]));
1334 else if(ruleMap[rule_opt[0]]==rule::median)
1335 theValue=stat.median(windowValues[index]);
1336 else if(ruleMap[rule_opt[0]]==rule::percentile)
1337 theValue=stat.percentile(windowValues[index],windowValues[index].begin(),windowValues[index].end(),percentile_opt[0]);
1338 else if(ruleMap[rule_opt[0]]==rule::sum)
1339 theValue=stat.sum(windowValues[index]);
1340 else if(ruleMap[rule_opt[0]]==rule::max)
1341 theValue=stat.mymax(windowValues[index]);
1342 else if(ruleMap[rule_opt[0]]==rule::min)
1343 theValue=stat.mymin(windowValues[index]);
1344 else if(ruleMap[rule_opt[0]]==rule::centroid){
1346 std::cout <<
"number of points in polygon: " << nPointWindow << std::endl;
1347 assert(nPointWindow<=1);
1348 assert(nPointWindow==windowValues[index].size());
1349 theValue=windowValues[index].back();
1352 std::string errorString=
"rule not supported";
1355 if(verbose_opt[0]>1)
1356 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1357 switch( fieldType ){
1361 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1363 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1367 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1369 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1372 std::string errorString=
"field type not supported";
1377 catch(std::string e){
1378 std::cout << e << std::endl;
1385 if(ruleMap[rule_opt[0]]==rule::proportion){
1387 std::cout <<
"number of points in polygon: " << nPointWindow << std::endl;
1388 stat.normalize_pct(windowClassValues);
1389 for(
int index=0;index<windowClassValues.size();++index){
1390 double theValue=windowClassValues[index];
1392 fs << class_opt[index];
1394 writePolygonFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1396 writeCentroidFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1399 else if(ruleMap[rule_opt[0]]==rule::custom){
1400 assert(polygon_opt[0]);
1402 std::cout <<
"number of points in polygon: " << nPointWindow << std::endl;
1403 stat.normalize_pct(windowClassValues);
1404 assert(windowClassValues.size()==2);
1405 if(windowClassValues[0]>=75)
1406 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(11));
1407 else if(windowClassValues[1]>=75)
1408 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(12));
1409 else if(windowClassValues[0]>25&&windowClassValues[1]>25)
1410 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(13));
1413 std::cout <<
"No valid value in windowClassValues..." << std::endl;
1414 for(
int index=0;index<windowClassValues.size();++index){
1415 double theValue=windowClassValues[index];
1416 std::cout << theValue <<
" ";
1418 std::cout << std::endl;
1420 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(20));
1423 else if(ruleMap[rule_opt[0]]==rule::mode){
1426 std::cout <<
"number of points in window: " << nPointWindow << std::endl;
1428 int maxClass=stat.mymin(class_opt);
1429 vector<double>::iterator maxit;
1430 maxit=stat.mymax(windowClassValues,windowClassValues.begin(),windowClassValues.end());
1431 int maxIndex=distance(windowClassValues.begin(),maxit);
1432 maxClass=class_opt[maxIndex];
1433 if(verbose_opt[0]>0)
1434 std::cout <<
"maxClass: " << maxClass << std::endl;
1436 writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
1438 writeCentroidFeature->SetField(label_opt[0].c_str(),maxClass);
1442 if(verbose_opt[0]>1)
1443 std::cout <<
"creating polygon feature" << std::endl;
1445 if(writeTestLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1446 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1451 if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1452 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1456 OGRFeature::DestroyFeature( writePolygonFeature );
1459 std::cout <<
"ntotalvalid(1): " << ntotalvalid << std::endl;
1462 if(verbose_opt[0]>1)
1463 std::cout <<
"creating point feature in centroid" << std::endl;
1465 if(writeTestLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1466 std::string errorString=
"Failed to create point feature in ogr vector dataset";
1472 assert(validFeature);
1473 if(writeLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1474 std::string errorString=
"Failed to create point feature in ogr vector dataset";
1478 OGRFeature::DestroyFeature( writeCentroidFeature );
1481 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
1485 else if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){
1487 OGRPolygon readPolygon = *((OGRPolygon *) poGeometry);
1488 OGRPolygon writePolygon;
1489 OGRLinearRing writeRing;
1490 OGRPoint writeCentroidPoint;
1491 OGRFeature *writePolygonFeature;
1492 OGRFeature *writeCentroidFeature;
1494 readPolygon.closeRings();
1496 if(verbose_opt[0]>1)
1497 std::cout <<
"get point on polygon" << std::endl;
1498 if(ruleMap[rule_opt[0]]==rule::centroid)
1499 readPolygon.Centroid(&writeCentroidPoint);
1500 else if(readPolygon.PointOnSurface(&writeCentroidPoint)!=OGRERR_NONE){
1501 cerr <<
"function PointOnSurface failed, trying centroid instead" << endl;
1502 readPolygon.Centroid(&writeCentroidPoint);
1504 double ulx,uly,lrx,lry;
1505 double uli,ulj,lri,lrj;
1506 if((polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point)||(ruleMap[rule_opt[0]]==rule::centroid)){
1507 ulx=writeCentroidPoint.getX();
1508 uly=writeCentroidPoint.getY();
1515 std::cout <<
"reading envelope for polygon " << ifeature << std::endl;
1516 OGREnvelope* psEnvelope=
new OGREnvelope();
1517 readPolygon.getEnvelope(psEnvelope);
1518 ulx=psEnvelope->MinX;
1519 uly=psEnvelope->MaxY;
1520 lrx=psEnvelope->MaxX;
1521 lry=psEnvelope->MinY;
1525 imgReader.geo2image(ulx,uly,uli,ulj);
1526 imgReader.geo2image(lrx,lry,lri,lrj);
1535 ulj=
static_cast<int>(ulj);
1536 uli=
static_cast<int>(uli);
1537 lrj=
static_cast<int>(lrj);
1538 lri=
static_cast<int>(lri);
1540 if(verbose_opt[0]>1)
1541 std::cout <<
"bounding box for polygon feature " << ifeature <<
": " << uli <<
" " << ulj <<
" " << lri <<
" " << lrj << std::endl;
1547 if(uli>=imgReader.nrOfCol())
1548 uli=imgReader.nrOfCol()-1;
1549 if(lri>=imgReader.nrOfCol())
1550 lri=imgReader.nrOfCol()-1;
1555 if(ulj>=imgReader.nrOfRow())
1556 ulj=imgReader.nrOfRow()-1;
1557 if(lrj>=imgReader.nrOfRow())
1558 lrj=imgReader.nrOfRow()-1;
1562 int nPointPolygon=0;
1566 writePolygonFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1568 writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1570 else if(ruleMap[rule_opt[0]]!=rule::point){
1572 writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1574 writeCentroidFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1578 vector<double> polyClassValues;
1580 if(class_opt.size()){
1582 polyClassValues.resize(class_opt.size());
1584 for(
int iclass=0;iclass<class_opt.size();++iclass)
1585 polyClassValues[iclass]=0;
1588 polyValues.resize(nband);
1589 vector< Vector2d<double> > readValues(nband);
1590 for(
int iband=0;iband<nband;++iband){
1591 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
1592 imgReader.readDataBlock(readValues[iband],GDT_Float64,uli,lri,ulj,lrj,theBand);
1596 for(
int j=ulj;j<=lrj;++j){
1597 for(
int i=uli;i<=lri;++i){
1599 if(i<0||i>=imgReader.nrOfCol())
1601 if(j<0||j>=imgReader.nrOfRow())
1606 imgReader.image2geo(i,j,theX,theY);
1607 thePoint.setX(theX);
1608 thePoint.setY(theY);
1609 if(ruleMap[rule_opt[0]]!=rule::centroid&&!readPolygon.Contains(&thePoint))
1614 if(srcnodata_opt.size()){
1615 for(
int vband=0;vband<bndnodata_opt.size();++vband){
1616 double value=((readValues[bndnodata_opt[vband]])[j-ulj])[i-uli];
1617 if(value==srcnodata_opt[vband]){
1629 writeRing.addPoint(&thePoint);
1632 if(verbose_opt[0]>1)
1633 std::cout <<
"point is on surface:" << thePoint.getX() <<
"," << thePoint.getY() << std::endl;
1636 if(polythreshold_opt.size())
1637 if(nPointPolygon>polythreshold_opt[0])
1640 OGRFeature *writePointFeature;
1641 if(!polygon_opt[0]){
1643 if(ruleMap[rule_opt[0]]==rule::point){
1645 writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
1647 writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
1648 if(verbose_opt[0]>1)
1649 std::cout <<
"copying fields from polygons " << std::endl;
1650 if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE)
1651 cerr <<
"writing feature failed" << std::endl;
1652 if(verbose_opt[0]>1)
1653 std::cout <<
"set geometry as point " << std::endl;
1655 writePointFeature->SetGeometry(&thePoint);
1656 assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
1660 if(verbose_opt[0]>1)
1661 std::cout <<
"write feature has " << writePointFeature->GetFieldCount() <<
" fields" << std::endl;
1664 if(class_opt.size()){
1665 short value=((readValues[0])[j-ulj])[i-uli];
1666 for(
int iclass=0;iclass<class_opt.size();++iclass){
1667 if(value==class_opt[iclass])
1668 polyClassValues[iclass]+=1;
1672 for(
int iband=0;iband<nband;++iband){
1673 double value=((readValues[iband])[j-ulj])[i-uli];
1674 if(verbose_opt[0]>1)
1675 std::cout <<
": " << value << std::endl;
1676 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point)
1677 polyValues[iband].push_back(value);
1679 if(verbose_opt[0]>1)
1680 std::cout <<
"set field " << fieldname_opt[iband] <<
" to " << value << std::endl;
1681 switch( fieldType ){
1684 writePointFeature->SetField(fieldname_opt[iband].c_str(),value);
1687 writePointFeature->SetField(fieldname_opt[iband].c_str(),type2string<double>(value).c_str());
1696 if(!polygon_opt[0]){
1697 if(ruleMap[rule_opt[0]]==rule::point){
1699 if(verbose_opt[0]>1)
1700 std::cout <<
"creating point feature" << std::endl;
1702 if(writeTestLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1703 std::string errorString=
"Failed to create feature in test ogr vector dataset";
1708 if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
1709 std::string errorString=
"Failed to create feature in ogr vector dataset";
1714 OGRFeature::DestroyFeature( writePointFeature );
1717 std::cout <<
"ntotalvalid(2): " << ntotalvalid << std::endl;
1722 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
1726 cout <<
"no points found in polygon, continuing" << endl;
1731 writePolygon.addRing(&writeRing);
1732 writePolygon.closeRings();
1737 if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE)
1738 cerr <<
"writing feature failed" << std::endl;
1739 if(verbose_opt[0]>1)
1740 std::cout <<
"copying new fields write polygon " << std::endl;
1741 if(verbose_opt[0]>1)
1742 std::cout <<
"write feature has " << writePolygonFeature->GetFieldCount() <<
" fields" << std::endl;
1747 if(verbose_opt[0]>1)
1748 std::cout <<
"copying fields from polygons " << std::endl;
1749 if(writeCentroidFeature->SetFrom(readFeature)!= OGRERR_NONE)
1750 cerr <<
"writing feature failed" << std::endl;
1751 writeCentroidFeature->SetGeometry(&writeCentroidPoint);
1752 assert(wkbFlatten(writeCentroidFeature->GetGeometryRef()->getGeometryType()) == wkbPoint );
1753 if(verbose_opt[0]>1)
1754 std::cout <<
"write feature has " << writeCentroidFeature->GetFieldCount() <<
" fields" << std::endl;
1756 if(class_opt.empty()){
1757 if(ruleMap[rule_opt[0]]==rule::point){
1759 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1760 for(
int index=0;index<polyValues.size();++index){
1761 assert(polyValues[index].size()==1);
1762 double theValue=polyValues[index].back();
1765 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1766 int theBand=(band_opt.size()) ? band_opt[index] : index;
1768 if(verbose_opt[0]>1)
1769 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1770 switch( fieldType ){
1774 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1776 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1780 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1782 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1785 std::string errorString=
"field type not supported";
1790 catch(std::string e){
1791 std::cout << e << std::endl;
1798 for(
int index=0;index<polyValues.size();++index){
1800 if(ruleMap[rule_opt[0]]==rule::mean)
1801 theValue=stat.mean(polyValues[index]);
1802 else if(ruleMap[rule_opt[0]]==rule::stdev)
1803 theValue=sqrt(stat.var(polyValues[index]));
1804 else if(ruleMap[rule_opt[0]]==rule::median)
1805 theValue=stat.median(polyValues[index]);
1806 else if(ruleMap[rule_opt[0]]==rule::percentile)
1807 theValue=stat.percentile(polyValues[index],polyValues[index].begin(),polyValues[index].end(),percentile_opt[0]);
1808 else if(ruleMap[rule_opt[0]]==rule::sum)
1809 theValue=stat.sum(polyValues[index]);
1810 else if(ruleMap[rule_opt[0]]==rule::max)
1811 theValue=stat.mymax(polyValues[index]);
1812 else if(ruleMap[rule_opt[0]]==rule::min)
1813 theValue=stat.mymin(polyValues[index]);
1814 else if(ruleMap[rule_opt[0]]==rule::centroid){
1816 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1817 assert(nPointPolygon<=1);
1818 assert(nPointPolygon==polyValues[index].size());
1819 theValue=polyValues[index].back();
1822 std::string errorString=
"rule not supported";
1825 if(verbose_opt[0]>1)
1826 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
1827 switch( fieldType ){
1831 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
1833 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
1837 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1839 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
1842 std::string errorString=
"field type not supported";
1847 catch(std::string e){
1848 std::cout << e << std::endl;
1855 if(ruleMap[rule_opt[0]]==rule::proportion){
1857 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1858 stat.normalize_pct(polyClassValues);
1859 for(
int index=0;index<polyClassValues.size();++index){
1860 double theValue=polyClassValues[index];
1862 fs << class_opt[index];
1864 writePolygonFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1866 writeCentroidFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
1869 else if(ruleMap[rule_opt[0]]==rule::custom){
1870 assert(polygon_opt[0]);
1872 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1873 stat.normalize_pct(polyClassValues);
1874 assert(polyClassValues.size()==2);
1875 if(polyClassValues[0]>=75)
1876 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(11));
1877 else if(polyClassValues[1]>=75)
1878 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(12));
1879 else if(polyClassValues[0]>25&&polyClassValues[1]>25)
1880 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(13));
1883 std::cout <<
"No valid value in polyClassValues..." << std::endl;
1884 for(
int index=0;index<polyClassValues.size();++index){
1885 double theValue=polyClassValues[index];
1886 std::cout << theValue <<
" ";
1888 std::cout << std::endl;
1890 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(20));
1893 else if(ruleMap[rule_opt[0]]==rule::mode){
1896 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
1898 int maxClass=stat.mymin(class_opt);
1899 vector<double>::iterator maxit;
1900 maxit=stat.mymax(polyClassValues,polyClassValues.begin(),polyClassValues.end());
1901 int maxIndex=distance(polyClassValues.begin(),maxit);
1902 maxClass=class_opt[maxIndex];
1903 if(verbose_opt[0]>0)
1904 std::cout <<
"maxClass: " << maxClass << std::endl;
1906 writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
1908 writeCentroidFeature->SetField(label_opt[0].c_str(),maxClass);
1912 if(verbose_opt[0]>1)
1913 std::cout <<
"creating polygon feature" << std::endl;
1915 if(writeTestLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1916 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1921 if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
1922 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
1926 OGRFeature::DestroyFeature( writePolygonFeature );
1929 std::cout <<
"ntotalvalid(1): " << ntotalvalid << std::endl;
1932 if(verbose_opt[0]>1)
1933 std::cout <<
"creating point feature in centroid" << std::endl;
1935 if(writeTestLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1936 std::string errorString=
"Failed to create point feature in ogr vector dataset";
1942 assert(validFeature);
1943 if(writeLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
1944 std::string errorString=
"Failed to create point feature in ogr vector dataset";
1948 OGRFeature::DestroyFeature( writeCentroidFeature );
1951 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
1955 else if(wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon){
1956 OGRMultiPolygon readPolygon = *((OGRMultiPolygon *) poGeometry);
1957 OGRPolygon writePolygon;
1958 OGRLinearRing writeRing;
1959 OGRPoint writeCentroidPoint;
1960 OGRFeature *writePolygonFeature;
1961 OGRFeature *writeCentroidFeature;
1963 readPolygon.closeRings();
1965 if(verbose_opt[0]>1)
1966 std::cout <<
"get centroid point from polygon" << std::endl;
1967 readPolygon.Centroid(&writeCentroidPoint);
1969 double ulx,uly,lrx,lry;
1970 double uli,ulj,lri,lrj;
1971 if((polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point)||(ruleMap[rule_opt[0]]==rule::centroid)){
1972 ulx=writeCentroidPoint.getX();
1973 uly=writeCentroidPoint.getY();
1980 std::cout <<
"reading envelope for polygon " << ifeature << std::endl;
1981 OGREnvelope* psEnvelope=
new OGREnvelope();
1982 readPolygon.getEnvelope(psEnvelope);
1983 ulx=psEnvelope->MinX;
1984 uly=psEnvelope->MaxY;
1985 lrx=psEnvelope->MaxX;
1986 lry=psEnvelope->MinY;
1990 imgReader.geo2image(ulx,uly,uli,ulj);
1991 imgReader.geo2image(lrx,lry,lri,lrj);
2000 ulj=
static_cast<int>(ulj);
2001 uli=
static_cast<int>(uli);
2002 lrj=
static_cast<int>(lrj);
2003 lri=
static_cast<int>(lri);
2005 if(verbose_opt[0]>1)
2006 std::cout <<
"bounding box for multipologon feature " << ifeature <<
": " << uli <<
" " << ulj <<
" " << lri <<
" " << lrj << std::endl;
2012 if(uli>=imgReader.nrOfCol())
2013 uli=imgReader.nrOfCol()-1;
2014 if(lri>=imgReader.nrOfCol())
2015 lri=imgReader.nrOfCol()-1;
2020 if(ulj>=imgReader.nrOfRow())
2021 ulj=imgReader.nrOfRow()-1;
2022 if(lrj>=imgReader.nrOfRow())
2023 lrj=imgReader.nrOfRow()-1;
2027 int nPointPolygon=0;
2030 writePolygonFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
2032 writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
2034 else if(ruleMap[rule_opt[0]]!=rule::point){
2036 writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
2038 writeCentroidFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
2042 vector<double> polyClassValues;
2044 if(class_opt.size()){
2045 polyClassValues.resize(class_opt.size());
2047 for(
int iclass=0;iclass<class_opt.size();++iclass)
2048 polyClassValues[iclass]=0;
2051 polyValues.resize(nband);
2052 vector< Vector2d<double> > readValues(nband);
2053 for(
int iband=0;iband<nband;++iband){
2054 int theBand=(band_opt.size()) ? band_opt[iband] : iband;
2055 imgReader.readDataBlock(readValues[iband],GDT_Float64,uli,lri,ulj,lrj,theBand);
2059 for(
int j=ulj;j<=lrj;++j){
2060 for(
int i=uli;i<=lri;++i){
2062 if(i<0||i>=imgReader.nrOfCol())
2064 if(j<0||j>=imgReader.nrOfRow())
2069 imgReader.image2geo(i,j,theX,theY);
2070 thePoint.setX(theX);
2071 thePoint.setY(theY);
2073 if(ruleMap[rule_opt[0]]!=rule::centroid&&!readPolygon.Contains(&thePoint))
2078 if(srcnodata_opt.size()){
2079 for(
int vband=0;vband<bndnodata_opt.size();++vband){
2080 double value=((readValues[bndnodata_opt[vband]])[j-ulj])[i-uli];
2081 if(value==srcnodata_opt[vband]){
2093 writeRing.addPoint(&thePoint);
2096 if(verbose_opt[0]>1)
2097 std::cout <<
"point is on surface:" << thePoint.getX() <<
"," << thePoint.getY() << std::endl;
2100 if(polythreshold_opt.size())
2101 if(nPointPolygon>polythreshold_opt[0])
2104 OGRFeature *writePointFeature;
2105 if(!polygon_opt[0]){
2107 if(ruleMap[rule_opt[0]]==rule::point){
2109 writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
2111 writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
2112 if(verbose_opt[0]>1)
2113 std::cout <<
"copying fields from polygons " << std::endl;
2114 if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE)
2115 cerr <<
"writing feature failed" << std::endl;
2116 if(verbose_opt[0]>1)
2117 std::cout <<
"set geometry as point " << std::endl;
2119 writePointFeature->SetGeometry(&thePoint);
2120 assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
2124 if(verbose_opt[0]>1)
2125 std::cout <<
"write feature has " << writePointFeature->GetFieldCount() <<
" fields" << std::endl;
2128 if(class_opt.size()){
2129 short value=((readValues[0])[j-ulj])[i-uli];
2130 for(
int iclass=0;iclass<class_opt.size();++iclass){
2131 if(value==class_opt[iclass])
2132 polyClassValues[iclass]+=1;
2136 for(
int iband=0;iband<nband;++iband){
2137 double value=((readValues[iband])[j-ulj])[i-uli];
2138 if(verbose_opt[0]>1)
2139 std::cout <<
": " << value << std::endl;
2140 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point)
2141 polyValues[iband].push_back(value);
2143 if(verbose_opt[0]>1)
2144 std::cout <<
"set field " << fieldname_opt[iband] <<
" to " << value << std::endl;
2145 switch( fieldType ){
2148 writePointFeature->SetField(fieldname_opt[iband].c_str(),value);
2151 writePointFeature->SetField(fieldname_opt[iband].c_str(),type2string<double>(value).c_str());
2160 if(!polygon_opt[0]){
2161 if(ruleMap[rule_opt[0]]==rule::point){
2163 if(verbose_opt[0]>1)
2164 std::cout <<
"creating point feature" << std::endl;
2166 if(writeTestLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
2167 std::string errorString=
"Failed to create feature in ogr vector dataset";
2172 if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){
2173 std::string errorString=
"Failed to create feature in ogr vector dataset";
2178 OGRFeature::DestroyFeature( writePointFeature );
2184 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
2189 if(polygon_opt[0]||ruleMap[rule_opt[0]]!=rule::point){
2195 writePolygon.addRing(&writeRing);
2196 writePolygon.closeRings();
2201 if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE)
2202 cerr <<
"writing feature failed" << std::endl;
2203 assert(writePolygonFeature->GetGeometryRef()->getGeometryType()==wkbMultiPolygon);
2204 if(verbose_opt[0]>1)
2205 std::cout <<
"copying new fields write polygon " << std::endl;
2206 if(verbose_opt[0]>1)
2207 std::cout <<
"write feature has " << writePolygonFeature->GetFieldCount() <<
" fields" << std::endl;
2212 if(verbose_opt[0]>1)
2213 std::cout <<
"copying fields from polygons " << std::endl;
2214 if(writeCentroidFeature->SetFrom(readFeature)!= OGRERR_NONE)
2215 cerr <<
"writing feature failed" << std::endl;
2216 writeCentroidFeature->SetGeometry(&writeCentroidPoint);
2217 assert(wkbFlatten(writeCentroidFeature->GetGeometryRef()->getGeometryType()) == wkbPoint);
2218 if(verbose_opt[0]>1)
2219 std::cout <<
"write feature has " << writeCentroidFeature->GetFieldCount() <<
" fields" << std::endl;
2221 if(class_opt.empty()){
2222 if(ruleMap[rule_opt[0]]==rule::point){
2224 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2225 for(
int index=0;index<polyValues.size();++index){
2227 assert(polyValues[index].size()==1);
2228 double theValue=polyValues[index].back();
2230 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2231 int theBand=(band_opt.size()) ? band_opt[index] : index;
2233 if(verbose_opt[0]>1)
2234 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
2235 switch( fieldType ){
2239 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
2241 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
2245 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2247 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2250 std::string errorString=
"field type not supported";
2255 catch(std::string e){
2256 std::cout << e << std::endl;
2263 for(
int index=0;index<polyValues.size();++index){
2265 if(ruleMap[rule_opt[0]]==rule::mean)
2266 theValue=stat.mean(polyValues[index]);
2267 else if(ruleMap[rule_opt[0]]==rule::stdev)
2268 theValue=sqrt(stat.var(polyValues[index]));
2269 else if(ruleMap[rule_opt[0]]==rule::median)
2270 theValue=stat.median(polyValues[index]);
2271 else if(ruleMap[rule_opt[0]]==rule::percentile)
2272 theValue=stat.percentile(polyValues[index],polyValues[index].begin(),polyValues[index].end(),percentile_opt[0]);
2273 else if(ruleMap[rule_opt[0]]==rule::sum)
2274 theValue=stat.sum(polyValues[index]);
2275 else if(ruleMap[rule_opt[0]]==rule::max)
2276 theValue=stat.mymax(polyValues[index]);
2277 else if(ruleMap[rule_opt[0]]==rule::min)
2278 theValue=stat.mymin(polyValues[index]);
2279 else if(ruleMap[rule_opt[0]]==rule::centroid){
2281 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2282 assert(nPointPolygon<=1);
2283 assert(nPointPolygon==polyValues[index].size());
2284 theValue=polyValues[index].back();
2287 std::string errorString=
"rule not supported";
2290 if(verbose_opt[0]>1)
2291 std::cout <<
"set field " << fieldname_opt[index] <<
" to " << theValue << std::endl;
2292 switch( fieldType ){
2296 writePolygonFeature->SetField(fieldname_opt[index].c_str(),theValue);
2298 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),theValue);
2302 writePolygonFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2304 writeCentroidFeature->SetField(fieldname_opt[index].c_str(),type2string<double>(theValue).c_str());
2307 std::string errorString=
"field type not supported";
2312 catch(std::string e){
2313 std::cout << e << std::endl;
2320 if(ruleMap[rule_opt[0]]==rule::proportion){
2322 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2323 stat.normalize_pct(polyClassValues);
2324 for(
int index=0;index<polyClassValues.size();++index){
2325 double theValue=polyClassValues[index];
2327 fs << class_opt[index];
2329 writePolygonFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
2331 writeCentroidFeature->SetField(fs.str().c_str(),
static_cast<int>(theValue));
2334 else if(ruleMap[rule_opt[0]]==rule::custom){
2335 assert(polygon_opt[0]);
2337 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2338 stat.normalize_pct(polyClassValues);
2339 assert(polyClassValues.size()==2);
2340 if(polyClassValues[0]>=75)
2341 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(11));
2342 else if(polyClassValues[1]>=75)
2343 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(12));
2344 else if(polyClassValues[0]>25&&polyClassValues[1]>25)
2345 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(13));
2348 std::cout <<
"No valid value in polyClassValues..." << std::endl;
2349 for(
int index=0;index<polyClassValues.size();++index){
2350 double theValue=polyClassValues[index];
2351 std::cout << theValue <<
" ";
2353 std::cout << std::endl;
2355 writePolygonFeature->SetField(label_opt[0].c_str(),static_cast<int>(20));
2358 else if(ruleMap[rule_opt[0]]==rule::mode){
2361 std::cout <<
"number of points in polygon: " << nPointPolygon << std::endl;
2363 int maxClass=stat.mymin(class_opt);
2364 vector<double>::iterator maxit;
2365 maxit=stat.mymax(polyClassValues,polyClassValues.begin(),polyClassValues.end());
2366 int maxIndex=distance(polyClassValues.begin(),maxit);
2367 maxClass=class_opt[maxIndex];
2368 if(verbose_opt[0]>0)
2369 std::cout <<
"maxClass: " << maxClass << std::endl;
2371 writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
2373 writeCentroidFeature->SetField(label_opt[0].c_str(),maxClass);
2378 if(verbose_opt[0]>1)
2379 std::cout <<
"creating polygon feature" << std::endl;
2381 if(writeTestLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
2382 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
2387 if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){
2388 std::string errorString=
"Failed to create polygon feature in ogr vector dataset";
2392 OGRFeature::DestroyFeature( writePolygonFeature );
2395 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
2398 if(verbose_opt[0]>1)
2399 std::cout <<
"creating point feature in centroid" << std::endl;
2401 if(writeTestLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
2402 std::string errorString=
"Failed to create point feature in ogr vector dataset";
2408 assert(validFeature);
2409 if(writeLayer->CreateFeature( writeCentroidFeature ) != OGRERR_NONE ){
2410 std::string errorString=
"Failed to create point feature in ogr vector dataset";
2414 OGRFeature::DestroyFeature( writeCentroidFeature );
2417 std::cout <<
"ntotalvalid: " << ntotalvalid << std::endl;
2423 test=poGeometry->getGeometryName();
2425 oss <<
"geometry " << test <<
" not supported";
2429 progress=
static_cast<float>(ifeature+1)/nfeature;
2430 pfnProgress(progress,pszMessage,pProgressArg);
2432 catch(std::string e){
2433 std::cout << e << std::endl;
2438 std::cout <<
"number of points read in polygon: " << npoint << std::endl;
2445 pfnProgress(progress,pszMessage,pProgressArg);
2448 sampleReaderOgr.close();
2451 ogrTestWriter.close();
2454 pfnProgress(progress,pszMessage,pProgressArg);