1 /******************************************************************************
2  * $Id: GDALInfo.cs 179218d76649a68fbee5edfc00b86b6397c1f149 2020-07-19 20:53:25 +0200 Tamas Szekeres $
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 using OSGeo.OSR;
35 
36 
37 /**
38 
39  * <p>Title: GDAL C# GDALRead example.</p>
40  * <p>Description: A sample app to read GDAL raster data information.</p>
41  * @author Tamas Szekeres (szekerest@gmail.com)
42  * @version 1.0
43  */
44 
45 
46 
47 /// <summary>
48 /// A C# based sample to read GDAL raster data information.
49 /// </summary>
50 
51 class GDALInfo {
52 
usage()53 	public static void usage()
54 
55 	{
56 		Console.WriteLine("usage: gdalinfo {GDAL dataset name}");
57 		System.Environment.Exit(-1);
58 	}
59 
Main(string[] args)60     public static void Main(string[] args)
61     {
62 
63         if (args.Length != 1) usage();
64 
65         Console.WriteLine("");
66 
67         try
68         {
69             /* -------------------------------------------------------------------- */
70             /*      Register driver(s).                                             */
71             /* -------------------------------------------------------------------- */
72             Gdal.AllRegister();
73 
74             /* -------------------------------------------------------------------- */
75             /*      Open dataset.                                                   */
76             /* -------------------------------------------------------------------- */
77             Dataset ds = Gdal.Open( args[0], Access.GA_ReadOnly );
78 
79             if (ds == null)
80             {
81                 Console.WriteLine("Can't open " + args[0]);
82                 System.Environment.Exit(-1);
83             }
84 
85             Console.WriteLine("Raster dataset parameters:");
86             Console.WriteLine("  Projection: " + ds.GetProjectionRef());
87             Console.WriteLine("  RasterCount: " + ds.RasterCount);
88             Console.WriteLine("  RasterSize (" + ds.RasterXSize + "," + ds.RasterYSize + ")");
89 
90             /* -------------------------------------------------------------------- */
91             /*      Get driver                                                      */
92             /* -------------------------------------------------------------------- */
93             Driver drv = ds.GetDriver();
94 
95             if (drv == null)
96             {
97                 Console.WriteLine("Can't get driver.");
98                 System.Environment.Exit(-1);
99             }
100 
101             Console.WriteLine("Using driver " + drv.LongName);
102 
103             /* -------------------------------------------------------------------- */
104             /*      Get metadata                                                    */
105             /* -------------------------------------------------------------------- */
106             string[] metadata = ds.GetMetadata("");
107             if (metadata.Length > 0)
108             {
109                 Console.WriteLine("  Metadata:");
110                 for (int iMeta = 0; iMeta < metadata.Length; iMeta++)
111                 {
112                     Console.WriteLine("    " + iMeta + ":  " + metadata[iMeta]);
113                 }
114                 Console.WriteLine("");
115             }
116 
117             /* -------------------------------------------------------------------- */
118             /*      Report "IMAGE_STRUCTURE" metadata.                              */
119             /* -------------------------------------------------------------------- */
120             metadata = ds.GetMetadata("IMAGE_STRUCTURE");
121             if (metadata.Length > 0)
122             {
123                 Console.WriteLine("  Image Structure Metadata:");
124                 for (int iMeta = 0; iMeta < metadata.Length; iMeta++)
125                 {
126                     Console.WriteLine("    " + iMeta + ":  " + metadata[iMeta]);
127                 }
128                 Console.WriteLine("");
129             }
130 
131             /* -------------------------------------------------------------------- */
132             /*      Report subdatasets.                                             */
133             /* -------------------------------------------------------------------- */
134             metadata = ds.GetMetadata("SUBDATASETS");
135             if (metadata.Length > 0)
136             {
137                 Console.WriteLine("  Subdatasets:");
138                 for (int iMeta = 0; iMeta < metadata.Length; iMeta++)
139                 {
140                     Console.WriteLine("    " + iMeta + ":  " + metadata[iMeta]);
141                 }
142                 Console.WriteLine("");
143             }
144 
145             /* -------------------------------------------------------------------- */
146             /*      Report geolocation.                                             */
147             /* -------------------------------------------------------------------- */
148             metadata = ds.GetMetadata("GEOLOCATION");
149             if (metadata.Length > 0)
150             {
151                 Console.WriteLine("  Geolocation:");
152                 for (int iMeta = 0; iMeta < metadata.Length; iMeta++)
153                 {
154                     Console.WriteLine("    " + iMeta + ":  " + metadata[iMeta]);
155                 }
156                 Console.WriteLine("");
157             }
158 
159             /* -------------------------------------------------------------------- */
160             /*      Report corners.                                                 */
161             /* -------------------------------------------------------------------- */
162             Console.WriteLine( "Corner Coordinates:" );
163             Console.WriteLine("  Upper Left (" + GDALInfoGetPosition( ds, 0.0, 0.0) + ")");
164             Console.WriteLine("  Lower Left (" + GDALInfoGetPosition( ds, 0.0, ds.RasterYSize) + ")");
165             Console.WriteLine("  Upper Right (" + GDALInfoGetPosition( ds, ds.RasterXSize, 0.0) + ")");
166             Console.WriteLine("  Lower Right (" + GDALInfoGetPosition( ds, ds.RasterXSize, ds.RasterYSize) + ")");
167             Console.WriteLine("  Center (" + GDALInfoGetPosition( ds, ds.RasterXSize / 2, ds.RasterYSize / 2) + ")");
168             Console.WriteLine("");
169 
170             /* -------------------------------------------------------------------- */
171             /*      Report projection.                                              */
172             /* -------------------------------------------------------------------- */
173             string projection = ds.GetProjectionRef();
174             if (projection != null)
175             {
176                 SpatialReference srs = new SpatialReference(null);
177                 if (srs.ImportFromWkt(ref projection) == 0)
178                 {
179                     string wkt;
180                     srs.ExportToPrettyWkt(out wkt, 0);
181                     Console.WriteLine("Coordinate System is:");
182                     Console.WriteLine(wkt);
183                 }
184                 else
185                 {
186                     Console.WriteLine("Coordinate System is:");
187                     Console.WriteLine(projection);
188                 }
189             }
190 
191             SpatialReference srsDef = ds.GetSpatialRef();
192             if (srsDef != null)
193             {
194                 string wkt;
195                 srsDef.ExportToPrettyWkt(out wkt, 0);
196                 Console.WriteLine("Coordinate System is (via GetSpatialRef):");
197                 Console.WriteLine(wkt);
198             }
199 
200             /* -------------------------------------------------------------------- */
201             /*      Report GCPs.                                                    */
202             /* -------------------------------------------------------------------- */
203             if ( ds.GetGCPCount( ) > 0 )
204             {
205                 Console.WriteLine( "GCP Projection: ", ds.GetGCPProjection());
206                 GCP[] GCPs = ds.GetGCPs();
207                 for( int i = 0; i < ds.GetGCPCount(); i++ )
208                 {
209                     Console.WriteLine("GCP[" + i + "]: Id=" + GCPs[i].Id + ", Info=" + GCPs[i].Info);
210                     Console.WriteLine("          (" + GCPs[i].GCPPixel + "," + GCPs[i].GCPLine + ") -> ("
211                                 + GCPs[i].GCPX + "," + GCPs[i].GCPY + "," + GCPs[i].GCPZ + ")");
212                     Console.WriteLine("");
213                 }
214                 Console.WriteLine("");
215 
216                 double[] transform = new double[6];
217                 Gdal.GCPsToGeoTransform(GCPs, transform, 0);
218                 Console.WriteLine("GCP Equivalent geotransformation parameters: ", ds.GetGCPProjection());
219                 for (int i = 0; i < 6; i++)
220                     Console.WriteLine("t[" + i + "] = " + transform[i].ToString());
221                 Console.WriteLine("");
222             }
223 
224             /* -------------------------------------------------------------------- */
225             /*      Get raster band                                                 */
226             /* -------------------------------------------------------------------- */
227             for (int iBand = 1; iBand <= ds.RasterCount; iBand++)
228             {
229                 Band band = ds.GetRasterBand(iBand);
230                 Console.WriteLine("Band " + iBand + " :");
231                 Console.WriteLine("   DataType: " + Gdal.GetDataTypeName(band.DataType));
232                 Console.WriteLine("   ColorInterpretation: " + Gdal.GetColorInterpretationName(band.GetRasterColorInterpretation()));
233                 ColorTable ct = band.GetRasterColorTable();
234 				if (ct != null)
235 					Console.WriteLine("   Band has a color table with " + ct.GetCount() + " entries.");
236 
237 				Console.WriteLine("   Description: " + band.GetDescription());
238                 Console.WriteLine("   Size (" + band.XSize + "," + band.YSize + ")");
239                 int BlockXSize, BlockYSize;
240                 band.GetBlockSize(out BlockXSize, out BlockYSize);
241                 Console.WriteLine("   BlockSize (" + BlockXSize + "," + BlockYSize + ")");
242                 double val;
243                 int hasval;
244                 band.GetMinimum(out val, out hasval);
245                 if (hasval != 0) Console.WriteLine("   Minimum: " + val.ToString());
246                 band.GetMaximum(out val, out hasval);
247                 if (hasval != 0) Console.WriteLine("   Maximum: " + val.ToString());
248                 band.GetNoDataValue(out val, out hasval);
249                 if (hasval != 0) Console.WriteLine("   NoDataValue: " + val.ToString());
250                 band.GetOffset(out val, out hasval);
251                 if (hasval != 0) Console.WriteLine("   Offset: " + val.ToString());
252                 band.GetScale(out val, out hasval);
253                 if (hasval != 0) Console.WriteLine("   Scale: " + val.ToString());
254 
255                 for (int iOver = 0; iOver < band.GetOverviewCount(); iOver++)
256                 {
257                     Band over = band.GetOverview(iOver);
258                     Console.WriteLine("      OverView " + iOver + " :");
259                     Console.WriteLine("         DataType: " + over.DataType);
260                     Console.WriteLine("         Size (" + over.XSize + "," + over.YSize + ")");
261                     Console.WriteLine("         PaletteInterp: " + over.GetRasterColorInterpretation().ToString());
262                 }
263             }
264         }
265         catch (Exception e)
266         {
267             Console.WriteLine("Application error: " + e.Message);
268         }
269     }
270 
GDALInfoGetPosition(Dataset ds, double x, double y)271     private static string GDALInfoGetPosition(Dataset ds, double x, double y)
272     {
273         double[] adfGeoTransform = new double[6];
274         double	dfGeoX, dfGeoY;
275         ds.GetGeoTransform(adfGeoTransform);
276 
277         dfGeoX = adfGeoTransform[0] + adfGeoTransform[1] * x + adfGeoTransform[2] * y;
278         dfGeoY = adfGeoTransform[3] + adfGeoTransform[4] * x + adfGeoTransform[5] * y;
279 
280         return dfGeoX.ToString() + ", " + dfGeoY.ToString();
281     }
282 }
283