c/c++ 使用libgeotiff读取全球高程数据ETOPO

cpp 复制代码
#include <geotiff.h>
#include <geotiffio.h>
#include <tiffio.h>
#include <iostream>
#include <xtiffio.h>
void MyTIFFErrorHandler(const char* module, const char* fmt, va_list args) {
    // 格式化错误消息
    char buffer[1024];
    vsnprintf(buffer, sizeof(buffer), fmt, args);

    // 输出到标准错误(或日志文件)
    std::cerr << "[TIFF ERROR] Module: " << (module ? module : "unknown")
              << ", Message: " << buffer << std::endl;
}
void ReadFloatTifGrid(const char *file) {
TIFFErrorHandler oldHandler = TIFFSetErrorHandler(MyTIFFErrorHandler);
    TIFF *tif = NULL;
    GTIF *gtif = NULL;

    tif = XTIFFOpen(file, "r");
    if (!tif) {
        return ;
    }

    gtif = GTIFNew(tif);
    if (!gtif) {
        XTIFFClose(tif);
        return ;
    }

    unsigned short sampleFormat, samplesPerPixel, bitsPerSample;
    TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesPerPixel);// 1: 无符号整数,2: 有符号整数,3: 浮点
    TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bitsPerSample);
    TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &sampleFormat);

    if (sampleFormat != SAMPLEFORMAT_IEEEFP || bitsPerSample != 32 || samplesPerPixel != 1) {
        GTIFFree(gtif);
        XTIFFClose(tif);
        return ;
    }

    int width, height;
    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);

    short tiepointsize, pixscalesize;
    double* tiepoints;//[6];
    double* pixscale;//[3];
    TIFFGetField(tif, TIFFTAG_GEOTIEPOINTS,  &tiepointsize,
                 &tiepoints);
    TIFFGetField(tif, TIFFTAG_GEOPIXELSCALE, &pixscalesize,
                 &pixscale);
    float data1[30000] = {0};

    auto cellSize = pixscale[0];
   auto top = tiepoints[4];
    auto left = tiepoints[3];
   auto bottom = tiepoints[4]-(pixscale[1] * float(height));
    auto right = tiepoints[3]+(pixscale[0] * float(width));


    if (TIFFIsTiled(tif)) {
        // 处理分块图像
        uint32_t tileWidth, tileHeight;
        TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tileWidth);
        TIFFGetField(tif, TIFFTAG_TILELENGTH, &tileHeight);
        int a=TIFFTileSize(tif);
        unsigned char* buf = (unsigned char*)_TIFFmalloc(TIFFTileSize(tif));

        for (uint32_t y = 0; y < height; y += tileHeight) {
            for (uint32_t x = 0; x < width; x += tileWidth) {
                TIFFReadTile(tif, buf, x, y, 0, 0);
                if(y>=3437&&x>18042)
                {
                    int a=0;
                }
                if (bitsPerSample == 32) {
                    float* floatData = (float*)buf;//高度
                    int a=0;
                    // 处理32位浮点数据...
                } else if (bitsPerSample == 64) {
                    double* doubleData = (double*)buf;
                    int a=0;
                    // 处理64位浮点数据...
                }
                else if (bitsPerSample == 16) {
                    uint16_t* shortData = (uint16_t*)buf;

                }

            }
        }
        _TIFFfree(buf);
    }
        else {
            for (long i = 0; i < height; i++) {
                if (TIFFReadScanline(tif, data1, (unsigned int)i, 0) == -1) {

                    GTIFFree(gtif);
                    XTIFFClose(tif);
                    return ;
                }
                if(i>3000)
                {
                    float sss=data1[17000];
                    int a=0;
                }
        }
    }
    GTIFFree(gtif);
    XTIFFClose(tif);

}
int main(int argc, char *argv[]) {

    ReadFloatTifGrid("ETOPO_2022_v1_60s_N90W180_surface.tif");
}

全球高程tif从https://www.ncei.noaa.gov/products/etopo-global-relief-model下载

自己编译tiff库要开启zlib,后续补充一个经纬度到xy的映射

相关推荐
mit6.8244 小时前
[openvela] Hello World :从零开始的完整实践与问题复盘
c++·嵌入式硬件
pusue_the_sun4 小时前
数据结构:二叉树oj练习
c语言·数据结构·算法·二叉树
啊阿狸不会拉杆6 小时前
《算法导论》第 32 章 - 字符串匹配
开发语言·c++·算法
小学生的信奥之路6 小时前
洛谷P3817题解:贪心算法解决糖果分配问题
c++·算法·贪心算法
曙曙学编程7 小时前
stm32——GPIO
c语言·c++·stm32·单片机·嵌入式硬件
△曉風殘月〆7 小时前
Visual Studio中的常用调试功能(下)
c++·ide·visual studio·调试
武当豆豆7 小时前
C++编程学习(第25天)
开发语言·c++·学习
minji...11 小时前
C++ string类(STL简介 , string类 , 访问修改字符)
开发语言·c++
Forward♞11 小时前
Qt——文件操作
开发语言·c++·qt
十五年专注C++开发11 小时前
CMake进阶: CMake Modules---简化CMake配置的利器
linux·c++·windows·cmake·自动化构建