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
    评论
GDAL是一个用于读写地理数据的库,它支持多种格式的地理数据,包括TIFF、JPEG、PNG、GeoTIFF等。在Python中,可以使用GDAL库对TIFF文件进行读写操作。 以下是利用GDAL对TIFF文件进行读写的方法: 1. 安装GDAL库 在Python中使用GDAL库前,需要先安装GDAL库。可以通过pip安装,命令如下: ``` pip install GDAL ``` 2. 打开TIFF文件 使用GDAL库打开TIFF文件,可以使用以下代码: ```python from osgeo import gdal # 打开TIFF文件 dataset = gdal.Open('filename.tif', gdal.GA_ReadOnly) ``` 其中,`filename.tif`是要打开的TIFF文件路径,`gdal.GA_ReadOnly`表示以只读方式打开。 3. 读取TIFF文件数据 读取TIFF文件的数据,可以使用以下代码: ```python # 读取TIFF文件数据 data = dataset.ReadAsArray() # 获取数据信息 rows = dataset.RasterYSize cols = dataset.RasterXSize bands = dataset.RasterCount ``` 其中,`ReadAsArray()`方法可以读取TIFF文件的所有波段数据,返回一个二维数组;`RasterYSize`和`RasterXSize`分别表示TIFF文件的行数和列数,`RasterCount`表示TIFF文件的波段数。 4. 写入TIFF文件数据 写入TIFF文件的数据,可以使用以下代码: ```python from osgeo import gdal # 创建TIFF文件 driver = gdal.GetDriverByName('GTiff') dataset = driver.Create('output.tif', cols, rows, bands, gdal.GDT_Float32) # 写入数据 dataset.GetRasterBand(1).WriteArray(data) # 释放资源 dataset.FlushCache() ``` 其中,`GetDriverByName('GTiff')`表示创建一个TIFF文件,`cols`、`rows`、`bands`分别表示写入数据的列数、行数和波段数,`gdal.GDT_Float32`表示数据类型为32位浮点型。`GetRasterBand(1)`表示获取第一波段的数据,`WriteArray(data)`表示将数据写入到TIFF文件中。最后,通过`FlushCache()`方法释放资源。 以上是利用GDAL对TIFF文件进行读写的方法,希望能对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IvanLJF

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

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

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

打赏作者

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

抵扣说明:

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

余额充值