Error handling

This commit is contained in:
Trygve 2024-04-09 13:03:29 +02:00
parent bd9d6e35d0
commit 3ff718376e

View File

@ -1,25 +1,27 @@
#include <stdexcept>
#include <stdfloat>
#include <gdal/gdal.h> #include <gdal/gdal.h>
#include "gdal/gdal_priv.h" #include "gdal/gdal_priv.h"
#include <iostream> #include <gdal/gdal_frmts.h>
#include <stdfloat>
#include "HeightMap.hh" #include "HeightMap.hh"
HeightMap::HeightMap(const char* filepath) HeightMap::HeightMap(const char* filepath)
{ {
// Open the file with some gdal nonsense: // Open the file with gdal:
const GDALAccess eAccess = GA_ReadOnly; const GDALAccess eAccess = GA_ReadOnly;
GDALDatasetUniquePtr file; GDALDatasetUniquePtr file;
GDALAllRegister(); GDALRegister_GTiff();
file = GDALDatasetUniquePtr(GDALDataset::FromHandle(GDALOpen( filepath, eAccess ))); file = GDALDatasetUniquePtr(GDALDataset::FromHandle(GDALOpen( filepath, eAccess )));
if( !file ) if( !file )
{ {
std::cout << "Could not open tiff file!"; // handle error throw std::runtime_error("Could not open tif file!");
} }
// The heigthmap only has one band // The heigthmap only has one band
auto band = file->GetBands()[0]; auto band = file->GetBands()[0];
// write the attrributes // write the attrributes
this->width = band->GetXSize(); this->width = band->GetXSize();
this->height = band->GetYSize(); this->height = band->GetYSize();
@ -27,10 +29,10 @@ HeightMap::HeightMap(const char* filepath)
this->max = band-> GetMaximum(); this->max = band-> GetMaximum();
this->data = (float *) CPLMalloc(sizeof(float)*width*height); this->data = (float *) CPLMalloc(sizeof(float)*width*height);
band->RasterIO( GF_Read, 0, 0, width, height, CPLErr error = band->RasterIO( GF_Read, 0, 0, width, height,
this->data, width, height, GDT_Float32, this->data, width, height, GDT_Float32,
0, 0 ); 0, 0 );
if (error) { throw std::runtime_error("Could not read tif file!"); }
band->FlushCache(); band->FlushCache();
} }
float HeightMap::get_pixel(int x, int y) float HeightMap::get_pixel(int x, int y)