GDAL读取数据构建金字塔可以加速下一次读取,函数也很简单。
下面是我整理的GDAL构建金字塔函数,只需传入读取图像的地址和构建金字塔所显示的进度条即可。
#region 局部变量 static double D_Count = 0; static double D_Complete = 0;
ToolStripProgressBar TSPB_Progress;//进度条 #endregion
private bool Ini_CreatePyramids(string S_Filename)//创建金字塔,S_Filename为要打开的图像 {
Gdal.AllRegister();
Gdal.SetConfigOption(“USE_RRD”, “YES”);//创建Erdas格式的字塔文件 Dataset DS_dataset = Gdal.Open(S_Filename, Access.GA_ReadOnly);
Driver Driver = DS_dataset.GetDriver();//获取驱动
Band band = DS_dataset.GetRasterBand(1);
if (band.GetOverviewCount() != 0)
return true;
int iWidth = DS_dataset.RasterXSize;
int iHeight = DS_dataset.RasterYSize;
int iPixelNum = iWidth * iHeight;//图像中的总像元个数; int iTopNum = 4096;//顶层金字塔大小,64*64; int iCurNum = iPixelNum / 4;
I_bandcount = DS_dataset.RasterCount;
int[] anLevels = new int[1024];
int nLevelCount = 0;
do
{
anLevels[nLevelCount] = Convert.ToInt32(Math.Pow(2.0, nLevelCount + 2));
nLevelCount++;
iCurNum /= 4;
}
while (iCurNum > iTopNum);
int[] levels = new int[nLevelCount];
for (int a = 0; a < nLevelCount; a++)
{
levels[a] = anLevels[a];
}
DS_dataset.BuildOverviews(“nearest”, levels,
new Gdal.GDALProgressFuncDelegate((double dfComplete, IntPtr strMessage, IntPtr Data) =>
{
D_Count = dfComplete * 100 / I_bandcount;
TSPB_Progress.Value = (int)(D_Count + D_Complete + 0.5);
if (dfComplete == 1.0)
D_Complete += 100.0 / I_bandcount;
return 1;
}), ” “);//配合lambda表达式,控制进度条进度 TSPB_Progress.Value = 100;
DS_dataset.Dispose();
Driver.Dispose();
return true;
}
|
转载自:https://blog.csdn.net/u011609113/article/details/46932885