C# & GDAL 学习一

最近一直琢磨如何用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

五、完整代码

 

ExpandedBlockStart.gif 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的遥感影像,不含投影坐标,运行结果如下:

 

转载于:https://www.cnblogs.com/ming5536/archive/2011/05/30/ming5536.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值