GDAL:创建栅格图像
生活随笔
收集整理的這篇文章主要介紹了
GDAL:创建栅格图像
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、鏈接
C/C++——常規——附加包含目錄——添加include目錄
鏈接器——輸入——附加依賴項——添加…\gdal_i.lib
#include “gdal_priv.h”
二、分享給有需要的人,代碼質量勿噴
bool CreateRaster(const QString &xjPathResult, /* 柵格路徑 */const int &xjRows, const int &xjCols, const int &xjBands, /* 行/高,列/寬,波段數量 */const double &xjOX, const double &xjOY, /* 左上角位置 */const double &xjResolutionX, const double &xjResolutionY /* 橫、縱向分辨率 */) {//注冊柵格驅動GDALAllRegister();//獲取指定格式的驅動const char *pszFormat = "GTiff";GDALDriver *poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);if (poDriver == NULL){return false;}//創建DatasetGDALDataset *poDS = poDriver->Create(xjPathResult.toStdString().c_str(), xjCols, xjRows, xjBands, GDT_Byte, NULL);if (poDS == NULL){return false;}//設置六參數:第1、4個為左上角的坐標;第2個是橫向分辨率,第6個是縱向分辨率;其余為旋轉角度double xjGeoTransform[6] = { xjOX, xjResolutionX, 0, xjOY, 0, -xjResolutionY };poDS->SetGeoTransform(xjGeoTransform);#pragma region /* 方法一:GDALRasterBand::RasterIO *///uint8_t noData = 999;//for (int b = 1; b <= xjBands; b++)/* 天坑:波段從1開始 *///{// GDALRasterBand *pBand = poDS->GetRasterBand(b);// //像素從左到右,從上到下// int i = 0;// uint8_t* xjPixelValue = new uint8_t[xjRows*xjCols];// for (int r = 0; r < xjRows; r++)// {// uint8_t xjValue = 0;// if (r % 2 == 0)// {// xjValue = b * (r + 1) % 255;// }// if ((50 < r) && (r < 100))// {// xjValue = noData;// }// for (int c = 0; c < xjCols; c++)// {// xjPixelValue[i] = xjValue;// i++;// }// }// pBand->SetNoDataValue(noData);// CPLErr err = pBand->RasterIO(GF_Write, /* 讀寫標記 */// 0, 0, /* 起始坐標,圖像左上角為(0,0) */// xjCols, xjRows, /* 寬/列數,高/行數 */// xjPixelValue, /* 像素值的指針數組 */// xjCols, xjRows, /* 緩沖區大小 */// GDT_Byte, /* 數據類型 */// 0, 0); /* 像素間隔,行間隔 */// if (err == CE_Failure)// {// return;// }// delete[]xjPixelValue;// xjPixelValue = nullptr;//} #pragma endregion#pragma region /* 方法二:GDALDataset::RasterIO *///波段從1開始,像素從左到右,從上到下int i = 0;/* 數據類型和RasterIO的第9個參數對應 */uint8_t* xjPixelValue = new uint8_t[xjBands*xjRows*xjCols];for (int b = 1; b <= xjBands; b++){for (int r = 0; r < xjRows; r++){uint8_t xjValue = 0;if (r % 2 == 1){xjValue = b * (r + 1) % 255;}for (int c = 0; c < xjCols; c++){xjPixelValue[i] = b * (r + 1) % 255;i++;}}}int xjBandMap[3] = { 3,2,1 };CPLErr err = poDS->RasterIO(GF_Write, /* 讀寫標記 */0, 0, /* 起始坐標,圖像左上角為(0,0) */xjCols, xjRows, /* 寬/列數,高/行數 */xjPixelValue, /* 全部像素值的指針數組 */xjCols, xjRows, /* 緩沖區大小 */GDT_Byte, /* 數據類型 */xjBands, /* 波段數 */xjBandMap, /* 波段號數組,順序不同,結果不同。全部寫0也行 */0, 0, 0); /* 像素間隔,行間隔,波段間隔 */if (err == CE_Failure){return false;}delete[]xjPixelValue;xjPixelValue = nullptr; #pragma endregionGDALClose((GDALDatasetH)poDS);return true; }bool bR = CreateRaster(“F:/xj.tif”, 800, 1000, 3, 123456.5, 1234567.5, 1.5, 2.0);
三、參考
[1]李明錄. GDAL源碼剖析與開發指南[M].北京:人民郵電出版社, 2014: 260-284.
[2]GDAL源碼剖析(七)之GDAL RasterIO使用說明(續)
總結
以上是生活随笔為你收集整理的GDAL:创建栅格图像的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mondrian mysql 实例_mo
- 下一篇: 100集华为HCIE安全培训视频教材整理