src/tools/lvr2_hdf5togeotiff/Main.cpp
Go to the documentation of this file.
1 
6 #include "lvr2/io/HDF5IO.hpp"
7 
8 #include <boost/range/iterator_range.hpp>
9 #include <boost/foreach.hpp>
10 #include <boost/filesystem.hpp>
11 
12 #include <string>
13 #include <fstream>
14 
15 #include <sys/stat.h>
16 
17 #include "lvr2/io/GeoTIFFIO.hpp"
18 #include "Options.hpp"
19 
20 
21 using namespace lvr2;
22 
32 int processConversion(std::string input_filename,
33  std::string position_code, std::string output_filename, size_t min_channel, size_t max_channel)
34 {
35  /*------------------- HDF5 INPUT ------------------------*/
36  HDF5IO hdf5(input_filename, false);
37  std::vector<size_t> dim;
38 
39  // extract radiometric data
40  std::string groupname = "raw/spectral/position_" + position_code;
41  std::string datasetname = "spectral";
42  boost::shared_array<uint16_t> spectrals = hdf5.getArray<uint16_t>(groupname, datasetname, dim);
43 
44  // extract array dimension information
45  size_t num_channels = dim[0];
46  size_t num_rows = dim[1];
47  size_t num_cols = dim[2];
48 
49  // set upper boundary
50  if(num_channels < max_channel)
51  {
52  std::cout << "The dataset has only " << num_channels << " channels. Using this as upper boundary." << std::endl;
53  max_channel = num_channels;
54  }
55  num_channels = max_channel - min_channel;
56 
57  GeoTIFFIO gtifio(output_filename, num_cols, num_rows, num_channels);
58 
59  /*--------------- FILE CONVERSION --------------------*/
60  // for each channel create a cv::Mat containing the spectral intensity data for the channel ...
61  for(size_t channel = 0; channel < num_channels; channel++)
62  {
63  cv::Mat *mat = new cv::Mat(num_rows, num_cols, CV_16UC1);
64  for(size_t row = 0; row < num_rows; row++)
65  {
66  for(size_t col = 0; col < num_cols; col++)
67  {
68  mat->at<uint16_t>(row, col) = spectrals.get()[(channel + min_channel) * num_cols * num_rows + row * num_cols + col];
69  }
70  }
71  // ... and write it to the output GeoTIFF file
72  int ret = gtifio.writeBand(mat, channel + 1);
73  delete mat;
74  if (ret != 0)
75  {
76  return ret;
77  }
78  }
79  return 0;
80 }
81 
82 int main(int argc, char**argv)
83 {
84  hdf5togeotiff::Options options(argc, argv);
85 
86  /*--------------- GET PROGRAM OPTIONS --------------------*/
87 
88  boost::filesystem::path input_filename(options.getH5File());
89  std::string input_extension = boost::filesystem::extension(input_filename);
90 
91  boost::filesystem::path output_filename(options.getGTIFFFile());
92  std::string output_extension = boost::filesystem::extension(output_filename);
93 
94  size_t min_channel = options.getMinChannel();
95  size_t max_channel = options.getMaxChannel();
96 
97  std::string position_code = options.getPositionCode();
98 
99  /*---------------- PREPARE CONVERSION -------------------*/
100 
101  boost::filesystem::path output_dir = output_filename.parent_path();
102  if (output_dir != "" && !boost::filesystem::exists(output_dir))
103  {
104  boost::filesystem::create_directory(output_dir);
105  }
106 
107  std::cout << "Starting conversion..." << std::endl;
108  if (processConversion(input_filename.string(), position_code, output_filename.string(), min_channel, max_channel) < 0)
109  {
110  std::cout << "An Error occurred during conversion." << std::endl;
111  }
112 
113  return 0;
114 }
const kaboom::Options * options
int processConversion(std::string input_filename, std::string position_code, std::string output_filename, size_t min_channel, size_t max_channel)
Extraction of radiometric data from a given HDF5 file into a new GeoTIFF file in (optionally) given o...
int main(int argc, char **argv)
class providing and encapsulating GDAL GeoTIFF I/O functions
Definition: GeoTIFFIO.hpp:40
boost::shared_array< T > getArray(std::string groupName, std::string datasetName, unsigned int &size)
int writeBand(cv::Mat *mat, int band)
Writing given band into open GeoTIFF file.
Definition: GeoTIFFIO.cpp:27
A class to parse the program options for the extraction of radiometric data from a HDF5 dataset...
char ** argv


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:08