使用C++版GDAL将一幅tif影像的坐标系设置为WGS84坐标系
时间: 2024-03-01 09:53:13 浏览: 139
好的,下面为您提供相应的C++代码。请确认您已经安装了C++版的GDAL库。
```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;
}
// create output raster
GDALDriver* driver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* output_ds = driver->CreateCopy("path/to/your/output/raster.tif", input_ds, FALSE, NULL, NULL, NULL);
if (output_ds == NULL)
{
std::cerr << "Error: Failed to create output raster file: path/to/your/output/raster.tif" << std::endl;
return 1;
}
// set output raster projection to WGS84
const char* wkt = "GEOGCS[\"WGS 84\",\
DATUM[\"WGS_1984\",\
SPHEROID[\"WGS 84\",6378137,298.257223563,\
AUTHORITY[\"EPSG\",\"7030\"]],\
AUTHORITY[\"EPSG\",\"6326\"]],\
PRIMEM[\"Greenwich\",0,\
AUTHORITY[\"EPSG\",\"8901\"]],\
UNIT[\"degree\",0.0174532925199433,\
AUTHORITY[\"EPSG\",\"9122\"]],\
AUTHORITY[\"EPSG\",\"4326\"]]";
output_ds->SetProjection(wkt);
// 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"`替换成您实际的输入栅格影像文件路径,将`"path/to/your/output/raster.tif"`替换成您期望的输出栅格影像文件路径。
另外,需要注意,该代码只是实现了将一幅tif影像的坐标系设置为WGS84坐标系,并没有进行实际的影像处理操作。如果您需要进行其他的影像处理操作,可以使用GDAL库提供的相关API进行实现。
阅读全文