From 3ff718376e777ed09365fc253e54c5cae0e7586f Mon Sep 17 00:00:00 2001 From: Trygve Date: Tue, 9 Apr 2024 13:03:29 +0200 Subject: [PATCH] Error handling --- src/HeightMap.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/HeightMap.cpp b/src/HeightMap.cpp index 88b78ef..6997768 100644 --- a/src/HeightMap.cpp +++ b/src/HeightMap.cpp @@ -1,25 +1,27 @@ +#include +#include + #include #include "gdal/gdal_priv.h" -#include - -#include +#include #include "HeightMap.hh" HeightMap::HeightMap(const char* filepath) { - // Open the file with some gdal nonsense: + // Open the file with gdal: const GDALAccess eAccess = GA_ReadOnly; GDALDatasetUniquePtr file; - GDALAllRegister(); + GDALRegister_GTiff(); file = GDALDatasetUniquePtr(GDALDataset::FromHandle(GDALOpen( filepath, eAccess ))); 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 auto band = file->GetBands()[0]; + // write the attrributes this->width = band->GetXSize(); this->height = band->GetYSize(); @@ -27,10 +29,10 @@ HeightMap::HeightMap(const char* filepath) this->max = band-> GetMaximum(); 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, 0, 0 ); - + if (error) { throw std::runtime_error("Could not read tif file!"); } band->FlushCache(); } float HeightMap::get_pixel(int x, int y)