gpt4 book ai didi

c - 使用 GDAL C API 编写栅格

转载 作者:行者123 更新时间:2023-11-30 16:59:49 25 4
gpt4 key购买 nike

我正在尝试了解 GDAL C API。我有一些包含 int、float 或 double 值的数组,当我尝试使用 GDALRasterIO 将它们转储到 tiff 文件时,我得到垂直带与 0 值交替的栅格(我怀疑是未初始化的值...

这是一个最小的例子:

#include "gdal.h"

void create_new_tiff(char * DstFilename, int nx, int ny);

int main()
{
char filename[10] = "test.tif";
int nx, ny;

nx = 600;
ny = 500;

create_new_tiff(filename, nx, ny);

return 0;
}

还有 create_new_tiff 函数:

void create_new_tiff(char * DstFilename, int nx, int ny)
{

const char *pszFormat = "GTiff";
int test[nx*ny];
GDALDatasetH hDstDS;
GDALRasterBandH hBand;
char **papszOptions = NULL;

GDALAllRegister();
GDALDriverH hDriver = GDALGetDriverByName( pszFormat );

hDstDS = GDALCreate( hDriver, DstFilename, nx, ny, 1, GDT_Int16, papszOptions);
hBand = GDALGetRasterBand( hDstDS, 1 );

for(int i=0; i<nx*ny; i++) test[i] = 4;

CPLErr Err = GDALRasterIO( hBand, GF_Write, 0, 0, nx, ny, test, nx, ny, GDT_Int16, 0, 0);
GDALClose( hDstDS );
}

看起来我混淆了数据类型,但无法弄清楚出了什么问题......

谢谢

PS:这是 QGis 中栅格的样子:

enter image description here

最佳答案

这个错误来自于我假设“int”默认情况下实际上是 2 个字节,这是错误的。 int 的大小取决于实现。请参阅is int by default long int in C?

查看凯尔评论

关于c - 使用 GDAL C API 编写栅格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37971557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com