GeoTIFFIO.cpp
Go to the documentation of this file.
1 //
2 // Created by ndettmer on 07.02.19.
3 //
4 
5 #include <iostream>
6 
7 #include "lvr2/io/GeoTIFFIO.hpp"
8 #include "lvr2/io/Timestamp.hpp"
9 
10 namespace lvr2
11 {
12 
13 
14 GeoTIFFIO::GeoTIFFIO(std::string filename, int cols, int rows, int bands) : m_cols(cols), m_rows(rows), m_bands(bands)
15 {
16  GDALAllRegister();
17  m_gtif_driver = GetGDALDriverManager()->GetDriverByName("GTiff");
18  m_gtif_dataset = m_gtif_driver->Create(filename.c_str(), m_cols, m_rows, m_bands, GDT_UInt16, NULL);
19 }
20 
22 {
23  GDALAllRegister();
24  m_gtif_dataset = (GDALDataset *) GDALOpen(filename.c_str(), GA_ReadOnly);
25 }
26 
27 int GeoTIFFIO::writeBand(cv::Mat *mat, int band)
28 {
29  if (!m_gtif_dataset)
30  {
31  std::cout << timestamp << "GeoTIFF dataset not initialized!" << std::endl;
32  return -1;
33  }
34 
35  uint16_t *rowBuff = (uint16_t *) CPLMalloc(sizeof(uint16_t) * m_cols);
36  for (int row = 0; row < m_rows; row++)
37  {
38  for (int col = 0; col < m_cols; col++)
39  {
40  rowBuff[col] = mat->at<uint16_t>(row, col);
41  }
42  if (m_gtif_dataset->GetRasterBand(band)->RasterIO(
43  GF_Write, 0, row, m_cols, 1, rowBuff, m_cols, 1, GDT_UInt16, 0, 0) != CPLE_None)
44  {
45  std::cout << timestamp << "An error occurred in GDAL while writing band "
46  << band << " in row " << row << "." << std::endl;
47  return -1;
48  }
49  }
50  return 0;
51 }
52 
54 {
55  if(m_gtif_dataset)
56  {
57  return m_gtif_dataset->GetRasterXSize();
58  }
59  else
60  {
61  return 0;
62  }
63 
64 }
65 
67 {
68  if(m_gtif_dataset)
69  {
70  return m_gtif_dataset->GetRasterYSize();
71  }
72  else
73  {
74  return 0;
75  }
76 
77 }
78 
80 {
81  if(m_gtif_dataset)
82  {
83  return m_gtif_dataset->GetRasterCount();
84  }
85  return 0;
86 }
87 
88 cv::Mat *GeoTIFFIO::readBand(int index)
89 {
90  GDALRasterBand *band = m_gtif_dataset->GetRasterBand(index);
91  if(band)
92  {
93  int nXSize = band->GetXSize();
94  int nYSize = band->GetYSize();
95  uint16_t *buf = (uint16_t *) CPLMalloc(sizeof(uint16_t) * nXSize * nYSize);
96 
97  CPLErr error = band->RasterIO(GF_Read, 0, 0, nXSize, nYSize, buf, nXSize, nYSize, GDT_UInt16, 0, 0);
98 
99  cv::Mat *mat = new cv::Mat(nXSize, nYSize, CV_16UC1, buf);
100 
101  return mat;
102  }
103  else
104  {
105  std::cout << timestamp << "Error getting raster band" << std::endl;
106  return new cv::Mat;
107  }
108 
109 }
110 
112 {
113  GDALClose(m_gtif_dataset);
114  GDALDestroyDriverManager();
115 }
116 
117 } // namespace lvr2
118 
int getRasterWidth()
Definition: GeoTIFFIO.cpp:53
static Timestamp timestamp
A global time stamp object for program runtime measurement.
Definition: Timestamp.hpp:116
GDALDataset * m_gtif_dataset
Definition: GeoTIFFIO.hpp:88
GeoTIFFIO(std::string filename, int cols, int rows, int bands)
Definition: GeoTIFFIO.cpp:14
GDALDriver * m_gtif_driver
Definition: GeoTIFFIO.hpp:89
int writeBand(cv::Mat *mat, int band)
Writing given band into open GeoTIFF file.
Definition: GeoTIFFIO.cpp:27
KF_EXPORTS void error(const char *error_string, const char *file, const int line, const char *func="")
Error handler. All GPU functions from this subsystem call the function to report an error...
int getRasterHeight()
Definition: GeoTIFFIO.cpp:66
cv::Mat * readBand(int band_index)
Definition: GeoTIFFIO.cpp:88
#define NULL
Definition: mydefs.hpp:141


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:06