使用C++版GDAL,裁剪一幅tif影像,设置该 影像的坐标系与源影像的坐标系一致,并另存为"E:\t1.tif"
时间: 2023-07-07 14:26:50 浏览: 60
好的,我可以为您提供相应的代码。请确认您已经安装了C++版的GDAL库。
以下是实现您需求的C++代码:
```cpp
#include <iostream>
#include "gdal_priv.h"
#include "cpl_conv.h" // for CPLMalloc()
int main()
{
// register GDAL drivers
GDALAllRegister();
// input raster file name
const char* input_raster = "path/to/your/input/raster.tif";
// open input raster file
GDALDataset* input_ds = (GDALDataset*)GDALOpen(input_raster, GA_ReadOnly);
if (input_ds == NULL)
{
std::cerr << "Error: Failed to open input raster file: " << input_raster << std::endl;
return 1;
}
// get input raster projection
const char* input_proj = input_ds->GetProjectionRef();
// create output raster
GDALDriver* driver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* output_ds = driver->CreateCopy("E:/t1.tif", input_ds, FALSE, NULL, NULL, NULL);
if (output_ds == NULL)
{
std::cerr << "Error: Failed to create output raster file: E:/t1.tif" << std::endl;
return 1;
}
// set output raster projection
if (output_ds->SetProjection(input_proj) != CE_None)
{
std::cerr << "Error: Failed to set output raster projection." << std::endl;
return 1;
}
// set output raster geotransform
double geotransform[6];
if (input_ds->GetGeoTransform(geotransform) == CE_None)
{
output_ds->SetGeoTransform(geotransform);
}
// close datasets
GDALClose(input_ds);
GDALClose(output_ds);
return 0;
}
```
以上代码中,需要将`"path/to/your/input/raster.tif"`替换成您实际的输入栅格影像文件路径。
另外,需要注意,该代码只是实现了裁剪一幅tif影像、设置该影像的坐标系与源影像的坐标系一致、并另存为"E:/t1.tif",并没有进行实际的裁剪操作。如果您需要进行裁剪操作,可以使用GDAL库提供的相关API进行实现。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)