在读之前,可以先读一下李民录先生的博文http://blog.csdn.net/liminlu0314/article/details/6127755
这段代码简单的实现了生成金字塔文件的功能,只是错误处理机制还不完善,可以参考第二段代码,同时,适当的加些try catch……
在此,贴上我的代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using OSGeo.GDAL; namespace Check { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "载入DEM"; openFileDialog.Filter = "TIFF文件(*.tif;*.tiff)|*.tif;*tiff|TIFF文件(*.tif;*.tiff)|*.img"; if (openFileDialog.ShowDialog(this) == DialogResult.OK) { string FileName = openFileDialog.FileName; CreatePyramids(FileName); } } private bool CreatePyramids(string filename) { Gdal.AllRegister(); Gdal.SetConfigOption("USE_RRD", "YES"); Dataset ds = Gdal.Open(filename, Access.GA_Update); Driver drv = ds.GetDriver(); // System.Type szDriver = drv.ShortName.GetType(); int iWidth = ds.RasterXSize; int iHeight = ds.RasterYSize; int iPixelNum = iWidth * iHeight; //图像中的总像元个数 int iTopNum = 4096; //顶层金字塔大小,64*64 int iCurNum = iPixelNum / 4; 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.BuildOverviews("nearest",levels); ds.Dispose(); drv.Dispose(); return true; } } }
此外
在贴上在官网下载的实例代码
/******************************************************************************
* $Id$
*
* Name: GDALOverviews.cs
* Project: GDAL CSharp Interface
* Purpose: A sample app to create GDAL raster overviews.
* Author: Tamas Szekeres, szekerest@gmail.com
*
******************************************************************************
* Copyright (c) 2007, Tamas Szekeres
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
using System;
using OSGeo.GDAL;
/**
* <p>Title: GDAL C# GDALOverviews example.</p>
* <p>Description: A sample app to create GDAL raster overviews.</p>
* @author Tamas Szekeres (szekerest@gmail.com)
* @version 1.0
*/
/// <summary>
/// A C# based sample to create GDAL raster overviews.
/// </summary>
class GDALOverviews {
public static void usage()
{
Console.WriteLine("usage: gdaloverviews {GDAL dataset name} {resamplealg} {level1} {level2} ....");
Console.WriteLine("example: gdaloverviews sample.tif \"NEAREST\" 2 4");
System.Environment.Exit(-1);
}
public static void Main(string[] args)
{
if (args.Length <= 2) usage();
Console.WriteLine("");
try
{
/* -------------------------------------------------------------------- */
/* Register driver(s). */
/* -------------------------------------------------------------------- */
Gdal.AllRegister();
/* -------------------------------------------------------------------- */
/* Open dataset. */
/* -------------------------------------------------------------------- */
Dataset ds = Gdal.Open( args[0], Access.GA_Update );
if (ds == null)
{
Console.WriteLine("Can't open " + args[0]);
System.Environment.Exit(-1);
}
Console.WriteLine("Raster dataset parameters:");
Console.WriteLine(" Projection: " + ds.GetProjectionRef());
Console.WriteLine(" RasterCount: " + ds.RasterCount);
Console.WriteLine(" RasterSize (" + ds.RasterXSize + "," + ds.RasterYSize + ")");
int[] levels = new int[args.Length -2];
Console.WriteLine(levels.Length);
for (int i = 2; i < args.Length; i++)
{
levels[i-2] = int.Parse(args[i]);
}
if (ds.BuildOverviews(args[1], levels, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Sample Data") != (int)CPLErr.CE_None)
{
Console.WriteLine("The BuildOverviews operation doesn't work");
System.Environment.Exit(-1);
}
/* -------------------------------------------------------------------- */
/* Displaying the raster parameters */
/* -------------------------------------------------------------------- */
for (int iBand = 1; iBand <= ds.RasterCount; iBand++)
{
Band band = ds.GetRasterBand(iBand);
Console.WriteLine("Band " + iBand + " :");
Console.WriteLine(" DataType: " + band.DataType);
Console.WriteLine(" Size (" + band.XSize + "," + band.YSize + ")");
Console.WriteLine(" PaletteInterp: " + band.GetRasterColorInterpretation().ToString());
for (int iOver = 0; iOver < band.GetOverviewCount(); iOver++)
{
Band over = band.GetOverview(iOver);
Console.WriteLine(" OverView " + iOver + " :");
Console.WriteLine(" DataType: " + over.DataType);
Console.WriteLine(" Size (" + over.XSize + "," + over.YSize + ")");
Console.WriteLine(" PaletteInterp: " + over.GetRasterColorInterpretation().ToString());
}
}
Console.WriteLine("Completed.");
Console.WriteLine("Use: gdalread " + args[0] + " outfile.png [overview] to extract a particular overview!" );
}
catch (Exception e)
{
Console.WriteLine("Application error: " + e.Message);
}
}
public static int ProgressFunc(double Complete, IntPtr Message, IntPtr Data)
{
Console.Write("Processing ... " + Complete * 100 + "% Completed.");
if (Message != IntPtr.Zero)
Console.Write(" Message:" + System.Runtime.InteropServices.Marshal.PtrToStringAnsi(Message));
if (Data != IntPtr.Zero)
Console.Write(" Data:" + System.Runtime.InteropServices.Marshal.PtrToStringAnsi(Data));
Console.WriteLine("");
return 1;
}
}
C# 代码中与李先生的有些出入
主要在
C#中hDataset = GDALOpen( pszFileName, GA_ReadOnly );
GA_ReadOnly似乎应该改成
Dataset ds = Gdal.Open(filename, Access.GA_Update);
我不知道这是语言问题还是其他。
ps 2012.3.24 /这个问题我请教过李民录先生了,他说这两个都行,一个是内金字塔,一个是外金字塔,好像外金字塔ERDAS不识,所以会有提醒