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.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
五、完整代碼
?
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
總結
以上是生活随笔為你收集整理的C# GDAL 学习一的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 白子画你若敢是什么歌啊?
- 下一篇: 加速