Debian Codegraph @

src/imageclasses/jlreclass_lib.cc

raw symbols SCIP
1 /**********************************************************************
2 jlreclass_lib.cc: program to replace categorical pixel values in raster dataset
3 Author(s): [email protected]
4 Copyright (C) 2016-2020 European Union (Joint Research Centre)
5
6 This file is part of jiplib.
7
8 jiplib is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 jiplib is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with jiplib. If not, see <https://www.gnu.org/licenses/>.
20 ***********************************************************************/
21 #include <assert.h>
22 #include <map>
23 #include "base/Optionjl.h"
24 #include "imageclasses/Jim.h"
25 #include "apps/AppFactory.h"
26
27 using namespace std;
28 using namespace app;
29
30 shared_ptr<Jim> Jim::reclass(app::AppFactory& app){
31 shared_ptr<Jim> imgWriter=createImg();
32 reclass(*imgWriter, app);
33 return(imgWriter);
34 }
35
36 void Jim::reclass(Jim& imgWriter, app::AppFactory& app){
37 Optionjl<string> mask_opt("m", "mask", "Mask image(s)");
38 Optionjl<unsigned short> masknodata_opt("msknodata", "msknodata", "Mask value(s) where image has nodata. Use one value for each mask, or multiple values for a single mask.", 1);
39 Optionjl<int> nodata_opt("nodata", "nodata", "nodata value to put in image if not valid (0)", 0);
40 Optionjl<string> colorTable_opt("ct", "ct", "color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid)");
41 Optionjl<unsigned short> band_opt("b", "band", "band index(es) to replace (other bands are copied to output)", 0);
42 Optionjl<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", "");
43 Optionjl<string> code_opt("code", "code", "Recode text file (2 columns: from to)");
44 Optionjl<string> class_opt("c", "class", "list of classes to reclass (in combination with reclass option)");
45 Optionjl<string> reclass_opt("r", "reclass", "list of recoded classes (in combination with class option)");
46 // Optionjl<string> fieldname_opt("n", "fname", "field name of the shape file to be replaced", "label");
47 // Optionjl<string> description_opt("d", "description", "Set image description");
48 Optionjl<short> verbose_opt("v", "verbose", "verbose", 0);
49
50 bool doProcess;//stop process when program was invoked with help option (-h --help)
51 try{
52 doProcess=mask_opt.retrieveOption(app);
53 masknodata_opt.retrieveOption(app);
54 nodata_opt.retrieveOption(app);
55 code_opt.retrieveOption(app);
56 class_opt.retrieveOption(app);
57 reclass_opt.retrieveOption(app);
58 colorTable_opt.retrieveOption(app);
59 otype_opt.retrieveOption(app);
60 band_opt.retrieveOption(app);
61 // fieldname_opt.retrieveOption(app);
62 // description_opt.retrieveOption(app);
63 verbose_opt.retrieveOption(app);
64
65 if(!doProcess){
66 cout << endl;
67 std::ostringstream helpStream;
68 helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
69 throw(helpStream.str());//help was invoked, stop processing
70 }
71
72 std::vector<std::string> badKeys;
73 app.badKeys(badKeys);
74 if(badKeys.size()){
75 std::ostringstream errorStream;
76 if(badKeys.size()>1)
77 errorStream << "Error: unknown keys: ";
78 else
79 errorStream << "Error: unknown key: ";
80 for(int ikey=0;ikey<badKeys.size();++ikey){
81 errorStream << badKeys[ikey] << " ";
82 }
83 errorStream << std::endl;
84 throw(errorStream.str());
85 }
86
87 map<string,string> codemapString;//map with codes: codemapString[theKey(from)]=theValue(to)
88 map<double,double> codemap;//map with codes: codemap[theKey(from)]=theValue(to)
89 if(code_opt.size()){
90 if(verbose_opt[0])
91 cout << "opening code text file " << code_opt[0] << endl;
92 ifstream codefile;
93 codefile.open(code_opt[0].c_str());
94 string theKey;
95 string theValue;
96 while(codefile>>theKey){
97 codefile >> theValue;
98 codemapString[theKey]=theValue;
99 codemap[string2type<double>(theKey)]=string2type<double>(theValue);
100 }
101 codefile.close();
102 }
103 else{//use combination of class_opt and reclass_opt
104 assert(class_opt.size()==reclass_opt.size());
105 for(int iclass=0;iclass<class_opt.size();++iclass){
106 codemapString[class_opt[iclass]]=reclass_opt[iclass];
107 codemap[string2type<double>(class_opt[iclass])]=string2type<double>(reclass_opt[iclass]);
108 }
109 }
110 assert(codemapString.size());
111 assert(codemap.size());
112 //if verbose true, print the codes to screen
113 if(verbose_opt[0]){
114 map<string,string>::iterator mit;
115 cout << codemapString.size() << " codes used: " << endl;
116 for(mit=codemapString.begin();mit!=codemapString.end();++mit)
117 cout << (*mit).first << " " << (*mit).second << endl;
118 }
119 bool refIsRaster=true;
120 // if(input_opt[0].find(".shp")!=string::npos){//shape file
121 if(!refIsRaster){
122 std::string errorString="Error: input must be raster dataset";
123 throw(errorString);
124 }
125 else{//image file
126 vector<Jim> maskReader(mask_opt.size());
127 for(int imask=0;imask<mask_opt.size();++imask){
128 if(verbose_opt[0])
129 cout << "opening mask image file " << mask_opt[imask] << endl;
130 maskReader[imask].open(mask_opt[imask]);
131 }
132 if(verbose_opt[0]){
133 cout << "data type: " << otype_opt[0] << endl;
134 }
135 GDALDataType theType=GDT_Unknown;
136 if(otype_opt.size()){
137 theType=string2GDAL(otype_opt[0]);
138 if(theType==GDT_Unknown)
139 std::cout << "Warning: unknown output pixel type: " << otype_opt[0] << ", using input type as default" << std::endl;
140 }
141 if(theType==GDT_Unknown){
142 theType=getGDALDataType();
143 if(verbose_opt[0])
144 cout << "Using data type from input image: " << GDALGetDataTypeName(theType) << endl;
145 }
146 if(verbose_opt[0])
147 cout << endl << "Output pixel type: " << GDALGetDataTypeName(theType) << endl;
148 imgWriter.open(nrOfCol(),nrOfRow(),nrOfBand(),theType);
149 for(int iband=0;iband<nrOfBand();++iband)
150 imgWriter.GDALSetNoDataValue(nodata_opt[0],iband);
151 // if(description_opt.size())
152 // imgWriter.setImageDescription(description_opt[0]);
153
154 if(colorTable_opt.size()){
155 if(colorTable_opt[0]!="none")
156 imgWriter.setColorTable(colorTable_opt[0]);
157 }
158 else if (getColorTable()!=NULL)//copy colorTable from input image
159 imgWriter.setColorTable(getColorTable());
160
161 //if input image is georeferenced, copy projection info to output image
162 if(isGeoRef()){
163 for(int imask=0;imask<mask_opt.size();++imask)
164 assert(maskReader[imask].isGeoRef());
165 }
166 imgWriter.copyGeoTransform(*this);
167 imgWriter.setProjection(getProjection());
168 double ulx,uly,lrx,lry;
169 getBoundingBox(ulx,uly,lrx,lry);
170 imgWriter.copyGeoTransform(*this);
171 assert(nodata_opt.size()==masknodata_opt.size());
172 if(verbose_opt[0]&&mask_opt.size()){
173 for(int iv=0;iv<masknodata_opt.size();++iv)
174 cout << masknodata_opt[iv] << "->" << nodata_opt[iv] << endl;
175 }
176
177 assert(imgWriter.nrOfCol()==nrOfCol());
178 // Vector2d<int> lineInput(nrOfBand(),nrOfCol());
179 Vector2d<double> lineInput(nrOfBand(),nrOfCol());
180 Vector2d<short> lineMask(mask_opt.size());
181 for(int imask=0;imask<mask_opt.size();++imask)
182 lineMask[imask].resize(maskReader[imask].nrOfCol());
183 Vector2d<double> lineOutput(imgWriter.nrOfBand(),imgWriter.nrOfCol());
184 unsigned int irow=0;
185 unsigned int icol=0;
186 const char* pszMessage;
187 void* pProgressArg=NULL;
188 GDALProgressFunc pfnProgress=GDALTermProgress;
189 double progress=0;
190 MyProgressFunc(progress,pszMessage,pProgressArg);
191 double oldRowMask=-1;
192 for(irow=0;irow<nrOfRow();++irow){
193 //read line in lineInput buffer
194 for(unsigned int iband=0;iband<nrOfBand();++iband){
195 try{
196 // readData(lineInput[iband],GDT_Int32,irow,iband);
197 readData(lineInput[iband],irow,iband);
198 }
199 catch(string errorstring){
200 cerr << errorstring << endl;
201 throw;
202 }
203 }
204 double x,y;//geo coordinates
205 double colMask,rowMask;//image coordinates in mask image
206 for(icol=0;icol<nrOfCol();++icol){
207 bool masked=false;
208 if(mask_opt.size()>1){//multiple masks
209 for(int imask=0;imask<mask_opt.size();++imask){
210 image2geo(icol,irow,x,y);
211 maskReader[imask].geo2image(x,y,colMask,rowMask);
212 if(static_cast<unsigned int>(rowMask)!=static_cast<unsigned int>(oldRowMask)){
213 assert(rowMask>=0&&rowMask<maskReader[imask].nrOfRow());
214 try{
215 maskReader[imask].readData(lineMask[imask],static_cast<unsigned int>(rowMask));
216 }
217 catch(string errorstring){
218 cerr << errorstring << endl;
219 throw;
220 }
221 oldRowMask=rowMask;
222 }
223 short ivalue=0;
224 if(mask_opt.size()==masknodata_opt.size())//one invalid value for each mask
225 ivalue=masknodata_opt[imask];
226 else//use same invalid value for each mask
227 ivalue=masknodata_opt[0];
228 if(lineMask[imask][colMask]==ivalue){
229 for(unsigned int iband=0;iband<nrOfBand();++iband)
230 lineInput[iband][icol]=nodata_opt[imask];
231 masked=true;
232 break;
233 }
234 }
235 }
236 else if(mask_opt.size()){//potentially more invalid values for single mask
237 image2geo(icol,irow,x,y);
238 maskReader[0].geo2image(x,y,colMask,rowMask);
239 if(static_cast<unsigned int>(rowMask)!=static_cast<unsigned int>(oldRowMask)){
240 assert(rowMask>=0&&rowMask<maskReader[0].nrOfRow());
241 try{
242 maskReader[0].readData(lineMask[0],static_cast<unsigned int>(rowMask));
243 }
244 catch(string errorstring){
245 cerr << errorstring << endl;
246 throw;
247 }
248 oldRowMask=rowMask;
249 }
250 for(int ivalue=0;ivalue<masknodata_opt.size();++ivalue){
251 assert(masknodata_opt.size()==nodata_opt.size());
252 if(lineMask[0][colMask]==masknodata_opt[ivalue]){
253 for(int iband=0;iband<nrOfBand();++iband)
254 lineInput[iband][icol]=nodata_opt[ivalue];
255 masked=true;
256 break;
257 }
258 }
259 }
260 for(unsigned int iband=0;iband<lineOutput.size();++iband){
261 lineOutput[iband][icol]=lineInput[iband][icol];
262 if(find(band_opt.begin(),band_opt.end(),iband)!=band_opt.end()){
263 if(!masked && codemap.find(lineInput[iband][icol])!=codemap.end()){
264 double toValue=codemap[lineInput[iband][icol]];
265 lineOutput[iband][icol]=toValue;
266 }
267 }
268 }
269 }
270 //write buffer lineOutput to output file
271 try{
272 for(unsigned int iband=0;iband<imgWriter.nrOfBand();++iband)
273 imgWriter.writeData(lineOutput[iband],irow,iband);
274 }
275 catch(string errorstring){
276 cerr << errorstring << endl;
277 throw;
278 }
279 //progress bar
280 progress=static_cast<float>((irow+1.0)/imgWriter.nrOfRow());
281 MyProgressFunc(progress,pszMessage,pProgressArg);
282 }
283 }
284 }
285 catch(string predefinedString){
286 std::cout << predefinedString << std::endl;
287 throw;
288 }
289 }
290
291 void Jim::d_reclass(app::AppFactory& app){
292 Optionjl<string> class_opt("c", "class", "list of classes to reclass (in combination with reclass option)");
293 Optionjl<string> reclass_opt("r", "reclass", "list of recoded classes (in combination with class option)");
294 Optionjl<short> verbose_opt("v", "verbose", "verbose", 0);
295
296 bool doProcess;//stop process when program was invoked with help option (-h --help)
297 try{
298 doProcess=class_opt.retrieveOption(app);
299 reclass_opt.retrieveOption(app);
300 verbose_opt.retrieveOption(app);
301
302 if(!doProcess){
303 cout << endl;
304 std::ostringstream helpStream;
305 helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
306 throw(helpStream.str());//help was invoked, stop processing
307 }
308
309 std::vector<std::string> badKeys;
310 app.badKeys(badKeys);
311 if(badKeys.size()){
312 std::ostringstream errorStream;
313 if(badKeys.size()>1)
314 errorStream << "Error: unknown keys: ";
315 else
316 errorStream << "Error: unknown key: ";
317 for(int ikey=0;ikey<badKeys.size();++ikey){
318 errorStream << badKeys[ikey] << " ";
319 }
320 errorStream << std::endl;
321 throw(errorStream.str());
322 }
323
324 if(class_opt.size()!=reclass_opt.size()){
325 std::ostringstream errorStream;
326 errorStream << "Error: size of class and reclass arguments are not equal" << std::endl;
327 throw(errorStream.str());
328 }
329 map<string,string> codemapString;//map with codes: codemapString[theKey(from)]=theValue(to)
330 map<double,double> codemap;//map with codes: codemap[theKey(from)]=theValue(to)
331 for(int iclass=0;iclass<class_opt.size();++iclass){
332 codemapString[class_opt[iclass]]=reclass_opt[iclass];
333 codemap[string2type<double>(class_opt[iclass])]=string2type<double>(reclass_opt[iclass]);
334 }
335 //if verbose true, print the codes to screen
336 if(verbose_opt[0]){
337 map<string,string>::iterator mit;
338 cout << codemapString.size() << " codes used: " << endl;
339 for(mit=codemapString.begin();mit!=codemapString.end();++mit)
340 cout << (*mit).first << " " << (*mit).second << endl;
341 }
342 for(int iband=0;iband<nrOfBand();++iband){
343 #if JIPLIB_PROCESS_IN_PARALLEL == 1
344 #pragma omp parallel for
345 #else
346 #endif
347 for(int irow=0;irow<nrOfRow();++irow){
348 for(int icol=0;icol<nrOfCol();++icol){
349 double value=readData(icol,irow,iband);
350 if(codemap.find(value)!=codemap.end())
351 value=codemap[value];
352 writeData(value,icol,irow,iband);
353 }
354 }
355 }
356 }
357 catch(string predefinedString){
358 std::cout << predefinedString << std::endl;
359 throw;
360 }
361 }