pktools  2.6.7
Processing Kernel for geospatial data
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ImgWriterGdal Class Reference

#include <ImgWriterGdal.h>

Inheritance diagram for ImgWriterGdal:
Inheritance graph
[legend]
Collaboration diagram for ImgWriterGdal:
Collaboration graph
[legend]

Public Member Functions

 ImgWriterGdal (void)
 default constructor. Image needs to be opened later with one of the open methods.
 
 ImgWriterGdal (const std::string &filename, const ImgReaderGdal &imgSrc, const std::vector< std::string > &options=std::vector< std::string >())
 constructor opening an image for writing, copying image attributes from a source image. Image is directly writen to file.
 
 ImgWriterGdal (const std::string &filename, int ncol, int nrow, int nband, const GDALDataType &dataType, const std::string &imageType, const std::vector< std::string > &options=std::vector< std::string >())
 constructor opening an image for writing, defining all image attributes. Image is directly written to file.
 
 ~ImgWriterGdal (void)
 destructor
 
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 file. More...
 
void open (const std::string &filename, int ncol, int nrow, int nband, const GDALDataType &dataType, const std::string &imageType, const std::vector< std::string > &options=std::vector< std::string >())
 Open an image for writing, defining all image attributes. Image is directly written to file. More...
 
void close (void)
 Close the image.
 
void setImageDescription (const std::string &imageDescription)
 Set the image description (only for GeoTiff format: TIFFTAG_IMAGEDESCRIPTION)
 
template<typename T >
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 counting from 0) More...
 
template<typename T >
bool writeData (std::vector< T > &buffer, int minCol, int maxCol, int row, int band=0)
 Write pixel cell values for a range of columns for a specific row and band (all indices start counting from 0) More...
 
template<typename T >
bool writeData (std::vector< T > &buffer, int row, int band=0)
 Write pixel cell values for an entire row for a specific band (all indices start counting from 0) More...
 
template<typename T >
bool writeDataBlock (Vector2d< T > &buffer2d, int minCol, int maxCol, int minRow, int maxRow, int band=0)
 Write pixel cell values for a range of columns and rows for a specific band (all indices start counting from 0). The buffer is a two dimensional vector (stl vector of stl vector) representing [row][col]. More...
 
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) More...
 
void setColorTable (GDALColorTable *colorTable, int band=0)
 Set the color table using the GDAL class GDALColorTable. More...
 
void setMetadata (char **metadata)
 Set specific metadata (driver specific) More...
 
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". More...
 
void rasterizeBuf (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 in memory using the gdal algorithm "GDALRasterizeLayersBuf".
 
- Public Member Functions inherited from ImgRasterGdal
 ImgRasterGdal (void)
 default constructor
 
virtual ~ImgRasterGdal (void)
 destructor
 
void setScale (double theScale, int band=0)
 Set scale for a specific band when writing the raster data values. The scaling and offset are applied on a per band basis. You need to set the scale for each band.
 
void setOffset (double theOffset, int band=0)
 Set offset for a specific band when writing the raster data values. The scaling and offset are applied on a per band basis. You need to set the offset for each band.
 
std::string getFileName () const
 Get the filename of this dataset.
 
int nrOfCol (void) const
 Get the number of columns of this dataset.
 
int nrOfRow (void) const
 Get the number of rows of this dataset.
 
int nrOfBand (void) const
 Get the number of bands of this dataset.
 
bool isGeoRef () const
 Is this dataset georeferenced (pixel size in y must be negative) ?
 
std::string getProjection (void) const
 Get the projection string (deprecated, use getProjectionRef instead) More...
 
std::string getProjectionRef (void) const
 Get the projection reference. More...
 
std::string getGeoTransform () const
 Get the geotransform data for this dataset as a string. More...
 
void getGeoTransform (double *gt) const
 Get the geotransform data for this dataset. More...
 
CPLErr setGeoTransform (double *gt)
 Set the geotransform data for this dataset. More...
 
void copyGeoTransform (const ImgRasterGdal &imgSrc)
 Copy geotransform information from another georeferenced image. More...
 
CPLErr setProjection (const std::string &projection)
 Set the projection for this dataset in well known text (wkt) format. More...
 
CPLErr setProjectionProj4 (const std::string &projection)
 Set the projection for this dataset from user input (supports epsg:<number> format) More...
 
bool getBoundingBox (double &ulx, double &uly, double &lrx, double &lry) const
 Get the bounding box of this dataset in georeferenced coordinates. More...
 
bool getCenterPos (double &x, double &y) const
 Get the center position of this dataset in georeferenced coordinates. More...
 
double getUlx () const
 Get the upper left corner x (georeferenced) coordinate of this dataset.
 
double getUly () const
 Get the upper left corner y (georeferenced) coordinate of this dataset.
 
double getLrx () const
 Get the lower right corner x (georeferenced) coordinate of this dataset.
 
double getLry () const
 Get the lower right corner y (georeferenced) coordinate of this dataset.
 
int getNoDataValues (std::vector< double > &noDataValues) const
 Get the no data values of this dataset as a standard template library (stl) vector. More...
 
bool isNoData (double value) const
 Check if value is nodata in this dataset.
 
int pushNoDataValue (double noDataValue)
 Push a no data value for this dataset. More...
 
int setNoData (const std::vector< double > nodata)
 Set the no data values of this dataset using a standard template library (stl) vector as input.
 
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 supported.
 
bool covers (double x, double y) const
 Check if a geolocation is covered by this dataset. Only the bounding box is checked, irrespective of no data values. More...
 
bool covers (double ulx, double uly, double lrx, double lry) const
 Check if a region of interest is (partially) covered by this dataset. Only the bounding box is checked, irrespective of no data values. More...
 
bool geo2image (double x, double y, double &i, double &j) const
 Convert georeferenced coordinates (x and y) to image coordinates (column and row) More...
 
bool image2geo (double i, double j, double &x, double &y) const
 Convert image coordinates (column and row) to georeferenced coordinates (x and y) More...
 
double getDeltaX (void) const
 Get the pixel cell spacing in x.
 
double getDeltaY (void) const
 Get the pixel cell spacing in y.
 
GDALDataType getDataType (int band=0) const
 Get the GDAL datatype for this dataset. More...
 
GDALRasterBand * getRasterBand (int band=0) const
 Get the GDAL rasterband for this dataset. More...
 
GDALColorTable * getColorTable (int band=0) const
 Get the GDAL color table for this dataset as an instance of the GDALColorTable class. More...
 
std::string getDriverDescription () const
 Get the GDAL driver description of this dataset. More...
 
std::string getImageType () const
 Get the image type (implemented as the driver description)
 
std::string getInterleave () const
 Get the band coding (interleave) More...
 
std::string getCompression () const
 Get the compression from the metadata of this dataset. More...
 
GDALDataset * getDataset ()
 
char ** getMetadata ()
 Get the metadata of this dataset. More...
 
char ** getMetadata () const
 Get the metadata of this dataset (const version) More...
 
void getMetadata (std::list< std::string > &metadata) const
 Get the metadata of this dataset in the form of a list of strings (const version) More...
 
std::string getDescription () const
 Get the image description from the driver of this dataset. More...
 
std::string getMetadataItem () const
 Get metadata item of this dataset. More...
 
std::string getImageDescription () const
 Get the image description from the metadata of this dataset. More...
 
int nrOfBlockX (int band=0)
 
int nrOfBlockY (int band=0)
 

Protected Member Functions

virtual void setCodec (const std::string &imageType)
 Register GDAL driver, setting the datatype, imagetype and some metadata. More...
 
virtual void setCodec (const ImgReaderGdal &ImgSrc)
 Register GDAL driver, setting the datatype, imagetype and some metadata. More...
 

Protected Attributes

std::vector< std::string > m_options
 
- Protected Attributes inherited from ImgRasterGdal
std::string m_filename
 filename of this dataset
 
GDALDataset * m_gds
 instance of the GDAL dataset of this dataset
 
int m_ncol
 number of columns in this dataset
 
int m_nrow
 number of rows in this dataset
 
int m_nband
 number of bands in this dataset
 
GDALDataType m_dataType
 GDAL data type for this dataset.
 
double m_gt [6]
 geotransform information of this dataset
 
std::string m_projection
 
std::vector< double > m_noDataValues
 no data values for this dataset
 
std::vector< double > m_scale
 Vector containing the scale factor to be applied (one scale value for each band)
 
std::vector< double > m_offset
 Vector containing the offset factor to be applied (one offset value for each band)
 

Detailed Description

Class to write a raster dataset in a format supported by GDAL. Currently only those formats where the drivers support the Create method can be written.

This class inherits from ImgRasterGdal, a general raster class to store e.g., filename, number of columns, rows and bands of the dataset.

Definition at line 37 of file ImgWriterGdal.h.

Member Function Documentation

◆ open() [1/2]

void ImgWriterGdal::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 file.

Parameters
filenameOpen a raster dataset with this filename
imgSrcUse this source image as a template to copy image attributes
optionsCreation options

Definition at line 43 of file ImgWriterGdal.cc.

44 {
45  m_ncol=imgSrc.nrOfCol();
46  m_nrow=imgSrc.nrOfRow();
47  m_nband=imgSrc.nrOfBand();
48  m_dataType=imgSrc.getDataType();
49  m_filename=filename;
50  m_options=options;
51  setCodec(imgSrc);
52 }
GDALDataType m_dataType
GDAL data type for this dataset.
int m_nband
number of bands in this dataset
int nrOfCol(void) const
Get the number of columns of this dataset.
Definition: ImgRasterGdal.h:98
std::string m_filename
filename of this dataset
virtual void setCodec(const std::string &imageType)
Register GDAL driver, setting the datatype, imagetype and some metadata.
int nrOfRow(void) const
Get the number of rows of this dataset.
int m_nrow
number of rows in this dataset
int m_ncol
number of columns in this dataset
GDALDataType getDataType(int band=0) const
Get the GDAL datatype for this dataset.
int nrOfBand(void) const
Get the number of bands of this dataset.

◆ open() [2/2]

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 = std::vector<std::string>() 
)

Open an image for writing, defining all image attributes. Image is directly written to file.

Parameters
filenameOpen a raster dataset with this filename
ncolNumber of columns in image
nrowNumber of rows in image
nbandNumber of bands in image
dataTypeThe data type of the image (one of the GDAL supported datatypes: GDT_Byte, GDT_[U]Int[16|32], GDT_Float[32|64])
imageTypeImage type. Currently only those formats where the drivers support the Create method can be written
optionsCreation options

Definition at line 63 of file ImgWriterGdal.cc.

64 {
65  m_ncol = ncol;
66  m_nrow = nrow;
67  m_nband = nband;
68  m_dataType = dataType;
69  m_filename = filename;
70  m_options=options;
71  setCodec(imageType);
72 }
GDALDataType m_dataType
GDAL data type for this dataset.
int m_nband
number of bands in this dataset
std::string m_filename
filename of this dataset
virtual void setCodec(const std::string &imageType)
Register GDAL driver, setting the datatype, imagetype and some metadata.
int m_nrow
number of rows in this dataset
int m_ncol
number of columns in this dataset

◆ rasterizeOgr()

void ImgWriterGdal::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".

Parameters
ogrReaderVector dataset as an instance of the ImgReaderOgr that must be rasterized
burnValuesValues to burn into raster cells (one value for each band)
controlOptionsspecial options controlling rasterization (ATTRIBUTE|CHUNKYSIZE|ALL_TOUCHED|BURN_VALUE_FROM|MERGE_ALG) "ATTRIBUTE": Identifies an attribute field on the features to be used for a burn in value. The value will be burned into all output bands. If specified, padfLayerBurnValues will not be used and can be a NULL pointer. "CHUNKYSIZE": The height in lines of the chunk to operate on. The larger the chunk size the less times we need to make a pass through all the shapes. If it is not set or set to zero the default chunk size will be used. Default size will be estimated based on the GDAL cache buffer size using formula: cache_size_bytes/scanline_size_bytes, so the chunk will not exceed the cache. "ALL_TOUCHED": May be set to TRUE to set all pixels touched by the line or polygons, not just those whose center is within the polygon or that are selected by brezenhams line algorithm. Defaults to FALSE. "BURN_VALUE_ May be set to "Z" to use the Z values of the geometries. The value from padfLayerBurnValues or the attribute field value is added to this before burning. In default case dfBurnValue is burned as it is. This is implemented properly only for points and lines for now. Polygons will be burned using the Z value from the first point. The M value may be supported in the future. "MERGE_ALG": May be REPLACE (the default) or ADD. REPLACE results in overwriting of value, while ADD adds the new value to the existing raster, suitable for heatmaps for instance.
layernamesNames of the vector dataset layers to process. Leave empty to process all layers

Definition at line 314 of file ImgWriterGdal.cc.

314  {
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";
318  throw(errorString);
319  }
320  for(int iband=0;iband<nrOfBand();++iband)
321  bands.push_back(iband+1);
322  std::vector<OGRLayerH> layers;
323  int nlayer=0;
324 
325  std::vector<double> burnBands;//burn values for all bands in a single layer
326  std::vector<double> burnLayers;//burn values for all bands and all layers
327  if(burnValues.size()){
328  burnBands=burnValues;
329  while(burnBands.size()<nrOfBand())
330  burnBands.push_back(burnValues[0]);
331  }
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())
336  continue;
337  std::cout << "processing layer " << currentLayername << std::endl;
338  layers.push_back((OGRLayerH)ogrReader.getLayer(ilayer));
339  ++nlayer;
340  if(burnValues.size()){
341  for(int iband=0;iband<nrOfBand();++iband)
342  burnLayers.insert(burnLayers.end(),burnBands.begin(),burnBands.end());
343  }
344  }
345  void* pTransformArg;
346  GDALProgressFunc pfnProgress=NULL;
347  void* pProgressArg=NULL;
348 
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());
352 
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());
356  throw(errorString);
357  }
358  }
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());
362  throw(errorString);
363  }
364  }
365  else{
366  std::string errorString="Error: either attribute fieldname or burn values must be set to rasterize vector dataset";
367  throw(errorString);
368  }
369 }
GDALDataset * m_gds
instance of the GDAL dataset of this dataset
int nrOfBand(void) const
Get the number of bands of this dataset.

◆ setCodec() [1/2]

void ImgWriterGdal::setCodec ( const std::string &  imageType)
protectedvirtual

Register GDAL driver, setting the datatype, imagetype and some metadata.

Parameters
dataTypeThe data type of the image (one of the GDAL supported datatypes: GDT_Byte, GDT_[U]Int[16|32], GDT_Float[32|64])
imageTypeImage type. Currently only those formats where the drivers support the Create method can be written

Definition at line 161 of file ImgWriterGdal.cc.

162 {
163  GDALAllRegister();
164  GDALDriver *poDriver;
165  poDriver = GetGDALDriverManager()->GetDriverByName(imageType.c_str());
166  if( poDriver == NULL ){
167  std::ostringstream s;
168  s << "FileOpenError (" << imageType << ")";
169  throw(s.str());
170  }
171  char **papszMetadata;
172  papszMetadata = poDriver->GetMetadata();
173  //todo: try and catch if CREATE is not supported (as in PNG)
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());
178  m_gds=poDriver->Create(m_filename.c_str(),m_ncol,m_nrow,m_nband,m_dataType,papszOptions);
179  double gt[6];
180  getGeoTransform(gt);
181  if(setGeoTransform(gt)!=CE_None)
182  std::cerr << "Warning: could not write geotransform information in " << m_filename << std::endl;
183  if(setProjection(m_projection)!=CE_None)
184  std::cerr << "Warning: could not write projection information in " << m_filename << std::endl;
185  m_gds->SetMetadataItem( "TIFFTAG_DOCUMENTNAME", m_filename.c_str());
186  std::string versionString="pktools ";
187  versionString+=VERSION;
188  versionString+=" by Pieter Kempeneers";
189  m_gds->SetMetadataItem( "TIFFTAG_SOFTWARE", versionString.c_str());
190  time_t rawtime;
191  time ( &rawtime );
192 
193  time_t tim=time(NULL);
194  tm *now=localtime(&tim);
195  std::ostringstream datestream;
196  //date std::string must be 20 characters long...
197  datestream << now->tm_year+1900;
198  if(now->tm_mon+1<10)
199  datestream << ":0" << now->tm_mon+1;
200  else
201  datestream << ":" << now->tm_mon+1;
202  if(now->tm_mday<10)
203  datestream << ":0" << now->tm_mday;
204  else
205  datestream << ":" << now->tm_mday;
206  if(now->tm_hour<10)
207  datestream << " 0" << now->tm_hour;
208  else
209  datestream << " " << now->tm_hour;
210  if(now->tm_min<10)
211  datestream << ":0" << now->tm_min;
212  else
213  datestream << ":" << now->tm_min;
214  if(now->tm_sec<10)
215  datestream << ":0" << now->tm_sec;
216  else
217  datestream << ":" << now->tm_sec;
218  m_gds->SetMetadataItem( "TIFFTAG_DATETIME", datestream.str().c_str());
219 }
GDALDataType m_dataType
GDAL data type for this dataset.
int m_nband
number of bands in this dataset
std::string m_filename
filename of this dataset
GDALDataset * m_gds
instance of the GDAL dataset of this dataset
CPLErr setGeoTransform(double *gt)
Set the geotransform data for this dataset.
int m_nrow
number of rows in this dataset
int m_ncol
number of columns in this dataset
CPLErr setProjection(const std::string &projection)
Set the projection for this dataset in well known text (wkt) format.
std::string getGeoTransform() const
Get the geotransform data for this dataset as a string.

◆ setCodec() [2/2]

void ImgWriterGdal::setCodec ( const ImgReaderGdal imgSrc)
protectedvirtual

Register GDAL driver, setting the datatype, imagetype and some metadata.

Parameters
imgSrcUse this source image as a template to copy image attributes

Definition at line 88 of file ImgWriterGdal.cc.

88  {
89  GDALAllRegister();
90  GDALDriver *poDriver;
91  poDriver = GetGDALDriverManager()->GetDriverByName(imgSrc.getDriverDescription().c_str());
92  if( poDriver == NULL ){
93  std::string errorString="FileOpenError";
94  throw(errorString);
95  }
96  char **papszMetadata;
97  papszMetadata = poDriver->GetMetadata();
98  //todo: try and catch if CREATE is not supported (as in PNG)
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());
103 
104  m_gds=poDriver->Create(m_filename.c_str(),m_ncol,m_nrow,m_nband,imgSrc.getDataType(),papszOptions);
105  double gt[6];
106  imgSrc.getGeoTransform(gt);
107  if(setGeoTransform(gt)!=CE_None)
108  std::cerr << "Warning: could not write geotransform information in " << m_filename << std::endl;
109  setProjection(imgSrc.getProjection());
110  if(setProjection(imgSrc.getProjection())!=CE_None)
111  std::cerr << "Warning: could not write projection information in " << m_filename << std::endl;
112 
113  if(m_noDataValues.size()){
114  for(int iband=0;iband<nrOfBand();++iband)
116  }
117 
118  m_gds->SetMetadata(imgSrc.getMetadata() );
119  m_gds->SetMetadataItem( "TIFFTAG_DOCUMENTNAME", m_filename.c_str());
120  std::string versionString="pktools ";
121  versionString+=VERSION;
122  versionString+=" by Pieter Kempeneers";
123  m_gds->SetMetadataItem( "TIFFTAG_SOFTWARE", versionString.c_str());
124  time_t rawtime;
125  time ( &rawtime );
126 
127  time_t tim=time(NULL);
128  tm *now=localtime(&tim);
129  std::ostringstream datestream;
130  //date std::string must be 20 characters long...
131  datestream << now->tm_year+1900;
132  if(now->tm_mon+1<10)
133  datestream << ":0" << now->tm_mon+1;
134  else
135  datestream << ":" << now->tm_mon+1;
136  if(now->tm_mday<10)
137  datestream << ":0" << now->tm_mday;
138  else
139  datestream << ":" << now->tm_mday;
140  if(now->tm_hour<10)
141  datestream << " 0" << now->tm_hour;
142  else
143  datestream << " " << now->tm_hour;
144  if(now->tm_min<10)
145  datestream << ":0" << now->tm_min;
146  else
147  datestream << ":" << now->tm_min;
148  if(now->tm_sec<10)
149  datestream << ":0" << now->tm_sec;
150  else
151  datestream << ":" << now->tm_sec;
152  m_gds->SetMetadataItem( "TIFFTAG_DATETIME", datestream.str().c_str());
153  if(imgSrc.getColorTable()!=NULL)
154  setColorTable(imgSrc.getColorTable());
155 }
int m_nband
number of bands in this dataset
std::string m_filename
filename of this dataset
std::vector< double > m_noDataValues
no data values for this dataset
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.
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
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.
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.

◆ setColorTable() [1/2]

void ImgWriterGdal::setColorTable ( const std::string &  filename,
int  band = 0 
)

Set the color table using an (ASCII) file with 5 columns (value R G B alpha)

Parameters
filenameASCII file containing 5 columns: index R G B ALFA (0:transparent, 255:solid)
bandband number to set color table (starting counting from 0)

Definition at line 254 of file ImgWriterGdal.cc.

255 {
256  //todo: fool proof table in file (no checking currently done...)
257  std::ifstream ftable(filename.c_str(),std::ios::in);
258  std::string line;
259 // poCT=new GDALColorTable();
260  GDALColorTable colorTable;
261  short nline=0;
262  while(getline(ftable,line)){
263  ++nline;
264  std::istringstream ist(line);
265  GDALColorEntry sEntry;
266  short id;
267  ist >> id >> sEntry.c1 >> sEntry.c2 >> sEntry.c3 >> sEntry.c4;
268  colorTable.SetColorEntry(id,&sEntry);
269  }
270  if(m_gds)
271  (m_gds->GetRasterBand(band+1))->SetColorTable(&colorTable);
272 }
GDALDataset * m_gds
instance of the GDAL dataset of this dataset

◆ setColorTable() [2/2]

void ImgWriterGdal::setColorTable ( GDALColorTable *  colorTable,
int  band = 0 
)

Set the color table using the GDAL class GDALColorTable.

Parameters
colorTableInstance of the GDAL class GDALColorTable
bandband number to set color table (starting counting from 0)

Definition at line 278 of file ImgWriterGdal.cc.

279 {
280  if(m_gds)
281  (m_gds->GetRasterBand(band+1))->SetColorTable(colorTable);
282 }
GDALDataset * m_gds
instance of the GDAL dataset of this dataset

◆ setMetadata()

void ImgWriterGdal::setMetadata ( char **  metadata)

Set specific metadata (driver specific)

Parameters
metadataSet this metadata when writing the image (if supported byt the driver)

Definition at line 224 of file ImgWriterGdal.cc.

225 {
226  if(m_gds)
227  m_gds->SetMetadata(metadata);
228 }
GDALDataset * m_gds
instance of the GDAL dataset of this dataset

◆ writeData() [1/3]

template<typename T >
bool ImgWriterGdal::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 counting from 0)

Parameters
[in]valueThe cell value to write
[in]colThe column number to write (counting starts from 0)
[in]rowThe row number to write (counting starts from 0)
[in]bandThe band number to write (counting starts from 0)
Returns
true if write successful

Definition at line 96 of file ImgWriterGdal.h.

97 {
98  if(band>=nrOfBand()+1){
99  std::ostringstream s;
100  s << "Error: band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
101  throw(s.str());
102  }
103  if(col>=nrOfCol()){
104  std::ostringstream s;
105  s << "Error: col (" << col << ") exceeds nrOfCol (" << nrOfCol() << ")";
106  throw(s.str());
107  }
108  if(col<0){
109  std::ostringstream s;
110  s << "Error: col (" << col << ") is negative";
111  throw(s.str());
112  }
113  if(row>=nrOfRow()){
114  std::ostringstream s;
115  s << "Error: row (" << row << ") exceeds nrOfRow (" << nrOfRow() << ")";
116  throw(s.str());
117  }
118  if(row<0){
119  std::ostringstream s;
120  s << "Error: row (" << row << ") is negative";
121  throw(s.str());
122  }
123  double theScale=1;
124  double theOffset=0;
125  if(m_scale.size()>band||m_offset.size()>band){
126  if(m_scale.size()>band)
127  theScale=m_scale[band];
128  if(m_offset.size()>band)
129  theOffset=m_offset[band];
130  }
131  //fetch raster band
132  GDALRasterBand *poBand;
133  T dvalue=theScale*value+theOffset;
134  poBand->RasterIO(GF_Write,col,row,1,1,&dvalue,1,1,getGDALDataType<T>(),0,0);
135  return true;
136 }
int nrOfCol(void) const
Get the number of columns of this dataset.
Definition: ImgRasterGdal.h:98
int nrOfRow(void) const
Get the number of rows of this dataset.
std::vector< double > m_scale
Vector containing the scale factor to be applied (one scale value for each band)
int nrOfBand(void) const
Get the number of bands of this dataset.
std::vector< double > m_offset
Vector containing the offset factor to be applied (one offset value for each band) ...

◆ writeData() [2/3]

template<typename T >
bool ImgWriterGdal::writeData ( std::vector< T > &  buffer,
int  minCol,
int  maxCol,
int  row,
int  band = 0 
)

Write pixel cell values for a range of columns for a specific row and band (all indices start counting from 0)

Parameters
[in]bufferThe vector with all cell values to write
[in]minColFirst column from where to start writing (counting starts from 0)
[in]maxColLast column that must be written (counting starts from 0)
[in]rowThe row number to write (counting starts from 0)
[in]bandThe band number to write (counting starts from 0)
Returns
true if write successful

Definition at line 146 of file ImgWriterGdal.h.

147 {
148  if(buffer.size()!=maxCol-minCol+1){
149  std::string errorstring="invalid size of buffer";
150  throw(errorstring);
151  }
152  if(minCol>=nrOfCol()){
153  std::ostringstream s;
154  s << "minCol (" << minCol << ") exceeds nrOfCol (" << nrOfCol() << ")";
155  throw(s.str());
156  }
157  if(minCol<0){
158  std::ostringstream s;
159  s << "mincol (" << minCol << ") is negative";
160  throw(s.str());
161  }
162  if(maxCol>=nrOfCol()){
163  std::ostringstream s;
164  s << "maxCol (" << maxCol << ") exceeds nrOfCol (" << nrOfCol() << ")";
165  throw(s.str());
166  }
167  if(maxCol<minCol){
168  std::ostringstream s;
169  s << "maxCol (" << maxCol << ") is less than minCol (" << minCol << ")";
170  throw(s.str());
171  }
172  if(row>=nrOfRow()){
173  std::ostringstream s;
174  s << "row (" << row << ") exceeds nrOfRow (" << nrOfRow() << ")";
175  throw(s.str());
176  }
177  if(row<0){
178  std::ostringstream s;
179  s << "row (" << row << ") is negative";
180  throw(s.str());
181  }
182  //todo: scaling and offset!
183  //fetch raster band
184  GDALRasterBand *poBand;
185  if(band>=nrOfBand()+1){
186  std::ostringstream s;
187  s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
188  throw(s.str());
189  }
190  poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
191  poBand->RasterIO(GF_Write,minCol,row,buffer.size(),1,&(buffer[0]),buffer.size(),1,getGDALDataType<T>(),0,0);
192  return true;
193 }
int nrOfCol(void) const
Get the number of columns of this dataset.
Definition: ImgRasterGdal.h:98
GDALDataset * m_gds
instance of the GDAL dataset of this dataset
int nrOfRow(void) const
Get the number of rows of this dataset.
int nrOfBand(void) const
Get the number of bands of this dataset.

◆ writeData() [3/3]

template<typename T >
bool ImgWriterGdal::writeData ( std::vector< T > &  buffer,
int  row,
int  band = 0 
)

Write pixel cell values for an entire row for a specific band (all indices start counting from 0)

Parameters
[in]bufferThe vector with all cell values to write
[in]rowThe row number to write (counting starts from 0)
[in]bandThe band number to write (counting starts from 0)
Returns
true if write successful

Definition at line 201 of file ImgWriterGdal.h.

202 {
203  return writeData(buffer,0,nrOfCol()-1,row,band);
204 }
int nrOfCol(void) const
Get the number of columns of this dataset.
Definition: ImgRasterGdal.h:98
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

◆ writeDataBlock()

template<typename T >
bool ImgWriterGdal::writeDataBlock ( Vector2d< T > &  buffer2d,
int  minCol,
int  maxCol,
int  minRow,
int  maxRow,
int  band = 0 
)

Write pixel cell values for a range of columns and rows for a specific band (all indices start counting from 0). The buffer is a two dimensional vector (stl vector of stl vector) representing [row][col].

Parameters
[in]buffer2dTwo dimensional vector of type Vector2d (stl vector of stl vector) representing [row][col]. This vector contains all cell values that must be written
[in]minColFirst column from where to start writing (counting starts from 0)
[in]maxColLast column that must be written (counting starts from 0)
[in]rowThe row number to write (counting starts from 0)
[in]bandThe band number to write (counting starts from 0)
Returns
true if write successful

Definition at line 214 of file ImgWriterGdal.h.

215 {
216  double theScale=1;
217  double theOffset=0;
218  if(m_scale.size()>band)
219  theScale=m_scale[band];
220  if(m_offset.size()>band)
221  theOffset=m_offset[band];
222  if(buffer2d.size()!=maxRow-minRow+1){
223  std::string errorstring="invalid buffer size";
224  throw(errorstring);
225  }
226  if(band>=nrOfBand()+1){
227  std::ostringstream s;
228  s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
229  throw(s.str());
230  }
231  if(minCol>=nrOfCol()){
232  std::ostringstream s;
233  s << "minCol (" << minCol << ") exceeds nrOfCol (" << nrOfCol() << ")";
234  throw(s.str());
235  }
236  if(minCol<0){
237  std::ostringstream s;
238  s << "mincol (" << minCol << ") is negative";
239  throw(s.str());
240  }
241  if(maxCol>=nrOfCol()){
242  std::ostringstream s;
243  s << "maxCol (" << maxCol << ") exceeds nrOfCol (" << nrOfCol() << ")";
244  throw(s.str());
245  }
246  if(maxCol<minCol){
247  std::ostringstream s;
248  s << "maxCol (" << maxCol << ") is less than minCol (" << minCol << ")";
249  throw(s.str());
250  }
251  //todo: apply scaling and offset!
252  typename std::vector<T> buffer((maxRow-minRow+1)*(maxCol-minCol+1));
253  //fetch raster band
254  GDALRasterBand *poBand;
255  // typename std::vector<T>::iterator startit=buffer.begin();
256  for(int irow=minRow;irow<=maxRow;++irow){
257  buffer.insert(buffer.begin()+(maxCol-minCol+1)*(irow-minRow),buffer2d[irow-minRow].begin(),buffer2d[irow-minRow].end());
258  }
259  poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
260  poBand->RasterIO(GF_Write,minCol,minRow,maxCol-minCol+1,maxRow-minRow+1,&(buffer[0]),(maxCol-minCol+1),(maxRow-minRow+1),getGDALDataType<T>(),0,0);
261  return true;
262 }
int nrOfCol(void) const
Get the number of columns of this dataset.
Definition: ImgRasterGdal.h:98
GDALDataset * m_gds
instance of the GDAL dataset of this dataset
std::vector< double > m_scale
Vector containing the scale factor to be applied (one scale value for each band)
int nrOfBand(void) const
Get the number of bands of this dataset.
std::vector< double > m_offset
Vector containing the offset factor to be applied (one offset value for each band) ...

The documentation for this class was generated from the following files: