20 #include "cpl_string.h" 21 #include "gdal_priv.h" 23 #include "imageclasses/ImgReaderGdal.h" 24 #include "imageclasses/ImgWriterGdal.h" 25 #include "imageclasses/ImgWriterOgr.h" 26 #include "base/Optionpk.h" 27 #include "ogrsf_frmts.h" 78 int main(
int argc,
char **argv) {
80 Optionpk<string> mask_opt(
"m",
"mask",
"All pixels in the mask band with a value other than zero will be considered suitable for collection as polygons. Use input file as mask to remove background polygon! ");
83 Optionpk<int> band_opt(
"b",
"band",
"the band to be used from input file", 0);
84 Optionpk<string> fname_opt(
"n",
"name",
"the field name of the output layer",
"DN");
85 Optionpk<double> nodata_opt(
"nodata",
"nodata",
"Disgard this nodata value when creating polygons.");
86 Optionpk<short> verbose_opt(
"v",
"verbose",
"verbose mode if > 0", 0,2);
90 doProcess=input_opt.retrieveOption(argc,argv);
91 mask_opt.retrieveOption(argc,argv);
92 output_opt.retrieveOption(argc,argv);
93 ogrformat_opt.retrieveOption(argc,argv);
94 band_opt.retrieveOption(argc,argv);
95 nodata_opt.retrieveOption(argc,argv);
96 fname_opt.retrieveOption(argc,argv);
97 verbose_opt.retrieveOption(argc,argv);
99 catch(
string predefinedString){
100 std::cout << predefinedString << std::endl;
105 cout <<
"Usage: pkpolygonize -i input [-m mask] -o output" << endl;
107 std::cout <<
"short option -h shows basic options only, use long option --help to show all options" << std::endl;
110 if(input_opt.empty()){
111 std::cerr <<
"No input file provided (use option -i). Use --help for help information";
114 if(output_opt.empty()){
115 std::cerr <<
"No output file provided (use option -o). Use --help for help information";
121 double dfComplete=0.0;
122 const char* pszMessage;
123 void* pProgressArg=NULL;
124 GDALProgressFunc pfnProgress=GDALTermProgress;
125 pfnProgress(dfComplete,pszMessage,pProgressArg);
129 GDALRasterBand *maskBand=NULL;
132 cout <<
"opening mask file " << mask_opt[0] << endl;
133 maskReader.
open(mask_opt[0]);
138 GDALRasterBand *inputBand;
139 inputBand=inputReader.getRasterBand(0);
140 if(nodata_opt.size())
141 inputBand->SetNoDataValue(nodata_opt[0]);
143 OGRLayer* theLayer=ogrWriter.createLayer(output_opt[0].substr(output_opt[0].rfind(
'/')+1), inputReader.getProjectionRef());
145 cout <<
"projection: " << inputReader.getProjection() << endl;
146 ogrWriter.createField(fname_opt[0],OFTInteger);
148 OGRLayerH hOutLayer=(OGRLayerH)ogrWriter.getLayer();
150 cout <<
"GDALPolygonize started..." << endl;
152 int index=theLayer->GetLayerDefn()->GetFieldIndex(fname_opt[0].c_str());
153 if(GDALPolygonize((GDALRasterBandH)inputBand, (GDALRasterBandH)maskBand, hOutLayer,index,NULL,pfnProgress,pProgressArg)!=CE_None)
154 cerr << CPLGetLastErrorMsg() << endl;
157 pfnProgress(dfComplete,pszMessage,pProgressArg);
159 cout <<
"number of features: " << OGR_L_GetFeatureCount(hOutLayer,TRUE) << endl;
void close(void)
Set the memory (in MB) to cache a number of rows in memory.
GDALRasterBand * getRasterBand(int band=0) const
Get the GDAL rasterband for this dataset.
void open(const std::string &filename, const GDALAccess &readMode=GA_ReadOnly)
Open an image.