pktools 2.6.7
Processing Kernel for geospatial data
pksieve.cc
1/**********************************************************************
2pksieve.cc: program to sieve filter raster image
3Copyright (C) 2008-2014 Pieter Kempeneers
4
5This file is part of pktools
6
7pktools is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
11
12pktools is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with pktools. If not, see <http://www.gnu.org/licenses/>.
19***********************************************************************/
20#include "cpl_string.h"
21#include "gdal_priv.h"
22#include "gdal.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"
28extern "C" {
29#include "gdal_alg.h"
30#include "ogr_api.h"
31}
32/******************************************************************************/
72using namespace std;
73
74int main(int argc,char **argv) {
75 Optionpk<string> input_opt("i", "input", "Input image file");
76 Optionpk<string> mask_opt("m", "mask", "Use the first band of the specified file as a validity mask (zero is invalid, non-zero is valid).");
77 Optionpk<string> output_opt("o", "output", "Output image file");
78 Optionpk<int> band_opt("b", "band", "the band to be used from input file", 0);
79 Optionpk<int> connect_opt("c", "connect", "the connectedness: 4 directions or 8 directions", 8);
80 Optionpk<int> size_opt("s", "size", "raster polygons with sizes smaller than this will be merged into their largest neighbour. No sieve is performed if size = 0", 0);
81 Optionpk<string> otype_opt("ot", "otype", "Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image", "");
82 Optionpk<string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified.");
83 Optionpk<string> colorTable_opt("ct", "ct", "color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid)");
84 Optionpk<short> verbose_opt("v", "verbose", "verbose mode if > 0", 0,2);
85
86 bool doProcess;//stop process when program was invoked with help option (-h --help)
87 try{
88 doProcess=input_opt.retrieveOption(argc,argv);
89 size_opt.retrieveOption(argc,argv);
90 output_opt.retrieveOption(argc,argv);
91 connect_opt.retrieveOption(argc,argv);
92 band_opt.retrieveOption(argc,argv);
93 mask_opt.retrieveOption(argc,argv);
94 otype_opt.retrieveOption(argc,argv);
95 option_opt.retrieveOption(argc,argv);
96 colorTable_opt.retrieveOption(argc,argv);
97 verbose_opt.retrieveOption(argc,argv);
98 }
99 catch(string predefinedString){
100 std::cout << predefinedString << std::endl;
101 exit(0);
102 }
103 if(!doProcess){
104 cout << endl;
105 cout << "Usage: pksieve -i input [-s size] -o output" << endl;
106 cout << endl;
107 std::cout << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
108 exit(0);//help was invoked, stop processing
109 }
110
111 GDALAllRegister();
112
113 double dfComplete=0.0;
114 const char* pszMessage;
115 void* pProgressArg=NULL;
116 GDALProgressFunc pfnProgress=GDALTermProgress;
117 pfnProgress(dfComplete,pszMessage,pProgressArg);
118
119 ImgReaderGdal maskReader;
120 GDALRasterBand *maskBand=NULL;
121 if(mask_opt.size()){
122 if(verbose_opt[0])
123 cout << "opening mask file " << mask_opt[0] << endl;
124 maskReader.open(mask_opt[0]);
125 maskBand = maskReader.getRasterBand(0);
126 }
127
128 assert(input_opt.size());
129 assert(output_opt.size());
130 ImgReaderGdal inputReader(input_opt[0]);
131 GDALRasterBand *inputBand;
132 inputBand=inputReader.getRasterBand(band_opt[0]);
133
134 ImgWriterGdal outputWriter;
135 GDALRasterBand *outputBand=NULL;
136 if(verbose_opt[0])
137 cout << "opening output file " << output_opt[0] << endl;
138 outputWriter.open(output_opt[0],inputReader);
139 if(colorTable_opt.size()){
140 if(colorTable_opt[0]!="none")
141 outputWriter.setColorTable(colorTable_opt[0]);
142 }
143 else if (inputReader.getColorTable()!=NULL)//copy colorTable from input image
144 outputWriter.setColorTable(inputReader.getColorTable());
145 outputBand = outputWriter.getRasterBand(0);
146 //sieve filter to remove small raster elements (overwrite input band)
147 if(size_opt[0]){
148 if(GDALSieveFilter((GDALRasterBandH)inputBand, (GDALRasterBandH)maskBand, (GDALRasterBandH)outputBand, size_opt[0], connect_opt[0],NULL,pfnProgress,pProgressArg)!=CE_None)
149 cerr << CPLGetLastErrorMsg() << endl;
150 else{
151 dfComplete=1.0;
152 pfnProgress(dfComplete,pszMessage,pProgressArg);
153 }
154 }
155 inputReader.close();
156 if(mask_opt.size())
157 maskReader.close();
158 outputWriter.close();
159}
160
GDALRasterBand * getRasterBand(int band=0) const
Get the GDAL rasterband for this dataset.
void close(void)
Set the memory (in MB) to cache a number of rows in memory.
void open(const std::string &filename, const GDALAccess &readMode=GA_ReadOnly)
Open an image.
void close(void)
Close the image.
void open(const std::string &filename, const ImgReaderGdal &imgSrc, const std::vector< std::string > &options=std::vector< std::string >())
Open an image for writing, copying image attributes from a source image. Image is directly written to...
void setColorTable(const std::string &filename, int band=0)
Set the color table using an (ASCII) file with 5 columns (value R G B alpha)