| 1 | /********************************************************************** |
| 2 | jldumpimg_lib.cc: dump image on screen or ASCII file |
| 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 <string> |
| 22 | #include <iostream> |
| 23 | #include <memory> |
| 24 | #include "imageclasses/Jim.h" |
| 25 | #include "base/Optionjl.h" |
| 26 | #include "apps/AppFactory.h" |
| 27 | |
| 28 | using namespace std; |
| 29 | using namespace app; |
| 30 | |
| 31 | /** |
| 32 | * @param imgWriter output raster dumpimg dataset |
| 33 | * @return CE_None if successful, CE_Failure if failed |
| 34 | **/ |
| 35 | CPLErr Jim::dumpImg(app::AppFactory& app){ |
| 36 | |
| 37 | Optionjl<string> output_opt("o", "output", "Output ascii file (Default is empty: dump to standard output)"); |
| 38 | Optionjl<string> oformat_opt("of", "oformat", "Output format: matrix or list (x,y,z) form. Default is matrix", "matrix"); |
| 39 | Optionjl<bool> geo_opt("geo", "geo", "Dump x and y in spatial reference system of raster dataset (for list form only)", false); |
| 40 | Optionjl<int> band_opt("b", "band", "Band index to crop"); |
| 41 | // Optionjl<short> dstnodata_opt("dstnodata", "dstnodata", "nodata value for output if out of bounds.", 0); |
| 42 | Optionjl<double> srcnodata_opt("srcnodata", "srcnodata", "Do not dump these no data values if oformat is in list form"); |
| 43 | Optionjl<bool> force_opt("f", "force", "Force full dump even for large images (above 100 rows and cols)", false); |
| 44 | Optionjl<short> verbose_opt("v", "verbose", "verbose", 0,2); |
| 45 | |
| 46 | srcnodata_opt.setHide(1); |
| 47 | // dstnodata_opt.setHide(1); |
| 48 | |
| 49 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 50 | try{ |
| 51 | doProcess=output_opt.retrieveOption(app); |
| 52 | oformat_opt.retrieveOption(app); |
| 53 | geo_opt.retrieveOption(app); |
| 54 | band_opt.retrieveOption(app); |
| 55 | srcnodata_opt.retrieveOption(app); |
| 56 | // dstnodata_opt.retrieveOption(app); |
| 57 | force_opt.retrieveOption(app); |
| 58 | verbose_opt.retrieveOption(app); |
| 59 | } |
| 60 | catch(string predefinedString){ |
| 61 | std::cout << predefinedString << std::endl; |
| 62 | throw; |
| 63 | } |
| 64 | if(!doProcess){ |
| 65 | cout << endl; |
| 66 | std::ostringstream helpStream; |
| 67 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 68 | throw(helpStream.str());//help was invoked, stop processing |
| 69 | } |
| 70 | ofstream outputStream; |
| 71 | if(output_opt.size()) |
| 72 | outputStream.open(output_opt[0].c_str()); |
| 73 | |
| 74 | for(int inodata=0;inodata<srcnodata_opt.size();++inodata) |
| 75 | pushNoDataValue(srcnodata_opt[inodata]); |
| 76 | |
| 77 | if(band_opt.empty()){ |
| 78 | for(int iband=0;iband<nrOfBand();++iband) |
| 79 | band_opt.push_back(iband); |
| 80 | } |
| 81 | std::vector<double> readBuffer(nrOfCol()); |
| 82 | for(int iband=0;iband<band_opt.size();++iband){ |
| 83 | assert(band_opt[iband]>=0); |
| 84 | assert(band_opt[iband]<nrOfBand()); |
| 85 | for(int irow=0;irow<nrOfRow();++irow){ |
| 86 | if(!force_opt[0]){ |
| 87 | if(irow>10&&irow<nrOfRow()-10) |
| 88 | continue; |
| 89 | else if(irow==10){ |
| 90 | std::cout << "..." << std::endl; |
| 91 | continue; |
| 92 | } |
| 93 | } |
| 94 | readData(readBuffer,irow,band_opt[iband]); |
| 95 | for(int icol=0;icol<nrOfCol();++icol){ |
| 96 | if(!force_opt[0]){ |
| 97 | if(icol>10&&icol<nrOfCol()-10) |
| 98 | continue; |
| 99 | else if(icol==10){ |
| 100 | std::cout << " ... "; |
| 101 | continue; |
| 102 | } |
| 103 | } |
| 104 | if(oformat_opt[0]=="matrix"){ |
| 105 | if(output_opt.empty()) |
| 106 | std::cout << readBuffer[icol] << " "; |
| 107 | else |
| 108 | outputStream << readBuffer[icol] << " "; |
| 109 | } |
| 110 | else if(!isNoData(readBuffer[icol])){ |
| 111 | if(geo_opt[0]){ |
| 112 | double ix,iy; |
| 113 | image2geo(icol,irow,ix,iy); |
| 114 | if(output_opt.empty()) |
| 115 | std::cout << ix << " " << iy << " " << readBuffer[icol] << std::endl; |
| 116 | else |
| 117 | outputStream << ix << " " << iy << " " << readBuffer[icol] << std::endl; |
| 118 | } |
| 119 | else{ |
| 120 | if(output_opt.empty()) |
| 121 | std::cout << icol << " " << irow << " " << readBuffer[icol] << std::endl; |
| 122 | else |
| 123 | outputStream << icol << " " << irow << " " << readBuffer[icol] << std::endl; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | std::cout << std::endl; |
| 128 | } |
| 129 | } |
| 130 | if(oformat_opt[0]=="matrix"){ |
| 131 | if(output_opt.empty()) |
| 132 | std::cout << std::endl; |
| 133 | else |
| 134 | outputStream << std::endl; |
| 135 | } |
| 136 | if(!output_opt.empty()) |
| 137 | outputStream.close(); |
| 138 | return(CE_None); |
| 139 | } |