当前位置: 首页 > news >正文

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

#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的映射

相关文章:

  • Prompt工程提示词(1-6章)
  • Python中NumPy的统计运算
  • 控制单元设计(Control Unit, CU)
  • 【C++初学】课后作业汇总复习(七) 指针-深浅copy
  • 批量将文件名称、文件路径、文件扩展名提取到 Excel 清单
  • QMT美林时钟策略(15年11倍,年化13.5%策略)
  • 【C++算法】57.哈希表_判断是否互为字符重排
  • AF3 Cropper类解读
  • 音视频转换器 AV 接口静电保护方案
  • 状态压缩dp
  • Odrive源码分析(六) 相关控制变量传递
  • 基于redis 实现我的收藏功能优化详细设计方案
  • HCIP第十一天
  • 攻防世界——Web题ez_curl
  • 代码学习总结(二)
  • AIDD-人工智能药物-pyecharts-gallery
  • 滑动窗口(4)—将x减到0的最⼩操作数
  • 基于时间序列分解与XGBoost的交通通行时间预测方法解析
  • x265 编码参数 rdLevel 详细解析
  • buuctf sql注入类练习
  • 手机网站设计案例/手机卡顿优化软件
  • 网站收录需要多久/网络推广产品公司
  • 如何做彩票网站推广图/国际羽联最新排名
  • 深圳做营销网站建设/网站建站推广
  • 诸暨哪些公司可以制作网站/优化设计