27 #include "ImgWriterGdal.h" 63 void ImgWriterGdal::open(
const std::string& filename,
int ncol,
int nrow,
int nband,
const GDALDataType& dataType,
const std::string& imageType,
const std::vector<std::string>& options)
77 char **papszOptions=NULL;
78 for(std::vector<std::string>::const_iterator optionIt=m_options.begin();optionIt!=m_options.end();++optionIt)
79 papszOptions=CSLAddString(papszOptions,optionIt->c_str());
81 CSLDestroy(papszOptions);
92 if( poDriver == NULL ){
93 std::string errorString=
"FileOpenError";
97 papszMetadata = poDriver->GetMetadata();
99 assert( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATE, FALSE ));
100 char **papszOptions=NULL;
101 for(std::vector<std::string>::const_iterator optionIt=m_options.begin();optionIt!=m_options.end();++optionIt)
102 papszOptions=CSLAddString(papszOptions,optionIt->c_str());
108 std::cerr <<
"Warning: could not write geotransform information in " <<
m_filename << std::endl;
111 std::cerr <<
"Warning: could not write projection information in " <<
m_filename << std::endl;
114 for(
int iband=0;iband<
nrOfBand();++iband)
120 std::string versionString=
"pktools ";
121 versionString+=VERSION;
122 versionString+=
" by Pieter Kempeneers";
123 m_gds->SetMetadataItem(
"TIFFTAG_SOFTWARE", versionString.c_str());
127 time_t tim=time(NULL);
128 tm *now=localtime(&tim);
129 std::ostringstream datestream;
131 datestream << now->tm_year+1900;
133 datestream <<
":0" << now->tm_mon+1;
135 datestream <<
":" << now->tm_mon+1;
137 datestream <<
":0" << now->tm_mday;
139 datestream <<
":" << now->tm_mday;
141 datestream <<
" 0" << now->tm_hour;
143 datestream <<
" " << now->tm_hour;
145 datestream <<
":0" << now->tm_min;
147 datestream <<
":" << now->tm_min;
149 datestream <<
":0" << now->tm_sec;
151 datestream <<
":" << now->tm_sec;
152 m_gds->SetMetadataItem(
"TIFFTAG_DATETIME", datestream.str().c_str());
164 GDALDriver *poDriver;
165 poDriver = GetGDALDriverManager()->GetDriverByName(imageType.c_str());
166 if( poDriver == NULL ){
167 std::ostringstream s;
168 s <<
"FileOpenError (" << imageType <<
")";
171 char **papszMetadata;
172 papszMetadata = poDriver->GetMetadata();
174 assert( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATE, FALSE ));
175 char **papszOptions=NULL;
176 for(std::vector<std::string>::const_iterator optionIt=m_options.begin();optionIt!=m_options.end();++optionIt)
177 papszOptions=CSLAddString(papszOptions,optionIt->c_str());
182 std::cerr <<
"Warning: could not write geotransform information in " <<
m_filename << std::endl;
184 std::cerr <<
"Warning: could not write projection information in " <<
m_filename << std::endl;
186 std::string versionString=
"pktools ";
187 versionString+=VERSION;
188 versionString+=
" by Pieter Kempeneers";
189 m_gds->SetMetadataItem(
"TIFFTAG_SOFTWARE", versionString.c_str());
193 time_t tim=time(NULL);
194 tm *now=localtime(&tim);
195 std::ostringstream datestream;
197 datestream << now->tm_year+1900;
199 datestream <<
":0" << now->tm_mon+1;
201 datestream <<
":" << now->tm_mon+1;
203 datestream <<
":0" << now->tm_mday;
205 datestream <<
":" << now->tm_mday;
207 datestream <<
" 0" << now->tm_hour;
209 datestream <<
" " << now->tm_hour;
211 datestream <<
":0" << now->tm_min;
213 datestream <<
":" << now->tm_min;
215 datestream <<
":0" << now->tm_sec;
217 datestream <<
":" << now->tm_sec;
218 m_gds->SetMetadataItem(
"TIFFTAG_DATETIME", datestream.str().c_str());
227 m_gds->SetMetadata(metadata);
257 std::ifstream ftable(filename.c_str(),std::ios::in);
260 GDALColorTable colorTable;
262 while(getline(ftable,line)){
264 std::istringstream ist(line);
265 GDALColorEntry sEntry;
267 ist >>
id >> sEntry.c1 >> sEntry.c2 >> sEntry.c3 >> sEntry.c4;
268 colorTable.SetColorEntry(
id,&sEntry);
271 (
m_gds->GetRasterBand(band+1))->SetColorTable(&colorTable);
281 (
m_gds->GetRasterBand(band+1))->SetColorTable(colorTable);
315 std::vector<int> bands;
316 if(burnValues.empty()&&controlOptions.empty()){
317 std::string errorString=
"Error: either burn values or control options must be provided";
320 for(
int iband=0;iband<
nrOfBand();++iband)
321 bands.push_back(iband+1);
322 std::vector<OGRLayerH> layers;
325 std::vector<double> burnBands;
326 std::vector<double> burnLayers;
327 if(burnValues.size()){
328 burnBands=burnValues;
330 burnBands.push_back(burnValues[0]);
332 for(
int ilayer=0;ilayer<ogrReader.getLayerCount();++ilayer){
333 std::string currentLayername=ogrReader.getLayer(ilayer)->GetName();
334 if(layernames.size())
335 if(find(layernames.begin(),layernames.end(),currentLayername)==layernames.end())
337 std::cout <<
"processing layer " << currentLayername << std::endl;
338 layers.push_back((OGRLayerH)ogrReader.getLayer(ilayer));
340 if(burnValues.size()){
341 for(
int iband=0;iband<
nrOfBand();++iband)
342 burnLayers.insert(burnLayers.end(),burnBands.begin(),burnBands.end());
346 GDALProgressFunc pfnProgress=NULL;
347 void* pProgressArg=NULL;
349 char **coptions=NULL;
350 for(std::vector<std::string>::const_iterator optionIt=controlOptions.begin();optionIt!=controlOptions.end();++optionIt)
351 coptions=CSLAddString(coptions,optionIt->c_str());
353 if(controlOptions.size()){
354 if(GDALRasterizeLayers( (GDALDatasetH)
m_gds,
nrOfBand(),&(bands[0]),layers.size(),&(layers[0]),NULL,pTransformArg,NULL,coptions,pfnProgress,pProgressArg)!=CE_None){
355 std::string errorString(CPLGetLastErrorMsg());
359 else if(burnValues.size()){
360 if(GDALRasterizeLayers( (GDALDatasetH)
m_gds,
nrOfBand(),&(bands[0]),layers.size(),&(layers[0]),NULL,pTransformArg,&(burnLayers[0]),NULL,pfnProgress,pProgressArg)!=CE_None){
361 std::string errorString(CPLGetLastErrorMsg());
366 std::string errorString=
"Error: either attribute fieldname or burn values must be set to rasterize vector dataset";
GDALDataType m_dataType
GDAL data type for this dataset.
int m_nband
number of bands in this dataset
void rasterizeOgr(ImgReaderOgr &ogrReader, const std::vector< double > &burnValues, const std::vector< std::string > &controlOptions=std::vector< std::string >(), const std::vector< std::string > &layernames=std::vector< std::string >())
Rasterize an OGR vector dataset using the gdal algorithm "GDALRasterizeLayers".
void setMetadata(char **metadata)
Set specific metadata (driver specific)
int nrOfCol(void) const
Get the number of columns of this dataset.
std::string m_filename
filename of this dataset
std::vector< double > m_noDataValues
no data values for this dataset
virtual void setCodec(const std::string &imageType)
Register GDAL driver, setting the datatype, imagetype and some metadata.
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)
GDALDataset * m_gds
instance of the GDAL dataset of this dataset
std::string getDriverDescription() const
Get the GDAL driver description of this dataset.
int nrOfRow(void) const
Get the number of rows of this dataset.
GDALColorTable * getColorTable(int band=0) const
Get the GDAL color table for this dataset as an instance of the GDALColorTable class.
CPLErr setGeoTransform(double *gt)
Set the geotransform data for this dataset.
int m_nrow
number of rows in this dataset
char ** getMetadata()
Get the metadata of this dataset.
int m_ncol
number of columns in this dataset
virtual void close(void)
Close the image.
ImgWriterGdal(void)
default constructor. Image needs to be opened later with one of the open methods. ...
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...
std::string getProjection(void) const
Get the projection string (deprecated, use getProjectionRef instead)
GDALDataType getDataType(int band=0) const
Get the GDAL datatype for this dataset.
CPLErr setProjection(const std::string &projection)
Set the projection for this dataset in well known text (wkt) format.
void close(void)
Close the image.
std::string getGeoTransform() const
Get the geotransform data for this dataset as a string.
CPLErr GDALSetNoDataValue(double noDataValue, int band=0)
Set the GDAL (internal) no data value for this data set. Only a single no data value per band is supp...
int nrOfBand(void) const
Get the number of bands of this dataset.
~ImgWriterGdal(void)
destructor