最近一直琢磨如何用C#+GDAL读取栅格数据(.tif或.img),运气不错的在GDAL 的官网上找到一部分源码。经过本人测试,效果还不错。学习还将继续深入下去。
参考网址:http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/csharp/apps/GDALRead.cs
开发环境:VS2008+GDAL1.5
所需dll :gdal15.dll、gdal_csharp.dll、gdal_wrap.dll、gdalconst_csharp.dll、gdalconst_warp.dll
一、将以上.dll添加到工程bin\debug目录下。
二、建立控制台程序,添加gdal_csharp引用。如图
三、工程GDALRead处右键打开属性对话框,调试一栏添加命令行参数,如图:
这里的命令行参数在程序中直接被数组args[]调用。
四、任务与目标
(1)、读取栅格数据的一般参数,如坐标投影(Projection)、波段数(Rsatercount)、数据驱动、栅格大小(RasterSize)
(2)、每个波段的数据类型(DataType)、大小(Size)、PaletteInterp
五、完整代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 }
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的遥感影像,不含投影坐标,运行结果如下: