自由与蓝天

昔日高山流水,快剑江湖,长街奔马。 今日谷歌百度,种菜发贴,写写代码。

博客园 首页 新随笔 联系 订阅 管理

最近一直琢磨如何用C#+GDAL读取栅格数据(.tif.img),运气不错的在GDAL 的官网上找到一部分源码。经过本人测试,效果还不错。学习还将继续深入下去。

参考网址:http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/csharp/apps/GDALRead.cs

开发环境:VS2008+GDAL1.5

所需dll gdal15.dllgdal_csharp.dllgdal_wrap.dllgdalconst_csharp.dllgdalconst_warp.dll

一、将以上.dll添加到工程bin\debug目录下。

二、建立控制台程序,添加gdal_csharp引用。如图

 

三、工程GDALRead处右键打开属性对话框,调试一栏添加命令行参数,如图:

 

这里的命令行参数在程序中直接被数组args[]调用。

四、任务与目标

(1)、读取栅格数据的一般参数,如坐标投影(Projection)、波段数(Rsatercount)、数据驱动、栅格大小(RasterSize)

(2)、每个波段的数据类型(DataType)、大小(Size)、PaletteInterp

五、完整代码

 

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using OSGeo.GDAL;
 6 namespace GDALRead
 7 {
 8     class Program
 9     {
10         public static void usage()
11         {
12             Console.WriteLine("usage");
13             System.Environment.Exit(-1);
14         }
15      public  static void Main(string[] args)
16         {
17             //Console.WriteLine(args[0]);
18             //Console.ReadLine();
19          try
20          {
21              Gdal.AllRegister();
22              Dataset ds = Gdal.Open(args[0],Access.GA_ReadOnly);
23              if (ds==null)
24              {
25                 Console.WriteLine("Can't open " + args[0]); 
26                  System.Environment.Exit(-1); 
27              }
28              Console.WriteLine("raster dataset parameters:");
29              Console.WriteLine("  Projection:" + ds.GetProjectionRef());
30              Console.WriteLine("  Rastercount:" + ds.RasterCount);//RasterCount是波段数
31              Console.WriteLine("  RasterSize (" + ds.RasterXSize + "," + ds.RasterYSize + ")");
32 
33              /************************************************************************/
34              /* Get Driver                                                                     */
35              /************************************************************************/
36              Driver drv = ds.GetDriver();
37              if (drv ==null)
38              {
39                  Console.WriteLine("Can't get driver");
40                  System.Environment.Exit(-1);
41              }
42              Console.WriteLine("using driver" + drv.LongName);
43              /************************************************************************/
44              /* Get raster band                                                                    */
45              /************************************************************************/
46              for (int iBand=1;iBand<=ds.RasterCount;iBand++)
47              {
48                  Band band = ds.GetRasterBand(iBand);
49                  Console.WriteLine("Band" + iBand + ":");
50                  Console.WriteLine("   DataType:" + band.DataType);
51                  Console.WriteLine("   Size (" + band.XSize + "," + band.YSize + ")");
52                 Console.WriteLine("   PaletteInterp: " + band.GetRasterColorInterpretation().ToString()); //调色说明?
53 
54                 for (int iOver = 0; iOver < band.GetOverviewCount(); iOver++)
55                 {
56                     Band over = band.GetOverview(iOver);
57                     Console.WriteLine("      OverView " + iOver + " :");
58                     Console.WriteLine("         DataType: " + over.DataType);
59                     Console.WriteLine("         Size (" + over.XSize + "," + over.YSize + ")");
60                     Console.WriteLine("         PaletteInterp: " + over.GetRasterColorInterpretation().ToString());
61                 } 
62 
63              }
64              /************************************************************************/
65              /* Processing the raster    
66               * To be continued
67              /************************************************************************/
68 
69          }
70              
71          catch (System.Exception ex)
72          {
73              Console.WriteLine("Application error: " + ex.Message);
74          }
75          Console.ReadLine();
76         }
77     }
78 }

六、运行结果

我找了一幅.img的遥感影像,不含投影坐标,运行结果如下:

 

posted on 2011-05-30 09:16  自由与蓝天  阅读(3831)  评论(0编辑  收藏  举报