- 积分
- 35
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2018-4-23
- 最后登录
- 1970-1-1

|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 Weitian 于 2019-5-20 14:51 编辑
接触到GDAL库,刚开始学习有点困难,代码分享。包括(构建Geotiff, 位置转换,写入数值,设置坐标系)
void Create_tif(char *out_tif_path,float *buffer, int tif_row, int tif_col ,double xll,double yll ,int x_pixel_size,int y_pixel_size)
{
GDALDataset *poDatasetCreate;
GDALDriver *poDriver;
GDALAllRegister();
const char *pszFormat = "GTiff";
poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
char **papszMetadata = poDriver->GetMetadata();
poDatasetCreate = poDriver->Create(out_tif_path, tif_col, tif_row, 1, GDT_Float32, papszMetadata);
//创建一个 tif_col X tif_row 的 tif文件,其内容全为0,左上角坐标为(0,0)
double GeoTransform[6] = { xll,x_pixel_size,0,yll,0,-(y_pixel_size) };
//{ 左上角坐标x , sizeX , 指北为0 , 左上角坐标y ,指北为0, -sizeY (注意,这里Y方向像元大小要加负号,否则图像上下颠倒的)
poDatasetCreate->SetGeoTransform(GeoTransform);
//设置tif信息,第三个和第五个设置为0(图像从 xll,yll为左下角坐标);由于我设置的像元为方形,故分辨率sizeX与sizeY一样都用pixel_size。
OGRSpatialReference oSRS;
oSRS.SetProjCS("UTM 49 (WGS84) in northern hemisphere.");
oSRS.SetWellKnownGeogCS("WGS84");
oSRS.SetUTM(49, TRUE);
char *pszWKT = NULL;
oSRS.exportToWkt(&pszWKT);
poDatasetCreate->SetProjection(pszWKT);
//设置tif图像的坐标系,这里设置的是 UTM49
poDatasetCreate->RasterIO(GF_Write, 0, 0, tif_col, tif_row, buffer, tif_col, tif_row, GDT_Float32, 1, 0, 0, 0, 0);
//GF_Write写入, 前两个0指 距离tif图左上角0 开始写入,随后tif_col和tif_row指tif图上需要做数值输入的行列。
// 第二个参数到 第五个参数表示 原来tif图上需要做修改的范围。
//buffer为输入数值的数组,需要自己在主函数定义 buffer[tif_row*tif_col],并给数值
//具体信息可找GDAL官网Rasterio
GDALClose((GDALDatasetH)poDatasetCreate);
}
|
|