pktools  2.6.3
Processing Kernel for geospatial data
pkfillnodata.cc
1 /**********************************************************************
2 pkfillnodata.cc: program to fill holes in raster image
3 Copyright (C) 2008-2014 Pieter Kempeneers
4 
5 This file is part of pktools
6 
7 pktools is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 pktools is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along 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 extern "C" {
24 #include "gdal_alg.h"
25 }
26 #include <string>
27 #include "base/Optionpk.h"
28 
29 using namespace std;
30 
31 int main(int argc,char **argv) {
32  Optionpk<std::string> input_opt("i", "input", "Input raster dataset");
33  Optionpk<int> band_opt("b", "band", "band(s) to process (Default is -1: process all bands)");
34  Optionpk<std::string> mask_opt("m", "mask", "Mask raster dataset indicating pixels to be interpolated (zero valued) ");
35  Optionpk<std::string> output_opt("o", "output", "Output image file");
36  Optionpk<double> distance_opt("d", "distance", "Maximum number of pixels to search in all directions to find values to interpolate from", 0);
37  Optionpk<int> iteration_opt("it", "iteration", "Number of 3x3 smoothing filter passes to run (default 0)", 0);
38  Optionpk<short> verbose_opt("v", "verbose", "verbose", 0,2);
39 
40  distance_opt.setHide(1);
41  iteration_opt.setHide(1);
42 
43  bool doProcess;//stop process when program was invoked with help option (-h --help)
44  try{
45  doProcess=input_opt.retrieveOption(argc,argv);
46  band_opt.retrieveOption(argc,argv);
47  output_opt.retrieveOption(argc,argv);
48  mask_opt.retrieveOption(argc,argv);
49  distance_opt.retrieveOption(argc,argv);
50  iteration_opt.retrieveOption(argc,argv);
51  verbose_opt.retrieveOption(argc,argv);
52  }
53  catch(std::string predefinedString){
54  std::cout << predefinedString << std::endl;
55  exit(0);
56  }
57  if(!doProcess){
58  cout << endl;
59  cout << "Usage: pkfillnodata -i input.txt -m mask -o output" << endl;
60  cout << endl;
61  std::cout << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
62  exit(0);//help was invoked, stop processing
63  }
64 
65  assert(input_opt.size());
66  assert(mask_opt.size());
67  assert(output_opt.size());
68  GDALAllRegister();
69  GDALDataset *gds_input;
70  if(verbose_opt[0])
71  std::cout << "opening input file " << input_opt[0] << std::endl;
72  gds_input = (GDALDataset *) GDALOpen(input_opt[0].c_str(), GA_ReadOnly);
73  if(gds_input == NULL){
74  std::string errorString="FileOpenError";
75  throw(errorString);
76  }
77 
78  GDALDataset *gds_mask;
79  if(verbose_opt[0])
80  std::cout << "opening mask file " << mask_opt[0] << std::endl;
81  gds_mask = (GDALDataset *) GDALOpen(mask_opt[0].c_str(), GA_ReadOnly );
82  if(gds_mask == NULL){
83  std::string errorString="FileOpenError";
84  throw(errorString);
85  }
86  GDALRasterBand *maskBand;
87  if(verbose_opt[0])
88  std::cout << "get mask raster band" << std::endl;
89  maskBand=gds_mask->GetRasterBand(1);
90 
91 
92  GDALDriver *poDriver;
93  poDriver = GetGDALDriverManager()->GetDriverByName(gds_input->GetDriver()->GetDescription());
94  if( poDriver == NULL ){
95  std::string errorString="FileOpenError";
96  throw(errorString);
97  }
98  if(verbose_opt[0])
99  std::cout << "copying input file to " << output_opt[0] << std::endl;
100  poDriver->CopyFiles(output_opt[0].c_str(),input_opt[0].c_str());
101  GDALDataset *gds_out;
102  gds_out=(GDALDataset *) GDALOpen(output_opt[0].c_str(), GA_Update);
103 
104  if(band_opt.empty()){
105  band_opt.clear();
106  for(int iband=0;iband<gds_input->GetRasterCount();++iband)
107  band_opt.push_back(iband);
108  }
109  GDALRasterBand *targetBand;
110  for(unsigned short iband=0;iband<band_opt.size();++iband){
111  targetBand=gds_out->GetRasterBand(band_opt[iband]+1);
112  if(verbose_opt[0])
113  std::cout << "copying input file to " << output_opt[0] << std::endl;
114  double dfComplete=0.0;
115  const char* pszMessage;
116  void* pProgressArg=NULL;
117  GDALProgressFunc pfnProgress=GDALTermProgress;
118  pfnProgress(dfComplete,pszMessage,pProgressArg);
119  if(GDALFillNodata(targetBand,maskBand,distance_opt[0],0,iteration_opt[0],NULL,pfnProgress,pProgressArg)!=CE_None){
120  std::cerr << CPLGetLastErrorMsg() << std::endl;
121  exit(1);
122  }
123  else{
124  dfComplete=1.0;
125  pfnProgress(dfComplete,pszMessage,pProgressArg);
126  }
127 
128  // gds_out=poDriver->CreateCopy(output_opt[0].c_str(),gds_input, FALSE,NULL,NULL,NULL);
129  // char **papszParmList=NULL;
130  // gds_out=poDriver->Create(output_opt[0].c_str(),targetBand->GetXSize(),targetBand->GetYSize(),1,targetBand->GetRasterDataType(),papszParmList);
131  // char **papszMetadata;
132  // papszMetadata = poDriver->GetMetadata();
133  // assert( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATE, FALSE ));
134  // ostringstream compressList;
135  // gds_out->SetMetadataItem("INTERLEAVE",gds_input->GetMetadataItem( "INTERLEAVE", "IMAGE_STRUCTURE"),"IMAGE_STRUCTURE");
136  // gds_out->SetMetadataItem("COMPRESSION",gds_input->GetMetadataItem( "COMPRESSION", "IMAGE_STRUCTURE"),"IMAGE_STRUCTURE");
137  // if(gds_input->GetProjectionRef()!=NULL){
138  // gds_out->SetProjection(gds_input->GetProjectionRef());
139  // double adfGeoTransform[6];
140  // gds_input->GetGeoTransform(adfGeoTransform);
141  // gds_out->SetGeoTransform(adfGeoTransform);
142  // }
143  }
144  GDALClose(gds_input);
145  GDALClose(gds_mask);
146  GDALClose(gds_out);
147  GDALDumpOpenDatasets(stderr);
148  GDALDestroyDriverManager();
149 }
150