VS2010 c#利用GDAL创建金字塔文件(金字塔文件包含在文件内部)

4 篇文章 0 订阅

源代码下载地址:http://download.csdn.net/detail/ivanljf/5832879

 

1、首先要配置工程环境,将编译好的9个dll文件放在debug目录下,将带有csharp的四个dll添加到引用;如下图:

2.添加命名空间:using OSGeo.GDAL

3、贴出代码:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OSGeo.GDAL;//添加该命名空间
namespace Official_BuildOverviews
{
   class Program
    {
        public static void usage()
        {
            Console.WriteLine("usage: Official_BuildOverviews {GDAL dataet name} {resampling} {level1} {level2} ...");
            Console.WriteLine("example: gdaloverviews sample.tif \"NEAREST\" 2 4");
            System.Environment.Exit(-1);
        }
        static void Main(string[] args)
        {
            if (args.Length <= 2)
                usage();
            Console.WriteLine("");
            try
            {
                Gdal.AllRegister();
                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];//减去前两个参数,该数组存储级别数(2,4,8,16...)
                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);
                }

                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 (System.Exception ex)
            {
                Console.WriteLine("Application error: " + ex.Message);
            }
        }
        public static int ProgressFunc(double Complete, IntPtr Message, IntPtr Data)//和委托参数一致 public delegate int GDALProgressFuncDelegate(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;
        }
    }
}

注:该程序代码是命令行参数程序,若想调试该程序,需要将传递的参数提前设置好:


 

 

源代码下载地址:http://download.csdn.net/detail/ivanljf/5832879

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IvanLJF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值