pktools 2.6.7
Processing Kernel for geospatial data
pkpolygonize.cc
1/**********************************************************************
2pkpolygonize.cc: program to make vector file from 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
33#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37/******************************************************************************/
76using namespace std;
77
78int main(int argc,char **argv) {
79 Optionpk<string> input_opt("i", "input", "Input image file");
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! ");
81 Optionpk<string> output_opt("o", "output", "Output vector file");
82 Optionpk<string> ogrformat_opt("f", "f", "Output OGR file format","SQLite");
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);
87
88 bool doProcess;//stop process when program was invoked with help option (-h --help)
89 try{
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);
98 }
99 catch(string predefinedString){
100 std::cout << predefinedString << std::endl;
101 exit(0);
102 }
103 if(!doProcess){
104 cout << endl;
105 cout << "Usage: pkpolygonize -i input [-m mask] -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 if(input_opt.empty()){
111 std::cerr << "No input file provided (use option -i). Use --help for help information";
112 exit(0);
113 }
114 if(output_opt.empty()){
115 std::cerr << "No output file provided (use option -o). Use --help for help information";
116 exit(0);
117 }
118
119 GDALAllRegister();
120
121 double dfComplete=0.0;
122 const char* pszMessage;
123 void* pProgressArg=NULL;
124 GDALProgressFunc pfnProgress=GDALTermProgress;
125 pfnProgress(dfComplete,pszMessage,pProgressArg);
126
127
128 ImgReaderGdal maskReader;
129 GDALRasterBand *maskBand=NULL;
130 if(mask_opt.size()){
131 if(verbose_opt[0])
132 cout << "opening mask file " << mask_opt[0] << endl;
133 maskReader.open(mask_opt[0]);
134 maskBand = maskReader.getRasterBand(0);
135 }
136
137 ImgReaderGdal inputReader(input_opt[0]);
138 GDALRasterBand *inputBand;
139 inputBand=inputReader.getRasterBand(0);
140 if(nodata_opt.size())
141 inputBand->SetNoDataValue(nodata_opt[0]);
142 ImgWriterOgr ogrWriter(output_opt[0],ogrformat_opt[0]);
143 OGRLayer* theLayer=ogrWriter.createLayer(output_opt[0].substr(output_opt[0].rfind('/')+1), inputReader.getProjectionRef());
144 if(verbose_opt[0])
145 cout << "projection: " << inputReader.getProjection() << endl;
146 ogrWriter.createField(fname_opt[0],OFTInteger);
147
148 OGRLayerH hOutLayer=(OGRLayerH)ogrWriter.getLayer();
149 if(verbose_opt[0])
150 cout << "GDALPolygonize started..." << endl;
151
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;
155 else{
156 dfComplete=1.0;
157 pfnProgress(dfComplete,pszMessage,pProgressArg);
158 }
159 cout << "number of features: " << OGR_L_GetFeatureCount(hOutLayer,TRUE) << endl;
160
161 inputReader.close();
162 if(mask_opt.size())
163 maskReader.close();
164 ogrWriter.close();
165}
166
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.