GDAL给定一套格网重投影坐标,分块进行影像投影转换c++
时间: 2023-08-04 08:23:38 浏览: 178
实现三维坐标变换、投影变换,C++实现。
3星 · 编辑精心推荐
要使用GDAL进行分块的影像投影转换,可以进行以下步骤:
1. 打开原始影像和目标影像的数据集,例如:
```c++
#include "gdal_priv.h"
#include "cpl_conv.h" // for CPLMalloc()
GDALDataset *src_ds = (GDALDataset *) GDALOpen("input.tif", GA_ReadOnly);
GDALDataset *dst_ds = (GDALDataset *) GDALOpen("output.tif", GA_Update);
```
2. 获取原始影像和目标影像的投影信息和地理转换信息,例如:
```c++
const char *src_proj = src_ds->GetProjectionRef();
const char *dst_proj = dst_ds->GetProjectionRef();
double src_geotransform[6];
src_ds->GetGeoTransform(src_geotransform);
double dst_geotransform[6];
dst_ds->GetGeoTransform(dst_geotransform);
```
3. 创建一个投影转换器,并设置投影转换的参数,例如:
```c++
OSRCoordinateTransformation *transform =
OGRCreateCoordinateTransformation(src_ds->GetSpatialRef(),
dst_ds->GetSpatialRef());
```
4. 读取原始影像的像素数据,并进行投影转换,例如:
```c++
int block_size = 256;
int x_blocks = (src_ds->GetRasterXSize() + block_size - 1) / block_size;
int y_blocks = (src_ds->GetRasterYSize() + block_size - 1) / block_size;
for (int y_block = 0; y_block < y_blocks; y_block++) {
for (int x_block = 0; x_block < x_blocks; x_block++) {
int x_offset = x_block * block_size;
int y_offset = y_block * block_size;
int x_size = MIN(block_size, src_ds->GetRasterXSize() - x_offset);
int y_size = MIN(block_size, src_ds->GetRasterYSize() - y_offset);
GDALRasterBand *src_band = src_ds->GetRasterBand(1);
GDALRasterBand *dst_band = dst_ds->GetRasterBand(1);
float *buf = (float *) CPLMalloc(sizeof(float) * x_size * y_size);
src_band->RasterIO(GF_Read, x_offset, y_offset, x_size, y_size,
buf, x_size, y_size, GDT_Float32, 0, 0);
double *x = (double *) CPLMalloc(sizeof(double) * x_size * y_size);
double *y = (double *) CPLMalloc(sizeof(double) * x_size * y_size);
for (int j = 0; j < y_size; j++) {
for (int i = 0; i < x_size; i++) {
x[i + j * x_size] = src_geotransform[0] + (i + x_offset) * src_geotransform[1] +
(j + y_offset) * src_geotransform[2];
y[i + j * x_size] = src_geotransform[3] + (i + x_offset) * src_geotransform[4] +
(j + y_offset) * src_geotransform[5];
}
}
transform->Transform(x_size * y_size, x, y, buf);
dst_band->RasterIO(GF_Write, x_offset, y_offset, x_size, y_size,
buf, x_size, y_size, GDT_Float32, 0, 0);
CPLFree(buf);
CPLFree(x);
CPLFree(y);
}
}
```
5. 保存目标影像的像素数据,并关闭数据集,例如:
```c++
dst_ds->SetGeoTransform(dst_geotransform);
dst_ds->SetProjection(dst_proj);
GDALClose(src_ds);
GDALClose(dst_ds);
```
阅读全文