From 5778f2fd8ed99e084179ff0f64f28abcbd00ef5e Mon Sep 17 00:00:00 2001 From: Trygve Date: Fri, 29 Mar 2024 17:12:42 +0100 Subject: [PATCH] =?UTF-8?q?begynner=20p=C3=A5=20tiff=20innlesning?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 12 +++++++ read_tiff.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 read_tiff.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..f5bba7a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,12 @@ +project(inf205 CXX) +cmake_minimum_required(VERSION 3.0) + +find_package(GDAL CONFIG REQUIRED) + +set(EXAMPLE_NAME read_tiff) +add_executable(${EXAMPLE_NAME} + read_tiff.cpp +) + + +target_link_libraries(${EXAMPLE_NAME} GDAL::GDAL) \ No newline at end of file diff --git a/read_tiff.cpp b/read_tiff.cpp new file mode 100644 index 0000000..575d790 --- /dev/null +++ b/read_tiff.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include "gdal/gdal_priv.h" + +#include +#include +#include + +#include +#include +#include +/* +class HeightMap +{ + public: + float* data; + int x_size; + int y_size; + + HeightMap(int x_size, int y_size); +}; + +HeightMap::HeightMap(int x_size, int y_size, char* filepath) { + this->x_size = x_size; + this->y_size = y_size; + this->data = (float *) CPLMalloc(sizeof(GDT_Float32)*x_size*y_size); + + +} +*/ +int main(int argc, const char* argv[]) +{ + if (argc != 2) { + return EINVAL; + } + const char* pszFilename = argv[1]; + + GDALDatasetUniquePtr poDataset; + GDALAllRegister(); + const GDALAccess eAccess = GA_ReadOnly; + poDataset = GDALDatasetUniquePtr(GDALDataset::FromHandle(GDALOpen( pszFilename, eAccess ))); + if( !poDataset ) + { + std::cout << "Error :/"; // handle error + } + std::cout << poDataset->GetDriverName(); + + + double adfGeoTransform[6]; + printf( "Driver: %s/%s\n", + poDataset->GetDriver()->GetDescription(), + poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) ); + printf( "Size is %dx%dx%d\n", + poDataset->GetRasterXSize(), poDataset->GetRasterYSize(), + poDataset->GetRasterCount() ); + if( poDataset->GetProjectionRef() != NULL ) + printf( "Projection is `%s'\n", poDataset->GetProjectionRef() ); + if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None ) + { + printf( "Origin = (%.6f,%.6f)\n", + adfGeoTransform[0], adfGeoTransform[3] ); + printf( "Pixel Size = (%.6f,%.6f)\n", + adfGeoTransform[1], adfGeoTransform[5] ); + } + + float *buffer; + auto poBand = poDataset->GetBands()[0]; + + int x_size = poBand->GetXSize(); + int y_size = poBand->GetYSize(); + + + buffer = (float *) CPLMalloc(sizeof(GDT_Float32)*x_size*y_size); + poBand->RasterIO( GF_Read, 0, 0, x_size, y_size, + buffer, x_size, y_size, GDT_Float32, + 0, 0 ); + + for (int i = 0; i < x_size*y_size; i++) + { + buffer+=sizeof(GDT_Float32); + if (*buffer) + { + std::cout << *buffer<<" "<