root/trunk/gdal/swig/csharp/apps/GDALInfo.cs

Revision 14912, 11.5 kB (checked in by tamas, 4 weeks ago)

Added examples for Dataset.GetGCPs, Dataset.SetGCPs and GCPsToGeoTransform.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /******************************************************************************
2  * $Id$
3  *
4  * Name:     GDALInfo.cs
5  * Project:  GDAL CSharp Interface
6  * Purpose:  A sample app to read GDAL raster data information.
7  * Author:   Tamas Szekeres, szekerest@gmail.com
8  *
9  ******************************************************************************
10  * Copyright (c) 2007, Tamas Szekeres
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  *****************************************************************************/
30
31 using System;
32
33 using OSGeo.GDAL;
34
35
36 /**
37
38  * <p>Title: GDAL C# GDALRead example.</p>
39  * <p>Description: A sample app to read GDAL raster data information.</p>
40  * @author Tamas Szekeres (szekerest@gmail.com)
41  * @version 1.0
42  */
43
44
45
46 /// <summary>
47 /// A C# based sample to read GDAL raster data information.
48 /// </summary>
49
50 class GDALInfo {
51        
52         public static void usage()
53
54         {
55                 Console.WriteLine("usage: gdalinfo {GDAL dataset name}");
56                 System.Environment.Exit(-1);
57         }
58  
59     public static void Main(string[] args)
60     {
61
62         if (args.Length != 1) usage();
63
64         Console.WriteLine("");
65
66         try
67         {
68             /* -------------------------------------------------------------------- */
69             /*      Register driver(s).                                             */
70             /* -------------------------------------------------------------------- */
71             Gdal.AllRegister();
72
73             /* -------------------------------------------------------------------- */
74             /*      Open dataset.                                                   */
75             /* -------------------------------------------------------------------- */
76             Dataset ds = Gdal.Open( args[0], Access.GA_ReadOnly );
77                
78             if (ds == null)
79             {
80                 Console.WriteLine("Can't open " + args[0]);
81                 System.Environment.Exit(-1);
82             }
83
84             Console.WriteLine("Raster dataset parameters:");
85             Console.WriteLine("  Projection: " + ds.GetProjectionRef());
86             Console.WriteLine("  RasterCount: " + ds.RasterCount);
87             Console.WriteLine("  RasterSize (" + ds.RasterXSize + "," + ds.RasterYSize + ")");
88            
89             /* -------------------------------------------------------------------- */
90             /*      Get driver                                                      */
91             /* -------------------------------------------------------------------- */ 
92             Driver drv = ds.GetDriver();
93
94             if (drv == null)
95             {
96                 Console.WriteLine("Can't get driver.");
97                 System.Environment.Exit(-1);
98             }
99            
100             Console.WriteLine("Using driver " + drv.LongName);
101
102             /* -------------------------------------------------------------------- */
103             /*      Get metadata                                                    */
104             /* -------------------------------------------------------------------- */
105             string[] metadata = ds.GetMetadata("");
106             if (metadata.Length > 0)
107             {
108                 Console.WriteLine("  Metadata:");
109                 for (int iMeta = 0; iMeta < metadata.Length; iMeta++)
110                 {
111                     Console.WriteLine("    " + iMeta + ":  " + metadata[iMeta]);
112                 }
113                 Console.WriteLine("");
114             }
115
116             /* -------------------------------------------------------------------- */
117             /*      Report "IMAGE_STRUCTURE" metadata.                              */
118             /* -------------------------------------------------------------------- */
119             metadata = ds.GetMetadata("IMAGE_STRUCTURE");
120             if (metadata.Length > 0)
121             {
122                 Console.WriteLine("  Image Structure Metadata:");
123                 for (int iMeta = 0; iMeta < metadata.Length; iMeta++)
124                 {
125                     Console.WriteLine("    " + iMeta + ":  " + metadata[iMeta]);
126                 }
127                 Console.WriteLine("");
128             }
129
130             /* -------------------------------------------------------------------- */
131             /*      Report subdatasets.                                             */
132             /* -------------------------------------------------------------------- */
133             metadata = ds.GetMetadata("SUBDATASETS");
134             if (metadata.Length > 0)
135             {
136                 Console.WriteLine("  Subdatasets:");
137                 for (int iMeta = 0; iMeta < metadata.Length; iMeta++)
138                 {
139                     Console.WriteLine("    " + iMeta + ":  " + metadata[iMeta]);
140                 }
141                 Console.WriteLine("");
142             }
143
144             /* -------------------------------------------------------------------- */
145             /*      Report geolocation.                                             */
146             /* -------------------------------------------------------------------- */
147             metadata = ds.GetMetadata("GEOLOCATION");
148             if (metadata.Length > 0)
149             {
150                 Console.WriteLine("  Geolocation:");
151                 for (int iMeta = 0; iMeta < metadata.Length; iMeta++)
152                 {
153                     Console.WriteLine("    " + iMeta + ":  " + metadata[iMeta]);
154                 }
155                 Console.WriteLine("");
156             }
157
158             /* -------------------------------------------------------------------- */
159             /*      Report corners.                                                 */
160             /* -------------------------------------------------------------------- */
161             Console.WriteLine( "Corner Coordinates:" );
162             Console.WriteLine("  Upper Left (" + GDALInfoGetPosition( ds, 0.0, 0.0) + ")");
163             Console.WriteLine("  Lower Left (" + GDALInfoGetPosition( ds, 0.0, ds.RasterYSize) + ")");
164             Console.WriteLine("  Upper Right (" + GDALInfoGetPosition( ds, ds.RasterXSize, 0.0) + ")");
165             Console.WriteLine("  Lower Right (" + GDALInfoGetPosition( ds, ds.RasterXSize, ds.RasterYSize) + ")");
166             Console.WriteLine("  Center (" + GDALInfoGetPosition( ds, ds.RasterXSize / 2, ds.RasterYSize / 2) + ")");
167             Console.WriteLine("");
168
169             /* -------------------------------------------------------------------- */
170             /*      Report GCPs.                                                    */
171             /* -------------------------------------------------------------------- */
172             if( ds.GetGCPCount( ) > 0 )
173             {
174                 Console.WriteLine( "GCP Projection: ", ds.GetGCPProjection());
175                 GCP[] GCPs = ds.GetGCPs();
176                 for( int i = 0; i < ds.GetGCPCount(); i++ )
177                 {
178                     Console.WriteLine("GCP[" + i + "]: Id=" + GCPs[i].Id + ", Info=" + GCPs[i].Info);
179                     Console.WriteLine("          (" + GCPs[i].GCPPixel + "," + GCPs[i].GCPLine + ") -> ("
180                                 + GCPs[i].GCPX + "," + GCPs[i].GCPY + "," + GCPs[i].GCPZ + ")");
181                     Console.WriteLine("");
182                 }
183                 Console.WriteLine("");
184
185                 double[] transform = new double[6];
186                 Gdal.GCPsToGeoTransform(GCPs, transform, 0);
187                 Console.WriteLine("GCP Equivalent geotransformation parameters: ", ds.GetGCPProjection());
188                 for (int i = 0; i < 6; i++)
189                     Console.WriteLine("t[" + i + "] = " + transform[i].ToString());
190                 Console.WriteLine("");
191             }
192
193             /* -------------------------------------------------------------------- */
194             /*      Get raster band                                                 */
195             /* -------------------------------------------------------------------- */
196             for (int iBand = 1; iBand <= ds.RasterCount; iBand++)
197             {
198                 Band band = ds.GetRasterBand(iBand);
199                 Console.WriteLine("Band " + iBand + " :");
200                 Console.WriteLine("   DataType: " + Gdal.GetDataTypeName(band.DataType));
201                 Console.WriteLine("   ColorInterpretation: " + Gdal.GetColorInterpretationName(band.GetRasterColorInterpretation()));
202                 ColorTable ct = band.GetRasterColorTable();
203                                 if (ct != null)
204                                         Console.WriteLine("   Band has a color table with " + ct.GetCount() + " entries.");
205                
206                                 Console.WriteLine("   Description: " + band.GetDescription());
207                 Console.WriteLine("   Size (" + band.XSize + "," + band.YSize + ")");
208                 int BlockXSize, BlockYSize;
209                 band.GetBlockSize(out BlockXSize, out BlockYSize);
210                 Console.WriteLine("   BlockSize (" + BlockXSize + "," + BlockYSize + ")");
211                 double val;
212                 int hasval;
213                 band.GetMinimum(out val, out hasval);
214                 if (hasval != 0) Console.WriteLine("   Minimum: " + val.ToString());
215                 band.GetMaximum(out val, out hasval);
216                 if (hasval != 0) Console.WriteLine("   Maximum: " + val.ToString());
217                 band.GetNoDataValue(out val, out hasval);
218                 if (hasval != 0) Console.WriteLine("   NoDataValue: " + val.ToString());
219                 band.GetOffset(out val, out hasval);
220                 if (hasval != 0) Console.WriteLine("   Offset: " + val.ToString());
221                 band.GetScale(out val, out hasval);
222                 if (hasval != 0) Console.WriteLine("   Scale: " + val.ToString());
223
224                 for (int iOver = 0; iOver < band.GetOverviewCount(); iOver++)
225                 {
226                     Band over = band.GetOverview(iOver);
227                     Console.WriteLine("      OverView " + iOver + " :");
228                     Console.WriteLine("         DataType: " + over.DataType);
229                     Console.WriteLine("         Size (" + over.XSize + "," + over.YSize + ")");
230                     Console.WriteLine("         PaletteInterp: " + over.GetRasterColorInterpretation().ToString());
231                 }
232             }
233         }
234         catch (Exception e)
235         {
236             Console.WriteLine("Application error: " + e.Message);
237         }
238     }
239
240     private static string GDALInfoGetPosition(Dataset ds, double x, double y)
241     {
242         double[] adfGeoTransform = new double[6];
243         double  dfGeoX, dfGeoY;
244         ds.GetGeoTransform(adfGeoTransform);
245
246         dfGeoX = adfGeoTransform[0] + adfGeoTransform[1] * x + adfGeoTransform[2] * y;
247         dfGeoY = adfGeoTransform[3] + adfGeoTransform[4] * x + adfGeoTransform[5] * y;
248
249         return dfGeoX.ToString() + ", " + dfGeoY.ToString();
250     }
251 }
Note: See TracBrowser for help on using the browser.