pktools 2.6.7
Processing Kernel for geospatial data
pkascii2img.cc
1/**********************************************************************
2pkascii2img.cc: program to create raster image based on ascii file
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 <string>
21#include <fstream>
22#include "base/Optionpk.h"
23#include <assert.h>
24#include "imageclasses/ImgWriterGdal.h"
25
26/******************************************************************************/
72using namespace std;
73
74int main(int argc, char *argv[])
75{
76 Optionpk<std::string> input_opt("i","input","input ASCII file");
77 Optionpk<string> output_opt("o", "output", "Output image file");
78 Optionpk<string> dataType_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","Byte");
79 Optionpk<string> imageType_opt("of", "oformat", "image type string (see also gdal_translate)", "GTiff");
80 Optionpk<string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified.");
81 Optionpk<double> ulx_opt("ulx", "ulx", "Upper left x value bounding box (in geocoordinates if georef is true)", 0.0);
82 Optionpk<double> uly_opt("uly", "uly", "Upper left y value bounding box (in geocoordinates if georef is true)", 0.0);
83 Optionpk<double> dx_opt("dx", "dx", "Output resolution in x (in meter)");
84 Optionpk<double> dy_opt("dy", "dy", "Output resolution in y (in meter)");
85 Optionpk<string> projection_opt("a_srs", "a_srs", "Override the projection for the output file");
86 Optionpk<string> colorTable_opt("ct", "ct", "color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid)");
87 Optionpk<string> description_opt("d", "description", "Set image description");
88 Optionpk<bool> verbose_opt("v", "verbose", "verbose", false,2);
89
90 bool doProcess;//stop process when program was invoked with help option (-h --help)
91 try{
92 doProcess=input_opt.retrieveOption(argc,argv);
93 output_opt.retrieveOption(argc,argv);
94 dataType_opt.retrieveOption(argc,argv);
95 imageType_opt.retrieveOption(argc,argv);
96 option_opt.retrieveOption(argc,argv);
97 ulx_opt.retrieveOption(argc,argv);
98 uly_opt.retrieveOption(argc,argv);
99 dx_opt.retrieveOption(argc,argv);
100 dy_opt.retrieveOption(argc,argv);
101 colorTable_opt.retrieveOption(argc,argv);
102 projection_opt.retrieveOption(argc,argv);
103 description_opt.retrieveOption(argc,argv);
104 verbose_opt.retrieveOption(argc,argv);
105 }
106 catch(string predefinedString){
107 std::cout << predefinedString << std::endl;
108 exit(0);
109 }
110 if(!doProcess){
111 cout << endl;
112 cout << "Usage: pkascii2img -i input.txt -o output" << endl;
113 cout << endl;
114 std::cout << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
115 exit(0);//help was invoked, stop processing
116 }
117
118 assert(input_opt.size());
119 assert(output_opt.size());
120 ImgWriterGdal imgWriter;
121 ifstream ifile(input_opt[0].c_str(),ios::in);
122 //get number of lines
123 string line;
124 int nrow=0;
125 int ncol=0;
126 int irow=0;
127 string interleave="BAND";
128 vector< vector<double> > data;
129 vector<double> row;
130 double value;
131 try{
132 while(getline(ifile,line)){
133 row.clear();
134 //read data from ascii file
135 istringstream ist(line);
136 while(ist>>value)
137 row.push_back(value);
138 if(!ncol){
139 ncol=row.size();
140 data.push_back(row);
141 }
142 else
143 data.push_back(row);
144 if(verbose_opt[0]){
145 for(int icol=0;icol<row.size();++icol)
146 cout << row[icol] << " ";
147 cout << endl;
148 }
149 ++irow;
150 }
151 nrow=irow;
152 assert(ncol);
153 assert(nrow);
154 if(verbose_opt[0]){
155 cout << "nrow: " << nrow << endl;
156 cout << "ncol: " << ncol << endl;
157 }
158 }
159 catch(string theError){
160 cout << theError << endl;
161 }
162
163 GDALDataType dataType=GDT_Unknown;
164 if(verbose_opt[0])
165 cout << "possible output data types: ";
166 for(int iType = 0; iType < GDT_TypeCount; ++iType){
167 if(verbose_opt[0])
168 cout << " " << GDALGetDataTypeName((GDALDataType)iType);
169 if( GDALGetDataTypeName((GDALDataType)iType) != NULL
170 && EQUAL(GDALGetDataTypeName((GDALDataType)iType),
171 dataType_opt[0].c_str()))
172 dataType=(GDALDataType) iType;
173 }
174 if(verbose_opt[0])
175 cout << endl;
176 if(verbose_opt[0]){
177 if(dataType==GDT_Unknown)
178 cout << "Unknown output pixel type: " << dataType_opt[0] << endl;
179 else
180 cout << "Output pixel type: " << GDALGetDataTypeName(dataType) << endl;
181 }
182
183 imgWriter.open(output_opt[0],ncol,nrow,1,dataType,imageType_opt[0],option_opt);
184 if(description_opt.size())
185 imgWriter.setImageDescription(description_opt[0]);
186 if(projection_opt.size()){
187 assert(dx_opt.size());
188 assert(dy_opt.size());
189 if(verbose_opt[0])
190 cout << output_opt[0] << " is georeferenced." << endl;
191 double gt[6];
192 gt[0]=ulx_opt[0];
193 gt[1]=dx_opt[0];
194 gt[2]=0;
195 gt[3]=uly_opt[0];
196 gt[4]=0;
197 gt[5]=-dy_opt[0];
198 imgWriter.setGeoTransform(gt);
199 imgWriter.setProjectionProj4(projection_opt[0]);
200 }
201 else{
202 if(verbose_opt[0])
203 cout << output_opt[0] << " is not georeferenced." << endl;
204 assert(!imgWriter.isGeoRef());
205 }
206 if(colorTable_opt.size()){
207 assert(imgWriter.getDataType()==GDT_Byte);
208 imgWriter.setColorTable(colorTable_opt[0]);
209 }
210 assert(data.size()==nrow);
211 for(irow=0;irow<nrow;++irow)
212 imgWriter.writeData(data[irow],irow);
213 imgWriter.close();
214}
215
bool isGeoRef() const
Is this dataset georeferenced (pixel size in y must be negative) ?
CPLErr setGeoTransform(double *gt)
Set the geotransform data for this dataset.
CPLErr setProjectionProj4(const std::string &projection)
Set the projection for this dataset from user input (supports epsg:<number> format)
GDALDataType getDataType(int band=0) const
Get the GDAL datatype for this dataset.
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)
void setImageDescription(const std::string &imageDescription)
Set the image description (only for GeoTiff format: TIFFTAG_IMAGEDESCRIPTION)
Definition: ImgWriterGdal.h:55
bool writeData(T &value, int col, int row, int band=0)
Write a single pixel cell value at a specific column and row for a specific band (all indices start c...
Definition: ImgWriterGdal.h:96