Merge branch 'main' of gitlab.com:Trygve/contour-creator

This commit is contained in:
Esther Zijerveld
2024-04-11 15:24:12 +02:00
5 changed files with 24 additions and 18 deletions

View File

@@ -1,25 +1,26 @@
#include <stdexcept>
#include <gdal/gdal.h>
#include "gdal/gdal_priv.h"
#include <iostream>
#include <stdfloat>
#include <gdal/gdal_frmts.h>
#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 +28,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)
@@ -38,4 +39,4 @@ float HeightMap::get_pixel(int x, int y)
// all the pixels are in an array of floats from left to right, top to bottom
int offset = ((this->width * y) + x);
return *(this->data + offset);
}
}