GDALSetProjection使用的一个注意事项

GDALSetProjection 简述

GDALSetProjection是用来给GDALDataset设定投影信息(坐标系统)的接口,实际上是GDALDataset::SetProjection这个虚函数的转调而已。官网文档描述如下:

CPLErr GDALDataset::SetProjection (const char * pszProjection )
Set the projection reference string for this dataset.
The string should be in OGC WKT or PROJ.4 format. An error may occur because of incorrectly specified projection strings, because the dataset is not writable, or because the dataset does not support the indicated projection. Many formats do not support writing projections.
This method is the same as the C GDALSetProjection() function.
Parameters
pszProjection projection reference string.
Returns
CE_Failure if an error occurs, otherwise CE_None.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用GDAL C API拼接多个tif影像的示例代码: ```c #include "gdal.h" int main() { GDALAllRegister(); // 注册所有的驱动 // 打开第一个tif影像 GDALDatasetH hDataset1 = GDALOpen("path/to/first/tif", GA_ReadOnly); if (hDataset1 == NULL) { printf("Open dataset failed.\n"); return 1; } // 获取第一个tif影像的基本信息 GDALRasterBandH hBand1 = GDALGetRasterBand(hDataset1, 1); int nXSize1 = GDALGetRasterXSize(hDataset1); int nYSize1 = GDALGetRasterYSize(hDataset1); GDALDataType eDataType1 = GDALGetRasterDataType(hBand1); double adfGeoTransform1[6]; GDALGetGeoTransform(hDataset1, adfGeoTransform1); // 创建输出影像 GDALDriverH hDriver = GDALGetDriverByName("GTiff"); GDALDatasetH hOutDataset = GDALCreate(hDriver, "path/to/output/tif", nXSize1, nYSize1, 1, eDataType1, NULL); GDALSetGeoTransform(hOutDataset, adfGeoTransform1); GDALSetProjection(hOutDataset, GDALGetProjectionRef(hDataset1)); // 将第一个tif影像写入输出影像 GDALRasterBandH hOutBand = GDALGetRasterBand(hOutDataset, 1); GDALRasterIO(hBand1, GF_Read, 0, 0, nXSize1, nYSize1, GDALGetRasterBand(hOutDataset, 1), nXSize1, nYSize1, eDataType1, 0, 0); // 关闭第一个tif影像 GDALClose(hDataset1); // 循环打开并拼接其余的tif影像 for (int i = 2; i <= nCount; i++) { // 打开第i个tif影像 GDALDatasetH hDataset = GDALOpen("path/to/ith/tif", GA_ReadOnly); if (hDataset == NULL) { printf("Open dataset failed.\n"); return 1; } // 获取第i个tif影像的基本信息 GDALRasterBandH hBand = GDALGetRasterBand(hDataset, 1); int nXSize = GDALGetRasterXSize(hDataset); int nYSize = GDALGetRasterYSize(hDataset); GDALDataType eDataType = GDALGetRasterDataType(hBand); double adfGeoTransform[6]; GDALGetGeoTransform(hDataset, adfGeoTransform); // 判断第i个tif影像是否和第一个tif影像兼容 if (nXSize != nXSize1 || nYSize != nYSize1 || eDataType != eDataType1 || adfGeoTransform[0] != adfGeoTransform1[0] || adfGeoTransform[1] != adfGeoTransform1[1] || adfGeoTransform[2] != adfGeoTransform1[2] || adfGeoTransform[3] != adfGeoTransform1[3] || adfGeoTransform[4] != adfGeoTransform1[4] || adfGeoTransform[5] != adfGeoTransform1[5]) { printf("Incompatible raster dimensions or parameters.\n"); return 1; } // 将第i个tif影像写入输出影像 GDALRasterIO(hBand, GF_Read, 0, 0, nXSize, nYSize, GDALGetRasterBand(hOutDataset, 1), nXSize, nYSize, eDataType, 0, 0); // 关闭第i个tif影像 GDALClose(hDataset); } // 关闭输出影像 GDALClose(hOutDataset); return 0; } ``` 该示例代码使用GDAL C API打开、读取、创建、写入和关闭tif影像,拼接多个tif影像并生成输出影像。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值